/* register functions from 'lib' in table 'to' by copying existing * closures from table 'from' or by creating new closures */ static void copyfields(lua_State *L, const luaL_reg *l, int from, int to) { for (to = absindex(L, to); l->name; l++) { lua_getfield(L, from, l->name); if (lua_isnil(L, -1)) { lua_pop(L, 1); lua_pushcfunction(L, l->func); } lua_setfield(L, to, l->name); } }
static FILE *check_file(lua_State *L, int idx, const char *argname) { #if LUA_VERSION_NUM <= 501 FILE **pf; if (idx > 0) pf = luaL_checkudata(L, idx, LUA_FILEHANDLE); else { idx = absindex(L, idx); pf = lua_touserdata(L, idx); luaL_getmetatable(L, LUA_FILEHANDLE); if (!pf || !lua_getmetatable(L, idx) || !lua_rawequal(L, -1, -2)) luaL_error(L, "bad %s option (%s expected, got %s)", argname, LUA_FILEHANDLE, luaL_typename(L, idx)); lua_pop(L, 2); } if (!*pf) return luaL_error(L, "attempt to use a closed file"), NULL; return *pf; #else luaL_Stream* p; idx = absindex(L, idx); p = (luaL_Stream *)luaL_checkudata(L, idx, LUA_FILEHANDLE); if (!p || !p->f) return luaL_error(L, "attempt to use a closed file"), NULL; return p->f; #endif }
/* Adds a reference from the archive at ar_idx to the object at * obj_idx. This reference will be a "weak" reference if is_weak is * true, otherwise it will be a normal reference that prevents the * value from being GC'ed. * * We need to keep these references around so we can assert that the * lifetime of "child" objects is always shorter than the lifetime of * the "parent" archive object. We also need to assert that any zip * source has a lifetime that lasts at least until the zip_close() * function is called. */ static void S_archive_add_ref(lua_State* L, int is_weak, int ar_idx, int obj_idx) { obj_idx = absindex(L, obj_idx); lua_getfenv(L, ar_idx); assert(lua_istable(L, -1) /* fenv of archive must exist! */); if ( is_weak ) { lua_pushvalue(L, obj_idx); lua_pushboolean(L, 1); lua_rawset(L, -3); } else { lua_pushvalue(L, obj_idx); lua_rawseti(L, -2, lua_objlen(L, -2)+1); } lua_pop(L, 1); /* Pop the fenv */ }