lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Feb 18, 2012 at 8:16 AM, John Tan <johnmave126@gmail.com> wrote:
> Thanks!
> After inspecting the code of lua_upvaluejoin()
> I decide to write an API myself...
> here is the api i write
>
> LUA_API void lua_upvalueref (lua_State *L, int fidx, int n) {
>  LClosure *f1;
>  UpVal **up1 = getupvalref(L, fidx, n, &f1);
>  UpVal *uv = &luaC_newobj(L, LUA_TUPVAL, sizeof(UpVal), NULL, 0)->uv;
>  api_checknelems(L, 1);
>  uv->v = &uv->u.value;
>  L->top--;
>  setobj(L, uv->v, L->top);
>  *up1 = uv;
>  luaC_objbarrier(L, f1, uv);
> }
>

This is a bad idea... You shouldn't be changing the Lua API.

This is much easier:

luaL_loadstring(L, "");
assert(strcmp("_ENV", lua_getupvalue(L, -1, 1)) == 0);
lua_upvaluejoin(L, fidx, n, -1, 1);

If you need to do the above in a loop, make a Lua function which
returns dummy closures (with unique _ENV upvalues) and cache that
function:

luaL_loadstring(L, "local _ENV; return function() end");

-- 
- Patrick Donnelly