Skip to content

Commit 8cc749b

Browse files
committed
feat: remove all Ex function variations
While these functions can sometimes be convenient, it makes the API more clean to not have separate functions just to avoid using _ = at the call site. Closes #8
1 parent 4d69624 commit 8cc749b

File tree

8 files changed

+197
-357
lines changed

8 files changed

+197
-357
lines changed

src/ziglua-5.1/lib.zig

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -695,15 +695,9 @@ pub const Lua = struct {
695695
lua.pushClosure(c_fn, 0);
696696
}
697697

698-
/// Push a formatted string onto the stack
699-
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring
700-
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void {
701-
_ = lua.pushFStringEx(fmt, args);
702-
}
703-
704698
/// Push a formatted string onto the stack and return a pointer to the string
705699
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring
706-
pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
700+
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
707701
return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args);
708702
}
709703

@@ -737,30 +731,17 @@ pub const Lua = struct {
737731
c.lua_pushnumber(lua.state, n);
738732
}
739733

740-
/// Pushes a zero-terminated string on to the stack
741-
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring
742-
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
743-
_ = lua.pushStringEx(str);
744-
}
745-
746734
/// Pushes a zero-terminated string onto the stack
747735
/// Lua makes a copy of the string so `str` may be freed immediately after return
748-
/// Returns a pointer to the internal Lua string
749736
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring
750-
pub fn pushStringEx(lua: *Lua, str: [:0]const u8) void {
737+
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
751738
c.lua_pushstring(lua.state, str.ptr);
752739
}
753740

754-
/// Pushes this thread onto the stack
755-
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread
756-
pub fn pushThread(lua: *Lua) void {
757-
_ = lua.pushThreadEx();
758-
}
759-
760741
/// Pushes this thread onto the stack
761742
/// Returns true if this thread is the main thread of its state
762743
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread
763-
pub fn pushThreadEx(lua: *Lua) bool {
744+
pub fn pushThread(lua: *Lua) bool {
764745
return c.lua_pushthread(lua.state) != 0;
765746
}
766747

@@ -1064,16 +1045,10 @@ pub const Lua = struct {
10641045
}
10651046
}
10661047

1067-
/// Gets information about a local variable
1068-
/// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal
1069-
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1070-
_ = try lua.getLocalEx(info, n);
1071-
}
1072-
10731048
/// Gets information about a local variable
10741049
/// Returns the name of the local variable
10751050
/// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal
1076-
pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1051+
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
10771052
var ar: Debug = undefined;
10781053
ar.i_ci = info.private;
10791054
if (c.lua_getlocal(lua.state, &ar, n)) |name| {
@@ -1106,17 +1081,11 @@ pub const Lua = struct {
11061081
_ = c.lua_sethook(lua.state, hook_fn, hook_mask, count);
11071082
}
11081083

1109-
/// Sets the value of a local variable
1110-
/// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal
1111-
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1112-
_ = try lua.setLocalEx(info, n);
1113-
}
1114-
11151084
/// Sets the value of a local variable
11161085
/// Returns an error when the index is greater than the number of active locals
11171086
/// Returns the name of the local variable
11181087
/// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal
1119-
pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1088+
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
11201089
var ar: Debug = undefined;
11211090
ar.i_ci = info.private;
11221091
if (c.lua_setlocal(lua.state, &ar, n)) |name| {
@@ -1125,17 +1094,10 @@ pub const Lua = struct {
11251094
return error.Fail;
11261095
}
11271096

1128-
/// Sets the value of a closure's upvalue
1129-
/// Returns an error if the upvalu does not exist
1130-
/// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue
1131-
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void {
1132-
_ = try lua.setUpvalueEx(func_index, n);
1133-
}
1134-
11351097
/// Sets the value of a closure's upvalue
11361098
/// Returns the name of the upvalue or an error if the upvalue does not exist
11371099
/// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue
1138-
pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
1100+
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
11391101
if (c.lua_setupvalue(lua.state, func_index, n)) |name| {
11401102
return std.mem.span(name);
11411103
}
@@ -1221,7 +1183,7 @@ pub const Lua = struct {
12211183
}
12221184
}
12231185

1224-
return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr}));
1186+
return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr}));
12251187
}
12261188

12271189
/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size

