lua-users home
lua-l archive

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


I implemented the suggested lua_getcclosure (new function for 4.0b lapi.c
below).  I noticed that the same function could cover both C and Lua
closures uniformly easily (in that case it would be named lua_getclosure).

I'm new to this, there may be some bugs.

-John

----

int lua_getcclosure (lua_State *L, int index) {
  StkId top = L->top;
  StkId o = Index(L, index);
  int nelems = clvalue(o)->nupvalues;
  TObject* upvalues = clvalue(o)->upvalue;
  int i;
  luaC_checkGC(L);
  if (o >= L->top) return 0;  /* index out-of-range */
  LUA_ASSERT(ttype(closure) == TAG_CCLOSURE, "C closure expected");
  luaD_checkstack(L, nelems);
  for (i = 0; i<nelems; i++)
    *top++ = upvalues[i];
  L->top = top;
  return nelems;
}