如何检查 Lua 中的两个值是否基本相等?
How to check if two values in Lua are primitively equal?
当覆盖 Lua 中的相等运算符时(通过“__eq
”元方法),有没有办法仍然检查原始相等性(即不调用被覆盖的 __eq
,但检查两个 table 值是否引用相同?)我需要从 C API 执行此操作,但我在那里找不到 suitable 函数。
例如:
lua_newtable(L);
lua_newtable(L);
assert(!some_comparison());
lua_pushvalue(L,-1);
assert(some_comparison());
其中 some_comparison()
不调用 __eq
元方法。
(请注意 lua_compare()
不满足这一点,特别是。我想要一个 lua_rawcompare()
,如果你愿意 - 或者更确切地说,一个技巧或解决方法会给我等效的。这个想法是防止 __eq
实现中的无限递归...)
如果我没有正确理解你的问题,我认为你的意思是使用 lua_rawequal
:
int lua_rawequal (lua_State *L, int index1, int index2);
Returns 1 if the two values in indices index1
and index2
are
primitively equal (that is, without calling metamethods). Otherwise
returns 0. Also returns 0 if any of the indices are not valid.
当覆盖 Lua 中的相等运算符时(通过“__eq
”元方法),有没有办法仍然检查原始相等性(即不调用被覆盖的 __eq
,但检查两个 table 值是否引用相同?)我需要从 C API 执行此操作,但我在那里找不到 suitable 函数。
例如:
lua_newtable(L);
lua_newtable(L);
assert(!some_comparison());
lua_pushvalue(L,-1);
assert(some_comparison());
其中 some_comparison()
不调用 __eq
元方法。
(请注意 lua_compare()
不满足这一点,特别是。我想要一个 lua_rawcompare()
,如果你愿意 - 或者更确切地说,一个技巧或解决方法会给我等效的。这个想法是防止 __eq
实现中的无限递归...)
如果我没有正确理解你的问题,我认为你的意思是使用 lua_rawequal
:
int lua_rawequal (lua_State *L, int index1, int index2);
Returns 1 if the two values in indices
index1
andindex2
are primitively equal (that is, without calling metamethods). Otherwise returns 0. Also returns 0 if any of the indices are not valid.