Lines Matching refs:L
42 #define setprogdir(L) ((void)0) argument
46 static void *ll_load (lua_State *L, const char *path);
47 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym);
68 static void *ll_load (lua_State *L, const char *path) { in ll_load() argument
70 if (lib == NULL) lua_pushstring(L, dlerror()); in ll_load()
75 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { in ll_sym() argument
77 if (f == NULL) lua_pushstring(L, dlerror()); in ll_sym()
97 static void setprogdir (lua_State *L) { in setprogdir() argument
103 luaL_error(L, "unable to get ModuleFileName"); in setprogdir()
106 luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); in setprogdir()
107 lua_remove(L, -2); /* remove original string */ in setprogdir()
112 static void pusherror (lua_State *L) { in pusherror() argument
117 lua_pushstring(L, buffer); in pusherror()
119 lua_pushfstring(L, "system error %d\n", error); in pusherror()
127 static void *ll_load (lua_State *L, const char *path) { in ll_load() argument
129 if (lib == NULL) pusherror(L); in ll_load()
134 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { in ll_sym() argument
136 if (f == NULL) pusherror(L); in ll_sym()
159 static void pusherror (lua_State *L) { in pusherror() argument
165 lua_pushstring(L, err_str); in pusherror()
191 static void *ll_load (lua_State *L, const char *path) { in ll_load() argument
196 lua_pushliteral(L, "dyld not present"); in ll_load()
204 if (mod == NULL) pusherror(L); in ll_load()
207 lua_pushstring(L, errorfromcode(ret)); in ll_load()
212 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { in ll_sym() argument
215 lua_pushfstring(L, "symbol " LUA_QS " not found", sym); in ll_sym()
244 static void *ll_load (lua_State *L, const char *path) { in ll_load() argument
246 lua_pushliteral(L, DLMSG); in ll_load()
251 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) { in ll_sym() argument
253 lua_pushliteral(L, DLMSG); in ll_sym()
262 static void **ll_register (lua_State *L, const char *path) { in ll_register() argument
264 lua_pushfstring(L, "%s%s", LIBPREFIX, path); in ll_register()
265 lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ in ll_register()
266 if (!lua_isnil(L, -1)) /* is there an entry? */ in ll_register()
267 plib = (void **)lua_touserdata(L, -1); in ll_register()
269 lua_pop(L, 1); in ll_register()
270 plib = (void **)lua_newuserdata(L, sizeof(const void *)); in ll_register()
272 luaL_getmetatable(L, "_LOADLIB"); in ll_register()
273 lua_setmetatable(L, -2); in ll_register()
274 lua_pushfstring(L, "%s%s", LIBPREFIX, path); in ll_register()
275 lua_pushvalue(L, -2); in ll_register()
276 lua_settable(L, LUA_REGISTRYINDEX); in ll_register()
286 static int gctm (lua_State *L) { in gctm() argument
287 void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); in gctm()
294 static int ll_loadfunc (lua_State *L, const char *path, const char *sym) { in ll_loadfunc() argument
295 void **reg = ll_register(L, path); in ll_loadfunc()
296 if (*reg == NULL) *reg = ll_load(L, path); in ll_loadfunc()
300 lua_CFunction f = ll_sym(L, *reg, sym); in ll_loadfunc()
303 lua_pushcfunction(L, f); in ll_loadfunc()
309 static int ll_loadlib (lua_State *L) { in ll_loadlib() argument
310 const char *path = luaL_checkstring(L, 1); in ll_loadlib()
311 const char *init = luaL_checkstring(L, 2); in ll_loadlib()
312 int stat = ll_loadfunc(L, path, init); in ll_loadlib()
316 lua_pushnil(L); in ll_loadlib()
317 lua_insert(L, -2); in ll_loadlib()
318 lua_pushstring(L, (stat == ERRLIB) ? LIB_FAIL : "init"); in ll_loadlib()
340 static const char *pushnexttemplate (lua_State *L, const char *path) { in pushnexttemplate() argument
346 lua_pushlstring(L, path, l - path); /* template */ in pushnexttemplate()
351 static const char *findfile (lua_State *L, const char *name, in findfile() argument
354 name = luaL_gsub(L, name, ".", LUA_DIRSEP); in findfile()
355 lua_getfield(L, LUA_ENVIRONINDEX, pname); in findfile()
356 path = lua_tostring(L, -1); in findfile()
358 luaL_error(L, LUA_QL("package.%s") " must be a string", pname); in findfile()
359 lua_pushliteral(L, ""); /* error accumulator */ in findfile()
360 while ((path = pushnexttemplate(L, path)) != NULL) { in findfile()
362 filename = luaL_gsub(L, lua_tostring(L, -1), LUA_PATH_MARK, name); in findfile()
363 lua_remove(L, -2); /* remove path template */ in findfile()
366 lua_pushfstring(L, "\n\tno file " LUA_QS, filename); in findfile()
367 lua_remove(L, -2); /* remove file name */ in findfile()
368 lua_concat(L, 2); /* add entry to possible error message */ in findfile()
374 static void loaderror (lua_State *L, const char *filename) { in loaderror() argument
375 luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", in loaderror()
376 lua_tostring(L, 1), filename, lua_tostring(L, -1)); in loaderror()
380 static int loader_Lua (lua_State *L) { in loader_Lua() argument
382 const char *name = luaL_checkstring(L, 1); in loader_Lua()
383 filename = findfile(L, name, "path"); in loader_Lua()
385 if (luaL_loadfile(L, filename) != 0) in loader_Lua()
386 loaderror(L, filename); in loader_Lua()
391 static const char *mkfuncname (lua_State *L, const char *modname) { in mkfuncname() argument
395 funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); in mkfuncname()
396 funcname = lua_pushfstring(L, POF"%s", funcname); in mkfuncname()
397 lua_remove(L, -2); /* remove 'gsub' result */ in mkfuncname()
402 static int loader_C (lua_State *L) { in loader_C() argument
404 const char *name = luaL_checkstring(L, 1); in loader_C()
405 const char *filename = findfile(L, name, "cpath"); in loader_C()
407 funcname = mkfuncname(L, name); in loader_C()
408 if (ll_loadfunc(L, filename, funcname) != 0) in loader_C()
409 loaderror(L, filename); in loader_C()
414 static int loader_Croot (lua_State *L) { in loader_Croot() argument
417 const char *name = luaL_checkstring(L, 1); in loader_Croot()
421 lua_pushlstring(L, name, p - name); in loader_Croot()
422 filename = findfile(L, lua_tostring(L, -1), "cpath"); in loader_Croot()
424 funcname = mkfuncname(L, name); in loader_Croot()
425 if ((stat = ll_loadfunc(L, filename, funcname)) != 0) { in loader_Croot()
426 if (stat != ERRFUNC) loaderror(L, filename); /* real error */ in loader_Croot()
427 lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, in loader_Croot()
435 static int loader_preload (lua_State *L) { in loader_preload() argument
436 const char *name = luaL_checkstring(L, 1); in loader_preload()
437 lua_getfield(L, LUA_ENVIRONINDEX, "preload"); in loader_preload()
438 if (!lua_istable(L, -1)) in loader_preload()
439 luaL_error(L, LUA_QL("package.preload") " must be a table"); in loader_preload()
440 lua_getfield(L, -1, name); in loader_preload()
441 if (lua_isnil(L, -1)) /* not found? */ in loader_preload()
442 lua_pushfstring(L, "\n\tno field package.preload['%s']", name); in loader_preload()
451 static int ll_require (lua_State *L) { in ll_require() argument
452 const char *name = luaL_checkstring(L, 1); in ll_require()
454 lua_settop(L, 1); /* _LOADED table will be at index 2 */ in ll_require()
455 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); in ll_require()
456 lua_getfield(L, 2, name); in ll_require()
457 if (lua_toboolean(L, -1)) { /* is it there? */ in ll_require()
458 if (lua_touserdata(L, -1) == sentinel) /* check loops */ in ll_require()
459 luaL_error(L, "loop or previous error loading module " LUA_QS, name); in ll_require()
463 lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); in ll_require()
464 if (!lua_istable(L, -1)) in ll_require()
465 luaL_error(L, LUA_QL("package.loaders") " must be a table"); in ll_require()
466 lua_pushliteral(L, ""); /* error message accumulator */ in ll_require()
468 lua_rawgeti(L, -2, i); /* get a loader */ in ll_require()
469 if (lua_isnil(L, -1)) in ll_require()
470 luaL_error(L, "module " LUA_QS " not found:%s", in ll_require()
471 name, lua_tostring(L, -2)); in ll_require()
472 lua_pushstring(L, name); in ll_require()
473 lua_call(L, 1, 1); /* call it */ in ll_require()
474 if (lua_isfunction(L, -1)) /* did it find module? */ in ll_require()
476 else if (lua_isstring(L, -1)) /* loader returned error message? */ in ll_require()
477 lua_concat(L, 2); /* accumulate it */ in ll_require()
479 lua_pop(L, 1); in ll_require()
481 lua_pushlightuserdata(L, sentinel); in ll_require()
482 lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ in ll_require()
483 lua_pushstring(L, name); /* pass name as argument to module */ in ll_require()
484 lua_call(L, 1, 1); /* run loaded module */ in ll_require()
485 if (!lua_isnil(L, -1)) /* non-nil return? */ in ll_require()
486 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ in ll_require()
487 lua_getfield(L, 2, name); in ll_require()
488 if (lua_touserdata(L, -1) == sentinel) { /* module did not set a value? */ in ll_require()
489 lua_pushboolean(L, 1); /* use true as result */ in ll_require()
490 lua_pushvalue(L, -1); /* extra copy to be returned */ in ll_require()
491 lua_setfield(L, 2, name); /* _LOADED[name] = true */ in ll_require()
507 static void setfenv (lua_State *L) { in setfenv() argument
509 if (lua_getstack(L, 1, &ar) == 0 || in setfenv()
510 lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ in setfenv()
511 lua_iscfunction(L, -1)) in setfenv()
512 luaL_error(L, LUA_QL("module") " not called from a Lua function"); in setfenv()
513 lua_pushvalue(L, -2); in setfenv()
514 lua_setfenv(L, -2); in setfenv()
515 lua_pop(L, 1); in setfenv()
519 static void dooptions (lua_State *L, int n) { in dooptions() argument
522 lua_pushvalue(L, i); /* get option (a function) */ in dooptions()
523 lua_pushvalue(L, -2); /* module */ in dooptions()
524 lua_call(L, 1, 0); in dooptions()
529 static void modinit (lua_State *L, const char *modname) { in modinit() argument
531 lua_pushvalue(L, -1); in modinit()
532 lua_setfield(L, -2, "_M"); /* module._M = module */ in modinit()
533 lua_pushstring(L, modname); in modinit()
534 lua_setfield(L, -2, "_NAME"); in modinit()
539 lua_pushlstring(L, modname, dot - modname); in modinit()
540 lua_setfield(L, -2, "_PACKAGE"); in modinit()
544 static int ll_module (lua_State *L) { in ll_module() argument
545 const char *modname = luaL_checkstring(L, 1); in ll_module()
546 int loaded = lua_gettop(L) + 1; /* index of _LOADED table */ in ll_module()
547 lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); in ll_module()
548 lua_getfield(L, loaded, modname); /* get _LOADED[modname] */ in ll_module()
549 if (!lua_istable(L, -1)) { /* not found? */ in ll_module()
550 lua_pop(L, 1); /* remove previous result */ in ll_module()
552 if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) in ll_module()
553 return luaL_error(L, "name conflict for module " LUA_QS, modname); in ll_module()
554 lua_pushvalue(L, -1); in ll_module()
555 lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ in ll_module()
558 lua_getfield(L, -1, "_NAME"); in ll_module()
559 if (!lua_isnil(L, -1)) /* is table an initialized module? */ in ll_module()
560 lua_pop(L, 1); in ll_module()
562 lua_pop(L, 1); in ll_module()
563 modinit(L, modname); in ll_module()
565 lua_pushvalue(L, -1); in ll_module()
566 setfenv(L); in ll_module()
567 dooptions(L, loaded - 1); in ll_module()
572 static int ll_seeall (lua_State *L) { in ll_seeall() argument
573 luaL_checktype(L, 1, LUA_TTABLE); in ll_seeall()
574 if (!lua_getmetatable(L, 1)) { in ll_seeall()
575 lua_createtable(L, 0, 1); /* create new metatable */ in ll_seeall()
576 lua_pushvalue(L, -1); in ll_seeall()
577 lua_setmetatable(L, 1); in ll_seeall()
579 lua_pushvalue(L, LUA_GLOBALSINDEX); in ll_seeall()
580 lua_setfield(L, -2, "__index"); /* mt.__index = _G */ in ll_seeall()
592 static void setpath (lua_State *L, const char *fieldname, const char *envname, in setpath() argument
596 lua_pushstring(L, def); /* use default */ in setpath()
599 path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, in setpath()
601 luaL_gsub(L, path, AUXMARK, def); in setpath()
602 lua_remove(L, -2); in setpath()
604 setprogdir(L); in setpath()
605 lua_setfield(L, -2, fieldname); in setpath()
627 LUALIB_API int luaopen_package (lua_State *L) { in luaopen_package() argument
630 luaL_newmetatable(L, "_LOADLIB"); in luaopen_package()
631 lua_pushcfunction(L, gctm); in luaopen_package()
632 lua_setfield(L, -2, "__gc"); in luaopen_package()
634 luaL_register(L, LUA_LOADLIBNAME, pk_funcs); in luaopen_package()
636 lua_getfield(L, -1, "loadlib"); in luaopen_package()
637 lua_setfield(L, LUA_GLOBALSINDEX, "loadlib"); in luaopen_package()
639 lua_pushvalue(L, -1); in luaopen_package()
640 lua_replace(L, LUA_ENVIRONINDEX); in luaopen_package()
642 lua_createtable(L, sizeof(loaders)/sizeof(loaders[0]) - 1, 0); in luaopen_package()
645 lua_pushcfunction(L, loaders[i]); in luaopen_package()
646 lua_rawseti(L, -2, i+1); in luaopen_package()
648 lua_setfield(L, -2, "loaders"); /* put it in field `loaders' */ in luaopen_package()
649 setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT); /* set field `path' */ in luaopen_package()
650 setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT); /* set field `cpath' */ in luaopen_package()
652 lua_pushliteral(L, LUA_DIRSEP "\n" LUA_PATHSEP "\n" LUA_PATH_MARK "\n" in luaopen_package()
654 lua_setfield(L, -2, "config"); in luaopen_package()
656 luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); in luaopen_package()
657 lua_setfield(L, -2, "loaded"); in luaopen_package()
659 lua_newtable(L); in luaopen_package()
660 lua_setfield(L, -2, "preload"); in luaopen_package()
661 lua_pushvalue(L, LUA_GLOBALSINDEX); in luaopen_package()
662 luaL_register(L, NULL, ll_funcs); /* open lib into global table */ in luaopen_package()
663 lua_pop(L, 1); in luaopen_package()