diff options
Diffstat (limited to 'lbaselib.c')
-rw-r--r-- | lbaselib.c | 66 |
1 files changed, 39 insertions, 27 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.81 2002/06/13 13:44:50 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.82 2002/06/18 15:19:27 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -81,41 +81,51 @@ static int luaB_error (lua_State *L) { | |||
81 | } | 81 | } |
82 | 82 | ||
83 | 83 | ||
84 | static int luaB_metatable (lua_State *L) { | 84 | static int luaB_getmetatable (lua_State *L) { |
85 | luaL_check_any(L, 1); | 85 | luaL_check_any(L, 1); |
86 | if (lua_isnone(L, 2)) { | 86 | if (!lua_getmetatable(L, 1)) |
87 | if (!lua_getmetatable(L, 1)) | 87 | return 0; /* no metatable */ |
88 | return 0; /* no metatable */ | ||
89 | else { | ||
90 | lua_pushliteral(L, "__metatable"); | ||
91 | lua_rawget(L, -2); | ||
92 | if (lua_isnil(L, -1)) | ||
93 | lua_pop(L, 1); | ||
94 | } | ||
95 | } | ||
96 | else { | 88 | else { |
97 | int t = lua_type(L, 2); | 89 | lua_pushliteral(L, "__metatable"); |
98 | luaL_check_type(L, 1, LUA_TTABLE); | 90 | lua_rawget(L, -2); |
99 | luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, | 91 | if (lua_isnil(L, -1)) |
100 | "nil or table expected"); | 92 | lua_pop(L, 1); |
101 | lua_settop(L, 2); | 93 | /* otherwise returns metatable.__metatable */ |
102 | lua_setmetatable(L, 1); | ||
103 | } | 94 | } |
104 | return 1; | 95 | return 1; |
105 | } | 96 | } |
106 | 97 | ||
107 | 98 | ||
108 | static int luaB_globals (lua_State *L) { | 99 | static int luaB_setmetatable (lua_State *L) { |
109 | lua_getglobals(L); /* value to be returned */ | 100 | int t = lua_type(L, 2); |
110 | if (!lua_isnoneornil(L, 1)) { | 101 | luaL_check_type(L, 1, LUA_TTABLE); |
111 | luaL_check_type(L, 1, LUA_TTABLE); | 102 | luaL_arg_check(L, t == LUA_TNIL || t == LUA_TTABLE, 2, |
112 | lua_pushvalue(L, 1); /* new table of globals */ | 103 | "nil or table expected"); |
113 | lua_setglobals(L); | 104 | lua_settop(L, 2); |
114 | } | 105 | lua_setmetatable(L, 1); |
115 | return 1; | 106 | return 1; |
116 | } | 107 | } |
117 | 108 | ||
118 | 109 | ||
110 | static int luaB_getglobals (lua_State *L) { | ||
111 | int level = luaL_opt_int(L, 1, 1); | ||
112 | luaL_arg_check(L, level >= 1, 2, "level must be positive"); | ||
113 | lua_getglobals(L, level); /* value to be returned */ | ||
114 | return 1; | ||
115 | } | ||
116 | |||
117 | |||
118 | static int luaB_setglobals (lua_State *L) { | ||
119 | int level = luaL_opt_int(L, 2, 1); | ||
120 | luaL_arg_check(L, level >= 1, 2, "level must be positive"); | ||
121 | luaL_check_type(L, 1, LUA_TTABLE); | ||
122 | lua_settop(L, 1); | ||
123 | if (lua_setglobals(L, level) == 0) | ||
124 | luaL_error(L, "cannot change global table at level %d", level); | ||
125 | return 0; | ||
126 | } | ||
127 | |||
128 | |||
119 | static int luaB_rawequal (lua_State *L) { | 129 | static int luaB_rawequal (lua_State *L) { |
120 | luaL_check_any(L, 1); | 130 | luaL_check_any(L, 1); |
121 | luaL_check_any(L, 2); | 131 | luaL_check_any(L, 2); |
@@ -385,8 +395,10 @@ static int luaB_require (lua_State *L) { | |||
385 | 395 | ||
386 | static const luaL_reg base_funcs[] = { | 396 | static const luaL_reg base_funcs[] = { |
387 | {"error", luaB_error}, | 397 | {"error", luaB_error}, |
388 | {"metatable", luaB_metatable}, | 398 | {"getmetatable", luaB_getmetatable}, |
389 | {"globals", luaB_globals}, | 399 | {"setmetatable", luaB_setmetatable}, |
400 | {"getglobals", luaB_getglobals}, | ||
401 | {"setglobals", luaB_setglobals}, | ||
390 | {"next", luaB_next}, | 402 | {"next", luaB_next}, |
391 | {"nexti", luaB_nexti}, | 403 | {"nexti", luaB_nexti}, |
392 | {"print", luaB_print}, | 404 | {"print", luaB_print}, |