diff --git a/tests/src/lib.rs b/tests/src/lib.rs index d92223d91b16b98769cb88e0b43ecef80e7ae596..869742d33c1ecc2b7b090c6dd9ae00aa5758ef2a 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -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, diff --git a/tests/src/tlua/functions_write.rs b/tests/src/tlua/functions_write.rs index f8b225fd31dcdbbb96d6c54b851612967b196456..b23aad91cf071429cb8d7f7949e53d9ef34dbc57 100644 --- a/tests/src/tlua/functions_write.rs +++ b/tests/src/tlua/functions_write.rs @@ -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); +}