| 10452 | |
| 10453 | |
| 10454 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, |
| 10455 | const luaL_Reg *l, int nup) { |
| 10456 | if (libname) { |
| 10457 | int size = libsize(l); |
| 10458 | /* check whether lib already exists */ |
| 10459 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); |
| 10460 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
| 10461 | if (!lua_istable(L, -1)) { /* not found? */ |
| 10462 | lua_pop(L, 1); /* remove previous result */ |
| 10463 | /* try global variable (and create one if it does not exist) */ |
| 10464 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) |
| 10465 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
| 10466 | lua_pushvalue(L, -1); |
| 10467 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
| 10468 | } |
| 10469 | lua_remove(L, -2); /* remove _LOADED table */ |
| 10470 | lua_insert(L, -(nup+1)); /* move library table to below upvalues */ |
| 10471 | } |
| 10472 | for (; l->name; l++) { |
| 10473 | int i; |
| 10474 | for (i=0; i<nup; i++) /* copy upvalues to the top */ |
| 10475 | lua_pushvalue(L, -nup); |
| 10476 | lua_pushcclosure(L, l->func, nup); |
| 10477 | lua_setfield(L, -(nup+2), l->name); |
| 10478 | } |
| 10479 | lua_pop(L, nup); /* remove upvalues */ |
| 10480 | } |
| 10481 | |
| 10482 | |
| 10483 |
no test coverage detected