Skip to content

Commit

Permalink
feat: remove X variants of load functions
Browse files Browse the repository at this point in the history
It is simpler to just have one type of function and make the argument
explicit
  • Loading branch information
natecraddock committed Feb 23, 2023
1 parent a7b78d9 commit 29f730a
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 47 deletions.
18 changes: 3 additions & 15 deletions src/ziglua-5.2/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ pub const Lua = struct {
/// See https://www.lua.org/manual/5.2/manual.html#luaL_dofile
pub fn doFile(lua: *Lua, file_name: [:0]const u8) !void {
// translate-c failure
try lua.loadFile(file_name);
try lua.loadFile(file_name, .binary_text);
try lua.protectedCall(0, mult_return, 0);
}

Expand Down Expand Up @@ -1483,15 +1483,9 @@ pub const Lua = struct {
return c.luaL_len(lua.state, index);
}

/// The same as `Lua.loadBufferX` with `mode` set to binary+text
/// See https://www.lua.org/manual/5.2/manual.html#luaL_loadbuffer
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8) !void {
try lua.loadBufferX(buf, name, .binary_text);
}

/// Loads a buffer as a Lua chunk
/// See https://www.lua.org/manual/5.2/manual.html#luaL_loadbufferx
pub fn loadBufferX(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand All @@ -1505,15 +1499,9 @@ pub const Lua = struct {
}
}

/// Equivalent to `Lua.loadFileX()` with mode equal to binary+text
/// See https://www.lua.org/manual/5.2/manual.html#luaL_loadfile
pub fn loadFile(lua: *Lua, file_name: [:0]const u8) !void {
try lua.loadFileX(file_name, .binary_text);
}

/// Loads a file as a Lua chunk
/// See https://www.lua.org/manual/5.2/manual.html#luaL_loadfilex
pub fn loadFileX(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
pub fn loadFile(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand Down
3 changes: 1 addition & 2 deletions src/ziglua-5.2/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ test "loadBuffer" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();

_ = try lua.loadBuffer("global = 10", "chunkname");
_ = try lua.loadBuffer("global = 10", "chunkname", .text);
try lua.protectedCall(0, ziglua.mult_return, 0);
lua.getGlobal("global");
try expectEqual(@as(Integer, 10), try lua.toInteger(-1));
Expand Down Expand Up @@ -1409,7 +1409,6 @@ test "refs" {
// no need to test file loading
_ = Lua.doFile;
_ = Lua.loadFile;
_ = Lua.loadFileX;

// probably not needed in ziglua
_ = Lua.execResult;
Expand Down
16 changes: 3 additions & 13 deletions src/ziglua-5.3/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ pub const Lua = struct {
/// Loads and runs the given file
pub fn doFile(lua: *Lua, file_name: [:0]const u8) !void {
// translate-c failure
try lua.loadFile(file_name);
try lua.loadFile(file_name, .binary_text);
try lua.protectedCall(0, mult_return, 0);
}

Expand Down Expand Up @@ -1430,13 +1430,8 @@ pub const Lua = struct {
return c.luaL_len(lua.state, index);
}

/// The same as `Lua.loadBufferX` with `mode` set to binary+text
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8) !void {
try lua.loadBufferX(buf, name, .binary_text);
}

/// Loads a buffer as a Lua chunk
pub fn loadBufferX(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand All @@ -1450,13 +1445,8 @@ pub const Lua = struct {
}
}

/// Equivalent to `Lua.loadFileX()` with mode equal to binary+text
pub fn loadFile(lua: *Lua, file_name: [:0]const u8) !void {
try lua.loadFileX(file_name, .binary_text);
}

/// Loads a file as a Lua chunk
pub fn loadFileX(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
pub fn loadFile(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand Down
3 changes: 1 addition & 2 deletions src/ziglua-5.3/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,7 @@ test "loadBuffer" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();

_ = try lua.loadBuffer("global = 10", "chunkname");
_ = try lua.loadBuffer("global = 10", "chunkname", .text);
try lua.protectedCall(0, ziglua.mult_return, 0);
_ = try lua.getGlobal("global");
try expectEqual(@as(Integer, 10), try lua.toInteger(-1));
Expand Down Expand Up @@ -1431,7 +1431,6 @@ test "refs" {
// no need to test file loading
_ = Lua.doFile;
_ = Lua.loadFile;
_ = Lua.loadFileX;

// probably not needed in ziglua
_ = Lua.execResult;
Expand Down
16 changes: 3 additions & 13 deletions src/ziglua-5.4/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ pub const Lua = struct {
/// Loads and runs the given file
pub fn doFile(lua: *Lua, file_name: [:0]const u8) !void {
// translate-c failure
try lua.loadFile(file_name);
try lua.loadFile(file_name, .binary_text);
try lua.protectedCall(0, mult_return, 0);
}

Expand Down Expand Up @@ -1474,13 +1474,8 @@ pub const Lua = struct {
return c.luaL_len(lua.state, index);
}

/// The same as `Lua.loadBufferX` with `mode` set to binary+text
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8) !void {
try lua.loadBufferX(buf, name, .binary_text);
}

/// Loads a buffer as a Lua chunk
pub fn loadBufferX(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
pub fn loadBuffer(lua: *Lua, buf: []const u8, name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand All @@ -1494,13 +1489,8 @@ pub const Lua = struct {
}
}

/// Equivalent to `Lua.loadFileX()` with mode equal to binary+text
pub fn loadFile(lua: *Lua, file_name: [:0]const u8) !void {
try lua.loadFileX(file_name, .binary_text);
}

/// Loads a file as a Lua chunk
pub fn loadFileX(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
pub fn loadFile(lua: *Lua, file_name: [:0]const u8, mode: Mode) !void {
const mode_str = switch (mode) {
.binary => "b",
.text => "t",
Expand Down
3 changes: 1 addition & 2 deletions src/ziglua-5.4/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ test "loadBuffer" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();

_ = try lua.loadBuffer("global = 10", "chunkname");
_ = try lua.loadBuffer("global = 10", "chunkname", .text);
try lua.protectedCall(0, ziglua.mult_return, 0);
_ = try lua.getGlobal("global");
try expectEqual(@as(Integer, 10), try lua.toInteger(-1));
Expand Down Expand Up @@ -1522,7 +1522,6 @@ test "refs" {
// no need to test file loading
_ = Lua.doFile;
_ = Lua.loadFile;
_ = Lua.loadFileX;

// probably not needed in ziglua
_ = Lua.execResult;
Expand Down

0 comments on commit 29f730a

Please sign in to comment.