From 3941af53adee868e2cccfb9b85783aba9ac311c1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 20 Jun 2002 17:41:46 -0300 Subject: first implementation of independent global table per function --- lbaselib.c | 66 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 27 deletions(-) (limited to 'lbaselib.c') diff --git a/lbaselib.c b/lbaselib.c index 14843ace..67d95f8e 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.81 2002/06/13 13:44:50 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.82 2002/06/18 15:19:27 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -81,41 +81,51 @@ static int luaB_error (lua_State *L) { } -static int luaB_metatable (lua_State *L) { +static int luaB_getmetatable (lua_State *L) { luaL_check_any(L, 1); - if (lua_isnone(L, 2)) { - if (!lua_getmetatable(L, 1)) - return 0; /* no metatable */ - else { - lua_pushliteral(L, "__metatable"); - lua_rawget(L, -2); - if (lua_isnil(L, -1)) - lua_pop(L, 1); - } - } + if (!lua_getmetatable(L, 1)) + return 0; /* no metatable */ else { - int t = lua_type(L, 2); - luaL_check_type(L, 1, LUA_TTABLE); - luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, - "nil or table expected"); - lua_settop(L, 2); - lua_setmetatable(L, 1); + lua_pushliteral(L, "__metatable"); + lua_rawget(L, -2); + if (lua_isnil(L, -1)) + lua_pop(L, 1); + /* otherwise returns metatable.__metatable */ } return 1; } -static int luaB_globals (lua_State *L) { - lua_getglobals(L); /* value to be returned */ - if (!lua_isnoneornil(L, 1)) { - luaL_check_type(L, 1, LUA_TTABLE); - lua_pushvalue(L, 1); /* new table of globals */ - lua_setglobals(L); - } +static int luaB_setmetatable (lua_State *L) { + int t = lua_type(L, 2); + luaL_check_type(L, 1, LUA_TTABLE); + luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, + "nil or table expected"); + lua_settop(L, 2); + lua_setmetatable(L, 1); return 1; } +static int luaB_getglobals (lua_State *L) { + int level = luaL_opt_int(L, 1, 1); + luaL_arg_check(L, level >= 1, 2, "level must be positive"); + lua_getglobals(L, level); /* value to be returned */ + return 1; +} + + +static int luaB_setglobals (lua_State *L) { + int level = luaL_opt_int(L, 2, 1); + luaL_arg_check(L, level >= 1, 2, "level must be positive"); + luaL_check_type(L, 1, LUA_TTABLE); + lua_settop(L, 1); + if (lua_setglobals(L, level) == 0) + luaL_error(L, "cannot change global table at level %d", level); + return 0; +} + + static int luaB_rawequal (lua_State *L) { luaL_check_any(L, 1); luaL_check_any(L, 2); @@ -385,8 +395,10 @@ static int luaB_require (lua_State *L) { static const luaL_reg base_funcs[] = { {"error", luaB_error}, - {"metatable", luaB_metatable}, - {"globals", luaB_globals}, + {"getmetatable", luaB_getmetatable}, + {"setmetatable", luaB_setmetatable}, + {"getglobals", luaB_getglobals}, + {"setglobals", luaB_setglobals}, {"next", luaB_next}, {"nexti", luaB_nexti}, {"print", luaB_print}, -- cgit v1.2.3-55-g6feb