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
135 if (lib == NULL) lua_pushstring(L, dlerror()); in lsys_load()
140 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
142 if (f == NULL) lua_pushstring(L, dlerror()); in lsys_sym()
175 static void setprogdir (lua_State *L) { in setprogdir() argument
181 luaL_error(L, "unable to get ModuleFileName"); in setprogdir()
184 luaL_gsub(L, lua_tostring(L, -1), LUA_EXEC_DIR, buff); in setprogdir()
185 lua_remove(L, -2); /* remove original string */ in setprogdir()
192 static void pusherror (lua_State *L) { in pusherror() argument
197 lua_pushstring(L, buffer); in pusherror()
199 lua_pushfstring(L, "system error %d\n", error); in pusherror()
207 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
210 if (lib == NULL) pusherror(L); in lsys_load()
215 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
217 if (f == NULL) pusherror(L); in lsys_sym()
243 static void *lsys_load (lua_State *L, const char *path, int seeglb) { in lsys_load() argument
245 lua_pushliteral(L, DLMSG); in lsys_load()
250 static lua_CFunction lsys_sym (lua_State *L, void *lib, const char *sym) { in lsys_sym() argument
252 lua_pushliteral(L, DLMSG); in lsys_sym()
283 static int noenv (lua_State *L) { in noenv() argument
285 lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); in noenv()
286 b = lua_toboolean(L, -1); in noenv()
287 lua_pop(L, 1); /* remove value */ in noenv()
295 static void setpath (lua_State *L, const char *fieldname, in setpath() argument
299 const char *nver = lua_pushfstring(L, "%s%s", envname, LUA_VERSUFFIX); in setpath()
303 if (path == NULL || noenv(L)) /* no environment variable? */ in setpath()
304 lua_pushstring(L, dft); /* use default */ in setpath()
306 lua_pushstring(L, path); /* nothing to change */ in setpath()
310 luaL_buffinit(L, &b); in setpath()
322 setprogdir(L); in setpath()
323 lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ in setpath()
324 lua_pop(L, 1); /* pop versioned variable name ('nver') */ in setpath()
333 static void *checkclib (lua_State *L, const char *path) { in checkclib() argument
335 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in checkclib()
336 lua_getfield(L, -1, path); in checkclib()
337 plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */ in checkclib()
338 lua_pop(L, 2); /* pop CLIBS table and 'plib' */ in checkclib()
347 static void addtoclib (lua_State *L, const char *path, void *plib) { in addtoclib() argument
348 lua_getfield(L, LUA_REGISTRYINDEX, CLIBS); in addtoclib()
349 lua_pushlightuserdata(L, plib); in addtoclib()
350 lua_pushvalue(L, -1); in addtoclib()
351 lua_setfield(L, -3, path); /* CLIBS[path] = plib */ in addtoclib()
352 lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ in addtoclib()
353 lua_pop(L, 1); /* pop CLIBS table */ in addtoclib()
361 static int gctm (lua_State *L) { in gctm() argument
362 lua_Integer n = luaL_len(L, 1); in gctm()
364 lua_rawgeti(L, 1, n); /* get handle CLIBS[n] */ in gctm()
365 lsys_unloadlib(lua_touserdata(L, -1)); in gctm()
366 lua_pop(L, 1); /* pop handle */ in gctm()
388 static int lookforfunc (lua_State *L, const char *path, const char *sym) { in lookforfunc() argument
389 void *reg = checkclib(L, path); /* check loaded C libraries */ in lookforfunc()
391 reg = lsys_load(L, path, *sym == '*'); /* global symbols if 'sym'=='*' */ in lookforfunc()
393 addtoclib(L, path, reg); in lookforfunc()
396 lua_pushboolean(L, 1); /* return 'true' */ in lookforfunc()
400 lua_CFunction f = lsys_sym(L, reg, sym); in lookforfunc()
403 lua_pushcfunction(L, f); /* else create new function */ in lookforfunc()
409 static int ll_loadlib (lua_State *L) { in ll_loadlib() argument
410 const char *path = luaL_checkstring(L, 1); in ll_loadlib()
411 const char *init = luaL_checkstring(L, 2); in ll_loadlib()
412 int stat = lookforfunc(L, path, init); in ll_loadlib()
416 luaL_pushfail(L); in ll_loadlib()
417 lua_insert(L, -2); in ll_loadlib()
418 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); in ll_loadlib()
469 static void pusherrornotfound (lua_State *L, const char *path) { in pusherrornotfound() argument
471 luaL_buffinit(L, &b); in pusherrornotfound()
479 static const char *searchpath (lua_State *L, const char *name, in searchpath() argument
489 name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ in searchpath()
490 luaL_buffinit(L, &buff); in searchpath()
498 return lua_pushstring(L, filename); /* save and return name */ in searchpath()
501 pusherrornotfound(L, lua_tostring(L, -1)); /* create error message */ in searchpath()
506 static int ll_searchpath (lua_State *L) { in ll_searchpath() argument
507 const char *f = searchpath(L, luaL_checkstring(L, 1), in ll_searchpath()
508 luaL_checkstring(L, 2), in ll_searchpath()
509 luaL_optstring(L, 3, "."), in ll_searchpath()
510 luaL_optstring(L, 4, LUA_DIRSEP)); in ll_searchpath()
513 luaL_pushfail(L); in ll_searchpath()
514 lua_insert(L, -2); in ll_searchpath()
520 static const char *findfile (lua_State *L, const char *name, in findfile() argument
524 lua_getfield(L, lua_upvalueindex(1), pname); in findfile()
525 path = lua_tostring(L, -1); in findfile()
527 luaL_error(L, "'package.%s' must be a string", pname); in findfile()
528 return searchpath(L, name, path, ".", dirsep); in findfile()
532 static int checkload (lua_State *L, int stat, const char *filename) { in checkload() argument
534 lua_pushstring(L, filename); /* will be 2nd argument to module */ in checkload()
538 return luaL_error(L, "error loading module '%s' from file '%s':\n\t%s", in checkload()
539 lua_tostring(L, 1), filename, lua_tostring(L, -1)); in checkload()
543 static int searcher_Lua (lua_State *L) { in searcher_Lua() argument
545 const char *name = luaL_checkstring(L, 1); in searcher_Lua()
546 filename = findfile(L, name, "path", LUA_LSUBSEP); in searcher_Lua()
548 return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename); in searcher_Lua()
560 static int loadfunc (lua_State *L, const char *filename, const char *modname) { in loadfunc() argument
563 modname = luaL_gsub(L, modname, ".", LUA_OFSEP); in loadfunc()
567 openfunc = lua_pushlstring(L, modname, mark - modname); in loadfunc()
568 openfunc = lua_pushfstring(L, LUA_POF"%s", openfunc); in loadfunc()
569 stat = lookforfunc(L, filename, openfunc); in loadfunc()
573 openfunc = lua_pushfstring(L, LUA_POF"%s", modname); in loadfunc()
574 return lookforfunc(L, filename, openfunc); in loadfunc()
578 static int searcher_C (lua_State *L) { in searcher_C() argument
579 const char *name = luaL_checkstring(L, 1); in searcher_C()
580 const char *filename = findfile(L, name, "cpath", LUA_CSUBSEP); in searcher_C()
582 return checkload(L, (loadfunc(L, filename, name) == 0), filename); in searcher_C()
586 static int searcher_Croot (lua_State *L) { in searcher_Croot() argument
588 const char *name = luaL_checkstring(L, 1); in searcher_Croot()
592 lua_pushlstring(L, name, p - name); in searcher_Croot()
593 filename = findfile(L, lua_tostring(L, -1), "cpath", LUA_CSUBSEP); in searcher_Croot()
595 if ((stat = loadfunc(L, filename, name)) != 0) { in searcher_Croot()
597 return checkload(L, 0, filename); /* real error */ in searcher_Croot()
599 lua_pushfstring(L, "no module '%s' in file '%s'", name, filename); in searcher_Croot()
603 lua_pushstring(L, filename); /* will be 2nd argument to module */ in searcher_Croot()
608 static int searcher_preload (lua_State *L) { in searcher_preload() argument
609 const char *name = luaL_checkstring(L, 1); in searcher_preload()
610 lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in searcher_preload()
611 if (lua_getfield(L, -1, name) == LUA_TNIL) { /* not found? */ in searcher_preload()
612 lua_pushfstring(L, "no field package.preload['%s']", name); in searcher_preload()
616 lua_pushliteral(L, ":preload:"); in searcher_preload()
622 static void findloader (lua_State *L, const char *name) { in findloader() argument
626 if (lua_getfield(L, lua_upvalueindex(1), "searchers") != LUA_TTABLE) in findloader()
627 luaL_error(L, "'package.searchers' must be a table"); in findloader()
628 luaL_buffinit(L, &msg); in findloader()
632 if (lua_rawgeti(L, 3, i) == LUA_TNIL) { /* no more searchers? */ in findloader()
633 lua_pop(L, 1); /* remove nil */ in findloader()
636 luaL_error(L, "module '%s' not found:%s", name, lua_tostring(L, -1)); in findloader()
638 lua_pushstring(L, name); in findloader()
639 lua_call(L, 1, 2); /* call it */ in findloader()
640 if (lua_isfunction(L, -2)) /* did it find a loader? */ in findloader()
642 else if (lua_isstring(L, -2)) { /* searcher returned error message? */ in findloader()
643 lua_pop(L, 1); /* remove extra return */ in findloader()
647 lua_pop(L, 2); /* remove both returns */ in findloader()
654 static int ll_require (lua_State *L) { in ll_require() argument
655 const char *name = luaL_checkstring(L, 1); in ll_require()
656 lua_settop(L, 1); /* LOADED table will be at index 2 */ in ll_require()
657 lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in ll_require()
658 lua_getfield(L, 2, name); /* LOADED[name] */ in ll_require()
659 if (lua_toboolean(L, -1)) /* is it there? */ in ll_require()
662 lua_pop(L, 1); /* remove 'getfield' result */ in ll_require()
663 findloader(L, name); in ll_require()
664 lua_rotate(L, -2, 1); /* function <-> loader data */ in ll_require()
665 lua_pushvalue(L, 1); /* name is 1st argument to module loader */ in ll_require()
666 lua_pushvalue(L, -3); /* loader data is 2nd argument */ in ll_require()
668 lua_call(L, 2, 1); /* run loader to load module */ in ll_require()
670 if (!lua_isnil(L, -1)) /* non-nil return? */ in ll_require()
671 lua_setfield(L, 2, name); /* LOADED[name] = returned value */ in ll_require()
673 lua_pop(L, 1); /* pop nil */ in ll_require()
674 if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */ in ll_require()
675 lua_pushboolean(L, 1); /* use true as result */ in ll_require()
676 lua_copy(L, -1, -2); /* replace loader result */ in ll_require()
677 lua_setfield(L, 2, name); /* LOADED[name] = true */ in ll_require()
679 lua_rotate(L, -2, 1); /* loader data <-> module result */ in ll_require()
707 static void createsearcherstable (lua_State *L) { in createsearcherstable() argument
712 lua_createtable(L, sizeof(searchers)/sizeof(searchers[0]) - 1, 0); in createsearcherstable()
715 lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ in createsearcherstable()
716 lua_pushcclosure(L, searchers[i], 1); in createsearcherstable()
717 lua_rawseti(L, -2, i+1); in createsearcherstable()
719 lua_setfield(L, -2, "searchers"); /* put it in field 'searchers' */ in createsearcherstable()
727 static void createclibstable (lua_State *L) { in createclibstable() argument
728 luaL_getsubtable(L, LUA_REGISTRYINDEX, CLIBS); /* create CLIBS table */ in createclibstable()
729 lua_createtable(L, 0, 1); /* create metatable for CLIBS */ in createclibstable()
730 lua_pushcfunction(L, gctm); in createclibstable()
731 lua_setfield(L, -2, "__gc"); /* set finalizer for CLIBS table */ in createclibstable()
732 lua_setmetatable(L, -2); in createclibstable()
736 LUAMOD_API int luaopen_package (lua_State *L) { in luaopen_package() argument
737 createclibstable(L); in luaopen_package()
738 luaL_newlib(L, pk_funcs); /* create 'package' table */ in luaopen_package()
739 createsearcherstable(L); in luaopen_package()
741 setpath(L, "path", LUA_PATH_VAR, LUA_PATH_DEFAULT); in luaopen_package()
742 setpath(L, "cpath", LUA_CPATH_VAR, LUA_CPATH_DEFAULT); in luaopen_package()
744 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATH_SEP "\n" LUA_PATH_MARK "\n" in luaopen_package()
746 lua_setfield(L, -2, "config"); in luaopen_package()
748 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); in luaopen_package()
749 lua_setfield(L, -2, "loaded"); in luaopen_package()
751 luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE); in luaopen_package()
752 lua_setfield(L, -2, "preload"); in luaopen_package()
753 lua_pushglobaltable(L); in luaopen_package()
754 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ in luaopen_package()
755 luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ in luaopen_package()
756 lua_pop(L, 1); /* pop global table */ in luaopen_package()