Skip to content
Snippets Groups Projects
Commit 0962caf2 authored by Georgy Moshkin's avatar Georgy Moshkin :speech_balloon:
Browse files

test(tlua): test receiving lua functions as callback arguments

parent 2540ccd5
No related branches found
No related tags found
1 merge request!237test(tlua): test receiving lua functions as callback arguments
......@@ -269,6 +269,7 @@ fn run_tests(cfg: TestConfig) -> Result<bool, io::Error> {
tlua::functions_write::pcall,
tlua::functions_write::error,
tlua::functions_write::optional_params,
tlua::functions_write::lua_function_as_argument,
tlua::any::read_numbers,
tlua::any::read_hashable_numbers,
tlua::any::read_strings,
......
......@@ -322,3 +322,30 @@ pub fn optional_params() {
"Sup, Sailor!"
);
}
pub fn lua_function_as_argument() {
let lua = Lua::new();
let my_data = std::rc::Rc::new(std::cell::Cell::new(0));
let my_data_in_lua = my_data.clone();
lua.set(
"apply_to_my_data",
Function::new(move |lua: tlua::StaticLua| {
let f: tlua::LuaFunction<_> = (&lua).read_at(1).unwrap();
if let Ok(y) = (&lua).read_at::<i32>(2) {
my_data_in_lua.set(f.call_with_args(&(my_data_in_lua.get(), y)).unwrap());
} else {
my_data_in_lua.set(f.call_with_args(my_data_in_lua.get()).unwrap());
}
}),
);
assert_eq!(my_data.get(), 0);
lua.exec("apply_to_my_data(function(x) return x + 1 end)")
.unwrap();
assert_eq!(my_data.get(), 1);
lua.exec("apply_to_my_data(function(x) return 42 end)")
.unwrap();
assert_eq!(my_data.get(), 42);
lua.exec("apply_to_my_data(function(x, y) return x + y end, 27)")
.unwrap();
assert_eq!(my_data.get(), 69);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment