diff --git a/src/function.rs b/src/function.rs index 60ee7ea..6341cec 100644 --- a/src/function.rs +++ b/src/function.rs @@ -213,6 +213,10 @@ impl<'lua> Function<'lua> { /// /// If `strip` is true, the binary representation may not include all debug information /// about the function, to save space. + /// + /// For Luau a [Compiler] can be used to compile Lua chunks to bytecode. + /// + /// [Compiler]: crate::chunk::Compiler #[cfg(not(feature = "luau"))] #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))] pub fn dump(&self, strip: bool) -> Vec { diff --git a/src/lua.rs b/src/lua.rs index 0ad15ce..74fd35c 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -48,7 +48,9 @@ use { use crate::{hook::HookTriggers, types::HookCallback}; #[cfg(feature = "luau")] -use crate::types::{InterruptCallback, VmState}; +use crate::types::InterruptCallback; +#[cfg(any(feature = "luau", doc))] +use crate::types::VmState; #[cfg(feature = "async")] use { @@ -137,6 +139,7 @@ pub enum GCMode { Incremental, /// Requires `feature = "lua54"` #[cfg(any(feature = "lua54"))] + #[cfg_attr(docsrs, doc(cfg(feature = "lua54")))] Generational, } @@ -311,6 +314,7 @@ impl Lua { /// /// [`StdLib`]: crate::StdLib pub fn new_with(libs: StdLib, options: LuaOptions) -> Result { + #[cfg(not(feature = "luau"))] if libs.contains(StdLib::DEBUG) { return Err(Error::SafetyError( "the unsafe `debug` module can't be loaded using safe `new_with`".to_string(), @@ -583,10 +587,10 @@ impl Lua { // Register `DestructedUserdataMT` type get_destructed_userdata_metatable(main_state); let destructed_mt_ptr = ffi::lua_topointer(main_state, -1); - (*extra.get()).registered_userdata_mt.insert( - destructed_mt_ptr, - Some(TypeId::of::()), - ); + let destructed_mt_typeid = Some(TypeId::of::()); + (*extra.get()) + .registered_userdata_mt + .insert(destructed_mt_ptr, destructed_mt_typeid); ffi::lua_pop(main_state, 1); mlua_debug_assert!( @@ -619,6 +623,7 @@ impl Lua { /// /// [`StdLib`]: crate::StdLib pub fn load_from_std_lib(&self, libs: StdLib) -> Result<()> { + #[cfg(not(feature = "luau"))] if self.safe && libs.contains(StdLib::DEBUG) { return Err(Error::SafetyError( "the unsafe `debug` module can't be loaded in safe mode".to_string(), @@ -805,7 +810,8 @@ impl Lua { /// ``` /// /// Requires `feature = "luau"` - #[cfg(feature = "luau")] + #[cfg(any(feature = "luau", docsrs))] + #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn sandbox(&self, enabled: bool) -> Result<()> { unsafe { let extra = &mut *self.extra.get(); @@ -954,7 +960,7 @@ impl Lua { /// # Ok(()) /// # } /// ``` - #[cfg(feature = "luau")] + #[cfg(any(feature = "luau", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn set_interrupt(&self, callback: F) where @@ -996,7 +1002,7 @@ impl Lua { /// Removes any 'interrupt' previously set by `set_interrupt`. /// /// This function has no effect if an 'interrupt' was not previously set. - #[cfg(feature = "luau")] + #[cfg(any(feature = "luau", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn remove_interrupt(&self) { let state = mlua_expect!(self.main_state, "Luau should always has main state"); @@ -1010,6 +1016,7 @@ impl Lua { /// /// Requires `feature = "lua54"` #[cfg(feature = "lua54")] + #[cfg_attr(docsrs, doc(cfg(feature = "lua54")))] pub fn set_warning_function(&self, callback: F) where F: 'static + MaybeSend + Fn(&Lua, &CStr, bool) -> Result<()>, @@ -1044,6 +1051,7 @@ impl Lua { /// /// Requires `feature = "lua54"` #[cfg(feature = "lua54")] + #[cfg_attr(docsrs, doc(cfg(feature = "lua54")))] pub fn remove_warning_function(&self) { let state = self.main_state.unwrap_or(self.state); unsafe { @@ -1058,6 +1066,7 @@ impl Lua { /// /// Requires `feature = "lua54"` #[cfg(feature = "lua54")] + #[cfg_attr(docsrs, doc(cfg(feature = "lua54")))] pub fn warning>>(&self, msg: S, tocont: bool) -> Result<()> { let msg = CString::new(msg).map_err(|err| Error::RuntimeError(err.to_string()))?; unsafe { ffi::lua_warning(self.state, msg.as_ptr(), if tocont { 1 } else { 0 }) }; @@ -1188,22 +1197,27 @@ impl Lua { /// Sets the 'pause' value of the collector. /// /// Returns the previous value of 'pause'. More information can be found in the Lua - /// [documentation][lua_doc]. + /// [documentation]. /// - /// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5 - #[cfg(not(feature = "luau"))] - #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))] + /// For Luau this parameter sets GC goal + /// + /// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5 pub fn gc_set_pause(&self, pause: c_int) -> c_int { let state = self.main_state.unwrap_or(self.state); - unsafe { ffi::lua_gc(state, ffi::LUA_GCSETPAUSE, pause) } + unsafe { + #[cfg(not(feature = "luau"))] + return ffi::lua_gc(state, ffi::LUA_GCSETPAUSE, pause); + #[cfg(feature = "luau")] + return ffi::lua_gc(state, ffi::LUA_GCSETGOAL, pause); + } } /// Sets the 'step multiplier' value of the collector. /// /// Returns the previous value of the 'step multiplier'. More information can be found in the - /// Lua [documentation][lua_doc]. + /// Lua [documentation]. /// - /// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5 + /// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5 pub fn gc_set_step_multiplier(&self, step_multiplier: c_int) -> c_int { let state = self.main_state.unwrap_or(self.state); unsafe { ffi::lua_gc(state, ffi::LUA_GCSETSTEPMUL, step_multiplier) } @@ -1212,11 +1226,9 @@ impl Lua { /// Changes the collector to incremental mode with the given parameters. /// /// Returns the previous mode (always `GCMode::Incremental` in Lua < 5.4). - /// More information can be found in the Lua [documentation][lua_doc]. + /// More information can be found in the Lua [documentation]. /// - /// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5.1 - #[cfg(not(feature = "luau"))] - #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))] + /// [documentation]: https://www.lua.org/manual/5.4/manual.html#2.5.1 pub fn gc_inc(&self, pause: c_int, step_multiplier: c_int, step_size: c_int) -> GCMode { let state = self.main_state.unwrap_or(self.state); @@ -1224,16 +1236,28 @@ impl Lua { feature = "lua53", feature = "lua52", feature = "lua51", - feature = "luajit" + feature = "luajit", + feature = "luau" ))] - { + unsafe { if pause > 0 { - unsafe { ffi::lua_gc(state, ffi::LUA_GCSETPAUSE, pause) }; + #[cfg(not(feature = "luau"))] + ffi::lua_gc(state, ffi::LUA_GCSETPAUSE, pause); + #[cfg(feature = "luau")] + ffi::lua_gc(state, ffi::LUA_GCSETGOAL, pause); } + if step_multiplier > 0 { - unsafe { ffi::lua_gc(state, ffi::LUA_GCSETSTEPMUL, step_multiplier) }; + ffi::lua_gc(state, ffi::LUA_GCSETSTEPMUL, step_multiplier); } + + #[cfg(feature = "luau")] + if step_size > 0 { + ffi::lua_gc(state, ffi::LUA_GCSETSTEPSIZE, step_size); + } + #[cfg(not(feature = "luau"))] let _ = step_size; // Ignored + GCMode::Incremental } @@ -1257,6 +1281,7 @@ impl Lua { /// /// [lua_doc]: https://www.lua.org/manual/5.4/manual.html#2.5.2 #[cfg(any(feature = "lua54"))] + #[cfg_attr(docsrs, doc(cfg(feature = "lua54")))] pub fn gc_gen(&self, minor_multiplier: c_int, major_multiplier: c_int) -> GCMode { let state = self.main_state.unwrap_or(self.state); let prev_mode = @@ -2972,7 +2997,7 @@ unsafe fn load_from_std_lib(state: *mut ffi::lua_State, libs: StdLib) -> Result< openf: ffi::lua_CFunction, glb: c_int, ) -> Result<()> { - let modname = mlua_expect!(CString::new(modname.as_ref()), "modname contains nil bytes"); + let modname = mlua_expect!(CString::new(modname.as_ref()), "modname contains nil byte"); protect_lua!(state, 0, 1, |state| { ffi::luaL_requiref(state, modname.as_ptr() as *const c_char, openf, glb) }) diff --git a/src/stdlib.rs b/src/stdlib.rs index 567fc6c..34c7629 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -46,12 +46,14 @@ impl StdLib { /// /// Requires `feature = "luajit"` #[cfg(any(feature = "luajit", doc))] + #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))] pub const JIT: StdLib = StdLib(1 << 9); /// (**unsafe**) [`ffi`](http://luajit.org/ext_ffi.html) library /// /// Requires `feature = "luajit"` #[cfg(any(feature = "luajit", doc))] + #[cfg_attr(docsrs, doc(cfg(feature = "luajit")))] pub const FFI: StdLib = StdLib(1 << 30); /// (**unsafe**) [`debug`](https://www.lua.org/manual/5.4/manual.html#6.10) library pub const DEBUG: StdLib = StdLib(1 << 31); @@ -61,7 +63,10 @@ impl StdLib { /// (**unsafe**) All standard libraries pub const ALL: StdLib = StdLib(u32::MAX); /// The safe subset of the standard libraries + #[cfg(not(feature = "luau"))] pub const ALL_SAFE: StdLib = StdLib((1 << 30) - 1); + #[cfg(feature = "luau")] + pub const ALL_SAFE: StdLib = StdLib(u32::MAX); pub fn contains(self, lib: Self) -> bool { (self & lib).0 != 0 diff --git a/src/table.rs b/src/table.rs index cb1e85c..05e24eb 100644 --- a/src/table.rs +++ b/src/table.rs @@ -351,7 +351,7 @@ impl<'lua> Table<'lua> { /// Sets `readonly` attribute on the table. /// /// Requires `feature = "luau"` - #[cfg(feature = "luau")] + #[cfg(any(feature = "luau", doc))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn set_readonly(&self, enabled: bool) { let lua = self.0.lua; @@ -369,7 +369,7 @@ impl<'lua> Table<'lua> { /// Returns `readonly` attribute of the table. /// /// Requires `feature = "luau"` - #[cfg(feature = "luau")] + #[cfg(any(feature = "luau", doc))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] pub fn is_readonly(&self) -> bool { let lua = self.0.lua; diff --git a/src/thread.rs b/src/thread.rs index b478ba4..88658aa 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -299,9 +299,8 @@ impl<'lua> Thread<'lua> { /// # Examples /// /// ``` - /// # #[cfg(feature = "luau")] - /// # fn main() -> mlua::Result<()> { - /// use mlua::Lua; + /// # use mlua::{Lua, Result}; + /// # fn main() -> Result<()> { /// let lua = Lua::new(); /// let thread = lua.create_thread(lua.create_function(|lua2, ()| { /// lua2.load("var = 123").exec()?; @@ -315,13 +314,10 @@ impl<'lua> Thread<'lua> { /// assert_eq!(lua.globals().get::<_, Option>("var")?, None); /// # Ok(()) /// # } - /// - /// # #[cfg(not(feature = "luau"))] - /// fn main() {} /// ``` /// /// Requires `feature = "luau"` - #[cfg(any(feature = "luau", doc))] + #[cfg(any(feature = "luau", docsrs))] #[cfg_attr(docsrs, doc(cfg(feature = "luau")))] #[doc(hidden)] pub fn sandbox(&self) -> Result<()> { diff --git a/tests/memory.rs b/tests/memory.rs index 36d094e..e359a5d 100644 --- a/tests/memory.rs +++ b/tests/memory.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use mlua::{Lua, Result, UserData}; +use mlua::{GCMode, Lua, Result, UserData}; #[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))] use mlua::Error; @@ -39,7 +39,10 @@ fn test_gc_control() -> Result<()> { let globals = lua.globals(); #[cfg(feature = "lua54")] - assert_eq!(lua.gc_gen(0, 0), mlua::GCMode::Incremental); + { + assert_eq!(lua.gc_gen(0, 0), GCMode::Incremental); + assert_eq!(lua.gc_inc(0, 0, 0), GCMode::Generational); + } #[cfg(any( feature = "lua54", @@ -55,6 +58,8 @@ fn test_gc_control() -> Result<()> { assert!(lua.gc_is_running()); } + assert_eq!(lua.gc_inc(200, 100, 13), GCMode::Incremental); + struct MyUserdata(Arc<()>); impl UserData for MyUserdata {} @@ -67,9 +72,6 @@ fn test_gc_control() -> Result<()> { lua.gc_collect()?; assert_eq!(Arc::strong_count(&rc), 1); - #[cfg(feature = "lua54")] - assert_eq!(lua.gc_inc(0, 0, 0), mlua::GCMode::Generational); - Ok(()) }