MCPcopy Create free account
hub / github.com/bloomberg/comdb2 / ll_require

Function ll_require

lua/loadlib.c:451–499  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

449
450
451static int ll_require (lua_State *L) {
452 extern int gbl_allow_lua_dynamic_libs;
453 if (!gbl_allow_lua_dynamic_libs) {
454 luaL_error(L, "dynamic libraries not enabled; check comdb2 documentation");
455 return 1;
456 }
457 const char *name = luaL_checkstring(L, 1);
458 int i;
459 lua_settop(L, 1); /* _LOADED table will be at index 2 */
460 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
461 lua_getfield(L, 2, name);
462 if (lua_toboolean(L, -1)) { /* is it there? */
463 if (lua_touserdata(L, -1) == sentinel) /* check loops */
464 luaL_error(L, "loop or previous error loading module " LUA_QS, name);
465 return 1; /* package is already loaded */
466 }
467 /* else must load it; iterate over available loaders */
468 lua_getfield(L, LUA_ENVIRONINDEX, "loaders");
469 if (!lua_istable(L, -1))
470 luaL_error(L, LUA_QL("package.loaders") " must be a table");
471 lua_pushliteral(L, ""); /* error message accumulator */
472 for (i=1; ; i++) {
473 lua_rawgeti(L, -2, i); /* get a loader */
474 if (lua_isnil(L, -1))
475 luaL_error(L, "module " LUA_QS " not found:%s",
476 name, lua_tostring(L, -2));
477 lua_pushstring(L, name);
478 lua_call(L, 1, 1); /* call it */
479 if (lua_isfunction(L, -1)) /* did it find module? */
480 break; /* module loaded successfully */
481 else if (lua_isstring(L, -1)) /* loader returned error message? */
482 lua_concat(L, 2); /* accumulate it */
483 else
484 lua_pop(L, 1);
485 }
486 lua_pushlightuserdata(L, sentinel);
487 lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */
488 lua_pushstring(L, name); /* pass name as argument to module */
489 lua_call(L, 1, 1); /* run loaded module */
490 if (!lua_isnil(L, -1)) /* non-nil return? */
491 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
492 lua_getfield(L, 2, name);
493 if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */
494 lua_pushboolean(L, 1); /* use true as result */
495 lua_pushvalue(L, -1); /* extra copy to be returned */
496 lua_setfield(L, 2, name); /* _LOADED[name] = true */
497 }
498 return 1;
499}
500
501/* }====================================================== */
502

Callers

nothing calls this directly

Calls 14

luaL_errorFunction · 0.85
lua_settopFunction · 0.85
lua_getfieldFunction · 0.85
lua_tobooleanFunction · 0.85
lua_touserdataFunction · 0.85
lua_rawgetiFunction · 0.85
lua_pushstringFunction · 0.85
lua_callFunction · 0.85
lua_isstringFunction · 0.85
lua_concatFunction · 0.85
lua_pushlightuserdataFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected