Lines Matching refs:L
58 #define setprogdir(L) ((void)0) argument
83 static void *lsys_load (lua_State *L, const char *path, int seeglb);
90 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
124 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
127 lua_pushstring(L, dlerror()); in lsys_load()
132 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
135 lua_pushstring(L, dlerror()); in lsys_sym()
168 static void setprogdir (lua_State *L) { in setprogdir() argument
174 luaL_error(L, "unable to get ModuleFileName"); in setprogdir()
177 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); in setprogdir()
178 lua_remove(L, -2); /* remove original string */ in setprogdir()
185 static void pusherror (lua_State *L) { in pusherror() argument
190 lua_pushstring(L, buffer); in pusherror()
192 lua_pushfstring(L, "system error %d\n", error); in pusherror()
200 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
203 if (lib == NULL) pusherror(L); in lsys_load()
208 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
210 if (f == NULL) pusherror(L); in lsys_sym()
236 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
238 lua_pushliteral(L, DLMSG); in lsys_load()
243 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
245 lua_pushliteral(L, DLMSG); in lsys_sym()
276 static int noenv (lua_State *L) { in noenv() argument
278 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); in noenv()
279 b = lua_toboolean(L, -1); in noenv()
280 lua_pop(L, 1); /* remove value */ in noenv()
288 static void setpath (lua_State *L, const char *fieldname, in setpath() argument
292 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); in setpath()
296 if (path == NULL || noenv(L)) /* no environment variable? */ in setpath()
297 lua_pushstring(L, dft); /* use default */ in setpath()
299 lua_pushstring(L, path); /* nothing to change */ in setpath()
303 luaL_buffinit(L, &b); in setpath()
315 setprogdir(L); in setpath()
316 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ in setpath()
317 lua_pop(L, 1); /* pop versioned variable name ('nver') */ in setpath()
326 static void *checkclib (lua_State *L, const char *path) { in checkclib() argument
328 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in checkclib()
329 lua_getfield(L, -1, path); in checkclib()
330 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ in checkclib()
331 lua_pop(L, 2); /* pop CLIBS table and 'plib' */ in checkclib()
340 static void addtoclib (lua_State *L, const char *path, void *plib) { in addtoclib() argument
341 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in addtoclib()
342 lua_pushlightuserdata(L, plib); in addtoclib()
343 lua_pushvalue(L, -1); in addtoclib()
344 lua_setfield(L, -3, path); /* CLIBS[path] = plib */ in addtoclib()
345 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ in addtoclib()
346 lua_pop(L, 1); /* pop CLIBS table */ in addtoclib()
354 static int gctm (lua_State *L) { in gctm() argument
355 lua_Integer n = luaL_len(L, 1); in gctm()
357 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ in gctm()
358 lsys_unloadlib(lua_touserdata(L, -1)); in gctm()
359 lua_pop(L, 1); /* pop handle */ in gctm()
381 static int lookforfunc (lua_State *L, const char *path, const char *sym) { in lookforfunc() argument
382 void *reg = checkclib(L, path); /* check loaded C libraries */ in lookforfunc()
384 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ in lookforfunc()
386 addtoclib(L, path, reg); in lookforfunc()
389 lua_pushboolean(L, 1); /* return 'true' */ in lookforfunc()
393 lua_CFunction f = lsys_sym(L, reg, sym); in lookforfunc()
396 lua_pushcfunction(L, f); /* else create new function */ in lookforfunc()
402 static int ll_loadlib (lua_State *L) { in ll_loadlib() argument
403 const char *path = luaL_checkstring(L, 1); in ll_loadlib()
404 const char *init = luaL_checkstring(L, 2); in ll_loadlib()
405 int stat = lookforfunc(L, path, init); in ll_loadlib()
409 luaL_pushfail(L); in ll_loadlib()
410 lua_insert(L, -2); in ll_loadlib()
411 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); in ll_loadlib()
462 static void pusherrornotfound (lua_State *L, const char *path) { in pusherrornotfound() argument
464 luaL_buffinit(L, &b); in pusherrornotfound()
472 static const char *searchpath (lua_State *L, const char *name, in searchpath() argument
482 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ in searchpath()
483 luaL_buffinit(L, &buff); in searchpath()
491 return lua_pushstring(L, filename); /* save and return name */ in searchpath()
494 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ in searchpath()
499 static int ll_searchpath (lua_State *L) { in ll_searchpath() argument
500 const char *f = searchpath(L, luaL_checkstring(L, 1), in ll_searchpath()
501 luaL_checkstring(L, 2), in ll_searchpath()
502 luaL_optstring(L, 3, "."), in ll_searchpath()
503 luaL_optstring(L, 4, LUA_DIRSEP)); in ll_searchpath()
506 luaL_pushfail(L); in ll_searchpath()
507 lua_insert(L, -2); in ll_searchpath()
513 static const char *findfile (lua_State *L, const char *name, in findfile() argument
517 lua_getfield(L, lua_upvalueindex(1), pname); in findfile()
518 path = lua_tostring(L, -1); in findfile()
520 luaL_error(L, "'package.%s' must be a string", pname); in findfile()
521 return searchpath(L, name, path, ".", dirsep); in findfile()
525 static int checkload (lua_State *L, int stat, const char *filename) { in checkload() argument
527 lua_pushstring(L, filename); /* will be 2nd argument to module */ in checkload()
531 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", in checkload()
532 lua_tostring(L, 1), filename, lua_tostring(L, -1)); in checkload()
536 static int searcher_Lua (lua_State *L) { in searcher_Lua() argument
538 const char *name = luaL_checkstring(L, 1); in searcher_Lua()
539 filename = findfile(L, name, "path", LUA_LSUBSEP); in searcher_Lua()
541 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); in searcher_Lua()
553 static int loadfunc (lua_State *L, const char *filename, const char *modname) { in loadfunc() argument
556 modname = luaL_gsub(L, modname, ".", LUA_OFSEP); in loadfunc()
560 openfunc = lua_pushlstring(L, modname, mark - modname); in loadfunc()
561 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); in loadfunc()
562 stat = lookforfunc(L, filename, openfunc); in loadfunc()
566 openfunc = lua_pushfstring(L, LUA_POF"%s", modname); in loadfunc()
567 return lookforfunc(L, filename, openfunc); in loadfunc()
571 static int searcher_C (lua_State *L) { in searcher_C() argument
572 const char *name = luaL_checkstring(L, 1); in searcher_C()
573 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); in searcher_C()
575 return checkload(L, (loadfunc(L, filename, name) == 0), filename); in searcher_C()
579 static int searcher_Croot (lua_State *L) { in searcher_Croot() argument
581 const char *name = luaL_checkstring(L, 1); in searcher_Croot()
585 lua_pushlstring(L, name, p - name); in searcher_Croot()
586 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); in searcher_Croot()
588 if ((stat = loadfunc(L, filename, name)) != 0) { in searcher_Croot()
590 return checkload(L, 0, filename); /* real error */ in searcher_Croot()
592 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); in searcher_Croot()
596 lua_pushstring(L, filename); /* will be 2nd argument to module */ in searcher_Croot()
601 static int searcher_preload (lua_State *L) { in searcher_preload() argument
602 const char *name = luaL_checkstring(L, 1); in searcher_preload()
603 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in searcher_preload()
604 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ in searcher_preload()
605 lua_pushfstring(L, "no field package.preload['%s']", name); in searcher_preload()
609 lua_pushliteral(L, ":preload:"); in searcher_preload()
615 static void findloader (lua_State *L, const char *name) { in findloader() argument
619 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") in findloader()
621 luaL_error(L, "'package.searchers' must be a table"); in findloader()
622 luaL_buffinit(L, &msg); in findloader()
626 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ in findloader()
627 lua_pop(L, 1); /* remove nil */ in findloader()
630 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); in findloader()
632 lua_pushstring(L, name); in findloader()
633 lua_call(L, 1, 2); /* call it */ in findloader()
634 if (lua_isfunction(L, -2)) /* did it find a loader? */ in findloader()
636 else if (lua_isstring(L, -2)) { /* searcher returned error message? */ in findloader()
637 lua_pop(L, 1); /* remove extra return */ in findloader()
641 lua_pop(L, 2); /* remove both returns */ in findloader()
648 static int ll_require (lua_State *L) { in ll_require() argument
649 const char *name = luaL_checkstring(L, 1); in ll_require()
650 lua_settop(L, 1); /* LOADED table will be at index 2 */ in ll_require()
651 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in ll_require()
652 lua_getfield(L, 2, name); /* LOADED[name] */ in ll_require()
653 if (lua_toboolean(L, -1)) /* is it there? */ in ll_require()
656 lua_pop(L, 1); /* remove 'getfield' result */ in ll_require()
657 findloader(L, name); in ll_require()
658 lua_rotate(L, -2, 1); /* function <-> loader data */ in ll_require()
659 lua_pushvalue(L, 1); /* name is 1st argument to module loader */ in ll_require()
660 lua_pushvalue(L, -3); /* loader data is 2nd argument */ in ll_require()
662 lua_call(L, 2, 1); /* run loader to load module */ in ll_require()
664 if (!lua_isnil(L, -1)) /* non-nil return? */ in ll_require()
665 lua_setfield(L, 2, name); /* LOADED[name] = returned value */ in ll_require()
667 lua_pop(L, 1); /* pop nil */ in ll_require()
668 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ in ll_require()
669 lua_pushboolean(L, 1); /* use true as result */ in ll_require()
670 lua_copy(L, -1, -2); /* replace loader result */ in ll_require()
671 lua_setfield(L, 2, name); /* LOADED[name] = true */ in ll_require()
673 lua_rotate(L, -2, 1); /* loader data <-> module result */ in ll_require()
701 static void createsearcherstable (lua_State *L) { in createsearcherstable() argument
711 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); in createsearcherstable()
714 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ in createsearcherstable()
715 lua_pushcclosure(L, searchers[i], 1); in createsearcherstable()
716 lua_rawseti(L, -2, i+1); in createsearcherstable()
718 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ in createsearcherstable()
726 static void createclibstable (lua_State *L) { in createclibstable() argument
727 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ in createclibstable()
728 lua_createtable(L, 0, 1); /* create metatable for CLIBS */ in createclibstable()
729 lua_pushcfunction(L, gctm); in createclibstable()
730 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ in createclibstable()
731 lua_setmetatable(L, -2); in createclibstable()
735 LUAMOD_API int luaopen_package (lua_State *L) { in luaopen_package() argument
736 createclibstable(L); in luaopen_package()
737 luaL_newlib(L, pk_funcs); /* create 'package' table */ in luaopen_package()
738 createsearcherstable(L); in luaopen_package()
740 setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); in luaopen_package()
741 setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); in luaopen_package()
743 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" in luaopen_package()
745 lua_setfield(L, -2, "config"); in luaopen_package()
747 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in luaopen_package()
748 lua_setfield(L, -2, "loaded"); in luaopen_package()
750 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in luaopen_package()
751 lua_setfield(L, -2, "preload"); in luaopen_package()
752 lua_pushglobaltable(L); in luaopen_package()
753 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ in luaopen_package()
754 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ in luaopen_package()
755 lua_pop(L, 1); /* pop global table */ in luaopen_package()