diff options
-rw-r--r-- | src/chunk.rs | 84 | ||||
-rw-r--r-- | src/lua.rs | 22 | ||||
-rw-r--r-- | tests/luau.rs | 4 |
3 files changed, 32 insertions, 78 deletions
diff --git a/src/chunk.rs b/src/chunk.rs index 40e8ce1..44076e1 100644 --- a/src/chunk.rs +++ b/src/chunk.rs @@ -104,7 +104,7 @@ impl Compiler { /// * 0 - no optimization /// * 1 - baseline optimization level that doesn't prevent debuggability (default) /// * 2 - includes optimizations that harm debuggability such as inlining - pub fn set_optimization_level(&mut self, level: u8) -> &mut Self { + pub fn set_optimization_level(mut self, level: u8) -> Self { self.optimization_level = level; self } @@ -115,7 +115,7 @@ impl Compiler { /// * 0 - no debugging support /// * 1 - line info & function names only; sufficient for backtraces (default) /// * 2 - full debug info with local & upvalue names; necessary for debugger - pub fn set_debug_level(&mut self, level: u8) -> &mut Self { + pub fn set_debug_level(mut self, level: u8) -> Self { self.debug_level = level; self } @@ -126,19 +126,19 @@ impl Compiler { /// * 0 - no code coverage support (default) /// * 1 - statement coverage /// * 2 - statement and expression coverage (verbose) - pub fn set_coverage_level(&mut self, level: u8) -> &mut Self { + pub fn set_coverage_level(mut self, level: u8) -> Self { self.coverage_level = level; self } #[doc(hidden)] - pub fn set_vector_lib(&mut self, lib: Option<String>) -> &mut Self { + pub fn set_vector_lib(mut self, lib: Option<String>) -> Self { self.vector_lib = lib; self } #[doc(hidden)] - pub fn set_vector_ctor(&mut self, ctor: Option<String>) -> &mut Self { + pub fn set_vector_ctor(mut self, ctor: Option<String>) -> Self { self.vector_ctor = ctor; self } @@ -146,7 +146,7 @@ impl Compiler { /// Sets a list of globals that are mutable. /// /// It disables the import optimization for fields accessed through these. - pub fn set_mutable_globals(&mut self, globals: Vec<String>) -> &mut Self { + pub fn set_mutable_globals(mut self, globals: Vec<String>) -> Self { self.mutable_globals = globals; self } @@ -232,79 +232,15 @@ impl<'lua, 'a> Chunk<'lua, 'a> { self } - /// Sets Luau compiler optimization level. - /// - /// See [`Compiler::set_optimization_level`] for details. - /// - /// Requires `feature = "luau"` - #[cfg(any(feature = "luau", doc))] - #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - pub fn set_optimization_level(mut self, level: u8) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_optimization_level(level); - self - } - - /// Sets Luau compiler debug level. - /// - /// See [`Compiler::set_debug_level`] for details. - /// - /// Requires `feature = "luau` - #[cfg(any(feature = "luau", doc))] - #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - pub fn set_debug_level(mut self, level: u8) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_debug_level(level); - self - } - - /// Sets Luau compiler code coverage level. + /// Sets or overwrites a Luau compiler used for this chunk. /// - /// See [`Compiler::set_coverage_level`] for details. + /// See [`Compiler`] for details and possible options. /// /// Requires `feature = "luau"` #[cfg(any(feature = "luau", doc))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - pub fn set_coverage_level(mut self, level: u8) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_coverage_level(level); - self - } - - #[cfg(any(feature = "luau", doc))] - #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - #[doc(hidden)] - pub fn set_vector_lib(mut self, lib: Option<String>) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_vector_lib(lib); - self - } - - #[cfg(any(feature = "luau", doc))] - #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - #[doc(hidden)] - pub fn set_vector_ctor(mut self, ctor: Option<String>) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_vector_ctor(ctor); - self - } - - /// Sets a list of globals that are mutable for Luau compiler. - /// - /// See [`Compiler::set_mutable_globals`] for details. - /// - /// Requires `feature = "luau"` - #[cfg(any(feature = "luau", doc))] - #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] - pub fn set_mutable_globals(mut self, globals: Vec<String>) -> Self { - self.compiler - .get_or_insert_with(Default::default) - .set_mutable_globals(globals); + pub fn set_compiler(mut self, compiler: Compiler) -> Self { + self.compiler = Some(compiler); self } @@ -52,7 +52,7 @@ use crate::{hook::HookTriggers, types::HookCallback}; #[cfg(feature = "luau")] use crate::types::InterruptCallback; #[cfg(any(feature = "luau", doc))] -use crate::types::VmState; +use crate::{chunk::Compiler, types::VmState}; #[cfg(feature = "async")] use { @@ -78,6 +78,8 @@ pub struct LuaInner { main_state: *mut ffi::lua_State, extra: Arc<UnsafeCell<ExtraData>>, safe: bool, + #[cfg(feature = "luau")] + compiler: Option<Compiler>, // Lua has lots of interior mutability, should not be RefUnwindSafe _no_ref_unwind_safe: PhantomData<UnsafeCell<()>>, } @@ -633,6 +635,8 @@ impl Lua { main_state, extra: Arc::clone(&extra), safe: false, + #[cfg(feature = "luau")] + compiler: None, _no_ref_unwind_safe: PhantomData, })); @@ -1333,6 +1337,20 @@ impl Lua { } } + /// Sets a default Luau compiler (with custom options). + /// + /// This compiler will be used by default to load all Lua chunks + /// including via `require` function. + /// + /// See [`Compiler`] for details and possible options. + /// + /// Requires `feature = "luau"` + #[cfg(any(feature = "luau", doc))] + #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] + pub fn set_compiler(&self, compiler: Compiler) { + unsafe { (*self.0.get()).compiler = Some(compiler) }; + } + /// Returns Lua source code as a `Chunk` builder type. /// /// In order to actually compile or run the resulting code, you must call [`Chunk::exec`] or @@ -1355,7 +1373,7 @@ impl Lua { env: source.env(self), mode: source.mode(), #[cfg(feature = "luau")] - compiler: None, + compiler: self.compiler.clone(), } } diff --git a/tests/luau.rs b/tests/luau.rs index aba581d..913ece2 100644 --- a/tests/luau.rs +++ b/tests/luau.rs @@ -5,7 +5,7 @@ use std::fs; use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::Arc; -use mlua::{Error, Lua, Result, Table, ThreadStatus, Value, VmState}; +use mlua::{Compiler, Error, Lua, Result, Table, ThreadStatus, Value, VmState}; #[test] fn test_require() -> Result<()> { @@ -59,7 +59,7 @@ fn test_vectors() -> Result<()> { assert(v.z == 3) "#, ) - .set_vector_ctor(Some("vector".to_string())) + .set_compiler(Compiler::new().set_vector_ctor(Some("vector".to_string()))) .exec()?; Ok(()) |