diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-12-01 16:22:56 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-12-01 16:22:56 -0200 |
commit | af850484a9e01b46b04e4c666f9a9e91308d81c7 (patch) | |
tree | 38d03f647ddc95d430e4600e432598b7b9d07cd0 | |
parent | 1d10acb35500df47d6052164e6c56476f520232e (diff) | |
download | lua-af850484a9e01b46b04e4c666f9a9e91308d81c7.tar.gz lua-af850484a9e01b46b04e4c666f9a9e91308d81c7.tar.bz2 lua-af850484a9e01b46b04e4c666f9a9e91308d81c7.zip |
default metatable can be NULL
-rw-r--r-- | lapi.c | 19 | ||||
-rw-r--r-- | lgc.c | 9 | ||||
-rw-r--r-- | lstate.c | 7 | ||||
-rw-r--r-- | lstate.h | 6 | ||||
-rw-r--r-- | lstring.c | 4 | ||||
-rw-r--r-- | ltable.c | 4 | ||||
-rw-r--r-- | ltm.c | 13 | ||||
-rw-r--r-- | ltm.h | 6 |
8 files changed, 34 insertions, 34 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 1.248 2003/10/20 12:25:23 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 1.249 2003/10/20 17:42:41 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -555,7 +555,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { | |||
555 | mt = uvalue(obj)->uv.metatable; | 555 | mt = uvalue(obj)->uv.metatable; |
556 | break; | 556 | break; |
557 | } | 557 | } |
558 | if (mt == NULL || mt == hvalue(defaultmeta(L))) | 558 | if (mt == NULL) |
559 | res = 0; | 559 | res = 0; |
560 | else { | 560 | else { |
561 | sethvalue(L->top, mt); | 561 | sethvalue(L->top, mt); |
@@ -634,21 +634,26 @@ LUA_API void lua_rawseti (lua_State *L, int idx, int n) { | |||
634 | 634 | ||
635 | 635 | ||
636 | LUA_API int lua_setmetatable (lua_State *L, int objindex) { | 636 | LUA_API int lua_setmetatable (lua_State *L, int objindex) { |
637 | TObject *obj, *mt; | 637 | TObject *obj; |
638 | Table *mt; | ||
638 | int res = 1; | 639 | int res = 1; |
639 | lua_lock(L); | 640 | lua_lock(L); |
640 | api_checknelems(L, 1); | 641 | api_checknelems(L, 1); |
641 | obj = luaA_index(L, objindex); | 642 | obj = luaA_index(L, objindex); |
642 | api_checkvalidindex(L, obj); | 643 | api_checkvalidindex(L, obj); |
643 | mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); | 644 | if (ttisnil(L->top - 1)) |
644 | api_check(L, ttistable(mt)); | 645 | mt = NULL; |
646 | else { | ||
647 | api_check(L, ttistable(L->top - 1)); | ||
648 | mt = hvalue(L->top - 1); | ||
649 | } | ||
645 | switch (ttype(obj)) { | 650 | switch (ttype(obj)) { |
646 | case LUA_TTABLE: { | 651 | case LUA_TTABLE: { |
647 | hvalue(obj)->metatable = hvalue(mt); /* write barrier */ | 652 | hvalue(obj)->metatable = mt; /* write barrier */ |
648 | break; | 653 | break; |
649 | } | 654 | } |
650 | case LUA_TUSERDATA: { | 655 | case LUA_TUSERDATA: { |
651 | uvalue(obj)->uv.metatable = hvalue(mt); /* write barrier */ | 656 | uvalue(obj)->uv.metatable = mt; /* write barrier */ |
652 | break; | 657 | break; |
653 | } | 658 | } |
654 | default: { | 659 | default: { |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lgc.c,v 1.180 2003/11/19 19:41:57 roberto Exp roberto $ | 2 | ** $Id: lgc.c,v 1.181 2003/12/01 16:33:30 roberto Exp roberto $ |
3 | ** Garbage Collector | 3 | ** Garbage Collector |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -114,8 +114,9 @@ static void reallymarkobject (global_State *g, GCObject *o) { | |||
114 | return; | 114 | return; |
115 | } | 115 | } |
116 | case LUA_TUSERDATA: { | 116 | case LUA_TUSERDATA: { |
117 | Table *mt = gcotou(o)->uv.metatable; | ||
117 | white2black(o); /* userdata do not go to gray list */ | 118 | white2black(o); /* userdata do not go to gray list */ |
118 | markobject(g, gcotou(o)->uv.metatable); | 119 | if (mt) markobject(g, mt); |
119 | return; | 120 | return; |
120 | } | 121 | } |
121 | case LUA_TFUNCTION: { | 122 | case LUA_TFUNCTION: { |
@@ -191,7 +192,8 @@ static void traversetable (global_State *g, Table *h) { | |||
191 | int weakkey = 0; | 192 | int weakkey = 0; |
192 | int weakvalue = 0; | 193 | int weakvalue = 0; |
193 | const TObject *mode; | 194 | const TObject *mode; |
194 | markobject(g, h->metatable); | 195 | if (h->metatable) |
196 | markobject(g, h->metatable); | ||
195 | lua_assert(h->lsizenode || h->node == g->dummynode); | 197 | lua_assert(h->lsizenode || h->node == g->dummynode); |
196 | mode = gfasttm(g, h->metatable, TM_MODE); | 198 | mode = gfasttm(g, h->metatable, TM_MODE); |
197 | if (mode && ttisstring(mode)) { /* is there a weak mode? */ | 199 | if (mode && ttisstring(mode)) { /* is there a weak mode? */ |
@@ -534,7 +536,6 @@ static void markroot (lua_State *L) { | |||
534 | g->weak = NULL; | 536 | g->weak = NULL; |
535 | makewhite(valtogco(g->mainthread)); | 537 | makewhite(valtogco(g->mainthread)); |
536 | markobject(g, g->mainthread); | 538 | markobject(g, g->mainthread); |
537 | markvalue(g, defaultmeta(L)); | ||
538 | markvalue(g, registry(L)); | 539 | markvalue(g, registry(L)); |
539 | if (L != g->mainthread) /* another thread is running? */ | 540 | if (L != g->mainthread) /* another thread is running? */ |
540 | markobject(g, L); /* cannot collect it */ | 541 | markobject(g, L); /* cannot collect it */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 1.128 2003/11/18 14:55:11 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 1.129 2003/12/01 16:33:30 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -76,10 +76,6 @@ static void freestack (lua_State *L, lua_State *L1) { | |||
76 | static void f_luaopen (lua_State *L, void *ud) { | 76 | static void f_luaopen (lua_State *L, void *ud) { |
77 | UNUSED(ud); | 77 | UNUSED(ud); |
78 | stack_init(L, L); /* init stack */ | 78 | stack_init(L, L); /* init stack */ |
79 | /* create default meta table with a dummy table, and then close the loop */ | ||
80 | defaultmeta(L)->tt = LUA_TTABLE; | ||
81 | sethvalue(defaultmeta(L), luaH_new(L, 0, 0)); | ||
82 | hvalue(defaultmeta(L))->metatable = hvalue(defaultmeta(L)); | ||
83 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ | 79 | sethvalue(gt(L), luaH_new(L, 0, 4)); /* table of globals */ |
84 | sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ | 80 | sethvalue(registry(L), luaH_new(L, 4, 4)); /* registry */ |
85 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ | 81 | luaS_resize(L, MINSTRTABSIZE); /* initial size of string table */ |
@@ -161,7 +157,6 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
161 | g->strt.size = 0; | 157 | g->strt.size = 0; |
162 | g->strt.nuse = 0; | 158 | g->strt.nuse = 0; |
163 | g->strt.hash = NULL; | 159 | g->strt.hash = NULL; |
164 | setnilvalue(defaultmeta(L)); | ||
165 | setnilvalue(registry(L)); | 160 | setnilvalue(registry(L)); |
166 | luaZ_initbuffer(L, &g->buff); | 161 | luaZ_initbuffer(L, &g->buff); |
167 | g->panic = NULL; | 162 | g->panic = NULL; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.h,v 1.113 2003/11/18 14:55:11 roberto Exp roberto $ | 2 | ** $Id: lstate.h,v 1.114 2003/12/01 16:33:30 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -42,9 +42,6 @@ | |||
42 | struct lua_longjmp; /* defined in ldo.c */ | 42 | struct lua_longjmp; /* defined in ldo.c */ |
43 | 43 | ||
44 | 44 | ||
45 | /* default meta table (both for tables and udata) */ | ||
46 | #define defaultmeta(L) (&G(L)->_defaultmeta) | ||
47 | |||
48 | /* table of globals */ | 45 | /* table of globals */ |
49 | #define gt(L) (&L->_gt) | 46 | #define gt(L) (&L->_gt) |
50 | 47 | ||
@@ -113,7 +110,6 @@ typedef struct global_State { | |||
113 | lu_mem nblocks; /* number of `bytes' currently allocated */ | 110 | lu_mem nblocks; /* number of `bytes' currently allocated */ |
114 | lua_CFunction panic; /* to be called in unprotected errors */ | 111 | lua_CFunction panic; /* to be called in unprotected errors */ |
115 | TObject _registry; | 112 | TObject _registry; |
116 | TObject _defaultmeta; | ||
117 | struct lua_State *mainthread; | 113 | struct lua_State *mainthread; |
118 | Node dummynode[1]; /* common node array for all empty tables */ | 114 | Node dummynode[1]; /* common node array for all empty tables */ |
119 | TString *tmname[TM_N]; /* array with tag-method names */ | 115 | TString *tmname[TM_N]; /* array with tag-method names */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstring.c,v 1.79 2003/04/28 19:26:16 roberto Exp roberto $ | 2 | ** $Id: lstring.c,v 1.80 2003/11/17 19:50:05 roberto Exp roberto $ |
3 | ** String table (keeps all strings handled by Lua) | 3 | ** String table (keeps all strings handled by Lua) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -94,7 +94,7 @@ Udata *luaS_newudata (lua_State *L, size_t s) { | |||
94 | u->uv.marked = 0; /* is not finalized */ | 94 | u->uv.marked = 0; /* is not finalized */ |
95 | u->uv.tt = LUA_TUSERDATA; | 95 | u->uv.tt = LUA_TUSERDATA; |
96 | u->uv.len = s; | 96 | u->uv.len = s; |
97 | u->uv.metatable = hvalue(defaultmeta(L)); | 97 | u->uv.metatable = NULL; |
98 | /* chain it on udata list */ | 98 | /* chain it on udata list */ |
99 | u->uv.next = G(L)->rootudata; | 99 | u->uv.next = G(L)->rootudata; |
100 | G(L)->rootudata = valtogco(u); | 100 | G(L)->rootudata = valtogco(u); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltable.c,v 1.135 2003/08/26 12:04:13 roberto Exp roberto $ | 2 | ** $Id: ltable.c,v 1.136 2003/11/27 18:05:14 roberto Exp roberto $ |
3 | ** Lua tables (hash) | 3 | ** Lua tables (hash) |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -328,7 +328,7 @@ static void rehash (lua_State *L, Table *t) { | |||
328 | Table *luaH_new (lua_State *L, int narray, int lnhash) { | 328 | Table *luaH_new (lua_State *L, int narray, int lnhash) { |
329 | Table *t = luaM_new(L, Table); | 329 | Table *t = luaM_new(L, Table); |
330 | luaC_link(L, valtogco(t), LUA_TTABLE); | 330 | luaC_link(L, valtogco(t), LUA_TTABLE); |
331 | t->metatable = hvalue(defaultmeta(L)); | 331 | t->metatable = NULL; |
332 | t->flags = cast(lu_byte, ~0); | 332 | t->flags = cast(lu_byte, ~0); |
333 | /* temporary values (kept only if some malloc fails) */ | 333 | /* temporary values (kept only if some malloc fails) */ |
334 | t->array = NULL; | 334 | t->array = NULL; |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltm.c,v 1.105 2002/12/04 17:38:31 roberto Exp roberto $ | 2 | ** $Id: ltm.c,v 1.106 2003/04/03 13:35:34 roberto Exp roberto $ |
3 | ** Tag methods | 3 | ** Tag methods |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -57,14 +57,17 @@ const TObject *luaT_gettm (Table *events, TMS event, TString *ename) { | |||
57 | 57 | ||
58 | 58 | ||
59 | const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) { | 59 | const TObject *luaT_gettmbyobj (lua_State *L, const TObject *o, TMS event) { |
60 | TString *ename = G(L)->tmname[event]; | 60 | Table *mt; |
61 | switch (ttype(o)) { | 61 | switch (ttype(o)) { |
62 | case LUA_TTABLE: | 62 | case LUA_TTABLE: |
63 | return luaH_getstr(hvalue(o)->metatable, ename); | 63 | mt = hvalue(o)->metatable; |
64 | break; | ||
64 | case LUA_TUSERDATA: | 65 | case LUA_TUSERDATA: |
65 | return luaH_getstr(uvalue(o)->uv.metatable, ename); | 66 | mt = uvalue(o)->uv.metatable; |
67 | break; | ||
66 | default: | 68 | default: |
67 | return &luaO_nilobject; | 69 | mt = NULL; |
68 | } | 70 | } |
71 | return (mt ? luaH_getstr(mt, G(L)->tmname[event]) : &luaO_nilobject); | ||
69 | } | 72 | } |
70 | 73 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ltm.h,v 1.40 2002/09/19 20:12:47 roberto Exp roberto $ | 2 | ** $Id: ltm.h,v 1.41 2002/11/14 11:51:50 roberto Exp roberto $ |
3 | ** Tag methods | 3 | ** Tag methods |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -36,8 +36,8 @@ typedef enum { | |||
36 | 36 | ||
37 | 37 | ||
38 | 38 | ||
39 | #define gfasttm(g,et,e) \ | 39 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ |
40 | (((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) | 40 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) |
41 | 41 | ||
42 | #define fasttm(l,et,e) gfasttm(G(l), et, e) | 42 | #define fasttm(l,et,e) gfasttm(G(l), et, e) |
43 | 43 | ||