Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 23, 2024
1 parent 8d8d521 commit 446d63a
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Function>("lua_function")?;
let rust_function = lua.create_function(|_, ()| Ok("hello"))?;

globals.set("rust_function", rust_function)?;
assert_eq!(lua_function.call::<String>(())?, "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::<bool>("c_function")?, true);

Ok(())
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_recursion() -> Result<()> {
Expand Down

0 comments on commit 446d63a

Please sign in to comment.