Skip to content

Commit e0d0112

Browse files
committed
Add support for lua.equal()
1 parent 5425e9e commit e0d0112

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ lua.doString(
7979

8080
| API | Support |
8181
|---|---|
82-
| Lua C API (`lua_*`) | 47.8% available (44/92) | <!-- 47.82% - Add 1.07 per -->
82+
| Lua C API (`lua_*`) | 48.87% available (45/92) | <!-- 47.82% - Add 1.07 per -->
8383
| Auxilary Library (`luaL_*`) | 6% available (3/48) | <!-- Always 2% * n, for n up to 48 -->
8484
| LuaJIT Extensions | *No plans to implement.* |
8585

@@ -104,7 +104,7 @@ pattern has changed, such as using the Zig `init()` function pattern instead of
104104
| `lua_cpcall`||
105105
| `lua_createtable`|☑️ `lua.createTable()` |
106106
| `lua_dump`||
107-
| `lua_equal`||
107+
| `lua_equal`|☑️ `lua.equal()` |
108108
| `lua_error`||
109109
| `lua_gc`||
110110
| `lua_getallocf`|`lua.getAllocF()` |

src/lua_api.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,6 @@ pub fn cpCall(lua: *Lua, func: CFn, userdata: ?*anyopaque) LuaError;
5555
/// Stack Behavior: `[-0, +0, m]`
5656
pub fn dump(lua: *Lua, writer: LuaWriter, data: *anyopaque) i32;
5757

58-
/// Returns whether the two values in acceptable indices index1 and index2 are equal,
59-
/// following the semantics of the Lua == operator (which may call metamethods).
60-
/// Returns false if any of the indices is not valid.
61-
///
62-
/// From: `int lua_equal(lua_State *L, int index1, int index2);`
63-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_equal
64-
/// Stack Behavior: `[-0, +0, e]`
65-
pub fn equal(lua: *Lua, index1: i32, index2: i32) bool;
66-
6758
/// Generates a Lua error. The error message (which can actually be a Lua value of any type)
6859
/// must be on the stack top. This function does a long jump and therefore never returns.
6960
///

src/root.zig

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,16 @@ pub const Lua = opaque {
879879
return c.lua_insert(asState(lua), index);
880880
}
881881

882+
/// Returns whether the two values in acceptable are equal, following the semantics of the Lua == operator
883+
/// (which may call metamethods). Returns false if any of the indices is not valid.
884+
///
885+
/// From: `int lua_equal(lua_State *L, int index1, int index2);`
886+
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_equal
887+
/// Stack Behavior: `[-0, +0, e]`
888+
pub fn equal(lua: *Lua, index_left: i32, index_right: i32) bool {
889+
return 1 == c.lua_equal(asState(lua), index_left, index_right);
890+
}
891+
882892
/// Returns the current status of the thread. The status will be `Status.ok` for a normal thread, an error
883893
/// code if the thread finished its execution with an error, or `status.yield` if the thread is suspended.
884894
///
@@ -1761,3 +1771,46 @@ test "doString should do the string" {
17611771
try std.testing.expectEqualSlices(u8, "Hello, world!", actual);
17621772
lua.pop(1);
17631773
}
1774+
1775+
test "equal should follow expected semantics" {
1776+
const lua = try Lua.init(std.testing.allocator);
1777+
defer lua.deinit();
1778+
1779+
lua.pushLString("Hello, world!");
1780+
lua.pushLString("hello, world!");
1781+
try std.testing.expect(!lua.equal(-2, -1));
1782+
1783+
lua.pop(1);
1784+
lua.pushLString("Hello, world!");
1785+
try std.testing.expect(lua.equal(-2, -1));
1786+
1787+
lua.pop(2);
1788+
lua.pushNumber(13.0);
1789+
lua.pushInteger(13);
1790+
try std.testing.expect(lua.equal(-2, -1));
1791+
1792+
lua.pop(2);
1793+
lua.pushNumber(13.5);
1794+
lua.pushNumber(13.4);
1795+
try std.testing.expect(!lua.equal(-2, -1));
1796+
1797+
lua.pop(2);
1798+
lua.pushNil();
1799+
lua.pushBoolean(true);
1800+
try std.testing.expect(!lua.equal(-2, -1));
1801+
1802+
lua.pop(2);
1803+
lua.pushNil();
1804+
lua.pushNil();
1805+
try std.testing.expect(lua.equal(-2, -1));
1806+
1807+
lua.pop(2);
1808+
lua.pushBoolean(true);
1809+
lua.pushBoolean(false);
1810+
try std.testing.expect(!lua.equal(-2, -1));
1811+
1812+
lua.pop(2);
1813+
lua.pushBoolean(true);
1814+
lua.pushBoolean(true);
1815+
try std.testing.expect(lua.equal(-2, -1));
1816+
}

0 commit comments

Comments
 (0)