src/ziglua-5.1/tests.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,11 @@ test "type of and getting values" {
166166
lua.pushLightUserdata(&value);
167167
lua.pushNil();
168168
lua.pushNumber(0.1);
169-
lua.pushThread();
170-
lua.pushStringEx("all your codebase are belong to us");
169+
_ = lua.pushThread();
170+
lua.pushString("all your codebase are belong to us");
171171
lua.pushFunction(ziglua.wrap(add));
172172
lua.pushBytes("hello world");
173-
lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
173+
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
174174
lua.pushValue(1);
175175

176176
// test both typeof and is functions
@@ -736,18 +736,18 @@ test "debug interface" {
736736
.call => {
737737
l.getInfo(.{ .l = true }, i);
738738
if (i.current_line.? != 2) panic("Expected line to be 2", .{});
739-
l.getLocal(i, 1) catch unreachable;
739+
_ = l.getLocal(i, 1) catch unreachable;
740740
if ((l.toNumber(-1)) != 3) panic("Expected x to equal 3", .{});
741741
},
742742
.line => if (i.current_line.? == 4) {
743743
// modify the value of y to be 0 right before returning
744744
l.pushNumber(0);
745-
l.setLocal(i, 2) catch unreachable;
745+
_ = l.setLocal(i, 2) catch unreachable;
746746
},
747747
.ret => {
748748
l.getInfo(.{ .l = true }, i);
749749
if (i.current_line.? != 4) panic("Expected line to be 4", .{});
750-
l.getLocal(i, 1) catch unreachable;
750+
_ = l.getLocal(i, 1) catch unreachable;
751751
if ((l.toNumber(-1)) != 3) panic("Expected result to equal 3", .{});
752752
},
753753
else => unreachable,
@@ -793,7 +793,7 @@ test "debug upvalues" {
793793

794794
// now make the function an "add five" function
795795
lua.pushNumber(5);
796-
try lua.setUpvalue(-2, 1);
796+
_ = try lua.setUpvalue(-2, 1);
797797

798798
// call the new function (should return 7)
799799
lua.pushNumber(2);

src/ziglua-5.2/lib.zig

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -766,13 +766,8 @@ pub const Lua = struct {
766766
lua.pushClosure(c_fn, 0);
767767
}
768768

769-
/// Push a formatted string onto the stack
770-
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void {
771-
_ = lua.pushFStringEx(fmt, args);
772-
}
773-
774769
/// Push a formatted string onto the stack and return a pointer to the string
775-
pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
770+
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
776771
return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args);
777772
}
778773

@@ -794,13 +789,8 @@ pub const Lua = struct {
794789
c.lua_pushlightuserdata(lua.state, ptr);
795790
}
796791

797-
/// Pushes a slice of bytes onto the stack
798-
pub fn pushBytes(lua: *Lua, bytes: []const u8) void {
799-
_ = lua.pushBytesEx(bytes);
800-
}
801-
802792
/// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string
803-
pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 {
793+
pub fn pushBytes(lua: *Lua, bytes: []const u8) []const u8 {
804794
return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len];
805795
}
806796

@@ -814,26 +804,16 @@ pub const Lua = struct {
814804
c.lua_pushnumber(lua.state, n);
815805
}
816806

817-
/// Pushes a zero-terminated string on to the stack
818-
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
819-
_ = lua.pushStringEx(str);
820-
}
821-
822807
/// Pushes a zero-terminated string onto the stack
823808
/// Lua makes a copy of the string so `str` may be freed immediately after return
824809
/// Returns a pointer to the internal Lua string
825-
pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 {
810+
pub fn pushString(lua: *Lua, str: [:0]const u8) [:0]const u8 {
826811
return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0];
827812
}
828813

829-
/// Pushes this thread onto the stack
830-
pub fn pushThread(lua: *Lua) void {
831-
_ = lua.pushThreadEx();
832-
}
833-
834814
/// Pushes this thread onto the stack
835815
/// Returns true if this thread is the main thread of its state
836-
pub fn pushThreadEx(lua: *Lua) bool {
816+
pub fn pushThread(lua: *Lua) bool {
837817
return c.lua_pushthread(lua.state) != 0;
838818
}
839819

@@ -1169,14 +1149,9 @@ pub const Lua = struct {
11691149
}
11701150
}
11711151

1172-
/// Gets information about a local variable
1173-
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1174-
_ = try lua.getLocalEx(info, n);
1175-
}
1176-
11771152
/// Gets information about a local variable
11781153
/// Returns the name of the local variable
1179-
pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1154+
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
11801155
var ar: Debug = undefined;
11811156
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
11821157
if (c.lua_getlocal(lua.state, &ar, n)) |name| {
@@ -1206,15 +1181,10 @@ pub const Lua = struct {
12061181
_ = c.lua_sethook(lua.state, hook_fn, hook_mask, count);
12071182
}
12081183

1209-
/// Sets the value of a local variable
1210-
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
1211-
_ = try lua.setLocalEx(info, n);
1212-
}
1213-
12141184
/// Sets the value of a local variable
12151185
/// Returns an error when the index is greater than the number of active locals
12161186
/// Returns the name of the local variable
1217-
pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
1187+
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
12181188
var ar: Debug = undefined;
12191189
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
12201190
if (c.lua_setlocal(lua.state, &ar, n)) |name| {
@@ -1223,15 +1193,9 @@ pub const Lua = struct {
12231193
return error.Fail;
12241194
}
12251195

1226-
/// Sets the value of a closure's upvalue
1227-
/// Returns an error if the upvalu does not exist
1228-
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void {
1229-
_ = try lua.setUpvalueEx(func_index, n);
1230-
}
1231-
12321196
/// Sets the value of a closure's upvalue
12331197
/// Returns the name of the upvalue or an error if the upvalue does not exist
1234-
pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
1198+
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
12351199
if (c.lua_setupvalue(lua.state, func_index, n)) |name| {
12361200
return std.mem.span(name);
12371201
}
@@ -1315,7 +1279,7 @@ pub const Lua = struct {
13151279
}
13161280
}
13171281

1318-
return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr}));
1282+
return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr}));
13191283
}
13201284

13211285
/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size

0 commit comments

Comments
 (0)