diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-08 16:32:53 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-05-08 16:32:53 -0300 |
commit | 11a70220670f25a9929439f0b27331f09f05235c (patch) | |
tree | c4a962b5a3e53ac6df8894fb3ad2248c4a1256cb /lvm.c | |
parent | 35a6ed283881f313152457f24cc6c677122d5058 (diff) | |
download | lua-11a70220670f25a9929439f0b27331f09f05235c.tar.gz lua-11a70220670f25a9929439f0b27331f09f05235c.tar.bz2 lua-11a70220670f25a9929439f0b27331f09f05235c.zip |
global variables are stored in a Lua table
Diffstat (limited to 'lvm.c')
-rw-r--r-- | lvm.c | 40 |
1 files changed, 19 insertions, 21 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 1.103 2000/04/14 17:45:25 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 1.104 2000/04/19 13:36:25 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 | */ |
@@ -164,18 +164,8 @@ void luaV_settable (lua_State *L, StkId t, StkId top) { | |||
164 | } | 164 | } |
165 | 165 | ||
166 | 166 | ||
167 | void luaV_rawsettable (lua_State *L, StkId t) { | 167 | void luaV_getglobal (lua_State *L, TString *s, StkId top) { |
168 | if (ttype(t) != TAG_TABLE) | 168 | const TObject *value = luaH_getstr(L->gt, s); |
169 | lua_error(L, "indexed expression not a table"); | ||
170 | else { | ||
171 | luaH_set(L, avalue(t), t+1, L->top-1); | ||
172 | L->top -= 3; | ||
173 | } | ||
174 | } | ||
175 | |||
176 | |||
177 | void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) { | ||
178 | const TObject *value = &gv->value; | ||
179 | TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); | 169 | TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); |
180 | if (ttype(im) == TAG_NIL) /* is there a tag method? */ | 170 | if (ttype(im) == TAG_NIL) /* is there a tag method? */ |
181 | *top = *value; /* default behavior */ | 171 | *top = *value; /* default behavior */ |
@@ -183,7 +173,7 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) { | |||
183 | luaD_checkstack(L, 3); | 173 | luaD_checkstack(L, 3); |
184 | *top = *im; | 174 | *top = *im; |
185 | ttype(top+1) = TAG_STRING; | 175 | ttype(top+1) = TAG_STRING; |
186 | tsvalue(top+1) = gv->name; /* global name */ | 176 | tsvalue(top+1) = s; /* global name */ |
187 | *(top+2) = *value; | 177 | *(top+2) = *value; |
188 | L->top = top+3; | 178 | L->top = top+3; |
189 | luaD_call(L, top, 1); | 179 | luaD_call(L, top, 1); |
@@ -191,17 +181,25 @@ void luaV_getglobal (lua_State *L, GlobalVar *gv, StkId top) { | |||
191 | } | 181 | } |
192 | 182 | ||
193 | 183 | ||
194 | void luaV_setglobal (lua_State *L, GlobalVar *gv, StkId top) { | 184 | void luaV_setglobal (lua_State *L, TString *s, StkId top) { |
195 | const TObject *oldvalue = &gv->value; | 185 | const TObject *oldvalue = luaH_getstr(L->gt, s); |
196 | const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); | 186 | const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); |
197 | if (ttype(im) == TAG_NIL) /* is there a tag method? */ | 187 | if (ttype(im) == TAG_NIL) { /* is there a tag method? */ |
198 | gv->value = *(top-1); | 188 | if (oldvalue != &luaO_nilobject) |
189 | *oldvalue = *(top-1); | ||
190 | else { | ||
191 | TObject key; | ||
192 | ttype(&key) = TAG_STRING; | ||
193 | tsvalue(&key) = s; | ||
194 | luaH_set(L, L->gt, &key, top-1); | ||
195 | } | ||
196 | } | ||
199 | else { | 197 | else { |
200 | luaD_checkstack(L, 3); | 198 | luaD_checkstack(L, 3); |
201 | *(top+2) = *(top-1); /* new value */ | 199 | *(top+2) = *(top-1); /* new value */ |
202 | *(top+1) = *oldvalue; | 200 | *(top+1) = *oldvalue; |
203 | ttype(top) = TAG_STRING; | 201 | ttype(top) = TAG_STRING; |
204 | tsvalue(top) = gv->name; | 202 | tsvalue(top) = s; |
205 | *(top-1) = *im; | 203 | *(top-1) = *im; |
206 | L->top = top+3; | 204 | L->top = top+3; |
207 | luaD_call(L, top-1, 0); | 205 | luaD_call(L, top-1, 0); |
@@ -415,7 +413,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
415 | break; | 413 | break; |
416 | 414 | ||
417 | case OP_GETGLOBAL: | 415 | case OP_GETGLOBAL: |
418 | luaV_getglobal(L, kstr[GETARG_U(i)]->u.s.gv, top); | 416 | luaV_getglobal(L, kstr[GETARG_U(i)], top); |
419 | top++; | 417 | top++; |
420 | break; | 418 | break; |
421 | 419 | ||
@@ -460,7 +458,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { | |||
460 | break; | 458 | break; |
461 | 459 | ||
462 | case OP_SETGLOBAL: | 460 | case OP_SETGLOBAL: |
463 | luaV_setglobal(L, kstr[GETARG_U(i)]->u.s.gv, top); | 461 | luaV_setglobal(L, kstr[GETARG_U(i)], top); |
464 | top--; | 462 | top--; |
465 | break; | 463 | break; |
466 | 464 | ||