diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-02-18 10:40:02 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-02-18 10:40:02 -0200 |
| commit | 7d45a5f48ff32a4e09a1734de23823943d6a6b28 (patch) | |
| tree | de7bef1faf37d9b639928e5269e89d7112b5b6fa /ldblib.c | |
| parent | 73d764024451c24bc43b8e5102fe90974a86b7f4 (diff) | |
| download | lua-7d45a5f48ff32a4e09a1734de23823943d6a6b28.tar.gz lua-7d45a5f48ff32a4e09a1734de23823943d6a6b28.tar.bz2 lua-7d45a5f48ff32a4e09a1734de23823943d6a6b28.zip | |
C functions and userdata also have environments
Diffstat (limited to 'ldblib.c')
| -rw-r--r-- | ldblib.c | 40 |
1 files changed, 39 insertions, 1 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ldblib.c,v 1.91 2005/01/10 17:21:10 roberto Exp roberto $ | 2 | ** $Id: ldblib.c,v 1.92 2005/01/18 17:23:25 roberto Exp roberto $ |
| 3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -19,6 +19,40 @@ | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | static int getmetatable (lua_State *L) { | ||
| 23 | luaL_checkany(L, 1); | ||
| 24 | if (!lua_getmetatable(L, 1)) { | ||
| 25 | lua_pushnil(L); /* no metatable */ | ||
| 26 | } | ||
| 27 | return 1; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | static int setmetatable (lua_State *L) { | ||
| 32 | int t = lua_type(L, 2); | ||
| 33 | luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2, | ||
| 34 | "nil or table expected"); | ||
| 35 | lua_settop(L, 2); | ||
| 36 | lua_pushboolean(L, lua_setmetatable(L, 1)); | ||
| 37 | return 1; | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | static int getfenv (lua_State *L) { | ||
| 42 | lua_getfenv(L, 1); | ||
| 43 | return 1; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | static int setfenv (lua_State *L) { | ||
| 48 | luaL_checktype(L, 2, LUA_TTABLE); | ||
| 49 | lua_settop(L, 2); | ||
| 50 | if (lua_setfenv(L, 1) == 0) | ||
| 51 | luaL_error(L, "`setfenv' cannot change environment of given object"); | ||
| 52 | return 1; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 22 | static void settabss (lua_State *L, const char *i, const char *v) { | 56 | static void settabss (lua_State *L, const char *i, const char *v) { |
| 23 | lua_pushstring(L, v); | 57 | lua_pushstring(L, v); |
| 24 | lua_setfield(L, -2, i); | 58 | lua_setfield(L, -2, i); |
| @@ -328,6 +362,10 @@ static int errorfb (lua_State *L) { | |||
| 328 | 362 | ||
| 329 | 363 | ||
| 330 | static const luaL_reg dblib[] = { | 364 | static const luaL_reg dblib[] = { |
| 365 | {"getmetatable", getmetatable}, | ||
| 366 | {"setmetatable", setmetatable}, | ||
| 367 | {"getfenv", getfenv}, | ||
| 368 | {"setfenv", setfenv}, | ||
| 331 | {"getlocal", getlocal}, | 369 | {"getlocal", getlocal}, |
| 332 | {"getinfo", getinfo}, | 370 | {"getinfo", getinfo}, |
| 333 | {"gethook", gethook}, | 371 | {"gethook", gethook}, |
