summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlex Orlenko <zxteam@protonmail.com>2022-04-13 22:59:35 +0100
committerAlex Orlenko <zxteam@protonmail.com>2022-04-14 00:54:29 +0100
commit21affdadfd69428883dfeca28f457e02bad0a162 (patch)
tree984aec97fc3a7d924fe3e6e05a7f5b8227ff2167 /src
parent17473269f8c652ff82f8d15b93a6be50db19fc71 (diff)
downloadmlua-21affdadfd69428883dfeca28f457e02bad0a162.zip
Remove Luau compiler options from Chunk in favour of setting Compiler instance.
Add ability to set global Luau compiler used for load all chunks including via require function.
Diffstat (limited to 'src')
-rw-r--r--src/chunk.rs84
-rw-r--r--src/lua.rs22
2 files changed, 30 insertions, 76 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
}
diff --git a/src/lua.rs b/src/lua.rs
index 0b4577a..be7b907 100644
--- a/src/lua.rs
+++ b/src/lua.rs
@@ -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(),
}
}