Skip to content

Commit c64538d

Browse files
committed
add more Ex variant functions
1 parent 933be7a commit c64538d

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

src/tests.zig

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ test "type of and getting values" {
252252
lua.pushLightUserdata(&value);
253253
lua.pushNil();
254254
lua.pushNumber(0.1);
255-
_ = lua.pushThread();
255+
lua.pushThread();
256256
try expectEqualStrings(
257257
"all your codebase are belong to us",
258258
lua.pushStringEx("all your codebase are belong to us"),
@@ -1024,18 +1024,18 @@ test "debug interface" {
10241024
.call => {
10251025
l.getInfo(.{ .l = true, .r = true }, i);
10261026
if (i.current_line.? != 2) panic("Expected line to be 2", .{});
1027-
_ = l.getLocal(i, i.first_transfer) catch unreachable;
1027+
l.getLocal(i, i.first_transfer) catch unreachable;
10281028
if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected x to equal 3", .{});
10291029
},
10301030
.line => if (i.current_line.? == 4) {
10311031
// modify the value of y to be 0 right before returning
10321032
l.pushNumber(0);
1033-
_ = l.setLocal(i, 2) catch unreachable;
1033+
l.setLocal(i, 2) catch unreachable;
10341034
},
10351035
.ret => {
10361036
l.getInfo(.{ .l = true, .r = true }, i);
10371037
if (i.current_line.? != 4) panic("Expected line to be 4", .{});
1038-
_ = l.getLocal(i, i.first_transfer) catch unreachable;
1038+
l.getLocal(i, i.first_transfer) catch unreachable;
10391039
if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected result to equal 3", .{});
10401040
},
10411041
else => unreachable,
@@ -1081,7 +1081,7 @@ test "debug upvalues" {
10811081

10821082
// now make the function an "add five" function
10831083
lua.pushNumber(5);
1084-
_ = try lua.setUpvalue(-2, 1);
1084+
try lua.setUpvalue(-2, 1);
10851085

10861086
// test a bad index (the valid one's result is unpredicable)
10871087
try expectError(Error.Fail, lua.upvalueId(-1, 2));
@@ -1142,7 +1142,7 @@ test "aux check functions" {
11421142
_ = l.checkBytes(3);
11431143
_ = l.checkNumber(4);
11441144
_ = l.checkString(5);
1145-
_ = l.checkType(6, .boolean);
1145+
l.checkType(6, .boolean);
11461146
return 0;
11471147
}
11481148
}.inner);

src/ziglua.zig

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -829,9 +829,14 @@ pub const Lua = struct {
829829
return c.lua_pushstring(lua.state, str.ptr).?[0..str.len :0];
830830
}
831831

832+
/// Pushes this thread onto the stack
833+
pub fn pushThread(lua: *Lua) void {
834+
_ = lua.pushThreadEx();
835+
}
836+
832837
/// Pushes this thread onto the stack
833838
/// Returns true if this thread is the main thread of its state
834-
pub fn pushThread(lua: *Lua) bool {
839+
pub fn pushThreadEx(lua: *Lua) bool {
835840
return c.lua_pushthread(lua.state) != 0;
836841
}
837842

@@ -1203,7 +1208,13 @@ pub const Lua = struct {
12031208
}
12041209

12051210
/// Gets information about a local variable
1206-
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1211+
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1212+
_ = try lua.getLocalEx(info, n);
1213+
}
1214+
1215+
/// Gets information about a local variable
1216+
/// Returns the name of the local variable
1217+
pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
12071218
var ar: Debug = undefined;
12081219
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
12091220
if (c.lua_getlocal(lua.state, &ar, n)) |name| {
@@ -1233,9 +1244,15 @@ pub const Lua = struct {
12331244
c.lua_sethook(lua.state, hook_fn, hook_mask, count);
12341245
}
12351246

1247+
/// Sets the value of a local variable
1248+
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1249+
_ = try lua.setLocalEx(info, n);
1250+
}
1251+
12361252
/// Sets the value of a local variable
12371253
/// Returns an error when the index is greater than the number of active locals
1238-
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1254+
/// Returns the name of the local variable
1255+
pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
12391256
var ar: Debug = undefined;
12401257
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
12411258
if (c.lua_setlocal(lua.state, &ar, n)) |name| {
@@ -1245,7 +1262,14 @@ pub const Lua = struct {
12451262
}
12461263

12471264
/// Sets the value of a closure's upvalue
1248-
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
1265+
/// Returns an error if the upvalu does not exist
1266+
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void {
1267+
_ = try lua.setUpvalueEx(func_index, n);
1268+
}
1269+
1270+
/// Sets the value of a closure's upvalue
1271+
/// Returns the name of the upvalue or an error if the upvalue does not exist
1272+
pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
12491273
if (c.lua_setupvalue(lua.state, func_index, n)) |name| {
12501274
return std.mem.span(name);
12511275
}

0 commit comments

Comments
 (0)