Skip to content

Commit

Permalink
feat: remove all Ex function variations
Browse files Browse the repository at this point in the history
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
  • Loading branch information
natecraddock committed Feb 19, 2023
1 parent 4d69624 commit 8cc749b
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 357 deletions.
52 changes: 7 additions & 45 deletions src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -695,15 +695,9 @@ pub const Lua = struct {
lua.pushClosure(c_fn, 0);
}

/// Push a formatted string onto the stack
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void {
_ = lua.pushFStringEx(fmt, args);
}

/// Push a formatted string onto the stack and return a pointer to the string
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring
pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args);
}

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

/// Pushes a zero-terminated string on to the stack
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
_ = lua.pushStringEx(str);
}

/// Pushes a zero-terminated string onto the stack
/// Lua makes a copy of the string so `str` may be freed immediately after return
/// Returns a pointer to the internal Lua string
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring
pub fn pushStringEx(lua: *Lua, str: [:0]const u8) void {
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
c.lua_pushstring(lua.state, str.ptr);
}

/// Pushes this thread onto the stack
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread
pub fn pushThread(lua: *Lua) void {
_ = lua.pushThreadEx();
}

/// Pushes this thread onto the stack
/// Returns true if this thread is the main thread of its state
/// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread
pub fn pushThreadEx(lua: *Lua) bool {
pub fn pushThread(lua: *Lua) bool {
return c.lua_pushthread(lua.state) != 0;
}

Expand Down Expand Up @@ -1064,16 +1045,10 @@ pub const Lua = struct {
}
}

/// Gets information about a local variable
/// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
_ = try lua.getLocalEx(info, n);
}

/// Gets information about a local variable
/// Returns the name of the local variable
/// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal
pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
var ar: Debug = undefined;
ar.i_ci = info.private;
if (c.lua_getlocal(lua.state, &ar, n)) |name| {
Expand Down Expand Up @@ -1106,17 +1081,11 @@ pub const Lua = struct {
_ = c.lua_sethook(lua.state, hook_fn, hook_mask, count);
}

/// Sets the value of a local variable
/// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
_ = try lua.setLocalEx(info, n);
}

/// Sets the value of a local variable
/// Returns an error when the index is greater than the number of active locals
/// Returns the name of the local variable
/// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal
pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
var ar: Debug = undefined;
ar.i_ci = info.private;
if (c.lua_setlocal(lua.state, &ar, n)) |name| {
Expand All @@ -1125,17 +1094,10 @@ pub const Lua = struct {
return error.Fail;
}

/// Sets the value of a closure's upvalue
/// Returns an error if the upvalu does not exist
/// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void {
_ = try lua.setUpvalueEx(func_index, n);
}

/// Sets the value of a closure's upvalue
/// Returns the name of the upvalue or an error if the upvalue does not exist
/// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue
pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
if (c.lua_setupvalue(lua.state, func_index, n)) |name| {
return std.mem.span(name);
}
Expand Down Expand Up @@ -1221,7 +1183,7 @@ pub const Lua = struct {
}
}

return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr}));
return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr}));
}

