[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua 5.2, setfenv and the C API
- From: steve donovan <steve.j.donovan@...>
- Date: Mon, 11 Jan 2010 12:42:24 +0200
On Mon, Jan 11, 2010 at 1:42 AM, Leo Razoumov <slonik.az@gmail.com> wrote:
> Taking this statement at its face value could one simply create a
> module fenv that exports lua_getfenv/lua_setenv as Lua functions and
> get back 5.1 behavior by simply doing require "fenv" ??
Indeed!
/* Exporting getfenv/setfenv in Lua 5.2
Note: this is basically the same code that is included if LUA_COMPAT_FENV
is enabled (default is off)
See lbaselib.c, search for LUA_COMPAT_FENV.
Steve Donovan, 2010, MIT/X11
*/
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
static void getfunc (lua_State *L, int opt) {
if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
else {
lua_Debug ar;
int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
if (lua_getstack(L, level, &ar) == 0)
luaL_argerror(L, 1, "invalid level");
lua_getinfo(L, "f", &ar);
}
}
static int l_getfenv (lua_State *L) {
getfunc(L, 1);
if (lua_iscfunction(L, -1)) /* is a C function? */
lua_pushglobaltable(L); /* return the global env. */
else
lua_getfenv(L, -1);
return 1;
}
static int l_setfenv (lua_State *L) {
luaL_checktype(L, 2, LUA_TTABLE);
getfunc(L, 0);
lua_pushvalue(L, 2);
if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
return luaL_error(L,
LUA_QL("setfenv") " cannot change environment of given object");
return 1;
}
static const luaL_reg fenv[] = {
{"getfenv",l_getfenv},
{"setfenv",l_setfenv},
{NULL,NULL}
};
LUALIB_API int luaopen_fenv(lua_State *L)
{
luaL_register (L, "fenv", fenv);
return 1;
}