| 265 | |
| 266 | |
| 267 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, |
| 268 | const luaL_Reg *l, int nup) { |
| 269 | if (libname) { |
| 270 | int size = libsize(l); |
| 271 | /* check whether lib already exists */ |
| 272 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); |
| 273 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
| 274 | if (!lua_istable(L, -1)) { /* not found? */ |
| 275 | lua_pop(L, 1); /* remove previous result */ |
| 276 | /* try global variable (and create one if it does not exist) */ |
| 277 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) |
| 278 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
| 279 | lua_pushvalue(L, -1); |
| 280 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
| 281 | } |
| 282 | lua_remove(L, -2); /* remove _LOADED table */ |
| 283 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
| 284 | } |
| 285 | for (; l->name; l++) { |
| 286 | int i; |
| 287 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
| 288 | lua_pushvalue(L, -nup); |
| 289 | lua_pushcclosure(L, l->func, nup); |
| 290 | lua_setfield(L, -(nup+2), l->name); |
| 291 | } |
| 292 | lua_pop(L, nup); /* remove upvalues */ |
| 293 | } |
| 294 | |
| 295 | |
| 296 |
no test coverage detected