| 555 | |
| 556 | |
| 557 | static int ll_require (lua_State *L) { |
| 558 | const char *name = luaL_checkstring(L, 1); |
| 559 | lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 560 | lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 561 | lua_getfield(L, 2, name); /* _LOADED[name] */ |
| 562 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 563 | return 1; /* package is already loaded */ |
| 564 | /* else must load package */ |
| 565 | lua_pop(L, 1); /* remove 'getfield' result */ |
| 566 | findloader(L, name); |
| 567 | lua_pushstring(L, name); /* pass name as argument to module loader */ |
| 568 | lua_insert(L, -2); /* name is 1st argument (before search data) */ |
| 569 | lua_call(L, 2, 1); /* run loader to load module */ |
| 570 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 571 | lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 572 | if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ |
| 573 | lua_pushboolean(L, 1); /* use true as result */ |
| 574 | lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 575 | lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 576 | } |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | /* }====================================================== */ |
| 581 |
nothing calls this directly
no test coverage detected