use ruau_ast::parse::{ParseConfig, SyntaxFlags};
use serde::{Deserialize, Serialize};
use crate::builder::DEFAULT_VERSION;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct CompileOptions {
pub optimization_level: u8,
pub debug_level: u8,
pub coverage_level: u8,
}
impl CompileOptions {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[doc(hidden)]
#[must_use]
pub fn to_upstream_options(&self) -> UpstreamCompilerOptions {
let mut options = UpstreamCompilerOptions {
clear_dead_stack_slots: true,
..UpstreamCompilerOptions::default()
};
options.optimization_level = self.optimization_level;
options.debug_level = self.debug_level;
options.coverage_level = self.coverage_level;
options
}
}
impl Default for CompileOptions {
fn default() -> Self {
Self {
optimization_level: 1,
debug_level: 1,
coverage_level: 0,
}
}
}
#[doc(hidden)]
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct UpstreamCompilerOptions {
pub optimization_level: u8,
pub debug_level: u8,
pub type_info_level: u8,
pub coverage_level: u8,
pub syntax_flags: SyntaxFlags,
pub parse_options: UpstreamParseOptions,
pub fast_flags: Vec<FastFlag>,
pub fast_ints: Vec<FastInt>,
pub vector_lib: Option<String>,
pub vector_ctor: Option<String>,
pub vector_type: Option<String>,
pub mutable_globals: Vec<String>,
pub userdata_types: Vec<String>,
pub disabled_builtins: Vec<String>,
pub known_members: Vec<KnownMember>,
#[serde(skip)]
pub clear_dead_stack_slots: bool,
#[serde(skip)]
pub preserve_fenv_semantics: bool,
}
impl UpstreamCompilerOptions {
#[must_use]
pub fn parse_config(&self) -> ParseConfig {
ParseConfig {
allow_declaration_syntax: self.parse_options.allow_declaration_syntax,
capture_comments: self.parse_options.capture_comments,
parse_fragment: self.parse_options.parse_fragment,
store_cst_data: self.parse_options.store_cst_data,
no_error_limit: self.parse_options.no_error_limit,
syntax: self.syntax_flags,
}
}
#[must_use]
pub fn for_vm_execution() -> Self {
Self {
clear_dead_stack_slots: true,
preserve_fenv_semantics: true,
..Self::default()
}
}
#[must_use]
pub fn fast_flag(&self, name: &str) -> bool {
self.fast_flags
.iter()
.rev()
.find(|flag| flag.name == name)
.map_or_else(|| default_fast_flag(name), |flag| flag.value)
}
#[must_use]
pub fn fast_int(&self, name: &str) -> i32 {
self.fast_ints
.iter()
.rev()
.find(|fast_int| fast_int.name == name)
.map_or_else(|| default_fast_int(name), |fast_int| fast_int.value)
}
pub(crate) fn bytecode_version(&self) -> u8 {
if self.fast_flag("LuauEmitCallFeedback") {
11
} else if self.fast_flag("DebugLuauUserDefinedClasses") {
10
} else if self.fast_flag("LuauCompileUdataDirect") {
9
} else if self.fast_flag("LuauIntegerType") || self.fast_flag("LuauIntegerType2") {
8
} else if self.coverage_level > 0 || self.fast_flag("LuauCompileDuptableConstantPack2") {
7
} else {
DEFAULT_VERSION
}
}
}
impl Default for UpstreamCompilerOptions {
fn default() -> Self {
Self {
optimization_level: 1,
debug_level: 1,
type_info_level: 0,
coverage_level: 0,
syntax_flags: SyntaxFlags::default(),
parse_options: UpstreamParseOptions::default(),
fast_flags: Vec::new(),
fast_ints: Vec::new(),
vector_lib: None,
vector_ctor: None,
vector_type: None,
mutable_globals: Vec::new(),
userdata_types: Vec::new(),
disabled_builtins: Vec::new(),
known_members: Vec::new(),
clear_dead_stack_slots: false,
preserve_fenv_semantics: false,
}
}
}
#[doc(hidden)]
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[serde(default)]
pub struct UpstreamParseOptions {
pub allow_declaration_syntax: bool,
pub capture_comments: bool,
pub parse_fragment: bool,
pub store_cst_data: bool,
pub no_error_limit: bool,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FastFlag {
pub name: String,
pub value: bool,
}
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FastInt {
pub name: String,
pub value: i32,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct KnownMember {
pub library: String,
pub member: String,
pub value: KnownMemberValue,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "kind", rename_all = "camelCase")]
pub enum KnownMemberValue {
Nil,
Boolean {
value: bool,
},
Number {
value: f64,
},
Integer {
value: i64,
},
Vector {
value: [f32; 4],
},
String {
value: String,
},
}
#[must_use]
pub fn effective_compile_options(
source: &str,
options: &UpstreamCompilerOptions,
) -> UpstreamCompilerOptions {
let mut effective = options.clone();
apply_leading_hot_comments(source.lines(), &mut effective);
if source.contains("@native") {
effective.optimization_level = 2;
effective.type_info_level = 1;
}
effective
}
#[must_use]
pub fn source_compile_options(
source: &str,
options: &UpstreamCompilerOptions,
) -> UpstreamCompilerOptions {
let mut effective = effective_compile_options(source, options);
apply_leading_hot_comments(
source.lines().skip_while(|line| line.trim().is_empty()),
&mut effective,
);
effective
}
fn apply_leading_hot_comments<'a>(
lines: impl Iterator<Item = &'a str>,
options: &mut UpstreamCompilerOptions,
) {
for line in lines.take_while(|line| line.trim_start().starts_with("--")) {
apply_hot_comment(line, options);
}
}
fn apply_hot_comment(line: &str, options: &mut UpstreamCompilerOptions) {
let trimmed = line.trim_start();
if let Some(value) = trimmed.strip_prefix("--!optimize") {
let level = value
.trim()
.parse::<u8>()
.map_or(options.optimization_level, |level| level.min(2));
options.optimization_level = level;
}
if trimmed.starts_with("--!native") {
options.optimization_level = 2;
options.type_info_level = 1;
}
}
fn default_fast_flag(_name: &str) -> bool {
false
}
fn default_fast_int(name: &str) -> i32 {
match name {
"LuauCompileLoopUnrollThreshold" => 25,
"LuauCompileLoopUnrollThresholdMaxBoost" => 300,
"LuauCompileInlineThreshold" => 25,
"LuauCompileInlineThresholdMaxBoost" => 300,
"LuauCompileInlineDepth" => 5,
_ => 0,
}
}
#[cfg(test)]
mod tests {
use super::{
FastFlag, FastInt, UpstreamCompilerOptions, effective_compile_options,
source_compile_options,
};
#[test]
fn default_options_match_upstream_defaults() {
let options = UpstreamCompilerOptions::default();
assert_eq!(options.optimization_level, 1);
assert_eq!(options.debug_level, 1);
assert_eq!(options.type_info_level, 0);
assert_eq!(options.coverage_level, 0);
}
#[test]
fn fast_flags_default_false_and_sidecars_override() {
let options = UpstreamCompilerOptions::default();
assert!(!options.fast_flag("LuauEmitCallFeedback"));
let options = UpstreamCompilerOptions {
fast_flags: vec![FastFlag {
name: "LuauEmitCallFeedback".to_owned(),
value: true,
}],
..Default::default()
};
assert!(options.fast_flag("LuauEmitCallFeedback"));
}
#[test]
fn fast_ints_use_upstream_defaults_and_sidecars_override() {
let options = UpstreamCompilerOptions::default();
assert_eq!(options.fast_int("LuauCompileInlineThreshold"), 25);
assert_eq!(options.fast_int("LuauCompileInlineThresholdMaxBoost"), 300);
assert_eq!(options.fast_int("LuauCompileInlineDepth"), 5);
assert_eq!(options.fast_int("UnknownFastInt"), 0);
let options = UpstreamCompilerOptions {
fast_ints: vec![FastInt {
name: "LuauCompileInlineThreshold".to_owned(),
value: 10,
}],
..Default::default()
};
assert_eq!(options.fast_int("LuauCompileInlineThreshold"), 10);
}
#[test]
fn bytecode_version_uses_fast_flag_helpers() {
let options = UpstreamCompilerOptions::default();
assert_eq!(options.bytecode_version(), 7);
let options = UpstreamCompilerOptions {
fast_flags: vec![FastFlag {
name: "LuauEmitCallFeedback".to_owned(),
value: true,
}],
..Default::default()
};
assert_eq!(options.bytecode_version(), 11);
let options = UpstreamCompilerOptions {
fast_flags: vec![FastFlag {
name: "LuauIntegerType2".to_owned(),
value: true,
}],
..Default::default()
};
assert_eq!(options.bytecode_version(), 8);
}
#[test]
fn source_hot_comments_override_options() {
let options = effective_compile_options(
"--!optimize 2\nreturn 1",
&UpstreamCompilerOptions::default(),
);
assert_eq!(options.optimization_level, 2);
let options = source_compile_options(
"\n--!optimize 2\nreturn 1",
&UpstreamCompilerOptions::default(),
);
assert_eq!(options.optimization_level, 2);
}
}