feat: add cProtectedCall to lua 5.1 api
This function was missed. It doesn't seem horribly useful, but might as well support it.
This commit is contained in:
parent
edf63862ac
commit
2beee535e5
|
@ -356,6 +356,20 @@ pub const Lua = struct {
|
||||||
c.lua_concat(lua.state, n);
|
c.lua_concat(lua.state, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calls the C function c_fn in protected mode. The function starts with only one element on its
|
||||||
|
/// stack, the userdata given to this function.
|
||||||
|
/// See https://www.lua.org/manual/5.1/manual.html#lua_cpcall
|
||||||
|
pub fn cProtectedCall(lua: *Lua, c_fn: CFn, userdata: *anyopaque) !void {
|
||||||
|
const ret = c.lua_cpcall(lua.state, c_fn, userdata);
|
||||||
|
switch (ret) {
|
||||||
|
StatusCode.ok => return,
|
||||||
|
StatusCode.err_runtime => return error.Runtime,
|
||||||
|
StatusCode.err_memory => return error.Memory,
|
||||||
|
StatusCode.err_error => return error.MsgHandler,
|
||||||
|
else => unreachable,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new empty table and pushes onto the stack
|
/// Creates a new empty table and pushes onto the stack
|
||||||
/// num_arr is a hint for how many elements the table will have as a sequence
|
/// num_arr is a hint for how many elements the table will have as a sequence
|
||||||
/// num_rec is a hint for how many other elements the table will have
|
/// num_rec is a hint for how many other elements the table will have
|
||||||
|
|
|
@ -322,6 +322,25 @@ test "calling a function" {
|
||||||
try expectEqual(@as(i64, 42), lua.toInteger(1));
|
try expectEqual(@as(i64, 42), lua.toInteger(1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "calling a function with cProtectedCall" {
|
||||||
|
var lua = try Lua.init(testing.allocator);
|
||||||
|
defer lua.deinit();
|
||||||
|
|
||||||
|
var value: i32 = 1234;
|
||||||
|
|
||||||
|
const testFn = struct {
|
||||||
|
fn inner(l: *Lua) i32 {
|
||||||
|
const passedValue = l.toUserdata(i32, 1) catch unreachable;
|
||||||
|
if (passedValue.* != 1234) unreachable;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}.inner;
|
||||||
|
|
||||||
|
// cProtectedCall doesn't return values on the stack, so the test just makes
|
||||||
|
// sure things work!
|
||||||
|
try lua.cProtectedCall(ziglua.wrap(testFn), &value);
|
||||||
|
}
|
||||||
|
|
||||||
test "string buffers" {
|
test "string buffers" {
|
||||||
var lua = try Lua.init(testing.allocator);
|
var lua = try Lua.init(testing.allocator);
|
||||||
defer lua.deinit();
|
defer lua.deinit();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user