Skip to content

Commit 1bfbc59

Browse files
committed
feat: add comparison functions to the 5.1 api
Adds equal and lessThan
1 parent 2beee53 commit 1bfbc59

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/ziglua-5.1/lib.zig

+13
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,13 @@ pub const Lua = struct {
387387
if (c.lua_dump(lua.state, writer, data) != 0) return error.Fail;
388388
}
389389

390+
/// Returns true if the two values at the indexes are equal following the semantics of the
391+
/// Lua == operator.
392+
/// See https://www.lua.org/manual/5.1/manual.html#lua_equal
393+
pub fn equal(lua: *Lua, index1: i32, index2: i32) bool {
394+
return c.lua_equal(lua.state, index1, index2) == 1;
395+
}
396+
390397
/// Raises a Lua error using the value at the top of the stack as the error object
391398
/// Does a longjump and therefore never returns
392399
/// See https://www.lua.org/manual/5.4/manual.html#lua_error
@@ -573,6 +580,12 @@ pub const Lua = struct {
573580
return c.lua_isuserdata(lua.state, index) != 0;
574581
}
575582

583+
/// Returns true if the value at index1 is smaller than the value at index2, following the
584+
/// semantics of the Lua < operator.
585+
pub fn lessThan(lua: *Lua, index1: i32, index2: i32) bool {
586+
return c.lua_lessthan(lua.state, index1, index2) == 1;
587+
}
588+
576589
/// Loads a Lua chunk without running it
577590
/// If there are no errors, pushes the compiled chunk on the top of the stack as a function
578591
/// See https://www.lua.org/manual/5.4/manual.html#lua_load

src/ziglua-5.1/tests.zig

+15
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@ test "filling and checking the stack" {
281281
try expectEqual(@as(i32, 40), lua.getTop());
282282
}
283283

284+
test "comparisions" {
285+
var lua = try Lua.init(testing.allocator);
286+
defer lua.deinit();
287+
288+
lua.pushInteger(1);
289+
lua.pushInteger(2);
290+
291+
try testing.expect(!lua.equal(1, 2));
292+
try testing.expect(lua.lessThan(1, 2));
293+
294+
lua.pushInteger(2);
295+
296+
try testing.expect(lua.equal(2, 3));
297+
}
298+
284299
test "stack manipulation" {
285300
var lua = try Lua.init(testing.allocator);
286301
defer lua.deinit();

0 commit comments

Comments
 (0)