diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-08 15:07:59 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-02-08 15:07:59 -0200 |
commit | fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9 (patch) | |
tree | 20aa6d78060600e477164e8d032fd8c33aa7c6d4 /lvm.c | |
parent | 19ca2087de4002600b891cf78b3c9b1d939ba2ca (diff) | |
download | lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.tar.gz lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.tar.bz2 lua-fb1cf6ab2d12bd61e3d429830ab20d4ff3c39fd9.zip |
clearer way to set tables.
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 70 |
1 files changed, 35 insertions, 35 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.44 1999/02/04 16:36:16 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.45 1999/02/04 17:47:59 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -110,64 +110,62 @@ void luaV_closure (int nelems) { | |||
110 | ** Receives the table at top-2 and the index at top-1. | 110 | ** Receives the table at top-2 and the index at top-1. |
111 | */ | 111 | */ |
112 | void luaV_gettable (void) { | 112 | void luaV_gettable (void) { |
113 | struct Stack *S = &L->stack; | 113 | TObject *table = L->stack.top-2; |
114 | TObject *im; | 114 | TObject *im; |
115 | if (ttype(S->top-2) != LUA_T_ARRAY) /* not a table, get "gettable" method */ | 115 | if (ttype(table) != LUA_T_ARRAY) { /* not a table, get gettable method */ |
116 | im = luaT_getimbyObj(S->top-2, IM_GETTABLE); | 116 | im = luaT_getimbyObj(table, IM_GETTABLE); |
117 | if (ttype(im) == LUA_T_NIL) | ||
118 | lua_error("indexed expression not a table"); | ||
119 | } | ||
117 | else { /* object is a table... */ | 120 | else { /* object is a table... */ |
118 | int tg = (S->top-2)->value.a->htag; | 121 | int tg = table->value.a->htag; |
119 | im = luaT_getim(tg, IM_GETTABLE); | 122 | im = luaT_getim(tg, IM_GETTABLE); |
120 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ | 123 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "gettable" method */ |
121 | TObject *h = luaH_get(avalue(S->top-2), S->top-1); | 124 | TObject *h = luaH_get(avalue(table), table+1); |
122 | if (ttype(h) != LUA_T_NIL) { | 125 | if (ttype(h) == LUA_T_NIL && |
123 | --S->top; | 126 | (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL)) { |
124 | *(S->top-1) = *h; | 127 | /* result is nil and there is an "index" tag method */ |
128 | luaD_callTM(im, 2, 1); /* calls it */ | ||
125 | } | 129 | } |
126 | else if (ttype(im=luaT_getim(tg, IM_INDEX)) != LUA_T_NIL) | ||
127 | luaD_callTM(im, 2, 1); | ||
128 | else { | 130 | else { |
129 | --S->top; | 131 | L->stack.top--; |
130 | ttype(S->top-1) = LUA_T_NIL; | 132 | *table = *h; /* "push" result into table position */ |
131 | } | 133 | } |
132 | return; | 134 | return; |
133 | } | 135 | } |
134 | /* else it has a "gettable" method, go through to next command */ | 136 | /* else it has a "gettable" method, go through to next command */ |
135 | } | 137 | } |
136 | /* object is not a table, or it has a "gettable" method */ | 138 | /* object is not a table, or it has a "gettable" method */ |
137 | if (ttype(im) == LUA_T_NIL) | ||
138 | lua_error("indexed expression not a table"); | ||
139 | luaD_callTM(im, 2, 1); | 139 | luaD_callTM(im, 2, 1); |
140 | } | 140 | } |
141 | 141 | ||
142 | 142 | ||
143 | /* | 143 | /* |
144 | ** Function to store indexed based on values at the stack.top | 144 | ** Receives table at *t, index at *(t+1) and value at top. |
145 | ** deep = 1: "deep L->stack.stack" store (with tag methods) | ||
146 | */ | 145 | */ |
147 | void luaV_settable (TObject *t, int deep) { | 146 | void luaV_settable (TObject *t) { |
148 | struct Stack *S = &L->stack; | 147 | struct Stack *S = &L->stack; |
149 | TObject *im; | 148 | TObject *im; |
150 | if (ttype(t) != LUA_T_ARRAY) /* not a table, get "settable" method */ | 149 | if (ttype(t) != LUA_T_ARRAY) { /* not a table, get "settable" method */ |
151 | im = luaT_getimbyObj(t, IM_SETTABLE); | 150 | im = luaT_getimbyObj(t, IM_SETTABLE); |
151 | if (ttype(im) == LUA_T_NIL) | ||
152 | lua_error("indexed expression not a table"); | ||
153 | } | ||
152 | else { /* object is a table... */ | 154 | else { /* object is a table... */ |
153 | im = luaT_getim(avalue(t)->htag, IM_SETTABLE); | 155 | im = luaT_getim(avalue(t)->htag, IM_SETTABLE); |
154 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ | 156 | if (ttype(im) == LUA_T_NIL) { /* and does not have a "settable" method */ |
155 | luaH_set(avalue(t), t+1, S->top-1); | 157 | luaH_set(avalue(t), t+1, S->top-1); |
156 | /* if deep, pop only value; otherwise, pop table, index and value */ | 158 | S->top--; /* pop value */ |
157 | S->top -= (deep) ? 1 : 3; | ||
158 | return; | 159 | return; |
159 | } | 160 | } |
160 | /* else it has a "settable" method, go through to next command */ | 161 | /* else it has a "settable" method, go through to next command */ |
161 | } | 162 | } |
162 | /* object is not a table, or it has a "settable" method */ | 163 | /* object is not a table, or it has a "settable" method */ |
163 | if (ttype(im) == LUA_T_NIL) | 164 | /* prepare arguments and call the tag method */ |
164 | lua_error("indexed expression not a table"); | 165 | *(S->top+1) = *(L->stack.top-1); |
165 | if (deep) { /* table and index were not on top; copy them */ | 166 | *(S->top) = *(t+1); |
166 | *(S->top+1) = *(L->stack.top-1); | 167 | *(S->top-1) = *t; |
167 | *(S->top) = *(t+1); | 168 | S->top += 2; /* WARNING: caller must assure stack space */ |
168 | *(S->top-1) = *t; | ||
169 | S->top += 2; /* WARNING: caller must assure stack space */ | ||
170 | } | ||
171 | luaD_callTM(im, 3, 0); | 169 | luaD_callTM(im, 3, 0); |
172 | } | 170 | } |
173 | 171 | ||
@@ -421,19 +419,21 @@ StkId luaV_execute (Closure *cl, TProtoFunc *tf, StkId base) { | |||
421 | luaV_setglobal(tsvalue(&consts[aux])); | 419 | luaV_setglobal(tsvalue(&consts[aux])); |
422 | break; | 420 | break; |
423 | 421 | ||
424 | case SETTABLE0: | 422 | case SETTABLEPOP: |
425 | luaV_settable(S->top-3, 0); | 423 | luaV_settable(S->top-3); |
424 | S->top -= 2; /* pop table and index */ | ||
426 | break; | 425 | break; |
427 | 426 | ||
428 | case SETTABLEDUP: { | 427 | case SETTABPPDUP: { |
429 | TObject temp = *(S->top-1); | 428 | TObject temp = *(S->top-1); |
430 | luaV_settable(S->top-3, 0); | 429 | luaV_settable(S->top-3); |
431 | *(S->top++) = temp; | 430 | S->top--; /* pop index (temp goes into "table" position) */ |
431 | *(S->top-1) = temp; | ||
432 | break; | 432 | break; |
433 | } | 433 | } |
434 | 434 | ||
435 | case SETTABLE: | 435 | case SETTABLE: |
436 | luaV_settable(S->top-3-(*pc++), 1); | 436 | luaV_settable(S->top-3-(*pc++)); |
437 | break; | 437 | break; |
438 | 438 | ||
439 | case SETLISTW: aux += highbyte(*pc++); | 439 | case SETLISTW: aux += highbyte(*pc++); |