From 11a70220670f25a9929439f0b27331f09f05235c Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 8 May 2000 16:32:53 -0300 Subject: global variables are stored in a Lua table --- lbuiltin.c | 102 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 43 insertions(+), 59 deletions(-) (limited to 'lbuiltin.c') diff --git a/lbuiltin.c b/lbuiltin.c index d57f9fa2..060127f7 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.106 2000/04/17 19:23:12 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.107 2000/04/25 16:55:09 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -52,13 +52,6 @@ void luaB_opentests (lua_State *L); */ -static void pushtagstring (lua_State *L, TString *s) { - ttype(L->top) = TAG_STRING; - tsvalue(L->top) = s; - incr_top; -} - - static Number getsize (const Hash *h) { Number max = 0; int i = h->size; @@ -191,21 +184,10 @@ void luaB_setglobal (lua_State *L) { lua_setglobal(L, name); } -void luaB_rawsetglobal (lua_State *L) { - const char *name = luaL_check_string(L, 1); - lua_Object value = luaL_nonnullarg(L, 2); - lua_pushobject(L, value); - lua_rawsetglobal(L, name); -} - void luaB_getglobal (lua_State *L) { lua_pushobject(L, lua_getglobal(L, luaL_check_string(L, 1))); } -void luaB_rawgetglobal (lua_State *L) { - lua_pushobject(L, lua_rawgetglobal(L, luaL_check_string(L, 1))); -} - void luaB_tag (lua_State *L) { lua_pushnumber(L, lua_tag(L, luaL_nonnullarg(L, 1))); } @@ -226,6 +208,12 @@ void luaB_copytagmethods (lua_State *L) { luaL_check_int(L, 2))); } +void luaB_globals (lua_State *L) { + lua_pushglobaltable(L); + if (lua_getparam(L, 1) != LUA_NOOBJECT) + lua_setglobaltable(L, luaL_tablearg(L, 1)); +} + void luaB_rawgettable (lua_State *L) { lua_pushobject(L, luaL_nonnullarg(L, 1)); lua_pushobject(L, luaL_nonnullarg(L, 2)); @@ -346,20 +334,6 @@ void luaB_call (lua_State *L) { } -void luaB_nextvar (lua_State *L) { - lua_Object o = lua_getparam(L, 1); - TString *name; - if (o == LUA_NOOBJECT || ttype(o) == TAG_NIL) - name = NULL; - else { - luaL_arg_check(L, ttype(o) == TAG_STRING, 1, "variable name expected"); - name = tsvalue(o); - } - if (!luaA_nextvar(L, name)) - lua_pushnil(L); -} - - void luaB_next (lua_State *L) { const Hash *a = gettable(L, 1); lua_Object k = lua_getparam(L, 2); @@ -463,28 +437,6 @@ void luaB_foreach (lua_State *L) { } -void luaB_foreachvar (lua_State *L) { - lua_Object f = luaL_functionarg(L, 1); - GlobalVar *gv; - luaD_checkstack(L, 4); /* for extra var name, f, var name, and globalval */ - for (gv = L->rootglobal; gv; gv = gv->next) { - if (gv->value.ttype != TAG_NIL) { - pushtagstring(L, gv->name); /* keep (extra) name on stack to avoid GC */ - *(L->top++) = *f; - pushtagstring(L, gv->name); - *(L->top++) = gv->value; - luaD_call(L, L->top-3, 1); - if (ttype(L->top-1) != TAG_NIL) { - *(L->top-2) = *(L->top-1); /* remove extra name */ - L->top--; - return; - } - L->top-=2; /* remove result and extra name */ - } - } -} - - void luaB_getn (lua_State *L) { lua_pushnumber(L, getnarg(L, gettable(L, 1))); } @@ -610,6 +562,39 @@ void luaB_sort (lua_State *L) { /* }====================================================== */ +/* +** {====================================================== +** Deprecated functions to manipulate global environment: +** all of them can be simulated through table operations +** over the global table. +** ======================================================= +*/ + +#define num_deprecated 4 + +static const struct luaL_reg deprecated_global_funcs[num_deprecated] = { + {"foreachvar", luaB_foreach}, + {"nextvar", luaB_next}, + {"rawgetglobal", luaB_rawgettable}, + {"rawsetglobal", luaB_rawsettable} +}; + + + +static void deprecated_funcs (lua_State *L) { + TObject gt; + int i; + ttype(>) = TAG_TABLE; + avalue(>) = L->gt; + for (i=0; i