Lines Matching refs:L

69 #define setprogdir(L)           ((void)0)  argument
94 static void *lsys_load (lua_State *L, const char *path, int seeglb);
101 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
135 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
138 lua_pushstring(L, dlerror()); in lsys_load()
143 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
146 lua_pushstring(L, dlerror()); in lsys_sym()
179 static void setprogdir (lua_State *L) { in setprogdir() argument
185 luaL_error(L, "unable to get ModuleFileName"); in setprogdir()
188 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); in setprogdir()
189 lua_remove(L, -2); /* remove original string */ in setprogdir()
196 static void pusherror (lua_State *L) { in pusherror() argument
201 lua_pushstring(L, buffer); in pusherror()
203 lua_pushfstring(L, "system error %d\n", error); in pusherror()
211 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
214 if (lib == NULL) pusherror(L); in lsys_load()
219 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
221 if (f == NULL) pusherror(L); in lsys_sym()
247 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
249 lua_pushliteral(L, DLMSG); in lsys_load()
254 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
256 lua_pushliteral(L, DLMSG); in lsys_sym()
287 static int noenv (lua_State *L) { in noenv() argument
289 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); in noenv()
290 b = lua_toboolean(L, -1); in noenv()
291 lua_pop(L, 1); /* remove value */ in noenv()
299 static void setpath (lua_State *L, const char *fieldname, in setpath() argument
303 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); in setpath()
307 if (path == NULL || noenv(L)) /* no environment variable? */ in setpath()
308 lua_pushstring(L, dft); /* use default */ in setpath()
310 lua_pushstring(L, path); /* nothing to change */ in setpath()
314 luaL_buffinit(L, &b); in setpath()
326 setprogdir(L); in setpath()
327 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ in setpath()
328 lua_pop(L, 1); /* pop versioned variable name ('nver') */ in setpath()
337 static void *checkclib (lua_State *L, const char *path) { in checkclib() argument
339 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in checkclib()
340 lua_getfield(L, -1, path); in checkclib()
341 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ in checkclib()
342 lua_pop(L, 2); /* pop CLIBS table and 'plib' */ in checkclib()
351 static void addtoclib (lua_State *L, const char *path, void *plib) { in addtoclib() argument
352 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in addtoclib()
353 lua_pushlightuserdata(L, plib); in addtoclib()
354 lua_pushvalue(L, -1); in addtoclib()
355 lua_setfield(L, -3, path); /* CLIBS[path] = plib */ in addtoclib()
356 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ in addtoclib()
357 lua_pop(L, 1); /* pop CLIBS table */ in addtoclib()
365 static int gctm (lua_State *L) { in gctm() argument
366 lua_Integer n = luaL_len(L, 1); in gctm()
368 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ in gctm()
369 lsys_unloadlib(lua_touserdata(L, -1)); in gctm()
370 lua_pop(L, 1); /* pop handle */ in gctm()
392 static int lookforfunc (lua_State *L, const char *path, const char *sym) { in lookforfunc() argument
393 void *reg = checkclib(L, path); /* check loaded C libraries */ in lookforfunc()
395 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ in lookforfunc()
397 addtoclib(L, path, reg); in lookforfunc()
400 lua_pushboolean(L, 1); /* return 'true' */ in lookforfunc()
404 lua_CFunction f = lsys_sym(L, reg, sym); in lookforfunc()
407 lua_pushcfunction(L, f); /* else create new function */ in lookforfunc()
413 static int ll_loadlib (lua_State *L) { in ll_loadlib() argument
414 const char *path = luaL_checkstring(L, 1); in ll_loadlib()
415 const char *init = luaL_checkstring(L, 2); in ll_loadlib()
416 int stat = lookforfunc(L, path, init); in ll_loadlib()
420 luaL_pushfail(L); in ll_loadlib()
421 lua_insert(L, -2); in ll_loadlib()
422 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); in ll_loadlib()
473 static void pusherrornotfound (lua_State *L, const char *path) { in pusherrornotfound() argument
475 luaL_buffinit(L, &b); in pusherrornotfound()
483 static const char *searchpath (lua_State *L, const char *name, in searchpath() argument
493 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ in searchpath()
494 luaL_buffinit(L, &buff); in searchpath()
502 return lua_pushstring(L, filename); /* save and return name */ in searchpath()
505 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ in searchpath()
510 static int ll_searchpath (lua_State *L) { in ll_searchpath() argument
511 const char *f = searchpath(L, luaL_checkstring(L, 1), in ll_searchpath()
512 luaL_checkstring(L, 2), in ll_searchpath()
513 luaL_optstring(L, 3, "."), in ll_searchpath()
514 luaL_optstring(L, 4, LUA_DIRSEP)); in ll_searchpath()
517 luaL_pushfail(L); in ll_searchpath()
518 lua_insert(L, -2); in ll_searchpath()
524 static const char *findfile (lua_State *L, const char *name, in findfile() argument
528 lua_getfield(L, lua_upvalueindex(1), pname); in findfile()
529 path = lua_tostring(L, -1); in findfile()
531 luaL_error(L, "'package.%s' must be a string", pname); in findfile()
532 return searchpath(L, name, path, ".", dirsep); in findfile()
536 static int checkload (lua_State *L, int stat, const char *filename) { in checkload() argument
538 lua_pushstring(L, filename); /* will be 2nd argument to module */ in checkload()
542 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", in checkload()
543 lua_tostring(L, 1), filename, lua_tostring(L, -1)); in checkload()
547 static int searcher_Lua (lua_State *L) { in searcher_Lua() argument
549 const char *name = luaL_checkstring(L, 1); in searcher_Lua()
550 filename = findfile(L, name, "path", LUA_LSUBSEP); in searcher_Lua()
552 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); in searcher_Lua()
564 static int loadfunc (lua_State *L, const char *filename, const char *modname) { in loadfunc() argument
567 modname = luaL_gsub(L, modname, ".", LUA_OFSEP); in loadfunc()
571 openfunc = lua_pushlstring(L, modname, mark - modname); in loadfunc()
572 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); in loadfunc()
573 stat = lookforfunc(L, filename, openfunc); in loadfunc()
577 openfunc = lua_pushfstring(L, LUA_POF"%s", modname); in loadfunc()
578 return lookforfunc(L, filename, openfunc); in loadfunc()
582 static int searcher_C (lua_State *L) { in searcher_C() argument
583 const char *name = luaL_checkstring(L, 1); in searcher_C()
584 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); in searcher_C()
586 return checkload(L, (loadfunc(L, filename, name) == 0), filename); in searcher_C()
590 static int searcher_Croot (lua_State *L) { in searcher_Croot() argument
592 const char *name = luaL_checkstring(L, 1); in searcher_Croot()
596 lua_pushlstring(L, name, p - name); in searcher_Croot()
597 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); in searcher_Croot()
599 if ((stat = loadfunc(L, filename, name)) != 0) { in searcher_Croot()
601 return checkload(L, 0, filename); /* real error */ in searcher_Croot()
603 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); in searcher_Croot()
607 lua_pushstring(L, filename); /* will be 2nd argument to module */ in searcher_Croot()
612 static int searcher_preload (lua_State *L) { in searcher_preload() argument
613 const char *name = luaL_checkstring(L, 1); in searcher_preload()
614 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in searcher_preload()
615 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ in searcher_preload()
616 lua_pushfstring(L, "no field package.preload['%s']", name); in searcher_preload()
620 lua_pushliteral(L, ":preload:"); in searcher_preload()
626 static void findloader (lua_State *L, const char *name) { in findloader() argument
630 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") in findloader()
632 luaL_error(L, "'package.searchers' must be a table"); in findloader()
633 luaL_buffinit(L, &msg); in findloader()
637 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ in findloader()
638 lua_pop(L, 1); /* remove nil */ in findloader()
641 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); in findloader()
643 lua_pushstring(L, name); in findloader()
644 lua_call(L, 1, 2); /* call it */ in findloader()
645 if (lua_isfunction(L, -2)) /* did it find a loader? */ in findloader()
647 else if (lua_isstring(L, -2)) { /* searcher returned error message? */ in findloader()
648 lua_pop(L, 1); /* remove extra return */ in findloader()
652 lua_pop(L, 2); /* remove both returns */ in findloader()
659 static int ll_require (lua_State *L) { in ll_require() argument
660 const char *name = luaL_checkstring(L, 1); in ll_require()
661 lua_settop(L, 1); /* LOADED table will be at index 2 */ in ll_require()
662 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in ll_require()
663 lua_getfield(L, 2, name); /* LOADED[name] */ in ll_require()
664 if (lua_toboolean(L, -1)) /* is it there? */ in ll_require()
667 lua_pop(L, 1); /* remove 'getfield' result */ in ll_require()
668 findloader(L, name); in ll_require()
669 lua_rotate(L, -2, 1); /* function <-> loader data */ in ll_require()
670 lua_pushvalue(L, 1); /* name is 1st argument to module loader */ in ll_require()
671 lua_pushvalue(L, -3); /* loader data is 2nd argument */ in ll_require()
673 lua_call(L, 2, 1); /* run loader to load module */ in ll_require()
675 if (!lua_isnil(L, -1)) /* non-nil return? */ in ll_require()
676 lua_setfield(L, 2, name); /* LOADED[name] = returned value */ in ll_require()
678 lua_pop(L, 1); /* pop nil */ in ll_require()
679 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ in ll_require()
680 lua_pushboolean(L, 1); /* use true as result */ in ll_require()
681 lua_copy(L, -1, -2); /* replace loader result */ in ll_require()
682 lua_setfield(L, 2, name); /* LOADED[name] = true */ in ll_require()
684 lua_rotate(L, -2, 1); /* loader data <-> module result */ in ll_require()
712 static void createsearcherstable (lua_State *L) { in createsearcherstable() argument
722 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); in createsearcherstable()
725 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ in createsearcherstable()
726 lua_pushcclosure(L, searchers[i], 1); in createsearcherstable()
727 lua_rawseti(L, -2, i+1); in createsearcherstable()
729 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ in createsearcherstable()
737 static void createclibstable (lua_State *L) { in createclibstable() argument
738 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ in createclibstable()
739 lua_createtable(L, 0, 1); /* create metatable for CLIBS */ in createclibstable()
740 lua_pushcfunction(L, gctm); in createclibstable()
741 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ in createclibstable()
742 lua_setmetatable(L, -2); in createclibstable()
746 LUAMOD_API int luaopen_package (lua_State *L) { in luaopen_package() argument
747 createclibstable(L); in luaopen_package()
748 luaL_newlib(L, pk_funcs); /* create 'package' table */ in luaopen_package()
749 createsearcherstable(L); in luaopen_package()
751 setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); in luaopen_package()
752 setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); in luaopen_package()
754 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" in luaopen_package()
756 lua_setfield(L, -2, "config"); in luaopen_package()
758 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in luaopen_package()
759 lua_setfield(L, -2, "loaded"); in luaopen_package()
761 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in luaopen_package()
762 lua_setfield(L, -2, "preload"); in luaopen_package()
763 lua_pushglobaltable(L); in luaopen_package()
764 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ in luaopen_package()
765 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ in luaopen_package()
766 lua_pop(L, 1); /* pop global table */ in luaopen_package()