Exemple #1
0
static int
luaB_getfenv(lua_State * L)
{
  getfunc(L);
  if (!aux_getfenv(L))          /* __fenv not defined? */
    lua_pop(L, 1);              /* remove it, to return real environment */
  return 1;
}
Exemple #2
0
static int luaB_setfenv (lua_State *L) {
  luaL_checktype(L, 2, LUA_TTABLE);
  getfunc(L);
  if (aux_getfenv(L))  /* __fenv defined? */
    luaL_error(L, "`setfenv' cannot change a protected environment");
  else
    lua_pop(L, 2);  /* remove __fenv and real environment table */
  lua_pushvalue(L, 2);
  if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0)
    lua_replace(L, LUA_GLOBALSINDEX);
  else if (lua_setfenv(L, -2) == 0)
    luaL_error(L, "`setfenv' cannot change environment of given function");
  return 0;
}