diff --git a/src/state.rs b/src/state.rs index fef8f39a..9734a601 100644 --- a/src/state.rs +++ b/src/state.rs @@ -344,36 +344,6 @@ impl Lua { loaded.raw_set(modname, Nil) } - /// Consumes and leaks `Lua` object, returning a static reference `&'static Lua`. - /// - /// This function is useful when the `Lua` object is supposed to live for the remainder - /// of the program's life. - /// - /// Dropping the returned reference will cause a memory leak. If this is not acceptable, - /// the reference should first be wrapped with the [`Lua::from_static`] function producing a - /// `Lua`. This `Lua` object can then be dropped which will properly release the allocated - /// memory. - /// - /// [`Lua::from_static`]: #method.from_static - /// - /// FIXME: remove - #[doc(hidden)] - pub fn into_static(self) -> &'static Self { - Box::leak(Box::new(self)) - } - - /// Constructs a `Lua` from a static reference to it. - /// - /// # Safety - /// This function is unsafe because improper use may lead to memory problems or undefined - /// behavior. - /// - /// FIXME: remove - #[doc(hidden)] - pub unsafe fn from_static(lua: &'static Lua) -> Self { - *Box::from_raw(lua as *const Lua as *mut Lua) - } - // Executes module entrypoint function, which returns only one Value. // The returned value then pushed onto the stack. #[doc(hidden)] diff --git a/tests/static.rs b/tests/static.rs deleted file mode 100644 index aeb7eca7..00000000 --- a/tests/static.rs +++ /dev/null @@ -1,122 +0,0 @@ -use std::cell::RefCell; - -use mlua::{Lua, Result, Table}; - -#[test] -fn test_static_lua() -> Result<()> { - let lua = Lua::new().into_static(); - - thread_local! { - static TABLE: RefCell> = RefCell::new(None); - } - - let f = lua.create_function(|_, table: Table| { - TABLE.with(|t| { - table.raw_insert(1, "hello")?; - *t.borrow_mut() = Some(table); - Ok(()) - }) - })?; - - f.call::<()>(lua.create_table()?)?; - drop(f); - lua.gc_collect()?; - - TABLE.with(|t| { - assert!(t.borrow().as_ref().unwrap().len().unwrap() == 1); - *t.borrow_mut() = None; - }); - - // Consume the Lua instance - unsafe { Lua::from_static(lua) }; - - Ok(()) -} - -#[test] -fn test_static_lua_coroutine() -> Result<()> { - let lua = Lua::new().into_static(); - - thread_local! { - static TABLE: RefCell> = RefCell::new(None); - } - - let f = lua.create_function(|_, table: Table| { - TABLE.with(|t| { - table.raw_insert(1, "hello")?; - *t.borrow_mut() = Some(table); - Ok(()) - }) - })?; - - let co = lua.create_thread(f)?; - co.resume::<()>(lua.create_table()?)?; - drop(co); - lua.gc_collect()?; - - TABLE.with(|t| { - assert_eq!( - t.borrow().as_ref().unwrap().get::(1i32).unwrap(), - "hello".to_string() - ); - *t.borrow_mut() = None; - }); - - // Consume the Lua instance - unsafe { Lua::from_static(lua) }; - - Ok(()) -} - -#[cfg(feature = "async")] -#[tokio::test] -async fn test_static_async() -> Result<()> { - let lua = Lua::new().into_static(); - - #[cfg(not(target_arch = "wasm32"))] - async fn sleep_ms(ms: u64) { - tokio::time::sleep(std::time::Duration::from_millis(ms)).await; - } - - #[cfg(target_arch = "wasm32")] - async fn sleep_ms(_ms: u64) { - tokio::task::yield_now().await; - } - - let timer = lua.create_async_function(|_, (i, n, f): (u64, u64, mlua::Function)| async move { - tokio::task::spawn_local(async move { - for _ in 0..n { - tokio::task::spawn_local(f.call_async::<()>(())); - sleep_ms(i).await; - } - }); - Ok(()) - })?; - lua.globals().set("timer", timer)?; - - { - let local_set = tokio::task::LocalSet::new(); - local_set - .run_until( - lua.load( - r#" - local cnt = 0 - timer(1, 100, function() - cnt = cnt + 1 - if cnt % 10 == 0 then - collectgarbage() - end - end) - "#, - ) - .exec_async(), - ) - .await?; - local_set.await; - } - - // Consume the Lua instance - unsafe { Lua::from_static(lua) }; - - Ok(()) -}