1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

no need for two different implementations for equality (one raw and

one with metamethods)
This commit is contained in:
Roberto Ierusalimschy 2011-05-31 15:24:36 -03:00
parent 3f04a9f2c0
commit 9b7dddad7d
7 changed files with 27 additions and 36 deletions

4
lapi.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lapi.c,v 2.144 2010/12/29 18:00:23 roberto Exp roberto $
** $Id: lapi.c,v 2.145 2011/04/05 14:26:23 roberto Exp roberto $
** Lua API
** See Copyright Notice in lua.h
*/
@ -278,7 +278,7 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) {
StkId o1 = index2addr(L, index1);
StkId o2 = index2addr(L, index2);
return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
: luaO_rawequalObj(o1, o2);
: luaV_rawequalObj(o1, o2);
}

View File

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.53 2011/04/19 16:22:13 roberto Exp roberto $
** $Id: lcode.c,v 2.54 2011/04/28 14:00:11 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -23,6 +23,7 @@
#include "lparser.h"
#include "lstring.h"
#include "ltable.h"
#include "lvm.h"
#define hasjumps(e) ((e)->t != (e)->f)
@ -295,7 +296,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) {
if (ttisnumber(idx)) {
lua_Number n = nvalue(idx);
lua_number2int(k, n);
if (luaO_rawequalObj(&f->k[k], v))
if (luaV_rawequalObj(&f->k[k], v))
return k;
/* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
go through and create a new entry for this value */

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.47 2011/04/05 18:32:06 roberto Exp roberto $
** $Id: lobject.c,v 2.48 2011/05/03 16:01:57 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -70,28 +70,6 @@ int luaO_ceillog2 (unsigned int x) {
}
int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
if (!ttisequal(t1, t2)) return 0;
else switch (ttype(t1)) {
case LUA_TNIL:
return 1;
case LUA_TNUMBER:
return luai_numeq(nvalue(t1), nvalue(t2));
case LUA_TBOOLEAN:
return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
case LUA_TLIGHTUSERDATA:
return pvalue(t1) == pvalue(t2);
case LUA_TSTRING:
return rawtsvalue(t1) == rawtsvalue(t2);
case LUA_TLCF:
return fvalue(t1) == fvalue(t2);
default:
lua_assert(iscollectable(t1));
return gcvalue(t1) == gcvalue(t2);
}
}
lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
switch (op) {
case LUA_OPADD: return luai_numadd(NULL, v1, v2);

View File

@ -1,5 +1,5 @@
/*
** $Id: lobject.h,v 2.53 2011/05/05 19:43:14 roberto Exp roberto $
** $Id: lobject.h,v 2.54 2011/05/26 17:12:31 roberto Exp roberto $
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
@ -431,11 +431,11 @@ typedef struct Table {
LUAI_DDEC const TValue luaO_nilobject_;
LUAI_FUNC int luaO_int2fb (unsigned int x);
LUAI_FUNC int luaO_fb2int (int x);
LUAI_FUNC int luaO_ceillog2 (unsigned int x);
LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
LUAI_FUNC int luaO_hexavalue (int c);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,

View File

@ -1,5 +1,5 @@
/*
** $Id: ltable.c,v 2.54 2011/04/05 18:32:28 roberto Exp roberto $
** $Id: ltable.c,v 2.55 2011/05/02 16:45:32 roberto Exp roberto $
** Lua tables (hash)
** See Copyright Notice in lua.h
*/
@ -33,6 +33,7 @@
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lvm.h"
/*
@ -148,7 +149,7 @@ static int findindex (lua_State *L, Table *t, StkId key) {
Node *n = mainposition(t, key);
do { /* check whether `key' is somewhere in the chain */
/* key may be dead already, but it is ok to use it in `next' */
if (luaO_rawequalObj(gkey(n), key) ||
if (luaV_rawequalObj(gkey(n), key) ||
(ttisdeadkey(gkey(n)) && iscollectable(key) &&
gcvalue(gkey(n)) == gcvalue(key))) {
i = cast_int(n - gnode(t, 0)); /* key index in hash table */
@ -481,7 +482,7 @@ const TValue *luaH_get (Table *t, const TValue *key) {
default: {
Node *n = mainposition(t, key);
do { /* check whether `key' is somewhere in the chain */
if (luaO_rawequalObj(gkey(n), key))
if (luaV_rawequalObj(gkey(n), key))
return gval(n); /* that's it */
else n = gnext(n);
} while (n);

13
lvm.c
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.136 2011/04/19 16:22:13 roberto Exp roberto $
** $Id: lvm.c,v 2.137 2011/05/05 16:16:33 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -174,7 +174,7 @@ static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2,
if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
tm2 = fasttm(L, mt2, event);
if (tm2 == NULL) return NULL; /* no metamethod */
if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
if (luaV_rawequalObj(tm1, tm2)) /* same metamethods? */
return tm1;
return NULL;
}
@ -237,6 +237,9 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) {
}
/*
** equality of Lua values. L == NULL means raw equality (no metamethods)
*/
int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
const TValue *tm;
lua_assert(ttisequal(t1, t2));
@ -249,15 +252,19 @@ int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
case LUA_TSTRING: return eqstr(rawtsvalue(t1), rawtsvalue(t2));
case LUA_TUSERDATA: {
if (uvalue(t1) == uvalue(t2)) return 1;
else if (L == NULL) return 0;
tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
break; /* will try TM */
}
case LUA_TTABLE: {
if (hvalue(t1) == hvalue(t2)) return 1;
else if (L == NULL) return 0;
tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
break; /* will try TM */
}
default: return gcvalue(t1) == gcvalue(t2);
default:
lua_assert(iscollectable(t1));
return gcvalue(t1) == gcvalue(t2);
}
if (tm == NULL) return 0; /* no TM? */
callTM(L, tm, t1, t2, L->top, 1); /* call TM */

6
lvm.h
View File

@ -1,5 +1,5 @@
/*
** $Id: lvm.h,v 2.14 2009/12/17 16:20:01 roberto Exp roberto $
** $Id: lvm.h,v 2.15 2011/04/05 18:32:06 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -19,10 +19,14 @@
#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalval_(L, o1, o2))
#define luaV_rawequalObj(t1,t2) \
(ttisequal(t1,t2) && luaV_equalval_(NULL,t1,t2))
/* not to called directly */
LUAI_FUNC int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2);
LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
LUAI_FUNC const TValue *luaV_tonumber (const TValue *obj, TValue *n);