Skip to content

Commit 933be7a

Browse files
committed
refactor getGlobal and add getGlobalEx
Adds an extended getGlobal function that returns the type. Both now return an error when the global is nil.
1 parent e06585e commit 933be7a

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

src/tests.zig

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ test "executing string contents" {
327327
try lua.loadString("a = f(2)");
328328
try lua.protectedCall(0, 0, 0);
329329

330-
try expectEqual(LuaType.function, lua.getGlobal("f"));
330+
try expectEqual(LuaType.function, try lua.getGlobalEx("f"));
331331
lua.pop(1);
332-
try expectEqual(LuaType.number, lua.getGlobal("a"));
332+
try expectEqual(LuaType.number, try lua.getGlobalEx("a"));
333333
try expectEqual(@as(i64, 12), try lua.toInteger(1));
334334

335335
try expectError(Error.Syntax, lua.loadString("bad syntax"));
@@ -413,7 +413,7 @@ test "calling a function" {
413413
defer lua.deinit();
414414

415415
lua.register("zigadd", ziglua.wrap(add));
416-
_ = lua.getGlobal("zigadd");
416+
try lua.getGlobal("zigadd");
417417
lua.pushInteger(10);
418418
lua.pushInteger(32);
419419

@@ -630,7 +630,7 @@ test "table access" {
630630
defer lua.deinit();
631631

632632
try lua.doString("a = { [1] = 'first', key = 'value', ['other one'] = 1234 }");
633-
_ = lua.getGlobal("a");
633+
try lua.getGlobal("a");
634634

635635
try expectEqual(LuaType.string, lua.getIndex(1, 1));
636636
try expectEqualStrings("first", try lua.toBytes(-1));
@@ -672,7 +672,7 @@ test "table access" {
672672
lua.setField(1, "bool");
673673

674674
try lua.doString("b = a.bool");
675-
try expectEqual(LuaType.boolean, lua.getGlobal("b"));
675+
try expectEqual(LuaType.boolean, try lua.getGlobalEx("b"));
676676
try expect(lua.toBoolean(-1));
677677

678678
// create array [1, 2, 3, 4, 5]
@@ -728,7 +728,7 @@ test "dump and load" {
728728
// store a function in a global
729729
try lua.doString("f = function(x) return function(n) return n + x end end");
730730
// put the function on the stack
731-
_ = lua.getGlobal("f");
731+
try lua.getGlobal("f");
732732

733733
const writer = struct {
734734
fn inner(l: *Lua, buf: []const u8, data: *anyopaque) bool {
@@ -841,7 +841,7 @@ test "upvalues" {
841841
// call the function repeatedly, each time ensuring the result increases by one
842842
var expected: Integer = 1;
843843
while (expected <= 10) : (expected += 1) {
844-
_ = lua.getGlobal("counter");
844+
try lua.getGlobal("counter");
845845
lua.call(0, 1);
846846
try expectEqual(expected, try lua.toInteger(-1));
847847
lua.pop(1);
@@ -853,7 +853,7 @@ test "table traversal" {
853853
defer lua.deinit();
854854

855855
try lua.doString("t = { key = 'value', second = true, third = 1 }");
856-
_ = lua.getGlobal("t");
856+
try lua.getGlobal("t");
857857

858858
lua.pushNil();
859859

@@ -905,21 +905,21 @@ test "closing vars" {
905905
);
906906

907907
lua.newTable();
908-
_ = lua.getGlobal("mt");
908+
try lua.getGlobal("mt");
909909
lua.setMetatable(-2);
910910
lua.toClose(-1);
911911
lua.closeSlot(-1);
912912
lua.pop(1);
913913

914914
lua.newTable();
915-
_ = lua.getGlobal("mt");
915+
try lua.getGlobal("mt");
916916
lua.setMetatable(-2);
917917
lua.toClose(-1);
918918
lua.closeSlot(-1);
919919
lua.pop(1);
920920

921921
// this should have incremented "closed_vars" to 2
922-
_ = lua.getGlobal("closed_vars");
922+
try lua.getGlobal("closed_vars");
923923
try expectEqual(@as(Number, 2), try lua.toNumber(-1));
924924
}
925925

@@ -993,7 +993,7 @@ test "debug interface" {
993993
\\ return x + y
994994
\\end
995995
);
996-
_ = lua.getGlobal("f");
996+
try lua.getGlobal("f");
997997

998998
var info: DebugInfo = undefined;
999999
lua.getInfo(.{
@@ -1052,7 +1052,7 @@ test "debug interface" {
10521052
try expectEqual(@as(?ziglua.CHookFn, ziglua.wrap(hook)), lua.getHook());
10531053
try expectEqual(ziglua.HookMask{ .call = true, .line = true, .ret = true }, lua.getHookMask());
10541054

1055-
_ = lua.getGlobal("f");
1055+
try lua.getGlobal("f");
10561056
lua.pushNumber(3);
10571057
try lua.protectedCall(1, 1, 0);
10581058
}
@@ -1069,7 +1069,7 @@ test "debug upvalues" {
10691069
\\end
10701070
\\addone = f(1)
10711071
);
1072-
_ = lua.getGlobal("addone");
1072+
try lua.getGlobal("addone");
10731073

10741074
// index doesn't exist
10751075
try expectError(Error.Fail, lua.getUpvalue(1, 2));
@@ -1096,8 +1096,8 @@ test "debug upvalues" {
10961096
\\addthree = f(3)
10971097
);
10981098

1099-
_ = lua.getGlobal("addone");
1100-
_ = lua.getGlobal("addthree");
1099+
try lua.getGlobal("addone");
1100+
try lua.getGlobal("addthree");
11011101

11021102
// now addone and addthree share the same upvalue
11031103
lua.upvalueJoin(-2, 1, -1, 1);
@@ -1220,6 +1220,13 @@ test "aux check functions" {
12201220
try lua.protectedCall(6, 0, 0);
12211221
}
12221222

1223+
test "get global fail" {
1224+
var lua = try Lua.init(testing.allocator);
1225+
defer lua.deinit();
1226+
1227+
try expectError(Error.Fail, lua.getGlobal("foo"));
1228+
}
1229+
12231230
test "refs" {
12241231
// temporary test that includes a reference to all functions so
12251232
// they will be type-checked

src/ziglua.zig

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ pub const Lua = struct {
484484

485485
/// Controls the garbage collector
486486
/// The return value type is dependent on the given action
487+
/// TODO: split into separate functions
487488
pub fn gc(lua: *Lua, comptime action: GCAction, args: anytype) TypeOfGC(action) {
488489
const val = @enumToInt(action);
489490
switch (action) {
@@ -526,10 +527,18 @@ pub const Lua = struct {
526527
return @intToEnum(LuaType, c.lua_getfield(lua.state, index, key));
527528
}
528529

530+
/// Pushes onto the stack the value of the global `name`
531+
/// Returns an error if the global does not exist (is nil)
532+
pub fn getGlobal(lua: *Lua, name: [:0]const u8) !void {
533+
_ = try lua.getGlobalEx(name);
534+
}
535+
529536
/// Pushes onto the stack the value of the global `name`. Returns the type of that value
530-
/// NOTE: could return an error if it was successful (not nil)
531-
pub fn getGlobal(lua: *Lua, name: [:0]const u8) LuaType {
532-
return @intToEnum(LuaType, c.lua_getglobal(lua.state, name));
537+
/// Returns an error if the global does not exist (is nil)
538+
pub fn getGlobalEx(lua: *Lua, name: [:0]const u8) !LuaType {
539+
const lua_type = @intToEnum(LuaType, c.lua_getglobal(lua.state, name));
540+
if (lua_type == .nil) return Error.Fail;
541+
return lua_type;
533542
}
534543

535544
/// Pushes onto the stack the value t[`i`] where t is the value at the given `index`

0 commit comments

Comments
 (0)