Skip to content

Commit b1f73ec

Browse files
committed
Update Luau Compiler methods to better control extra options:
- Add `add_mutable_global` - Add `add_userdata_type` - Replace `set_library_constants` with `add_library_constant` - Add `add_disabled_builtin`
1 parent 61a2141 commit b1f73ec

File tree

2 files changed

+77
-32
lines changed

2 files changed

+77
-32
lines changed

src/chunk.rs

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,36 @@ pub enum CompileConstant {
163163
String(String),
164164
}
165165

166-
#[cfg(feature = "luau")]
167-
impl From<&'static str> for CompileConstant {
168-
fn from(s: &'static str) -> Self {
169-
CompileConstant::String(s.to_string())
166+
#[cfg(any(feature = "luau", doc))]
167+
impl From<bool> for CompileConstant {
168+
fn from(b: bool) -> Self {
169+
CompileConstant::Boolean(b)
170+
}
171+
}
172+
173+
#[cfg(any(feature = "luau", doc))]
174+
impl From<crate::Number> for CompileConstant {
175+
fn from(n: crate::Number) -> Self {
176+
CompileConstant::Number(n)
170177
}
171178
}
172179

173180
#[cfg(any(feature = "luau", doc))]
174-
type LibraryMemberConstantMap = std::sync::Arc<HashMap<(String, String), CompileConstant>>;
181+
impl From<crate::Vector> for CompileConstant {
182+
fn from(v: crate::Vector) -> Self {
183+
CompileConstant::Vector(v)
184+
}
185+
}
186+
187+
#[cfg(any(feature = "luau", doc))]
188+
impl From<&str> for CompileConstant {
189+
fn from(s: &str) -> Self {
190+
CompileConstant::String(s.to_owned())
191+
}
192+
}
193+
194+
#[cfg(any(feature = "luau", doc))]
195+
type LibraryMemberConstantMap = HashMap<(String, String), CompileConstant>;
175196

176197
/// Luau compiler
177198
#[cfg(any(feature = "luau", doc))]
@@ -288,49 +309,75 @@ impl Compiler {
288309
self
289310
}
290311

312+
/// Adds a mutable global.
313+
///
314+
/// It disables the import optimization for fields accessed through it.
315+
#[must_use]
316+
pub fn add_mutable_global(mut self, global: impl Into<StdString>) -> Self {
317+
self.mutable_globals.push(global.into());
318+
self
319+
}
320+
291321
/// Sets a list of globals that are mutable.
292322
///
293323
/// It disables the import optimization for fields accessed through these.
294324
#[must_use]
295-
pub fn set_mutable_globals<S: Into<String>>(mut self, globals: Vec<S>) -> Self {
325+
pub fn set_mutable_globals<S: Into<StdString>>(mut self, globals: impl IntoIterator<Item = S>) -> Self {
296326
self.mutable_globals = globals.into_iter().map(|s| s.into()).collect();
297327
self
298328
}
299329

330+
/// Adds a userdata type to the list that will be included in the type information.
331+
#[must_use]
332+
pub fn add_userdata_type(mut self, r#type: impl Into<StdString>) -> Self {
333+
self.userdata_types.push(r#type.into());
334+
self
335+
}
336+
300337
/// Sets a list of userdata types that will be included in the type information.
301338
#[must_use]
302-
pub fn set_userdata_types<S: Into<String>>(mut self, types: Vec<S>) -> Self {
339+
pub fn set_userdata_types<S: Into<StdString>>(mut self, types: impl IntoIterator<Item = S>) -> Self {
303340
self.userdata_types = types.into_iter().map(|s| s.into()).collect();
304341
self
305342
}
306343

307-
/// Sets constants for known library members.
344+
/// Adds a constant for a known library member.
308345
///
309346
/// The constants are used by the compiler to optimize the generated bytecode.
310347
/// Optimization level must be at least 2 for this to have any effect.
311348
///
312349
/// The first element of the tuple is the library name,the second is the member name, and the
313350
/// third is the constant value.
314351
#[must_use]
315-
pub fn set_library_constants<L, M>(mut self, constants: Vec<(L, M, CompileConstant)>) -> Self
316-
where
317-
L: Into<String>,
318-
M: Into<String>,
319-
{
320-
let map = constants
321-
.into_iter()
322-
.map(|(lib, member, cons)| ((lib.into(), member.into()), cons))
323-
.collect::<HashMap<_, _>>();
324-
self.library_constants = Some(std::sync::Arc::new(map));
325-
self.libraries_with_known_members = (self.library_constants.clone())
326-
.map(|map| map.keys().map(|(lib, _)| lib.clone()).collect())
327-
.unwrap_or_default();
352+
pub fn add_library_constant(
353+
mut self,
354+
lib: impl Into<StdString>,
355+
member: impl Into<StdString>,
356+
r#const: impl Into<CompileConstant>,
357+
) -> Self {
358+
let (lib, member) = (lib.into(), member.into());
359+
if !self.libraries_with_known_members.contains(&lib) {
360+
self.libraries_with_known_members.push(lib.clone());
361+
}
362+
self.library_constants
363+
.get_or_insert_with(HashMap::new)
364+
.insert((lib, member), r#const.into());
365+
self
366+
}
367+
368+
/// Adds a builtin that should be disabled.
369+
#[must_use]
370+
pub fn add_disabled_builtin(mut self, builtin: impl Into<StdString>) -> Self {
371+
self.disabled_builtins.push(builtin.into());
328372
self
329373
}
330374

331375
/// Sets a list of builtins that should be disabled.
332376
#[must_use]
333-
pub fn set_disabled_builtins<S: Into<String>>(mut self, builtins: Vec<S>) -> Self {
377+
pub fn set_disabled_builtins<S: Into<StdString>>(
378+
mut self,
379+
builtins: impl IntoIterator<Item = S>,
380+
) -> Self {
334381
self.disabled_builtins = builtins.into_iter().map(|s| s.into()).collect();
335382
self
336383
}

tests/chunk.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ fn test_compiler() -> Result<()> {
122122
.set_vector_lib("vector")
123123
.set_vector_ctor("new")
124124
.set_vector_type("vector")
125-
.set_mutable_globals(vec!["mutable_global"])
126-
.set_userdata_types(vec!["MyUserdata"])
127-
.set_disabled_builtins(vec!["tostring"]);
125+
.set_mutable_globals(["mutable_global"])
126+
.set_userdata_types(["MyUserdata"])
127+
.set_disabled_builtins(["tostring"]);
128128

129129
assert!(compiler.compile("return tostring(vector.new(1, 2, 3))").is_ok());
130130

@@ -142,16 +142,14 @@ fn test_compiler() -> Result<()> {
142142
#[cfg(feature = "luau")]
143143
#[test]
144144
fn test_compiler_library_constants() {
145-
use mlua::{CompileConstant, Compiler, Vector};
145+
use mlua::{Compiler, Vector};
146146

147147
let compiler = Compiler::new()
148148
.set_optimization_level(2)
149-
.set_library_constants(vec![
150-
("mylib", "const_bool", CompileConstant::Boolean(true)),
151-
("mylib", "const_num", CompileConstant::Number(123.0)),
152-
("mylib", "const_vec", CompileConstant::Vector(Vector::zero())),
153-
("mylib", "const_str", "value1".into()),
154-
]);
149+
.add_library_constant("mylib", "const_bool", true)
150+
.add_library_constant("mylib", "const_num", 123.0)
151+
.add_library_constant("mylib", "const_vec", Vector::zero())
152+
.add_library_constant("mylib", "const_str", "value1");
155153

156154
let lua = Lua::new();
157155
lua.set_compiler(compiler);

0 commit comments

Comments
 (0)