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 /lbuiltin.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 'lbuiltin.c')
-rw-r--r-- | lbuiltin.c | 102 |
1 files changed, 43 insertions, 59 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbuiltin.c,v 1.106 2000/04/17 19:23:12 roberto Exp roberto $ | 2 | ** $Id: lbuiltin.c,v 1.107 2000/04/25 16:55:09 roberto Exp roberto $ |
3 | ** Built-in functions | 3 | ** Built-in functions |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -52,13 +52,6 @@ void luaB_opentests (lua_State *L); | |||
52 | */ | 52 | */ |
53 | 53 | ||
54 | 54 | ||
55 | static void pushtagstring (lua_State *L, TString *s) { | ||
56 | ttype(L->top) = TAG_STRING; | ||
57 | tsvalue(L->top) = s; | ||
58 | incr_top; | ||
59 | } | ||
60 | |||
61 | |||
62 | static Number getsize (const Hash *h) { | 55 | static Number getsize (const Hash *h) { |
63 | Number max = 0; | 56 | Number max = 0; |
64 | int i = h->size; | 57 | int i = h->size; |
@@ -191,21 +184,10 @@ void luaB_setglobal (lua_State *L) { | |||
191 | lua_setglobal(L, name); | 184 | lua_setglobal(L, name); |
192 | } | 185 | } |
193 | 186 | ||
194 | void luaB_rawsetglobal (lua_State *L) { | ||
195 | const char *name = luaL_check_string(L, 1); | ||
196 | lua_Object value = luaL_nonnullarg(L, 2); | ||
197 | lua_pushobject(L, value); | ||
198 | lua_rawsetglobal(L, name); | ||
199 | } | ||
200 | |||
201 | void luaB_getglobal (lua_State *L) { | 187 | void luaB_getglobal (lua_State *L) { |
202 | lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); | 188 | lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); |
203 | } | 189 | } |
204 | 190 | ||
205 | void luaB_rawgetglobal (lua_State *L) { | ||
206 | lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); | ||
207 | } | ||
208 | |||
209 | void luaB_tag (lua_State *L) { | 191 | void luaB_tag (lua_State *L) { |
210 | lua_pushnumber(L, lua_tag(L, luaL_nonnullarg(L, 1))); | 192 | lua_pushnumber(L, lua_tag(L, luaL_nonnullarg(L, 1))); |
211 | } | 193 | } |
@@ -226,6 +208,12 @@ void luaB_copytagmethods (lua_State *L) { | |||
226 | luaL_check_int(L, 2))); | 208 | luaL_check_int(L, 2))); |
227 | } | 209 | } |
228 | 210 | ||
211 | void luaB_globals (lua_State *L) { | ||
212 | lua_pushglobaltable(L); | ||
213 | if (lua_getparam(L, 1) != LUA_NOOBJECT) | ||
214 | lua_setglobaltable(L, luaL_tablearg(L, 1)); | ||
215 | } | ||
216 | |||
229 | void luaB_rawgettable (lua_State *L) { | 217 | void luaB_rawgettable (lua_State *L) { |
230 | lua_pushobject(L, luaL_nonnullarg(L, 1)); | 218 | lua_pushobject(L, luaL_nonnullarg(L, 1)); |
231 | lua_pushobject(L, luaL_nonnullarg(L, 2)); | 219 | lua_pushobject(L, luaL_nonnullarg(L, 2)); |
@@ -346,20 +334,6 @@ void luaB_call (lua_State *L) { | |||
346 | } | 334 | } |
347 | 335 | ||
348 | 336 | ||
349 | void luaB_nextvar (lua_State *L) { | ||
350 | lua_Object o = lua_getparam(L, 1); | ||
351 | TString *name; | ||
352 | if (o == LUA_NOOBJECT || ttype(o) == TAG_NIL) | ||
353 | name = NULL; | ||
354 | else { | ||
355 | luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "variable name expected"); | ||
356 | name = tsvalue(o); | ||
357 | } | ||
358 | if (!luaA_nextvar(L, name)) | ||
359 | lua_pushnil(L); | ||
360 | } | ||
361 | |||
362 | |||
363 | void luaB_next (lua_State *L) { | 337 | void luaB_next (lua_State *L) { |
364 | const Hash *a = gettable(L, 1); | 338 | const Hash *a = gettable(L, 1); |
365 | lua_Object k = lua_getparam(L, 2); | 339 | lua_Object k = lua_getparam(L, 2); |
@@ -463,28 +437,6 @@ void luaB_foreach (lua_State *L) { | |||
463 | } | 437 | } |
464 | 438 | ||
465 | 439 | ||
466 | void luaB_foreachvar (lua_State *L) { | ||
467 | lua_Object f = luaL_functionarg(L, 1); | ||
468 | GlobalVar *gv; | ||
469 | luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ | ||
470 | for (gv = L->rootglobal; gv; gv = gv->next) { | ||
471 | if (gv->value.ttype != TAG_NIL) { | ||
472 | pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */ | ||
473 | *(L->top++) = *f; | ||
474 | pushtagstring(L, gv->name); | ||
475 | *(L->top++) = gv->value; | ||
476 | luaD_call(L, L->top-3, 1); | ||
477 | if (ttype(L->top-1) != TAG_NIL) { | ||
478 | *(L->top-2) = *(L->top-1); /* remove extra name */ | ||
479 | L->top--; | ||
480 | return; | ||
481 | } | ||
482 | L->top-=2; /* remove result and extra name */ | ||
483 | } | ||
484 | } | ||
485 | } | ||
486 | |||
487 | |||
488 | void luaB_getn (lua_State *L) { | 440 | void luaB_getn (lua_State *L) { |
489 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); | 441 | lua_pushnumber(L, getnarg(L, gettable(L, 1))); |
490 | } | 442 | } |
@@ -610,6 +562,39 @@ void luaB_sort (lua_State *L) { | |||
610 | /* }====================================================== */ | 562 | /* }====================================================== */ |
611 | 563 | ||
612 | 564 | ||
565 | /* | ||
566 | ** {====================================================== | ||
567 | ** Deprecated functions to manipulate global environment: | ||
568 | ** all of them can be simulated through table operations | ||
569 | ** over the global table. | ||
570 | ** ======================================================= | ||
571 | */ | ||
572 | |||
573 | #define num_deprecated 4 | ||
574 | |||
575 | static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { | ||
576 | {"foreachvar", luaB_foreach}, | ||
577 | {"nextvar", luaB_next}, | ||
578 | {"rawgetglobal", luaB_rawgettable}, | ||
579 | {"rawsetglobal", luaB_rawsettable} | ||
580 | }; | ||
581 | |||
582 | |||
583 | |||
584 | static void deprecated_funcs (lua_State *L) { | ||
585 | TObject gt; | ||
586 | int i; | ||
587 | ttype(>) = TAG_TABLE; | ||
588 | avalue(>) = L->gt; | ||
589 | for (i=0; i<num_deprecated; i++) { | ||
590 | lua_pushobject(L, >); | ||
591 | lua_pushcclosure(L, deprecated_global_funcs[i].func, 1); | ||
592 | lua_setglobal(L, deprecated_global_funcs[i].name); | ||
593 | } | ||
594 | } | ||
595 | |||
596 | /* }====================================================== */ | ||
597 | |||
613 | static const struct luaL_reg builtin_funcs[] = { | 598 | static const struct luaL_reg builtin_funcs[] = { |
614 | {"_ALERT", luaB__ALERT}, | 599 | {"_ALERT", luaB__ALERT}, |
615 | {"_ERRORMESSAGE", luaB__ERRORMESSAGE}, | 600 | {"_ERRORMESSAGE", luaB__ERRORMESSAGE}, |
@@ -621,13 +606,11 @@ static const struct luaL_reg builtin_funcs[] = { | |||
621 | {"error", luaB_error}, | 606 | {"error", luaB_error}, |
622 | {"getglobal", luaB_getglobal}, | 607 | {"getglobal", luaB_getglobal}, |
623 | {"gettagmethod", luaB_gettagmethod}, | 608 | {"gettagmethod", luaB_gettagmethod}, |
609 | {"globals", luaB_globals}, | ||
624 | {"newtag", luaB_newtag}, | 610 | {"newtag", luaB_newtag}, |
625 | {"next", luaB_next}, | 611 | {"next", luaB_next}, |
626 | {"nextvar", luaB_nextvar}, | ||
627 | {"print", luaB_print}, | 612 | {"print", luaB_print}, |
628 | {"rawgetglobal", luaB_rawgetglobal}, | ||
629 | {"rawgettable", luaB_rawgettable}, | 613 | {"rawgettable", luaB_rawgettable}, |
630 | {"rawsetglobal", luaB_rawsetglobal}, | ||
631 | {"rawsettable", luaB_rawsettable}, | 614 | {"rawsettable", luaB_rawsettable}, |
632 | {"setglobal", luaB_setglobal}, | 615 | {"setglobal", luaB_setglobal}, |
633 | {"settag", luaB_settag}, | 616 | {"settag", luaB_settag}, |
@@ -640,7 +623,6 @@ static const struct luaL_reg builtin_funcs[] = { | |||
640 | {"assert", luaB_assert}, | 623 | {"assert", luaB_assert}, |
641 | {"foreach", luaB_foreach}, | 624 | {"foreach", luaB_foreach}, |
642 | {"foreachi", luaB_foreachi}, | 625 | {"foreachi", luaB_foreachi}, |
643 | {"foreachvar", luaB_foreachvar}, | ||
644 | {"getn", luaB_getn}, | 626 | {"getn", luaB_getn}, |
645 | {"sort", luaB_sort}, | 627 | {"sort", luaB_sort}, |
646 | {"tinsert", luaB_tinsert}, | 628 | {"tinsert", luaB_tinsert}, |
@@ -648,6 +630,7 @@ static const struct luaL_reg builtin_funcs[] = { | |||
648 | }; | 630 | }; |
649 | 631 | ||
650 | 632 | ||
633 | |||
651 | void luaB_predefine (lua_State *L) { | 634 | void luaB_predefine (lua_State *L) { |
652 | /* pre-register mem error messages, to avoid loop when error arises */ | 635 | /* pre-register mem error messages, to avoid loop when error arises */ |
653 | luaS_newfixed(L, tableEM); | 636 | luaS_newfixed(L, tableEM); |
@@ -658,5 +641,6 @@ void luaB_predefine (lua_State *L) { | |||
658 | #endif | 641 | #endif |
659 | lua_pushstring(L, LUA_VERSION); | 642 | lua_pushstring(L, LUA_VERSION); |
660 | lua_setglobal(L, "_VERSION"); | 643 | lua_setglobal(L, "_VERSION"); |
644 | deprecated_funcs(L); | ||
661 | } | 645 | } |
662 | 646 | ||