| 647 | |
| 648 | |
| 649 | static int ll_require (lua_State *L) { |
| 650 | const char *name = luaL_checkstring(L, 1); |
| 651 | lua_settop(L, 1); /* LOADED table will be at index 2 */ |
| 652 | lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); |
| 653 | lua_getfield(L, 2, name); /* LOADED[name] */ |
| 654 | if (lua_toboolean(L, -1)) /* is it there? */ |
| 655 | return 1; /* package is already loaded */ |
| 656 | /* else must load package */ |
| 657 | lua_pop(L, 1); /* remove 'getfield' result */ |
| 658 | findloader(L, name); |
| 659 | lua_rotate(L, -2, 1); /* function <-> loader data */ |
| 660 | lua_pushvalue(L, 1); /* name is 1st argument to module loader */ |
| 661 | lua_pushvalue(L, -3); /* loader data is 2nd argument */ |
| 662 | /* stack: ...; loader data; loader function; mod. name; loader data */ |
| 663 | lua_call(L, 2, 1); /* run loader to load module */ |
| 664 | /* stack: ...; loader data; result from loader */ |
| 665 | if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 666 | lua_setfield(L, 2, name); /* LOADED[name] = returned value */ |
| 667 | else |
| 668 | lua_pop(L, 1); /* pop nil */ |
| 669 | if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ |
| 670 | lua_pushboolean(L, 1); /* use true as result */ |
| 671 | lua_copy(L, -1, -2); /* replace loader result */ |
| 672 | lua_setfield(L, 2, name); /* LOADED[name] = true */ |
| 673 | } |
| 674 | lua_rotate(L, -2, 1); /* loader data <-> module result */ |
| 675 | return 2; /* return module result and loader data */ |
| 676 | } |
| 677 | |
| 678 | /* }====================================================== */ |
| 679 |
nothing calls this directly
no test coverage detected