Bläddra i källkod

a small optimization

Roberto Ierusalimschy 24 år sedan
förälder
incheckning
c8559e3c8d
2 ändrade filer med 23 tillägg och 6 borttagningar
  1. 16 1
      ltm.h
  2. 7 5
      lvm.c

+ 16 - 1
ltm.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.h,v 1.19 2000/12/26 18:46:09 roberto Exp roberto $
+** $Id: ltm.h,v 1.20 2001/01/19 13:20:30 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -35,6 +35,21 @@ typedef enum {
 } TMS;
 
 
+
+/*
+** masks for allowable tag methods
+*/
+#define HAS_TM_GETGLOBAL(L,t)	(1<<(t) & ((1<<LUA_TUSERDATA) | \
+                                           (1<<LUA_TTABLE) | \
+                                           (1<<LUA_TNIL)))
+
+#define HAS_TM_SETGLOBAL(L,t)	(1<<(t) & ((1<<LUA_TUSERDATA) | \
+                                           (1<<LUA_TTABLE) | \
+                                           (1<<LUA_TNIL) | \
+                                           (1<<LUA_TFUNCTION)))
+
+
+
 struct TM {
   Closure *method[TM_N];
   TString *collected;  /* list of garbage-collected udata with this tag */

+ 7 - 5
lvm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lvm.c,v 1.155 2001/01/19 13:20:30 roberto Exp roberto $
+** $Id: lvm.c,v 1.156 2001/01/24 15:45:33 roberto Exp roberto $
 ** Lua virtual machine
 ** See Copyright Notice in lua.h
 */
@@ -174,8 +174,9 @@ void luaV_settable (lua_State *L, StkId t, StkId key) {
 
 const TObject *luaV_getglobal (lua_State *L, TString *s) {
   const TObject *value = luaH_getstr(L->gt, s);
-  Closure *tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL);
-  if (tm == NULL)  /* is there a tag method? */
+  Closure *tm;
+  if (!HAS_TM_GETGLOBAL(L, ttype(value)) ||  /* is there a tag method? */
+      (tm = luaT_gettmbyObj(G(L), value, TM_GETGLOBAL)) == NULL)
     return value;  /* default behavior */
   else {  /* tag method */
     luaD_checkstack(L, 3);
@@ -191,8 +192,9 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) {
 
 void luaV_setglobal (lua_State *L, TString *s) {
   TObject *oldvalue = luaH_setstr(L, L->gt, s);
-  Closure *tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL);
-  if (tm == NULL) {  /* no tag methods? */
+  Closure *tm;
+  if (!HAS_TM_SETGLOBAL(L, ttype(oldvalue)) ||  /* no tag methods? */
+     (tm = luaT_gettmbyObj(G(L), oldvalue, TM_SETGLOBAL)) == NULL) {
     setobj(oldvalue, L->top - 1);  /* raw set */
   }
   else {  /* call tag method */