tests: test upvalue debug functions
This commit is contained in:
parent
56a823586c
commit
99904a48d4
|
@ -1054,16 +1054,59 @@ test "debug interface" {
|
||||||
try lua.protectedCall(1, 1, 0);
|
try lua.protectedCall(1, 1, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test "debug upvalues" {
|
||||||
|
var lua = try Lua.init(testing.allocator);
|
||||||
|
defer lua.deinit();
|
||||||
|
|
||||||
|
try lua.doString(
|
||||||
|
\\f = function(x)
|
||||||
|
\\ return function(y)
|
||||||
|
\\ return x + y
|
||||||
|
\\ end
|
||||||
|
\\end
|
||||||
|
\\addone = f(1)
|
||||||
|
);
|
||||||
|
_ = lua.getGlobal("addone");
|
||||||
|
|
||||||
|
// index doesn't exist
|
||||||
|
try expectError(Error.Fail, lua.getUpvalue(1, 2));
|
||||||
|
|
||||||
|
// inspect the upvalue (should be x)
|
||||||
|
try expectEqualStrings("x", try lua.getUpvalue(-1, 1));
|
||||||
|
try expectEqual(@as(Number, 1), try lua.toNumber(-1));
|
||||||
|
lua.pop(1);
|
||||||
|
|
||||||
|
// now make the function an "add five" function
|
||||||
|
lua.pushNumber(5);
|
||||||
|
_ = try lua.setUpvalue(-2, 1);
|
||||||
|
|
||||||
|
// test a bad index (the valid one's result is unpredicable)
|
||||||
|
try expectError(Error.Fail, lua.upvalueId(-1, 2));
|
||||||
|
|
||||||
|
// call the new function (should return 7)
|
||||||
|
lua.pushNumber(2);
|
||||||
|
try lua.protectedCall(1, 1, 0);
|
||||||
|
try expectEqual(@as(Number, 7), try lua.toNumber(-1));
|
||||||
|
lua.pop(1);
|
||||||
|
|
||||||
|
try lua.doString(
|
||||||
|
\\addthree = f(3)
|
||||||
|
);
|
||||||
|
|
||||||
|
_ = lua.getGlobal("addone");
|
||||||
|
_ = lua.getGlobal("addthree");
|
||||||
|
|
||||||
|
// now addone and addthree share the same upvalue
|
||||||
|
lua.upvalueJoin(-2, 1, -1, 1);
|
||||||
|
try expect((try lua.upvalueId(-2, 1)) == try lua.upvalueId(-1, 1));
|
||||||
|
}
|
||||||
|
|
||||||
test "refs" {
|
test "refs" {
|
||||||
// temporary test that includes a reference to all functions so
|
// temporary test that includes a reference to all functions so
|
||||||
// they will be type-checked
|
// they will be type-checked
|
||||||
|
|
||||||
// debug
|
// debug
|
||||||
_ = Lua.getStack;
|
_ = Lua.getStack;
|
||||||
_ = Lua.getUpvalue;
|
|
||||||
_ = Lua.setUpvalue;
|
|
||||||
_ = Lua.upvalueId;
|
|
||||||
_ = Lua.upvalueJoin;
|
|
||||||
|
|
||||||
// auxlib
|
// auxlib
|
||||||
_ = Lua.argCheck;
|
_ = Lua.argCheck;
|
||||||
|
|
|
@ -1229,8 +1229,9 @@ pub const Lua = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a unique identifier for the upvalue numbered `n` from the closure index `func_index`
|
/// Returns a unique identifier for the upvalue numbered `n` from the closure index `func_index`
|
||||||
pub fn upvalueId(lua: *Lua, func_index: i32, n: i32) ?*anyopaque {
|
pub fn upvalueId(lua: *Lua, func_index: i32, n: i32) !*anyopaque {
|
||||||
return c.lua_upvalueid(lua.state, func_index, n);
|
if (c.lua_upvalueid(lua.state, func_index, n)) |ptr| return ptr;
|
||||||
|
return Error.Fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Make the `n1`th upvalue of the Lua closure at index `func_index1` refer to the `n2`th upvalue
|
/// Make the `n1`th upvalue of the Lua closure at index `func_index1` refer to the `n2`th upvalue
|
||||||
|
|
Loading…
Reference in New Issue
Block a user