Skip to main content

join_varargs/
join_varargs.rs

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