| 1 |
/* |
| 2 |
** Package library. |
| 3 |
** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.h |
| 4 |
** |
| 5 |
** Major portions taken verbatim or adapted from the Lua interpreter. |
| 6 |
** Copyright (C) 1994-2012 Lua.org, PUC-Rio. See Copyright Notice in lua.h |
| 7 |
*/ |
| 8 |
|
| 9 |
#define lib_package_c |
| 10 |
#define LUA_LIB |
| 11 |
|
| 12 |
#include "lua.h" |
| 13 |
#include "lauxlib.h" |
| 14 |
#include "lualib.h" |
| 15 |
|
| 16 |
#include "lj_obj.h" |
| 17 |
#include "lj_err.h" |
| 18 |
#include "lj_lib.h" |
| 19 |
|
| 20 |
/* ------------------------------------------------------------------------ */ |
| 21 |
|
| 22 |
/* Error codes for ll_loadfunc. */ |
| 23 |
#define PACKAGE_ERR_LIB 1 |
| 24 |
#define PACKAGE_ERR_FUNC 2 |
| 25 |
#define PACKAGE_ERR_LOAD 3 |
| 26 |
|
| 27 |
/* Redefined in platform specific part. */ |
| 28 |
#define PACKAGE_LIB_FAIL "open" |
| 29 |
#define setprogdir(L) ((void)0) |
| 30 |
|
| 31 |
/* Symbol name prefixes. */ |
| 32 |
#define SYMPREFIX_CF "luaopen_%s" |
| 33 |
#define SYMPREFIX_BC "luaJIT_BC_%s" |
| 34 |
|
| 35 |
#if LJ_TARGET_DLOPEN |
| 36 |
|
| 37 |
#include <dlfcn.h> |
| 38 |
|
| 39 |
static void ll_unloadlib(void *lib) |
| 40 |
{ |
| 41 |
dlclose(lib); |
| 42 |
} |
| 43 |
|
| 44 |
static void *ll_load(lua_State *L, const char *path, int gl) |
| 45 |
{ |
| 46 |
void *lib = dlopen(path, RTLD_NOW | (gl ? RTLD_GLOBAL : RTLD_LOCAL)); |
| 47 |
if (lib == NULL) lua_pushstring(L, dlerror()); |
| 48 |
return lib; |
| 49 |
} |
| 50 |
|
| 51 |
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) |
| 52 |
{ |
| 53 |
lua_CFunction f = (lua_CFunction)dlsym(lib, sym); |
| 54 |
if (f == NULL) lua_pushstring(L, dlerror()); |
| 55 |
return f; |
| 56 |
} |
| 57 |
|
| 58 |
static const char *ll_bcsym(void *lib, const char *sym) |
| 59 |
{ |
| 60 |
#if defined(RTLD_DEFAULT) && !defined(NO_RTLD_DEFAULT) |
| 61 |
if (lib == NULL) lib = RTLD_DEFAULT; |
| 62 |
#elif LJ_TARGET_OSX || LJ_TARGET_BSD |
| 63 |
if (lib == NULL) lib = (void *)(intptr_t)-2; |
| 64 |
#endif |
| 65 |
return (const char *)dlsym(lib, sym); |
| 66 |
} |
| 67 |
|
| 68 |
#elif LJ_TARGET_WINDOWS |
| 69 |
|
| 70 |
#define WIN32_LEAN_AND_MEAN |
| 71 |
#include <windows.h> |
| 72 |
|
| 73 |
#ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
| 74 |
#define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4 |
| 75 |
#define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2 |
| 76 |
BOOL WINAPI GetModuleHandleExA(DWORD, LPCSTR, HMODULE*); |
| 77 |
#endif |
| 78 |
|
| 79 |
#if LJ_TARGET_UWP |
| 80 |
void *LJ_WIN_LOADLIBA(const char *path) |
| 81 |
{ |
| 82 |
DWORD err = GetLastError(); |
| 83 |
wchar_t wpath[256]; |
| 84 |
HANDLE lib = NULL; |
| 85 |
if (MultiByteToWideChar(CP_ACP, 0, path, -1, wpath, 256) > 0) { |
| 86 |
lib = LoadPackagedLibrary(wpath, 0); |
| 87 |
} |
| 88 |
SetLastError(err); |
| 89 |
return lib; |
| 90 |
} |
| 91 |
#endif |
| 92 |
|
| 93 |
#undef setprogdir |
| 94 |
|
| 95 |
static void setprogdir(lua_State *L) |
| 96 |
{ |
| 97 |
char buff[MAX_PATH + 1]; |
| 98 |
char *lb; |
| 99 |
DWORD nsize = sizeof(buff); |
| 100 |
DWORD n = GetModuleFileNameA(NULL, buff, nsize); |
| 101 |
if (n == 0 || n == nsize || (lb = strrchr(buff, '\\')) == NULL) { |
| 102 |
luaL_error(L, "unable to get ModuleFileName"); |
| 103 |
} else { |
| 104 |
*lb = '\0'; |
| 105 |
luaL_gsub(L, lua_tostring(L, -1), LUA_EXECDIR, buff); |
| 106 |
lua_remove(L, -2); /* remove original string */ |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
static void pusherror(lua_State *L) |
| 111 |
{ |
| 112 |
DWORD error = GetLastError(); |
| 113 |
#if LJ_TARGET_XBOXONE |
| 114 |
wchar_t wbuffer[128]; |
| 115 |
char buffer[128*2]; |
| 116 |
if (FormatMessageW(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, |
| 117 |
NULL, error, 0, wbuffer, sizeof(wbuffer)/sizeof(wchar_t), NULL) && |
| 118 |
WideCharToMultiByte(CP_ACP, 0, wbuffer, 128, buffer, 128*2, NULL, NULL)) |
| 119 |
#else |
| 120 |
char buffer[128]; |
| 121 |
if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM, |
| 122 |
NULL, error, 0, buffer, sizeof(buffer), NULL)) |
| 123 |
#endif |
| 124 |
lua_pushstring(L, buffer); |
| 125 |
else |
| 126 |
lua_pushfstring(L, "system error %d\n", error); |
| 127 |
} |
| 128 |
|
| 129 |
static void ll_unloadlib(void *lib) |
| 130 |
{ |
| 131 |
FreeLibrary((HINSTANCE)lib); |
| 132 |
} |
| 133 |
|
| 134 |
static void *ll_load(lua_State *L, const char *path, int gl) |
| 135 |
{ |
| 136 |
HINSTANCE lib = LJ_WIN_LOADLIBA(path); |
| 137 |
if (lib == NULL) pusherror(L); |
| 138 |
UNUSED(gl); |
| 139 |
return lib; |
| 140 |
} |
| 141 |
|
| 142 |
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) |
| 143 |
{ |
| 144 |
lua_CFunction f = (lua_CFunction)GetProcAddress((HINSTANCE)lib, sym); |
| 145 |
if (f == NULL) pusherror(L); |
| 146 |
return f; |
| 147 |
} |
| 148 |
|
| 149 |
#if LJ_TARGET_UWP |
| 150 |
EXTERN_C IMAGE_DOS_HEADER __ImageBase; |
| 151 |
#endif |
| 152 |
|
| 153 |
static const char *ll_bcsym(void *lib, const char *sym) |
| 154 |
{ |
| 155 |
if (lib) { |
| 156 |
return (const char *)GetProcAddress((HINSTANCE)lib, sym); |
| 157 |
} else { |
| 158 |
#if LJ_TARGET_UWP |
| 159 |
return (const char *)GetProcAddress((HINSTANCE)&__ImageBase, sym); |
| 160 |
#else |
| 161 |
HINSTANCE h = GetModuleHandleA(NULL); |
| 162 |
const char *p = (const char *)GetProcAddress(h, sym); |
| 163 |
if (p == NULL && GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS|GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, |
| 164 |
(const char *)ll_bcsym, &h)) |
| 165 |
p = (const char *)GetProcAddress(h, sym); |
| 166 |
return p; |
| 167 |
#endif |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
#else |
| 172 |
|
| 173 |
#undef PACKAGE_LIB_FAIL |
| 174 |
#define PACKAGE_LIB_FAIL "absent" |
| 175 |
|
| 176 |
#define DLMSG "dynamic libraries not enabled; no support for target OS" |
| 177 |
|
| 178 |
static void ll_unloadlib(void *lib) |
| 179 |
{ |
| 180 |
UNUSED(lib); |
| 181 |
} |
| 182 |
|
| 183 |
static void *ll_load(lua_State *L, const char *path, int gl) |
| 184 |
{ |
| 185 |
UNUSED(path); UNUSED(gl); |
| 186 |
lua_pushliteral(L, DLMSG); |
| 187 |
return NULL; |
| 188 |
} |
| 189 |
|
| 190 |
static lua_CFunction ll_sym(lua_State *L, void *lib, const char *sym) |
| 191 |
{ |
| 192 |
UNUSED(lib); UNUSED(sym); |
| 193 |
lua_pushliteral(L, DLMSG); |
| 194 |
return NULL; |
| 195 |
} |
| 196 |
|
| 197 |
static const char *ll_bcsym(void *lib, const char *sym) |
| 198 |
{ |
| 199 |
UNUSED(lib); UNUSED(sym); |
| 200 |
return NULL; |
| 201 |
} |
| 202 |
|
| 203 |
#endif |
| 204 |
|
| 205 |
/* ------------------------------------------------------------------------ */ |
| 206 |
|
| 207 |
static void **ll_register(lua_State *L, const char *path) |
| 208 |
{ |
| 209 |
void **plib; |
| 210 |
lua_pushfstring(L, "LOADLIB: %s", path); |
| 211 |
lua_gettable(L, LUA_REGISTRYINDEX); /* check library in registry? */ |
| 212 |
if (!lua_isnil(L, -1)) { /* is there an entry? */ |
| 213 |
plib = (void **)lua_touserdata(L, -1); |
| 214 |
} else { /* no entry yet; create one */ |
| 215 |
lua_pop(L, 1); |
| 216 |
plib = (void **)lua_newuserdata(L, sizeof(void *)); |
| 217 |
*plib = NULL; |
| 218 |
luaL_setmetatable(L, "_LOADLIB"); |
| 219 |
lua_pushfstring(L, "LOADLIB: %s", path); |
| 220 |
lua_pushvalue(L, -2); |
| 221 |
lua_settable(L, LUA_REGISTRYINDEX); |
| 222 |
} |
| 223 |
return plib; |
| 224 |
} |
| 225 |
|
| 226 |
static const char *mksymname(lua_State *L, const char *modname, |
| 227 |
const char *prefix) |
| 228 |
{ |
| 229 |
const char *funcname; |
| 230 |
const char *mark = strchr(modname, *LUA_IGMARK); |
| 231 |
if (mark) modname = mark + 1; |
| 232 |
funcname = luaL_gsub(L, modname, ".", "_"); |
| 233 |
funcname = lua_pushfstring(L, prefix, funcname); |
| 234 |
lua_remove(L, -2); /* remove 'gsub' result */ |
| 235 |
return funcname; |
| 236 |
} |
| 237 |
|
| 238 |
static int ll_loadfunc(lua_State *L, const char *path, const char *name, int r) |
| 239 |
{ |
| 240 |
void **reg; |
| 241 |
if (strlen(path) >= 4096) { |
| 242 |
lua_pushliteral(L, "path too long"); |
| 243 |
return PACKAGE_ERR_LIB; |
| 244 |
} |
| 245 |
reg = ll_register(L, path); |
| 246 |
if (*reg == NULL) *reg = ll_load(L, path, (*name == '*')); |
| 247 |
if (*reg == NULL) { |
| 248 |
return PACKAGE_ERR_LIB; /* Unable to load library. */ |
| 249 |
} else if (*name == '*') { /* Only load library into global namespace. */ |
| 250 |
lua_pushboolean(L, 1); |
| 251 |
return 0; |
| 252 |
} else { |
| 253 |
const char *sym = r ? name : mksymname(L, name, SYMPREFIX_CF); |
| 254 |
lua_CFunction f = ll_sym(L, *reg, sym); |
| 255 |
if (f) { |
| 256 |
lua_pushcfunction(L, f); |
| 257 |
return 0; |
| 258 |
} |
| 259 |
if (!r) { |
| 260 |
const char *bcdata = ll_bcsym(*reg, mksymname(L, name, SYMPREFIX_BC)); |
| 261 |
lua_pop(L, 1); |
| 262 |
if (bcdata) { |
| 263 |
if (luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) |
| 264 |
return PACKAGE_ERR_LOAD; |
| 265 |
return 0; |
| 266 |
} |
| 267 |
} |
| 268 |
return PACKAGE_ERR_FUNC; /* Unable to find function. */ |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
static int lj_cf_package_loadlib(lua_State *L) |
| 273 |
{ |
| 274 |
const char *path = luaL_checkstring(L, 1); |
| 275 |
const char *init = luaL_checkstring(L, 2); |
| 276 |
int st = ll_loadfunc(L, path, init, 1); |
| 277 |
if (st == 0) { /* no errors? */ |
| 278 |
return 1; /* return the loaded function */ |
| 279 |
} else { /* error; error message is on stack top */ |
| 280 |
lua_pushnil(L); |
| 281 |
lua_insert(L, -2); |
| 282 |
lua_pushstring(L, (st == PACKAGE_ERR_LIB) ? PACKAGE_LIB_FAIL : "init"); |
| 283 |
return 3; /* return nil, error message, and where */ |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
static int lj_cf_package_unloadlib(lua_State *L) |
| 288 |
{ |
| 289 |
void **lib = (void **)luaL_checkudata(L, 1, "_LOADLIB"); |
| 290 |
if (*lib) ll_unloadlib(*lib); |
| 291 |
*lib = NULL; /* mark library as closed */ |
| 292 |
return 0; |
| 293 |
} |
| 294 |
|
| 295 |
/* ------------------------------------------------------------------------ */ |
| 296 |
|
| 297 |
static int readable(const char *filename) |
| 298 |
{ |
| 299 |
FILE *f = fopen(filename, "r"); /* try to open file */ |
| 300 |
if (f == NULL) return 0; /* open failed */ |
| 301 |
fclose(f); |
| 302 |
return 1; |
| 303 |
} |
| 304 |
|
| 305 |
static const char *pushnexttemplate(lua_State *L, const char *path) |
| 306 |
{ |
| 307 |
const char *l; |
| 308 |
while (*path == *LUA_PATHSEP) path++; /* skip separators */ |
| 309 |
if (*path == '\0') return NULL; /* no more templates */ |
| 310 |
l = strchr(path, *LUA_PATHSEP); /* find next separator */ |
| 311 |
if (l == NULL) l = path + strlen(path); |
| 312 |
lua_pushlstring(L, path, (size_t)(l - path)); /* template */ |
| 313 |
return l; |
| 314 |
} |
| 315 |
|
| 316 |
static const char *searchpath (lua_State *L, const char *name, |
| 317 |
const char *path, const char *sep, |
| 318 |
const char *dirsep) |
| 319 |
{ |
| 320 |
luaL_Buffer msg; /* to build error message */ |
| 321 |
luaL_buffinit(L, &msg); |
| 322 |
if (*sep != '\0') /* non-empty separator? */ |
| 323 |
name = luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ |
| 324 |
while ((path = pushnexttemplate(L, path)) != NULL) { |
| 325 |
const char *filename = luaL_gsub(L, lua_tostring(L, -1), |
| 326 |
LUA_PATH_MARK, name); |
| 327 |
lua_remove(L, -2); /* remove path template */ |
| 328 |
if (readable(filename)) /* does file exist and is readable? */ |
| 329 |
return filename; /* return that file name */ |
| 330 |
lua_pushfstring(L, "\n\tno file " LUA_QS, filename); |
| 331 |
lua_remove(L, -2); /* remove file name */ |
| 332 |
luaL_addvalue(&msg); /* concatenate error msg. entry */ |
| 333 |
} |
| 334 |
luaL_pushresult(&msg); /* create error message */ |
| 335 |
return NULL; /* not found */ |
| 336 |
} |
| 337 |
|
| 338 |
static int lj_cf_package_searchpath(lua_State *L) |
| 339 |
{ |
| 340 |
const char *f = searchpath(L, luaL_checkstring(L, 1), |
| 341 |
luaL_checkstring(L, 2), |
| 342 |
luaL_optstring(L, 3, "."), |
| 343 |
luaL_optstring(L, 4, LUA_DIRSEP)); |
| 344 |
if (f != NULL) { |
| 345 |
return 1; |
| 346 |
} else { /* error message is on top of the stack */ |
| 347 |
lua_pushnil(L); |
| 348 |
lua_insert(L, -2); |
| 349 |
return 2; /* return nil + error message */ |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
static const char *findfile(lua_State *L, const char *name, |
| 354 |
const char *pname) |
| 355 |
{ |
| 356 |
const char *path; |
| 357 |
lua_getfield(L, LUA_ENVIRONINDEX, pname); |
| 358 |
path = lua_tostring(L, -1); |
| 359 |
if (path == NULL) |
| 360 |
luaL_error(L, LUA_QL("package.%s") " must be a string", pname); |
| 361 |
return searchpath(L, name, path, ".", LUA_DIRSEP); |
| 362 |
} |
| 363 |
|
| 364 |
static void loaderror(lua_State *L, const char *filename) |
| 365 |
{ |
| 366 |
luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s", |
| 367 |
lua_tostring(L, 1), filename, lua_tostring(L, -1)); |
| 368 |
} |
| 369 |
|
| 370 |
static int lj_cf_package_loader_lua(lua_State *L) |
| 371 |
{ |
| 372 |
const char *filename; |
| 373 |
const char *name = luaL_checkstring(L, 1); |
| 374 |
filename = findfile(L, name, "path"); |
| 375 |
if (filename == NULL) return 1; /* library not found in this path */ |
| 376 |
if (luaL_loadfile(L, filename) != 0) |
| 377 |
loaderror(L, filename); |
| 378 |
return 1; /* library loaded successfully */ |
| 379 |
} |
| 380 |
|
| 381 |
#define LUA_POF "luaopen_" |
| 382 |
#define LUA_OFSEP "_" |
| 383 |
#define POF LUA_POF |
| 384 |
|
| 385 |
static const char *mkfuncname (lua_State *L, const char *modname) { |
| 386 |
const char *funcname; |
| 387 |
const char *mark = strchr(modname, *LUA_IGMARK); |
| 388 |
if (mark) modname = mark + 1; |
| 389 |
funcname = luaL_gsub(L, modname, ".", LUA_OFSEP); |
| 390 |
funcname = lua_pushfstring(L, POF"%s", funcname); |
| 391 |
lua_remove(L, -2); /* remove 'gsub' result */ |
| 392 |
return funcname; |
| 393 |
} |
| 394 |
|
| 395 |
int loader_C_luatex (lua_State *L, const char *name, const char *filename) { |
| 396 |
const char *funcname; |
| 397 |
funcname = mkfuncname(L, name); |
| 398 |
if (ll_loadfunc(L, filename, funcname,0) != 0) |
| 399 |
loaderror(L, filename); |
| 400 |
return 1; /* library loaded successfully */ |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
static int lj_cf_package_loader_c(lua_State *L) |
| 405 |
{ |
| 406 |
const char *name = luaL_checkstring(L, 1); |
| 407 |
const char *filename = findfile(L, name, "cpath"); |
| 408 |
if (filename == NULL) return 1; /* library not found in this path */ |
| 409 |
if (ll_loadfunc(L, filename, name, 0) != 0) |
| 410 |
loaderror(L, filename); |
| 411 |
return 1; /* library loaded successfully */ |
| 412 |
} |
| 413 |
|
| 414 |
static int lj_cf_package_loader_croot(lua_State *L) |
| 415 |
{ |
| 416 |
const char *filename; |
| 417 |
const char *name = luaL_checkstring(L, 1); |
| 418 |
const char *p = strchr(name, '.'); |
| 419 |
int st; |
| 420 |
if (p == NULL) return 0; /* is root */ |
| 421 |
lua_pushlstring(L, name, (size_t)(p - name)); |
| 422 |
filename = findfile(L, lua_tostring(L, -1), "cpath"); |
| 423 |
if (filename == NULL) return 1; /* root not found */ |
| 424 |
if ((st = ll_loadfunc(L, filename, name, 0)) != 0) { |
| 425 |
if (st != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */ |
| 426 |
lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, |
| 427 |
name, filename); |
| 428 |
return 1; /* function not found */ |
| 429 |
} |
| 430 |
return 1; |
| 431 |
} |
| 432 |
|
| 433 |
int loader_Call_luatex (lua_State *L, const char *name, const char *filename) { |
| 434 |
const char *funcname; |
| 435 |
int stat; |
| 436 |
if (filename == NULL) return 1; /* root not found */ |
| 437 |
funcname = mkfuncname(L, name); |
| 438 |
if ((stat = ll_loadfunc(L, filename, funcname,0)) != 0) { |
| 439 |
if (stat != PACKAGE_ERR_FUNC) loaderror(L, filename); /* real error */ |
| 440 |
lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS, |
| 441 |
name, filename); |
| 442 |
return 1; /* function not found */ |
| 443 |
} |
| 444 |
return 1; /* library loaded successfully */ |
| 445 |
} |
| 446 |
|
| 447 |
static int lj_cf_package_loader_preload(lua_State *L) |
| 448 |
{ |
| 449 |
const char *name = luaL_checkstring(L, 1); |
| 450 |
lua_getfield(L, LUA_ENVIRONINDEX, "preload"); |
| 451 |
if (!lua_istable(L, -1)) |
| 452 |
luaL_error(L, LUA_QL("package.preload") " must be a table"); |
| 453 |
lua_getfield(L, -1, name); |
| 454 |
if (lua_isnil(L, -1)) { /* Not found? */ |
| 455 |
const char *bcname = mksymname(L, name, SYMPREFIX_BC); |
| 456 |
const char *bcdata = ll_bcsym(NULL, bcname); |
| 457 |
if (bcdata == NULL || luaL_loadbuffer(L, bcdata, ~(size_t)0, name) != 0) |
| 458 |
lua_pushfstring(L, "\n\tno field package.preload['%s']", name); |
| 459 |
} |
| 460 |
return 1; |
| 461 |
} |
| 462 |
|
| 463 |
/* ------------------------------------------------------------------------ */ |
| 464 |
|
| 465 |
#define KEY_SENTINEL (U64x(80000000,00000000)|'s') |
| 466 |
|
| 467 |
static int lj_cf_package_require(lua_State *L) |
| 468 |
{ |
| 469 |
const char *name = luaL_checkstring(L, 1); |
| 470 |
int i; |
| 471 |
lua_settop(L, 1); /* _LOADED table will be at index 2 */ |
| 472 |
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); |
| 473 |
lua_getfield(L, 2, name); |
| 474 |
if (lua_toboolean(L, -1)) { /* is it there? */ |
| 475 |
if ((L->top-1)->u64 == KEY_SENTINEL) /* check loops */ |
| 476 |
luaL_error(L, "loop or previous error loading module " LUA_QS, name); |
| 477 |
return 1; /* package is already loaded */ |
| 478 |
} |
| 479 |
/* else must load it; iterate over available loaders */ |
| 480 |
lua_getfield(L, LUA_ENVIRONINDEX, "loaders"); |
| 481 |
if (!lua_istable(L, -1)) |
| 482 |
luaL_error(L, LUA_QL("package.loaders") " must be a table"); |
| 483 |
lua_pushliteral(L, ""); /* error message accumulator */ |
| 484 |
for (i = 1; ; i++) { |
| 485 |
lua_rawgeti(L, -2, i); /* get a loader */ |
| 486 |
if (lua_isnil(L, -1)) |
| 487 |
luaL_error(L, "module " LUA_QS " not found:%s", |
| 488 |
name, lua_tostring(L, -2)); |
| 489 |
lua_pushstring(L, name); |
| 490 |
lua_call(L, 1, 1); /* call it */ |
| 491 |
if (lua_isfunction(L, -1)) /* did it find module? */ |
| 492 |
break; /* module loaded successfully */ |
| 493 |
else if (lua_isstring(L, -1)) /* loader returned error message? */ |
| 494 |
lua_concat(L, 2); /* accumulate it */ |
| 495 |
else |
| 496 |
lua_pop(L, 1); |
| 497 |
} |
| 498 |
(L->top++)->u64 = KEY_SENTINEL; |
| 499 |
lua_setfield(L, 2, name); /* _LOADED[name] = sentinel */ |
| 500 |
lua_pushstring(L, name); /* pass name as argument to module */ |
| 501 |
lua_call(L, 1, 1); /* run loaded module */ |
| 502 |
if (!lua_isnil(L, -1)) /* non-nil return? */ |
| 503 |
lua_setfield(L, 2, name); /* _LOADED[name] = returned value */ |
| 504 |
lua_getfield(L, 2, name); |
| 505 |
if ((L->top-1)->u64 == KEY_SENTINEL) { /* module did not set a value? */ |
| 506 |
lua_pushboolean(L, 1); /* use true as result */ |
| 507 |
lua_pushvalue(L, -1); /* extra copy to be returned */ |
| 508 |
lua_setfield(L, 2, name); /* _LOADED[name] = true */ |
| 509 |
} |
| 510 |
lj_lib_checkfpu(L); |
| 511 |
return 1; |
| 512 |
} |
| 513 |
|
| 514 |
/* ------------------------------------------------------------------------ */ |
| 515 |
|
| 516 |
static void setfenv(lua_State *L) |
| 517 |
{ |
| 518 |
lua_Debug ar; |
| 519 |
if (lua_getstack(L, 1, &ar) == 0 || |
| 520 |
lua_getinfo(L, "f", &ar) == 0 || /* get calling function */ |
| 521 |
lua_iscfunction(L, -1)) |
| 522 |
luaL_error(L, LUA_QL("module") " not called from a Lua function"); |
| 523 |
lua_pushvalue(L, -2); |
| 524 |
lua_setfenv(L, -2); |
| 525 |
lua_pop(L, 1); |
| 526 |
} |
| 527 |
|
| 528 |
static void dooptions(lua_State *L, int n) |
| 529 |
{ |
| 530 |
int i; |
| 531 |
for (i = 2; i <= n; i++) { |
| 532 |
lua_pushvalue(L, i); /* get option (a function) */ |
| 533 |
lua_pushvalue(L, -2); /* module */ |
| 534 |
lua_call(L, 1, 0); |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
static void modinit(lua_State *L, const char *modname) |
| 539 |
{ |
| 540 |
const char *dot; |
| 541 |
lua_pushvalue(L, -1); |
| 542 |
lua_setfield(L, -2, "_M"); /* module._M = module */ |
| 543 |
lua_pushstring(L, modname); |
| 544 |
lua_setfield(L, -2, "_NAME"); |
| 545 |
dot = strrchr(modname, '.'); /* look for last dot in module name */ |
| 546 |
if (dot == NULL) dot = modname; else dot++; |
| 547 |
/* set _PACKAGE as package name (full module name minus last part) */ |
| 548 |
lua_pushlstring(L, modname, (size_t)(dot - modname)); |
| 549 |
lua_setfield(L, -2, "_PACKAGE"); |
| 550 |
} |
| 551 |
|
| 552 |
static int lj_cf_package_module(lua_State *L) |
| 553 |
{ |
| 554 |
const char *modname = luaL_checkstring(L, 1); |
| 555 |
int lastarg = (int)(L->top - L->base); |
| 556 |
luaL_pushmodule(L, modname, 1); |
| 557 |
lua_getfield(L, -1, "_NAME"); |
| 558 |
if (!lua_isnil(L, -1)) { /* Module already initialized? */ |
| 559 |
lua_pop(L, 1); |
| 560 |
} else { |
| 561 |
lua_pop(L, 1); |
| 562 |
modinit(L, modname); |
| 563 |
} |
| 564 |
lua_pushvalue(L, -1); |
| 565 |
setfenv(L); |
| 566 |
dooptions(L, lastarg); |
| 567 |
return LJ_52; |
| 568 |
} |
| 569 |
|
| 570 |
static int lj_cf_package_seeall(lua_State *L) |
| 571 |
{ |
| 572 |
luaL_checktype(L, 1, LUA_TTABLE); |
| 573 |
if (!lua_getmetatable(L, 1)) { |
| 574 |
lua_createtable(L, 0, 1); /* create new metatable */ |
| 575 |
lua_pushvalue(L, -1); |
| 576 |
lua_setmetatable(L, 1); |
| 577 |
} |
| 578 |
lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 579 |
lua_setfield(L, -2, "__index"); /* mt.__index = _G */ |
| 580 |
return 0; |
| 581 |
} |
| 582 |
|
| 583 |
/* ------------------------------------------------------------------------ */ |
| 584 |
|
| 585 |
#define AUXMARK "\1" |
| 586 |
|
| 587 |
static void setpath(lua_State *L, const char *fieldname, const char *envname, |
| 588 |
const char *def, int noenv) |
| 589 |
{ |
| 590 |
#if LJ_TARGET_CONSOLE |
| 591 |
const char *path = NULL; |
| 592 |
UNUSED(envname); |
| 593 |
#else |
| 594 |
const char *path = getenv(envname); |
| 595 |
#endif |
| 596 |
if (path == NULL || noenv) { |
| 597 |
lua_pushstring(L, def); |
| 598 |
} else { |
| 599 |
path = luaL_gsub(L, path, LUA_PATHSEP LUA_PATHSEP, |
| 600 |
LUA_PATHSEP AUXMARK LUA_PATHSEP); |
| 601 |
luaL_gsub(L, path, AUXMARK, def); |
| 602 |
lua_remove(L, -2); |
| 603 |
} |
| 604 |
setprogdir(L); |
| 605 |
lua_setfield(L, -2, fieldname); |
| 606 |
} |
| 607 |
|
| 608 |
static const luaL_Reg package_lib[] = { |
| 609 |
{ "loadlib", lj_cf_package_loadlib }, |
| 610 |
{ "searchpath", lj_cf_package_searchpath }, |
| 611 |
{ "seeall", lj_cf_package_seeall }, |
| 612 |
{ NULL, NULL } |
| 613 |
}; |
| 614 |
|
| 615 |
static const luaL_Reg package_global[] = { |
| 616 |
{ "module", lj_cf_package_module }, |
| 617 |
{ "require", lj_cf_package_require }, |
| 618 |
{ NULL, NULL } |
| 619 |
}; |
| 620 |
|
| 621 |
static const lua_CFunction package_loaders[] = |
| 622 |
{ |
| 623 |
lj_cf_package_loader_preload, |
| 624 |
lj_cf_package_loader_lua, |
| 625 |
lj_cf_package_loader_c, |
| 626 |
lj_cf_package_loader_croot, |
| 627 |
NULL |
| 628 |
}; |
| 629 |
|
| 630 |
LUALIB_API int luaopen_package(lua_State *L) |
| 631 |
{ |
| 632 |
int i; |
| 633 |
int noenv; |
| 634 |
luaL_newmetatable(L, "_LOADLIB"); |
| 635 |
lj_lib_pushcf(L, lj_cf_package_unloadlib, 1); |
| 636 |
lua_setfield(L, -2, "__gc"); |
| 637 |
luaL_register(L, LUA_LOADLIBNAME, package_lib); |
| 638 |
lua_copy(L, -1, LUA_ENVIRONINDEX); |
| 639 |
lua_createtable(L, sizeof(package_loaders)/sizeof(package_loaders[0])-1, 0); |
| 640 |
for (i = 0; package_loaders[i] != NULL; i++) { |
| 641 |
lj_lib_pushcf(L, package_loaders[i], 1); |
| 642 |
lua_rawseti(L, -2, i+1); |
| 643 |
} |
| 644 |
#if LJ_52 |
| 645 |
lua_pushvalue(L, -1); |
| 646 |
lua_setfield(L, -3, "searchers"); |
| 647 |
#endif |
| 648 |
lua_setfield(L, -2, "loaders"); |
| 649 |
lua_getfield(L, LUA_REGISTRYINDEX, "LUA_NOENV"); |
| 650 |
noenv = lua_toboolean(L, -1); |
| 651 |
lua_pop(L, 1); |
| 652 |
setpath(L, "path", LUA_PATH, LUA_PATH_DEFAULT, noenv); |
| 653 |
setpath(L, "cpath", LUA_CPATH, LUA_CPATH_DEFAULT, noenv); |
| 654 |
lua_pushliteral(L, LUA_PATH_CONFIG); |
| 655 |
lua_setfield(L, -2, "config"); |
| 656 |
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 16); |
| 657 |
lua_setfield(L, -2, "loaded"); |
| 658 |
luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD", 4); |
| 659 |
lua_setfield(L, -2, "preload"); |
| 660 |
lua_pushvalue(L, LUA_GLOBALSINDEX); |
| 661 |
luaL_register(L, NULL, package_global); |
| 662 |
lua_pop(L, 1); |
| 663 |
return 1; |
| 664 |
} |
| 665 |
|