@@ -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