From 446d63a77e3a368adea0e10d2634d81e97c5b97d Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Wed, 23 Oct 2024 15:50:58 +0100 Subject: [PATCH] More tests --- tests/tests.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/tests.rs b/tests/tests.rs index 65335845..00158c6f 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -872,6 +872,49 @@ fn test_application_data() -> Result<()> { Ok(()) } +#[test] +fn test_rust_function() -> Result<()> { + let lua = Lua::new(); + + let globals = lua.globals(); + lua.load( + r#" + function lua_function() + return rust_function() + end + + -- Test to make sure chunk return is ignored + return 1 + "#, + ) + .exec()?; + + let lua_function = globals.get::("lua_function")?; + let rust_function = lua.create_function(|_, ()| Ok("hello"))?; + + globals.set("rust_function", rust_function)?; + assert_eq!(lua_function.call::(())?, "hello"); + + Ok(()) +} + +#[test] +fn test_c_function() -> Result<()> { + let lua = Lua::new(); + + unsafe extern "C-unwind" fn c_function(state: *mut mlua::lua_State) -> std::os::raw::c_int { + ffi::lua_pushboolean(state, 1); + ffi::lua_setglobal(state, b"c_function\0" as *const _ as *const _); + 0 + } + + let func = unsafe { lua.create_c_function(c_function)? }; + func.call::<()>(())?; + assert_eq!(lua.globals().get::("c_function")?, true); + + Ok(()) +} + #[test] #[cfg(not(target_arch = "wasm32"))] fn test_recursion() -> Result<()> {