Skip to content

Commit

Permalink
feat: add comparison functions to the 5.1 api
Browse files Browse the repository at this point in the history
Adds equal and lessThan
  • Loading branch information
natecraddock committed Feb 16, 2023
1 parent 2beee53 commit 1bfbc59
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ziglua-5.1/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,13 @@ pub const Lua = struct {
if (c.lua_dump(lua.state, writer, data) != 0) return error.Fail;
}

/// Returns true if the two values at the indexes are equal following the semantics of the
/// Lua == operator.
/// See https://www.lua.org/manual/5.1/manual.html#lua_equal
pub fn equal(lua: *Lua, index1: i32, index2: i32) bool {
return c.lua_equal(lua.state, index1, index2) == 1;
}

/// Raises a Lua error using the value at the top of the stack as the error object
/// Does a longjump and therefore never returns
/// See https://www.lua.org/manual/5.4/manual.html#lua_error
Expand Down Expand Up @@ -573,6 +580,12 @@ pub const Lua = struct {
return c.lua_isuserdata(lua.state, index) != 0;
}

/// Returns true if the value at index1 is smaller than the value at index2, following the
/// semantics of the Lua < operator.
pub fn lessThan(lua: *Lua, index1: i32, index2: i32) bool {
return c.lua_lessthan(lua.state, index1, index2) == 1;
}

/// Loads a Lua chunk without running it
/// If there are no errors, pushes the compiled chunk on the top of the stack as a function
/// See https://www.lua.org/manual/5.4/manual.html#lua_load
Expand Down
15 changes: 15 additions & 0 deletions src/ziglua-5.1/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,21 @@ test "filling and checking the stack" {
try expectEqual(@as(i32, 40), lua.getTop());
}

test "comparisions" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();

lua.pushInteger(1);
lua.pushInteger(2);

try testing.expect(!lua.equal(1, 2));
try testing.expect(lua.lessThan(1, 2));

lua.pushInteger(2);

try testing.expect(lua.equal(2, 3));
}

test "stack manipulation" {
var lua = try Lua.init(testing.allocator);
defer lua.deinit();
Expand Down

0 comments on commit 1bfbc59

Please sign in to comment.