| 288 | |
| 289 | |
| 290 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, |
| 291 | const luaL_Reg *l, int nup) { |
| 292 | if (libname) { |
| 293 | int size = libsize(l); |
| 294 | /* check whether lib already exists */ |
| 295 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); |
| 296 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
| 297 | if (!lua_istable(L, -1)) { /* not found? */ |
| 298 | lua_pop(L, 1); /* remove previous result */ |
| 299 | /* try global variable (and create one if it does not exist) */ |
| 300 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) |
| 301 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
| 302 | lua_pushvalue(L, -1); |
| 303 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
| 304 | } |
| 305 | lua_remove(L, -2); /* remove _LOADED table */ |
| 306 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
| 307 | } |
| 308 | for (; l->name; l++) { |
| 309 | int i; |
| 310 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
| 311 | lua_pushvalue(L, -nup); |
| 312 | lua_pushcclosure(L, l->func, nup); |
| 313 | lua_setfield(L, -(nup+2), l->name); |
| 314 | } |
| 315 | lua_pop(L, nup); /* remove upvalues */ |
| 316 | } |
| 317 | |
| 318 | |
| 319 |
no test coverage detected