Skip to main content

join_varargs/
join_varargs.rs

1use low_luajit::{AmountReturned, LuaState, LuaType};
2
3extern "C-unwind" fn join(mut lua: LuaState) -> AmountReturned {
4	// Check that the first argument is a function.
5	lua.check_type(1, LuaType::Function);
6
7	let top = lua.get_top();
8
9	// Move the function to the top of the stack and call it to have all its returned values pushed onto the stack.
10	lua.push_value(1);
11	lua.remove(1);
12	lua.call(0, LuaState::MULTI_RETURN);
13
14	// The values returned by the function should come *before* join's arguments, so let's move them back to the bottom of the stack.
15	let final_top = lua.get_top();
16	for _ in top..=final_top {
17		lua.insert(1);
18	}
19
20	// Return all the values in the stack.
21	lua.return_any(final_top)
22}
23
24#[unsafe(no_mangle)]
25extern "C-unwind" fn luaopen_join_varargs(lua: LuaState) -> AmountReturned {
26	lua.return_cfunction(join)
27}