Lines Matching refs:L
67 #define setprogdir(L) ((void)0) argument
92 static void *lsys_load (lua_State *L, const char *path, int seeglb);
99 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym);
133 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
136 lua_pushstring(L, dlerror()); in lsys_load()
141 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
144 lua_pushstring(L, dlerror()); in lsys_sym()
177 static void setprogdir (lua_State *L) { in setprogdir() argument
183 luaL_error(L, "unable to get ModuleFileName"); in setprogdir()
186 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); in setprogdir()
187 lua_remove(L, -2); /* remove original string */ in setprogdir()
194 static void pusherror (lua_State *L) { in pusherror() argument
199 lua_pushstring(L, buffer); in pusherror()
201 lua_pushfstring(L, "system error %d\n", error); in pusherror()
209 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
212 if (lib == NULL) pusherror(L); in lsys_load()
217 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
219 if (f == NULL) pusherror(L); in lsys_sym()
245 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
247 lua_pushliteral(L, DLMSG); in lsys_load()
252 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
254 lua_pushliteral(L, DLMSG); in lsys_sym()
285 static int noenv (lua_State *L) { in noenv() argument
287 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); in noenv()
288 b = lua_toboolean(L, -1); in noenv()
289 lua_pop(L, 1); /* remove value */ in noenv()
297 static void setpath (lua_State *L, const char *fieldname, in setpath() argument
301 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); in setpath()
305 if (path == NULL || noenv(L)) /* no environment variable? */ in setpath()
306 lua_pushstring(L, dft); /* use default */ in setpath()
308 lua_pushstring(L, path); /* nothing to change */ in setpath()
312 luaL_buffinit(L, &b); in setpath()
324 setprogdir(L); in setpath()
325 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ in setpath()
326 lua_pop(L, 1); /* pop versioned variable name ('nver') */ in setpath()
335 static void *checkclib (lua_State *L, const char *path) { in checkclib() argument
337 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in checkclib()
338 lua_getfield(L, -1, path); in checkclib()
339 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ in checkclib()
340 lua_pop(L, 2); /* pop CLIBS table and 'plib' */ in checkclib()
349 static void addtoclib (lua_State *L, const char *path, void *plib) { in addtoclib() argument
350 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in addtoclib()
351 lua_pushlightuserdata(L, plib); in addtoclib()
352 lua_pushvalue(L, -1); in addtoclib()
353 lua_setfield(L, -3, path); /* CLIBS[path] = plib */ in addtoclib()
354 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ in addtoclib()
355 lua_pop(L, 1); /* pop CLIBS table */ in addtoclib()
363 static int gctm (lua_State *L) { in gctm() argument
364 lua_Integer n = luaL_len(L, 1); in gctm()
366 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ in gctm()
367 lsys_unloadlib(lua_touserdata(L, -1)); in gctm()
368 lua_pop(L, 1); /* pop handle */ in gctm()
390 static int lookforfunc (lua_State *L, const char *path, const char *sym) { in lookforfunc() argument
391 void *reg = checkclib(L, path); /* check loaded C libraries */ in lookforfunc()
393 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ in lookforfunc()
395 addtoclib(L, path, reg); in lookforfunc()
398 lua_pushboolean(L, 1); /* return 'true' */ in lookforfunc()
402 lua_CFunction f = lsys_sym(L, reg, sym); in lookforfunc()
405 lua_pushcfunction(L, f); /* else create new function */ in lookforfunc()
411 static int ll_loadlib (lua_State *L) { in ll_loadlib() argument
412 const char *path = luaL_checkstring(L, 1); in ll_loadlib()
413 const char *init = luaL_checkstring(L, 2); in ll_loadlib()
414 int stat = lookforfunc(L, path, init); in ll_loadlib()
418 luaL_pushfail(L); in ll_loadlib()
419 lua_insert(L, -2); in ll_loadlib()
420 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); in ll_loadlib()
471 static void pusherrornotfound (lua_State *L, const char *path) { in pusherrornotfound() argument
473 luaL_buffinit(L, &b); in pusherrornotfound()
481 static const char *searchpath (lua_State *L, const char *name, in searchpath() argument
491 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ in searchpath()
492 luaL_buffinit(L, &buff); in searchpath()
500 return lua_pushstring(L, filename); /* save and return name */ in searchpath()
503 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ in searchpath()
508 static int ll_searchpath (lua_State *L) { in ll_searchpath() argument
509 const char *f = searchpath(L, luaL_checkstring(L, 1), in ll_searchpath()
510 luaL_checkstring(L, 2), in ll_searchpath()
511 luaL_optstring(L, 3, "."), in ll_searchpath()
512 luaL_optstring(L, 4, LUA_DIRSEP)); in ll_searchpath()
515 luaL_pushfail(L); in ll_searchpath()
516 lua_insert(L, -2); in ll_searchpath()
522 static const char *findfile (lua_State *L, const char *name, in findfile() argument
526 lua_getfield(L, lua_upvalueindex(1), pname); in findfile()
527 path = lua_tostring(L, -1); in findfile()
529 luaL_error(L, "'package.%s' must be a string", pname); in findfile()
530 return searchpath(L, name, path, ".", dirsep); in findfile()
534 static int checkload (lua_State *L, int stat, const char *filename) { in checkload() argument
536 lua_pushstring(L, filename); /* will be 2nd argument to module */ in checkload()
540 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", in checkload()
541 lua_tostring(L, 1), filename, lua_tostring(L, -1)); in checkload()
545 static int searcher_Lua (lua_State *L) { in searcher_Lua() argument
547 const char *name = luaL_checkstring(L, 1); in searcher_Lua()
548 filename = findfile(L, name, "path", LUA_LSUBSEP); in searcher_Lua()
550 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); in searcher_Lua()
562 static int loadfunc (lua_State *L, const char *filename, const char *modname) { in loadfunc() argument
565 modname = luaL_gsub(L, modname, ".", LUA_OFSEP); in loadfunc()
569 openfunc = lua_pushlstring(L, modname, mark - modname); in loadfunc()
570 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); in loadfunc()
571 stat = lookforfunc(L, filename, openfunc); in loadfunc()
575 openfunc = lua_pushfstring(L, LUA_POF"%s", modname); in loadfunc()
576 return lookforfunc(L, filename, openfunc); in loadfunc()
580 static int searcher_C (lua_State *L) { in searcher_C() argument
581 const char *name = luaL_checkstring(L, 1); in searcher_C()
582 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); in searcher_C()
584 return checkload(L, (loadfunc(L, filename, name) == 0), filename); in searcher_C()
588 static int searcher_Croot (lua_State *L) { in searcher_Croot() argument
590 const char *name = luaL_checkstring(L, 1); in searcher_Croot()
594 lua_pushlstring(L, name, p - name); in searcher_Croot()
595 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); in searcher_Croot()
597 if ((stat = loadfunc(L, filename, name)) != 0) { in searcher_Croot()
599 return checkload(L, 0, filename); /* real error */ in searcher_Croot()
601 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); in searcher_Croot()
605 lua_pushstring(L, filename); /* will be 2nd argument to module */ in searcher_Croot()
610 static int searcher_preload (lua_State *L) { in searcher_preload() argument
611 const char *name = luaL_checkstring(L, 1); in searcher_preload()
612 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in searcher_preload()
613 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ in searcher_preload()
614 lua_pushfstring(L, "no field package.preload['%s']", name); in searcher_preload()
618 lua_pushliteral(L, ":preload:"); in searcher_preload()
624 static void findloader (lua_State *L, const char *name) { in findloader() argument
628 if (l_unlikely(lua_getfield(L, lua_upvalueindex(1), "searchers") in findloader()
630 luaL_error(L, "'package.searchers' must be a table"); in findloader()
631 luaL_buffinit(L, &msg); in findloader()
635 if (l_unlikely(lua_rawgeti(L, 3, i) == LUA_TNIL)) { /* no more searchers? */ in findloader()
636 lua_pop(L, 1); /* remove nil */ in findloader()
639 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); in findloader()
641 lua_pushstring(L, name); in findloader()
642 lua_call(L, 1, 2); /* call it */ in findloader()
643 if (lua_isfunction(L, -2)) /* did it find a loader? */ in findloader()
645 else if (lua_isstring(L, -2)) { /* searcher returned error message? */ in findloader()
646 lua_pop(L, 1); /* remove extra return */ in findloader()
650 lua_pop(L, 2); /* remove both returns */ in findloader()
657 static int ll_require (lua_State *L) { in ll_require() argument
658 const char *name = luaL_checkstring(L, 1); in ll_require()
659 lua_settop(L, 1); /* LOADED table will be at index 2 */ in ll_require()
660 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in ll_require()
661 lua_getfield(L, 2, name); /* LOADED[name] */ in ll_require()
662 if (lua_toboolean(L, -1)) /* is it there? */ in ll_require()
665 lua_pop(L, 1); /* remove 'getfield' result */ in ll_require()
666 findloader(L, name); in ll_require()
667 lua_rotate(L, -2, 1); /* function <-> loader data */ in ll_require()
668 lua_pushvalue(L, 1); /* name is 1st argument to module loader */ in ll_require()
669 lua_pushvalue(L, -3); /* loader data is 2nd argument */ in ll_require()
671 lua_call(L, 2, 1); /* run loader to load module */ in ll_require()
673 if (!lua_isnil(L, -1)) /* non-nil return? */ in ll_require()
674 lua_setfield(L, 2, name); /* LOADED[name] = returned value */ in ll_require()
676 lua_pop(L, 1); /* pop nil */ in ll_require()
677 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ in ll_require()
678 lua_pushboolean(L, 1); /* use true as result */ in ll_require()
679 lua_copy(L, -1, -2); /* replace loader result */ in ll_require()
680 lua_setfield(L, 2, name); /* LOADED[name] = true */ in ll_require()
682 lua_rotate(L, -2, 1); /* loader data <-> module result */ in ll_require()
710 static void createsearcherstable (lua_State *L) { in createsearcherstable() argument
720 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); in createsearcherstable()
723 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ in createsearcherstable()
724 lua_pushcclosure(L, searchers[i], 1); in createsearcherstable()
725 lua_rawseti(L, -2, i+1); in createsearcherstable()
727 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ in createsearcherstable()
735 static void createclibstable (lua_State *L) { in createclibstable() argument
736 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ in createclibstable()
737 lua_createtable(L, 0, 1); /* create metatable for CLIBS */ in createclibstable()
738 lua_pushcfunction(L, gctm); in createclibstable()
739 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ in createclibstable()
740 lua_setmetatable(L, -2); in createclibstable()
744 LUAMOD_API int luaopen_package (lua_State *L) { in luaopen_package() argument
745 createclibstable(L); in luaopen_package()
746 luaL_newlib(L, pk_funcs); /* create 'package' table */ in luaopen_package()
747 createsearcherstable(L); in luaopen_package()
749 setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); in luaopen_package()
750 setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); in luaopen_package()
752 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" in luaopen_package()
754 lua_setfield(L, -2, "config"); in luaopen_package()
756 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in luaopen_package()
757 lua_setfield(L, -2, "loaded"); in luaopen_package()
759 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in luaopen_package()
760 lua_setfield(L, -2, "preload"); in luaopen_package()
761 lua_pushglobaltable(L); in luaopen_package()
762 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ in luaopen_package()
763 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ in luaopen_package()
764 lua_pop(L, 1); /* pop global table */ in luaopen_package()