aboutsummaryrefslogtreecommitdiff
path: root/lapi.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-04 15:23:12 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1999-11-04 15:23:12 -0200
commitcde179b36979c58d9380d3c4dd29b61412d13b51 (patch)
tree804c457691024d797a7479eddf711e103966b513 /lapi.c
parent80b39d83c3512e1dd627842e64c69841638d9088 (diff)
downloadlua-cde179b36979c58d9380d3c4dd29b61412d13b51.tar.gz
lua-cde179b36979c58d9380d3c4dd29b61412d13b51.tar.bz2
lua-cde179b36979c58d9380d3c4dd29b61412d13b51.zip
new implementation for global variable values (separated from strings)
Diffstat (limited to 'lapi.c')
-rw-r--r--lapi.c67
1 files changed, 28 insertions, 39 deletions
diff --git a/lapi.c b/lapi.c
index 80e4d120..020a4642 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.53 1999/10/11 16:13:11 roberto Exp roberto $ 2** $Id: lapi.c,v 1.54 1999/10/14 19:13:31 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*/
@@ -60,18 +60,6 @@ static const TObject *luaA_protovalue (const TObject *o) {
60} 60}
61 61
62 62
63void luaA_packresults (void) {
64 luaV_pack(L->Cstack.lua2C, L->Cstack.num, L->stack.top);
65 incr_top;
66}
67
68
69int luaA_passresults (void) {
70 L->Cstack.base = L->Cstack.lua2C; /* position of first result */
71 return L->Cstack.num;
72}
73
74
75static void checkCparams (int nParams) { 63static void checkCparams (int nParams) {
76 if (L->stack.top-L->stack.stack < L->Cstack.base+nParams) 64 if (L->stack.top-L->stack.stack < L->Cstack.base+nParams)
77 lua_error("API error - wrong number of arguments in C2lua stack"); 65 lua_error("API error - wrong number of arguments in C2lua stack");
@@ -191,28 +179,28 @@ lua_Object lua_createtable (void) {
191 179
192lua_Object lua_getglobal (const char *name) { 180lua_Object lua_getglobal (const char *name) {
193 luaD_checkstack(2); /* may need that to call T.M. */ 181 luaD_checkstack(2); /* may need that to call T.M. */
194 luaV_getglobal(luaS_new(name)); 182 luaV_getglobal(luaS_assertglobalbyname(name));
195 return luaA_putObjectOnTop(); 183 return luaA_putObjectOnTop();
196} 184}
197 185
198 186
199lua_Object lua_rawgetglobal (const char *name) { 187lua_Object lua_rawgetglobal (const char *name) {
200 TaggedString *ts = luaS_new(name); 188 GlobalVar *gv = luaS_assertglobalbyname(name);
201 return put_luaObject(&ts->u.s.globalval); 189 return put_luaObject(&gv->value);
202} 190}
203 191
204 192
205void lua_setglobal (const char *name) { 193void lua_setglobal (const char *name) {
206 checkCparams(1); 194 checkCparams(1);
207 luaD_checkstack(2); /* may need that to call T.M. */ 195 luaD_checkstack(2); /* may need that to call T.M. */
208 luaV_setglobal(luaS_new(name)); 196 luaV_setglobal(luaS_assertglobalbyname(name));
209} 197}
210 198
211 199
212void lua_rawsetglobal (const char *name) { 200void lua_rawsetglobal (const char *name) {
213 TaggedString *ts = luaS_new(name); 201 GlobalVar *gv = luaS_assertglobalbyname(name);
214 checkCparams(1); 202 checkCparams(1);
215 luaS_rawsetglobal(ts, --L->stack.top); 203 gv->value = *(--L->stack.top);
216} 204}
217 205
218 206
@@ -274,7 +262,7 @@ long lua_strlen (lua_Object object) {
274void *lua_getuserdata (lua_Object object) { 262void *lua_getuserdata (lua_Object object) {
275 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA) 263 if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
276 return NULL; 264 return NULL;
277 else return tsvalue(Address(object))->u.d.v; 265 else return tsvalue(Address(object))->u.d.value;
278} 266}
279 267
280lua_CFunction lua_getcfunction (lua_Object object) { 268lua_CFunction lua_getcfunction (lua_Object object) {
@@ -388,31 +376,32 @@ void lua_settag (int tag) {
388} 376}
389 377
390 378
391TaggedString *luaA_nextvar (TaggedString *g) { 379GlobalVar *luaA_nextvar (TaggedString *ts) {
392 if (g == NULL) 380 GlobalVar *gv;
393 g = L->rootglobal; /* first variable */ 381 if (ts == NULL)
382 gv = L->rootglobal; /* first variable */
394 else { 383 else {
395 /* check whether name is in global var list */ 384 /* check whether name is in global var list */
396 luaL_arg_check(g != g->nextglobal, 1, "variable name expected"); 385 luaL_arg_check(ts->u.s.gv, 1, "variable name expected");
397 g = g->nextglobal; /* get next */ 386 gv = ts->u.s.gv->next; /* get next */
398 } 387 }
399 while (g && g->u.s.globalval.ttype == LUA_T_NIL) /* skip globals with nil */ 388 while (gv && gv->value.ttype == LUA_T_NIL) /* skip globals with nil */
400 g = g->nextglobal; 389 gv = gv->next;
401 if (g) { 390 if (gv) {
402 ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g; 391 ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = gv->name;
403 incr_top; 392 incr_top;
404 luaA_pushobject(&g->u.s.globalval); 393 luaA_pushobject(&gv->value);
405 } 394 }
406 return g; 395 return gv;
407} 396}
408 397
409 398
410const char *lua_nextvar (const char *varname) { 399const char *lua_nextvar (const char *varname) {
411 TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname); 400 TaggedString *ts = (varname == NULL) ? NULL : luaS_new(varname);
412 g = luaA_nextvar(g); 401 GlobalVar *gv = luaA_nextvar(ts);
413 if (g) { 402 if (gv) {
414 top2LC(2); 403 top2LC(2);
415 return g->str; 404 return gv->name->str;
416 } 405 }
417 else { 406 else {
418 top2LC(0); 407 top2LC(0);
@@ -577,11 +566,11 @@ static int checkfunc (TObject *o) {
577 566
578const char *lua_getobjname (lua_Object o, const char **name) { 567const char *lua_getobjname (lua_Object o, const char **name) {
579 /* try to find a name for given function */ 568 /* try to find a name for given function */
580 TaggedString *g; 569 GlobalVar *g;
581 set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */ 570 set_normalized(L->stack.top, Address(o)); /* to be accessed by "checkfunc" */
582 for (g=L->rootglobal; g; g=g->nextglobal) { 571 for (g=L->rootglobal; g; g=g->next) {
583 if (checkfunc(&g->u.s.globalval)) { 572 if (checkfunc(&g->value)) {
584 *name = g->str; 573 *name = g->name->str;
585 return "global"; 574 return "global";
586 } 575 }
587 } 576 }