Skip to content

Commit

Permalink
feat: rename all *Aux() functions
Browse files Browse the repository at this point in the history
The new names are more clear on the use
  • Loading branch information
natecraddock committed Feb 19, 2023
1 parent 837048b commit 7070be0
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 66 deletions.
16 changes: 6 additions & 10 deletions src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,6 @@ pub const Lua = struct {
/// Calls a function (or callable object) in protected mode
/// See https://www.lua.org/manual/5.1/manual.html#lua_pcall
pub fn protectedCall(lua: *Lua, num_args: i32, num_results: i32, msg_handler: i32) !void {
// NOTE: it might be good to make the args named struct params?
// The translate-c version of lua_pcall does not type-check so we must rewrite it
// (macros don't always translate well with translate-c)
const ret = c.lua_pcall(lua.state, num_args, num_results, msg_handler);
Expand Down Expand Up @@ -1229,7 +1228,7 @@ pub const Lua = struct {
/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size
/// `msg` is an additional text to go into the error message
/// See https://www.lua.org/manual/5.1/manual.html#luaL_checkstack
pub fn checkStackAux(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
pub fn checkStackErr(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
c.luaL_checkstack(lua.state, size, msg);
}

Expand Down Expand Up @@ -1272,14 +1271,13 @@ pub const Lua = struct {

/// Raises an error
/// See https://www.lua.org/manual/5.1/manual.html#luaL_error
pub fn raiseErrorAux(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
pub fn raiseErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
_ = @call(.auto, c.luaL_error, .{ lua.state, fmt.ptr } ++ args);
unreachable;
}

/// Pushes onto the stack the field `e` from the metatable of the object at index `obj`
/// and returns the type of the pushed value
/// TODO: possibly return an error if nil
/// See https://www.lua.org/manual/5.1/manual.html#luaL_getmetafield
pub fn getMetaField(lua: *Lua, obj: i32, field: [:0]const u8) !LuaType {
const val_type = @intToEnum(LuaType, c.luaL_getmetafield(lua.state, obj, field.ptr));
Expand All @@ -1289,10 +1287,9 @@ pub const Lua = struct {

/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
/// or nil if there is no metatable associated with that name. Returns the type of the pushed value
/// TODO: return error when type is nil?
/// See https://www.lua.org/manual/5.1/manual.html#luaL_getmetatable
pub fn getMetatableAux(lua: *Lua, type_name: [:0]const u8) void {
c.luaL_getmetatable(lua.state, type_name);
pub fn getMetatableRegistry(lua: *Lua, table_name: [:0]const u8) void {
c.luaL_getmetatable(lua.state, table_name);
}

/// Creates a copy of string `str`, replacing any occurrence of the string `pat` with the string `rep`
Expand Down Expand Up @@ -1351,7 +1348,7 @@ pub const Lua = struct {

/// Creates a new Lua state with an allocator using the default libc allocator
/// See https://www.lua.org/manual/5.1/manual.html#luaL_newstate
pub fn newStateAux() !Lua {
pub fn newStateLibc() !Lua {
const state = c.luaL_newstate() orelse return error.Memory;
return Lua{ .state = state };
}
Expand Down Expand Up @@ -1416,7 +1413,7 @@ pub const Lua = struct {
if (!lua.isTable(-1)) {
lua.pop(1);
if (c.luaL_findtable(lua.state, globals_index, name, @intCast(c_int, funcs.len))) |_| {
lua.raiseErrorAux("name conflict for module " ++ c.LUA_QS, .{name.ptr});
lua.raiseErrorStr("name conflict for module " ++ c.LUA_QS, .{name.ptr});
}
lua.pushValue(-1);
lua.setField(-3, name);
Expand Down Expand Up @@ -1554,7 +1551,6 @@ pub const Buffer = struct {

/// Adds the string to the buffer
/// See https://www.lua.org/manual/5.1/manual.html#luaL_addlstring
/// TODO: rename to addBytes
pub fn addBytes(buf: *Buffer, str: []const u8) void {
c.luaL_addlstring(&buf.b, str.ptr, str.len);
}
Expand Down
6 changes: 3 additions & 3 deletions src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test "initialization" {
try expectError(error.Memory, Lua.newState(failing_alloc, null));

// use the auxiliary library (uses libc realloc and cannot be checked for leaks!)
lua = try Lua.newStateAux();
lua = try Lua.newStateLibc();
lua.close();
}

Expand Down Expand Up @@ -273,7 +273,7 @@ test "filling and checking the stack" {
try expectError(error.Fail, lua.checkStack(1_000_000));

// this is small enough it won't fail (would raise an error if it did)
lua.checkStackAux(40, null);
lua.checkStackErr(40, null);
while (count < 40) : (count += 1) {
lua.pushNil();
}
Expand Down Expand Up @@ -1099,7 +1099,7 @@ test "args and errors" {

const raisesError = ziglua.wrap(struct {
fn inner(l: *Lua) i32 {
l.raiseErrorAux("some error %s!", .{"zig"});
l.raiseErrorStr("some error %s!", .{"zig"});
unreachable;
}
}.inner);
Expand Down
19 changes: 9 additions & 10 deletions src/ziglua-5.2/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,6 @@ pub const Lua = struct {
}

/// Calls a function (or callable object) in protected mode
/// NOTE: it might be good to make the args named struct params?
pub fn protectedCall(lua: *Lua, num_args: i32, num_results: i32, msg_handler: i32) !void {
// The translate-c version of lua_pcall does not type-check so we must rewrite it
// (macros don't always translate well with translate-c)
Expand Down Expand Up @@ -1321,7 +1320,7 @@ pub const Lua = struct {

/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size
/// `msg` is an additional text to go into the error message
pub fn checkStackAux(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
pub fn checkStackErr(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
c.luaL_checkstack(lua.state, size, msg);
}

Expand Down Expand Up @@ -1369,7 +1368,7 @@ pub const Lua = struct {
}

/// Raises an error
pub fn raiseErrorAux(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
pub fn raiseErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
_ = @call(.auto, c.luaL_error, .{ lua.state, fmt.ptr } ++ args);
unreachable;
}
Expand All @@ -1396,8 +1395,8 @@ pub const Lua = struct {
/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
/// or nil if there is no metatable associated with that name. Returns the type of the pushed value
/// TODO: return error when type is nil?
pub fn getMetatableAux(lua: *Lua, type_name: [:0]const u8) void {
c.luaL_getmetatable(lua.state, type_name.ptr);
pub fn getMetatableRegistry(lua: *Lua, table_name: [:0]const u8) void {
c.luaL_getmetatable(lua.state, table_name.ptr);
}

/// Ensures that the value t[`field`], where t is the value at `index`, is a table, and pushes that table onto the stack.
Expand All @@ -1413,7 +1412,7 @@ pub const Lua = struct {

/// Returns the "length" of the value at the given index as a number
/// it is equivalent to the '#' operator in Lua
pub fn lenAux(lua: *Lua, index: i32) i64 {
pub fn lenRaiseErr(lua: *Lua, index: i32) i64 {
return c.luaL_len(lua.state, index);
}

Expand Down Expand Up @@ -1495,7 +1494,7 @@ pub const Lua = struct {
}

/// Creates a new Lua state with an allocator using the default libc allocator
pub fn newStateAux() !Lua {
pub fn newStateLibc() !Lua {
const state = c.luaL_newstate() orelse return error.Memory;
return Lua{ .state = state };
}
Expand Down Expand Up @@ -1553,7 +1552,7 @@ pub const Lua = struct {
/// Registers all functions in the array `fns` into the table on the top of the stack
/// All functions are created with `num_upvalues` upvalues
pub fn setFuncs(lua: *Lua, funcs: []const FnReg, num_upvalues: i32) void {
lua.checkStackAux(num_upvalues, "too many upvalues");
lua.checkStackErr(num_upvalues, "too many upvalues");
for (funcs) |f| {
if (f.func) |func| {
var i: i32 = 0;
Expand All @@ -1568,7 +1567,7 @@ pub const Lua = struct {

/// Sets the metatable of the object on the top of the stack as the metatable associated
/// with `table_name` in the registry
pub fn setMetatableAux(lua: *Lua, table_name: [:0]const u8) void {
pub fn setMetatableRegistry(lua: *Lua, table_name: [:0]const u8) void {
c.luaL_setmetatable(lua.state, table_name.ptr);
}

Expand All @@ -1580,7 +1579,7 @@ pub const Lua = struct {
}

/// Converts any Lua value at the given index into a string in a reasonable format
pub fn toBytesAux(lua: *Lua, index: i32) [:0]const u8 {
pub fn toBytesFmt(lua: *Lua, index: i32) [:0]const u8 {
var length: usize = undefined;
const ptr = c.luaL_tolstring(lua.state, index, &length);
return ptr[0..length :0];
Expand Down
18 changes: 9 additions & 9 deletions src/ziglua-5.2/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test "initialization" {
try expectError(error.Memory, Lua.newState(failing_alloc, null));

// use the auxiliary library (uses libc realloc and cannot be checked for leaks!)
lua = try Lua.newStateAux();
lua = try Lua.newStateLibc();
lua.close();
}

Expand Down Expand Up @@ -346,7 +346,7 @@ test "filling and checking the stack" {
try expectError(error.Fail, lua.checkStack(1_000_000));

// this is small enough it won't fail (would raise an error if it did)
lua.checkStackAux(40, null);
lua.checkStackErr(40, null);
while (count < 40) : (count += 1) {
lua.pushNil();
}
Expand Down Expand Up @@ -443,7 +443,7 @@ test "string buffers" {
b = buffer.prep();
std.mem.copy(u8, b, "defghijklmnopqrstuvwxyz");
buffer.pushResultSize(23);
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", lua.toBytesAux(-1));
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", lua.toBytesFmt(-1));
lua.pop(1);

lua.len(-1);
Expand Down Expand Up @@ -626,7 +626,7 @@ test "table access" {
lua.rawSetIndex(-2, index);
}
try expectEqual(@as(usize, 5), lua.rawLen(-1));
try expectEqual(@as(Integer, 5), lua.lenAux(-1));
try expectEqual(@as(Integer, 5), lua.lenRaiseErr(-1));

// add a few more
while (index <= 10) : (index += 1) {
Expand Down Expand Up @@ -1116,7 +1116,7 @@ test "metatables" {
try lua.doString("f = function() return 10 end");

try lua.newMetatable("mt");
_ = lua.getMetatableAux("mt");
_ = lua.getMetatableRegistry("mt");
try expect(lua.compare(1, 2, .eq));
lua.pop(1);

Expand All @@ -1125,7 +1125,7 @@ test "metatables" {
lua.setField(1, "__len");

lua.newTable();
lua.setMetatableAux("mt");
lua.setMetatableRegistry("mt");

try lua.callMeta(-1, "__len");
try expectEqual(@as(Number, 10), try lua.toNumber(-1));
Expand Down Expand Up @@ -1285,7 +1285,7 @@ test "args and errors" {

const raisesError = ziglua.wrap(struct {
fn inner(l: *Lua) i32 {
l.raiseErrorAux("some error %s!", .{"zig"});
l.raiseErrorStr("some error %s!", .{"zig"});
unreachable;
}
}.inner);
Expand Down Expand Up @@ -1361,7 +1361,7 @@ test "userdata" {

{
var t = lua.newUserdata(Type);
lua.setMetatableAux(@typeName(Type));
lua.setMetatableRegistry(@typeName(Type));
t.a = 1234;
t.b = 3.14;

Expand Down Expand Up @@ -1392,7 +1392,7 @@ test "userdata" {

{
var t = lua.newUserdata(Type);
lua.setMetatableAux(@typeName(Type));
lua.setMetatableRegistry(@typeName(Type));
t.a = 1234;
t.b = 3.14;

Expand Down
18 changes: 9 additions & 9 deletions src/ziglua-5.3/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1382,7 +1382,7 @@ pub const Lua = struct {

/// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size
/// `msg` is an additional text to go into the error message
pub fn checkStackAux(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
pub fn checkStackErr(lua: *Lua, size: i32, msg: ?[*:0]const u8) void {
c.luaL_checkstack(lua.state, size, msg);
}

Expand Down Expand Up @@ -1425,7 +1425,7 @@ pub const Lua = struct {
}

/// Raises an error
pub fn raiseErrorAux(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
pub fn raiseErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
_ = @call(.auto, c.luaL_error, .{ lua.state, fmt.ptr } ++ args);
unreachable;
}
Expand All @@ -1452,8 +1452,8 @@ pub const Lua = struct {
/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
/// or nil if there is no metatable associated with that name. Returns the type of the pushed value
/// TODO: return error when type is nil?
pub fn getMetatableAux(lua: *Lua, type_name: [:0]const u8) LuaType {
return @intToEnum(LuaType, c.luaL_getmetatable(lua.state, type_name.ptr));
pub fn getMetatableRegistry(lua: *Lua, table_name: [:0]const u8) LuaType {
return @intToEnum(LuaType, c.luaL_getmetatable(lua.state, table_name.ptr));
}

/// Ensures that the value t[`field`], where t is the value at `index`, is a table, and pushes that table onto the stack.
Expand All @@ -1469,7 +1469,7 @@ pub const Lua = struct {

/// Returns the "length" of the value at the given index as a number
/// it is equivalent to the '#' operator in Lua
pub fn lenAux(lua: *Lua, index: i32) i64 {
pub fn lenRaiseErr(lua: *Lua, index: i32) i64 {
return c.luaL_len(lua.state, index);
}

Expand Down Expand Up @@ -1551,7 +1551,7 @@ pub const Lua = struct {
}

/// Creates a new Lua state with an allocator using the default libc allocator
pub fn newStateAux() !Lua {
pub fn newStateLibc() !Lua {
const state = c.luaL_newstate() orelse return error.Memory;
return Lua{ .state = state };
}
Expand Down Expand Up @@ -1603,7 +1603,7 @@ pub const Lua = struct {
/// Registers all functions in the array `fns` into the table on the top of the stack
/// All functions are created with `num_upvalues` upvalues
pub fn setFuncs(lua: *Lua, funcs: []const FnReg, num_upvalues: i32) void {
lua.checkStackAux(num_upvalues, "too many upvalues");
lua.checkStackErr(num_upvalues, "too many upvalues");
for (funcs) |f| {
if (f.func) |func| {
var i: i32 = 0;
Expand All @@ -1618,7 +1618,7 @@ pub const Lua = struct {

/// Sets the metatable of the object on the top of the stack as the metatable associated
/// with `table_name` in the registry
pub fn setMetatableAux(lua: *Lua, table_name: [:0]const u8) void {
pub fn setMetatableRegistry(lua: *Lua, table_name: [:0]const u8) void {
c.luaL_setmetatable(lua.state, table_name.ptr);
}

Expand All @@ -1630,7 +1630,7 @@ pub const Lua = struct {
}

/// Converts any Lua value at the given index into a string in a reasonable format
pub fn toBytesAux(lua: *Lua, index: i32) [:0]const u8 {
pub fn toBytesFmt(lua: *Lua, index: i32) [:0]const u8 {
var length: usize = undefined;
const ptr = c.luaL_tolstring(lua.state, index, &length);
return ptr[0..length :0];
Expand Down
Loading

0 comments on commit 7070be0

Please sign in to comment.