| 240 | |
| 241 | |
| 242 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, |
| 243 | const luaL_Reg *l, int nup) { |
| 244 | if (libname) { |
| 245 | int size = libsize(l); |
| 246 | /* check whether lib already exists */ |
| 247 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); |
| 248 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
| 249 | if (!lua_istable(L, -1)) { /* not found? */ |
| 250 | lua_pop(L, 1); /* remove previous result */ |
| 251 | /* try global variable (and create one if it does not exist) */ |
| 252 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) |
| 253 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
| 254 | lua_pushvalue(L, -1); |
| 255 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
| 256 | } |
| 257 | lua_remove(L, -2); /* remove _LOADED table */ |
| 258 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
| 259 | } |
| 260 | for (; l->name; l++) { |
| 261 | int i; |
| 262 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
| 263 | lua_pushvalue(L, -nup); |
| 264 | lua_pushcclosure(L, l->func, nup); |
| 265 | lua_setfield(L, -(nup+2), l->name); |
| 266 | } |
| 267 | lua_pop(L, nup); /* remove upvalues */ |
| 268 | } |
| 269 | |
| 270 | |
| 271 |
no test coverage detected