From af850484a9e01b46b04e4c666f9a9e91308d81c7 Mon Sep 17 00:00:00 2001
From: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Mon, 1 Dec 2003 16:22:56 -0200
Subject: default metatable can be NULL

---
 lapi.c    | 19 ++++++++++++-------
 lgc.c     |  9 +++++----
 lstate.c  |  7 +------
 lstate.h  |  6 +-----
 lstring.c |  4 ++--
 ltable.c  |  4 ++--
 ltm.c     | 13 ++++++++-----
 ltm.h     |  6 +++---
 8 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/lapi.c b/lapi.c
index c078160a..5c974e41 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.248 2003/10/20 12:25:23 roberto Exp roberto $
+** $Id: lapi.c,v 1.249 2003/10/20 17:42:41 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -555,7 +555,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) {
       mt = uvalue(obj)->uv.metatable;
       break;
   }
-  if (mt == NULL || mt == hvalue(defaultmeta(L)))
+  if (mt == NULL)
     res = 0;
   else {
     sethvalue(L->top, mt);
@@ -634,21 +634,26 @@ LUA_API void lua_rawseti (lua_State *L, int idx, int n) {
 
 
 LUA_API int lua_setmetatable (lua_State *L, int objindex) {
-  TObject *obj, *mt;
+  TObject *obj;
+  Table *mt;
   int res = 1;
   lua_lock(L);
   api_checknelems(L, 1);
   obj = luaA_index(L, objindex);
   api_checkvalidindex(L, obj);
-  mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L);
-  api_check(L, ttistable(mt));
+  if (ttisnil(L->top - 1))
+    mt = NULL;
+  else {
+    api_check(L, ttistable(L->top - 1));
+    mt = hvalue(L->top - 1);
+  }
   switch (ttype(obj)) {
     case LUA_TTABLE: {
-      hvalue(obj)->metatable = hvalue(mt);  /* write barrier */
+      hvalue(obj)->metatable = mt;  /* write barrier */
       break;
     }
     case LUA_TUSERDATA: {
-      uvalue(obj)->uv.metatable = hvalue(mt);  /* write barrier */
+      uvalue(obj)->uv.metatable = mt;  /* write barrier */
       break;
     }
     default: {
diff --git a/lgc.c b/lgc.c
index cb9fa289..f8fdcb4c 100644
--- a/lgc.c
+++ b/lgc.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lgc.c,v 1.180 2003/11/19 19:41:57 roberto Exp roberto $
+** $Id: lgc.c,v 1.181 2003/12/01 16:33:30 roberto Exp roberto $
 ** Garbage Collector
 ** See Copyright Notice in lua.h
 */
@@ -114,8 +114,9 @@ static void reallymarkobject (global_State *g, GCObject *o) {
       return;
     }
     case LUA_TUSERDATA: {
+      Table *mt = gcotou(o)->uv.metatable;
       white2black(o);  /* userdata do not go to gray list */
-      markobject(g, gcotou(o)->uv.metatable);
+      if (mt) markobject(g, mt);
       return;
     }
     case LUA_TFUNCTION: {
@@ -191,7 +192,8 @@ static void traversetable (global_State *g, Table *h) {
   int weakkey = 0;
   int weakvalue = 0;
   const TObject *mode;
-  markobject(g, h->metatable);
+  if (h->metatable)
+    markobject(g, h->metatable);
   lua_assert(h->lsizenode || h->node == g->dummynode);
   mode = gfasttm(g, h->metatable, TM_MODE);
   if (mode && ttisstring(mode)) {  /* is there a weak mode? */
@@ -534,7 +536,6 @@ static void markroot (lua_State *L) {
   g->weak = NULL;
   makewhite(valtogco(g->mainthread));
   markobject(g, g->mainthread);
-  markvalue(g, defaultmeta(L));
   markvalue(g, registry(L));
   if (L != g->mainthread)  /* another thread is running? */
     markobject(g, L);  /* cannot collect it */
diff --git a/lstate.c b/lstate.c
index 49c71719..40e29c6f 100644
--- a/lstate.c
+++ b/lstate.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.c,v 1.128 2003/11/18 14:55:11 roberto Exp roberto $
+** $Id: lstate.c,v 1.129 2003/12/01 16:33:30 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -76,10 +76,6 @@ static void freestack (lua_State *L, lua_State *L1) {
 static void f_luaopen (lua_State *L, void *ud) {
   UNUSED(ud);
   stack_init(L, L);  /* init stack */
-  /* create default meta table with a dummy table, and then close the loop */
-  defaultmeta(L)->tt = LUA_TTABLE;
-  sethvalue(defaultmeta(L), luaH_new(L, 0, 0));
-  hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L));
   sethvalue(gt(L), luaH_new(L, 0, 4));  /* table of globals */
   sethvalue(registry(L), luaH_new(L, 4, 4));  /* registry */
   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
@@ -161,7 +157,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
   g->strt.size = 0;
   g->strt.nuse = 0;
   g->strt.hash = NULL;
-  setnilvalue(defaultmeta(L));
   setnilvalue(registry(L));
   luaZ_initbuffer(L, &g->buff);
   g->panic = NULL;
diff --git a/lstate.h b/lstate.h
index ed46fb1f..a62bcef8 100644
--- a/lstate.h
+++ b/lstate.h
@@ -1,5 +1,5 @@
 /*
-** $Id: lstate.h,v 1.113 2003/11/18 14:55:11 roberto Exp roberto $
+** $Id: lstate.h,v 1.114 2003/12/01 16:33:30 roberto Exp roberto $
 ** Global State
 ** See Copyright Notice in lua.h
 */
@@ -42,9 +42,6 @@
 struct lua_longjmp;  /* defined in ldo.c */
 
 
-/* default meta table (both for tables and udata) */
-#define defaultmeta(L)	(&G(L)->_defaultmeta)
-
 /* table of globals */
 #define gt(L)	(&L->_gt)
 
@@ -113,7 +110,6 @@ typedef struct global_State {
   lu_mem nblocks;  /* number of `bytes' currently allocated */
   lua_CFunction panic;  /* to be called in unprotected errors */
   TObject _registry;
-  TObject _defaultmeta;
   struct lua_State *mainthread;
   Node dummynode[1];  /* common node array for all empty tables */
   TString *tmname[TM_N];  /* array with tag-method names */
diff --git a/lstring.c b/lstring.c
index 1b0a0050..e7e84609 100644
--- a/lstring.c
+++ b/lstring.c
@@ -1,5 +1,5 @@
 /*
-** $Id: lstring.c,v 1.79 2003/04/28 19:26:16 roberto Exp roberto $
+** $Id: lstring.c,v 1.80 2003/11/17 19:50:05 roberto Exp roberto $
 ** String table (keeps all strings handled by Lua)
 ** See Copyright Notice in lua.h
 */
@@ -94,7 +94,7 @@ Udata *luaS_newudata (lua_State *L, size_t s) {
   u->uv.marked = 0;  /* is not finalized */
   u->uv.tt = LUA_TUSERDATA;
   u->uv.len = s;
-  u->uv.metatable = hvalue(defaultmeta(L));
+  u->uv.metatable = NULL;
   /* chain it on udata list */
   u->uv.next = G(L)->rootudata;
   G(L)->rootudata = valtogco(u);
diff --git a/ltable.c b/ltable.c
index 8b55a1f5..ccd333f3 100644
--- a/ltable.c
+++ b/ltable.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltable.c,v 1.135 2003/08/26 12:04:13 roberto Exp roberto $
+** $Id: ltable.c,v 1.136 2003/11/27 18:05:14 roberto Exp roberto $
 ** Lua tables (hash)
 ** See Copyright Notice in lua.h
 */
@@ -328,7 +328,7 @@ static void rehash (lua_State *L, Table *t) {
 Table *luaH_new (lua_State *L, int narray, int lnhash) {
   Table *t = luaM_new(L, Table);
   luaC_link(L, valtogco(t), LUA_TTABLE);
-  t->metatable = hvalue(defaultmeta(L));
+  t->metatable = NULL;
   t->flags = cast(lu_byte, ~0);
   /* temporary values (kept only if some malloc fails) */
   t->array = NULL;
diff --git a/ltm.c b/ltm.c
index 5687e28b..b8fbb1f9 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 1.105 2002/12/04 17:38:31 roberto Exp roberto $
+** $Id: ltm.c,v 1.106 2003/04/03 13:35:34 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -57,14 +57,17 @@ const TObject *luaT_gettm (Table *events, TMS event, TString *ename) {
 
 
 const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) {
-  TString *ename = G(L)->tmname[event];
+  Table *mt;
   switch (ttype(o)) {
     case LUA_TTABLE:
-      return luaH_getstr(hvalue(o)->metatable, ename);
+      mt = hvalue(o)->metatable;
+      break;
     case LUA_TUSERDATA:
-      return luaH_getstr(uvalue(o)->uv.metatable, ename);
+      mt = uvalue(o)->uv.metatable;
+      break;
     default:
-      return &luaO_nilobject;
+      mt = NULL;
   }
+  return (mt ?  luaH_getstr(mt, G(L)->tmname[event]) : &luaO_nilobject);
 }
 
diff --git a/ltm.h b/ltm.h
index 4c56a093..d40bf6c5 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.h,v 1.40 2002/09/19 20:12:47 roberto Exp roberto $
+** $Id: ltm.h,v 1.41 2002/11/14 11:51:50 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -36,8 +36,8 @@ typedef enum {
 
 
 
-#define gfasttm(g,et,e) \
-  (((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
+#define gfasttm(g,et,e) ((et) == NULL ? NULL : \
+  ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e]))
 
 #define fasttm(l,et,e)	gfasttm(G(l), et, e)
 
-- 
cgit v1.2.3-55-g6feb