/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size
Expand Down
14 changes: 7 additions & 7 deletions src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,11 @@ test "type of and getting values" {
lua.pushLightUserdata(&value);
lua.pushNil();
lua.pushNumber(0.1);
lua.pushThread();
lua.pushStringEx("all your codebase are belong to us");
_ = lua.pushThread();
lua.pushString("all your codebase are belong to us");
lua.pushFunction(ziglua.wrap(add));
lua.pushBytes("hello world");
lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
lua.pushValue(1);

// test both typeof and is functions
Expand Down Expand Up @@ -736,18 +736,18 @@ test "debug interface" {
.call => {
l.getInfo(.{ .l = true }, i);
if (i.current_line.? != 2) panic("Expected line to be 2", .{});
l.getLocal(i, 1) catch unreachable;
_ = l.getLocal(i, 1) catch unreachable;
if ((l.toNumber(-1)) != 3) panic("Expected x to equal 3", .{});
},
.line => if (i.current_line.? == 4) {
// modify the value of y to be 0 right before returning
l.pushNumber(0);
l.setLocal(i, 2) catch unreachable;
_ = l.setLocal(i, 2) catch unreachable;
},
.ret => {
l.getInfo(.{ .l = true }, i);
if (i.current_line.? != 4) panic("Expected line to be 4", .{});
l.getLocal(i, 1) catch unreachable;
_ = l.getLocal(i, 1) catch unreachable;
if ((l.toNumber(-1)) != 3) panic("Expected result to equal 3", .{});
},
else => unreachable,
Expand Down Expand Up @@ -793,7 +793,7 @@ test "debug upvalues" {

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

// call the new function (should return 7)
lua.pushNumber(2);
Expand Down
52 changes: 8 additions & 44 deletions src/ziglua-5.2/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -766,13 +766,8 @@ pub const Lua = struct {
lua.pushClosure(c_fn, 0);
}

/// Push a formatted string onto the stack
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void {
_ = lua.pushFStringEx(fmt, args);
}

/// Push a formatted string onto the stack and return a pointer to the string
pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 {
return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args);
}

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

/// Pushes a slice of bytes onto the stack
pub fn pushBytes(lua: *Lua, bytes: []const u8) void {
_ = lua.pushBytesEx(bytes);
}

/// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string
pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 {
pub fn pushBytes(lua: *Lua, bytes: []const u8) []const u8 {
return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len];
}

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

/// Pushes a zero-terminated string on to the stack
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
_ = lua.pushStringEx(str);
}

/// Pushes a zero-terminated string onto the stack
/// Lua makes a copy of the string so `str` may be freed immediately after return
/// Returns a pointer to the internal Lua string
pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 {
pub fn pushString(lua: *Lua, str: [:0]const u8) [:0]const u8 {
return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0];
}

/// Pushes this thread onto the stack
pub fn pushThread(lua: *Lua) void {
_ = lua.pushThreadEx();
}

/// Pushes this thread onto the stack
/// Returns true if this thread is the main thread of its state
pub fn pushThreadEx(lua: *Lua) bool {
pub fn pushThread(lua: *Lua) bool {
return c.lua_pushthread(lua.state) != 0;
}

Expand Down Expand Up @@ -1169,14 +1149,9 @@ pub const Lua = struct {
}
}

/// Gets information about a local variable
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
_ = try lua.getLocalEx(info, n);
}

/// Gets information about a local variable
/// Returns the name of the local variable
pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
var ar: Debug = undefined;
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
if (c.lua_getlocal(lua.state, &ar, n)) |name| {
Expand Down Expand Up @@ -1206,15 +1181,10 @@ pub const Lua = struct {
_ = c.lua_sethook(lua.state, hook_fn, hook_mask, count);
}

/// Sets the value of a local variable
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void {
_ = try lua.setLocalEx(info, n);
}

/// Sets the value of a local variable
/// Returns an error when the index is greater than the number of active locals
/// Returns the name of the local variable
pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 {
var ar: Debug = undefined;
ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private);
if (c.lua_setlocal(lua.state, &ar, n)) |name| {
Expand All @@ -1223,15 +1193,9 @@ pub const Lua = struct {
return error.Fail;
}

/// Sets the value of a closure's upvalue
/// Returns an error if the upvalu does not exist
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void {
_ = try lua.setUpvalueEx(func_index, n);
}

/// Sets the value of a closure's upvalue
/// Returns the name of the upvalue or an error if the upvalue does not exist
pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 {
if (c.lua_setupvalue(lua.state, func_index, n)) |name| {
return std.mem.span(name);
}
Expand Down Expand Up @@ -1315,7 +1279,7 @@ pub const Lua = struct {
}
}

return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr}));
return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr}));
}

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

0 comments on commit 8cc749b

Please sign in to comment.