aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-08 16:32:53 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-05-08 16:32:53 -0300
commit11a70220670f25a9929439f0b27331f09f05235c (patch)
treec4a962b5a3e53ac6df8894fb3ad2248c4a1256cb /lapi.c
parent35a6ed283881f313152457f24cc6c677122d5058 (diff)
downloadlua-11a70220670f25a9929439f0b27331f09f05235c.tar.gz
lua-11a70220670f25a9929439f0b27331f09f05235c.tar.bz2
lua-11a70220670f25a9929439f0b27331f09f05235c.zip
global variables are stored in a Lua table
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c78
1 files changed, 33 insertions, 45 deletions
diff --git a/lapi.c b/lapi.c
index 6685abdc..60a9b45c 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.77 2000/03/29 20:19:20 roberto Exp roberto $ 2** $Id: lapi.c,v 1.78 2000/04/17 19:23:12 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*/
@@ -65,6 +65,20 @@ lua_Object lua_pop (lua_State *L) {
65} 65}
66 66
67 67
68void lua_pushglobaltable (lua_State *L) {
69 avalue(L->top) = L->gt;
70 ttype(L->top) = TAG_TABLE;
71 incr_top;
72}
73
74
75void lua_setglobaltable (lua_State *L, lua_Object newtable) {
76 if (lua_type(L, newtable)[0] != 't') /* type == "table"? */
77 lua_error(L, "Lua API error - invalid value for global table");
78 L->gt = avalue(newtable);
79}
80
81
68/* 82/*
69** Get a parameter, returning the object handle or LUA_NOOBJECT on error. 83** Get a parameter, returning the object handle or LUA_NOOBJECT on error.
70** `number' must be 1 to get the first parameter. 84** `number' must be 1 to get the first parameter.
@@ -131,7 +145,10 @@ void lua_settable (lua_State *L) {
131 145
132void lua_rawsettable (lua_State *L) { 146void lua_rawsettable (lua_State *L) {
133 luaA_checkCargs(L, 3); 147 luaA_checkCargs(L, 3);
134 luaV_rawsettable(L, L->top-3); 148 if (ttype(L->top-3) != TAG_TABLE)
149 lua_error(L, "indexed expression not a table");
150 luaH_set(L, avalue(L->top-3), L->top-2, L->top-1);
151 L->top -= 3;
135} 152}
136 153
137 154
@@ -145,27 +162,32 @@ lua_Object lua_createtable (lua_State *L) {
145 162
146 163
147lua_Object lua_getglobal (lua_State *L, const char *name) { 164lua_Object lua_getglobal (lua_State *L, const char *name) {
148 luaV_getglobal(L, luaS_assertglobalbyname(L, name), L->top++); 165 luaV_getglobal(L, luaS_new(L, name), L->top++);
149 return luaA_putObjectOnTop(L); 166 return luaA_putObjectOnTop(L);
150} 167}
151 168
152 169
153lua_Object lua_rawgetglobal (lua_State *L, const char *name) { 170void lua_setglobal (lua_State *L, const char *name) {
154 GlobalVar *gv = luaS_assertglobalbyname(L, name); 171 luaA_checkCargs(L, 1);
155 return luaA_putluaObject(L, &gv->value); 172 luaV_setglobal(L, luaS_new(L, name), L->top--);
156} 173}
157 174
158 175
159void lua_setglobal (lua_State *L, const char *name) { 176/* deprecated */
160 luaA_checkCargs(L, 1); 177lua_Object lua_rawgetglobal (lua_State *L, const char *name) {
161 luaV_setglobal(L, luaS_assertglobalbyname(L, name), L->top--); 178 lua_pushglobaltable(L);
179 lua_pushstring(L, name);
180 return lua_rawgettable(L);
162} 181}
163 182
164 183
184/* deprecated */
165void lua_rawsetglobal (lua_State *L, const char *name) { 185void lua_rawsetglobal (lua_State *L, const char *name) {
166 GlobalVar *gv = luaS_assertglobalbyname(L, name); 186 TObject key;
167 luaA_checkCargs(L, 1); 187 luaA_checkCargs(L, 1);
168 gv->value = *(--L->top); 188 ttype(&key) = TAG_STRING;
189 tsvalue(&key) = luaS_new(L, name);
190 luaH_set(L, L->gt, &key, --L->top);
169} 191}
170 192
171 193
@@ -334,40 +356,6 @@ void lua_settag (lua_State *L, int tag) {
334} 356}
335 357
336 358
337GlobalVar *luaA_nextvar (lua_State *L, TString *ts) {
338 GlobalVar *gv;
339 if (ts == NULL)
340 gv = L->rootglobal; /* first variable */
341 else {
342 /* check whether name is in global var list */
343 luaL_arg_check(L, ts->u.s.gv, 1, "variable name expected");
344 gv = ts->u.s.gv->next; /* get next */
345 }
346 while (gv && gv->value.ttype == TAG_NIL) /* skip globals with nil */
347 gv = gv->next;
348 if (gv) {
349 ttype(L->top) = TAG_STRING; tsvalue(L->top) = gv->name;
350 incr_top;
351 luaA_pushobject(L, &gv->value);
352 }
353 return gv;
354}
355
356
357const char *lua_nextvar (lua_State *L, const char *varname) {
358 TString *ts = (varname == NULL) ? NULL : luaS_new(L, varname);
359 GlobalVar *gv = luaA_nextvar(L, ts);
360 if (gv) {
361 top2LC(L, 2);
362 return gv->name->str;
363 }
364 else {
365 top2LC(L, 0);
366 return NULL;
367 }
368}
369
370
371int luaA_next (lua_State *L, const Hash *t, int i) { 359int luaA_next (lua_State *L, const Hash *t, int i) {
372 int tsize = t->size; 360 int tsize = t->size;
373 for (; i<tsize; i++) { 361 for (; i<tsize; i++) {