| 506 | |
| 507 | |
| 508 | static int ll_require (lua_State *L) { |
| 509 | const char *name = luaL_checkstring(L, 1); |
| 510 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 511 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 512 | lua_getfield(L, 2, name); /* _LOADED[name] */ |
| 513 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 514 | return 1; /* package is already loaded */ |
| 515 | /* else must load package */ |
| 516 | lua_pop(L, 1); /* remove 'getfield' result */ |
| 517 | findloader(L, name); |
| 518 | lua_pushstring(L, name); /* pass name as argument to module loader */ |
| 519 | lua_insert(L, -2); /* name is 1st argument (before search data) */ |
| 520 | lua_call(L, 2, 1); /* run loader to load module */ |
| 521 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 522 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 523 | lua_getfield(L, 2, name); |
| 524 | if (lua_isnil(L, -1)) { /* module did not set a value? */ |
| 525 | lua_pushboolean(L, 1); /* use true as result */ |
| 526 | lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 527 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 528 | } |
| 529 | return 1; |
| 530 | } |
| 531 | |
| 532 | /* }====================================================== */ |
| 533 |
nothing calls this directly
no test coverage detected