diff options
author | Benoit Giannangeli <giann008@gmail.com> | 2017-05-02 15:01:54 +0200 |
---|---|---|
committer | Benoit Giannangeli <giann008@gmail.com> | 2017-05-03 13:28:11 +0200 |
commit | d231ed14d0816eb26856e948a9ab72f64d46c1eb (patch) | |
tree | 4e85b5237878770195aedb362b6bd446f7f2596c /src | |
parent | 5ac211f983cd628dfb95a6add7e5386071f4dada (diff) | |
download | fengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.tar.gz fengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.tar.bz2 fengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.zip |
loadlib.js
Diffstat (limited to 'src')
-rw-r--r-- | src/defs.js | 77 | ||||
-rw-r--r-- | src/lauxlib.js | 34 | ||||
-rw-r--r-- | src/linit.js | 14 | ||||
-rw-r--r-- | src/loadlib.js | 317 | ||||
-rw-r--r-- | src/lua.js | 11 | ||||
-rw-r--r-- | src/lualib.js | 2 |
6 files changed, 454 insertions, 1 deletions
diff --git a/src/defs.js b/src/defs.js index 34316e9..c580ee5 100644 --- a/src/defs.js +++ b/src/defs.js @@ -263,6 +263,83 @@ const LUA_MASKRET = (1 << LUA_HOOKRET); const LUA_MASKLINE = (1 << LUA_HOOKLINE); const LUA_MASKCOUNT = (1 << LUA_HOOKCOUNT); +/* +** LUA_PATH_SEP is the character that separates templates in a path. +** LUA_PATH_MARK is the string that marks the substitution points in a +** template. +** LUA_EXEC_DIR in a Windows path is replaced by the executable's +** directory. +*/ +const LUA_PATH_SEP = ";"; +module.exports.LUA_PATH_SEP = LUA_PATH_SEP; + +const LUA_PATH_MARK = "?"; +module.exports.LUA_PATH_MARK = LUA_PATH_MARK; + +const LUA_EXEC_DIR = "!"; +module.exports.LUA_EXEC_DIR = LUA_EXEC_DIR; + + +/* +@@ LUA_PATH_DEFAULT is the default path that Lua uses to look for +** Lua libraries. +@@ LUA_CPATH_DEFAULT is the default path that Lua uses to look for +** C libraries. +** CHANGE them if your machine has a non-conventional directory +** hierarchy or if you want to install your libraries in +** non-conventional directories. +*/ +const LUA_VDIR = LUA_VERSION_MAJOR + "." + LUA_VERSION_MINOR; +module.exports.LUA_VDIR = LUA_VDIR; + +let os = false; +if (typeof require === "function" && (os = require('os')) && typeof os.platform === "function" && os.platform() === 'win32') { + /* + ** In Windows, any exclamation mark ('!') in the path is replaced by the + ** path of the directory of the executable file of the current process. + */ + const LUA_LDIR = "!\\lua\\"; + module.exports.LUA_LDIR = LUA_LDIR; + + const LUA_CDIR = "!\\"; + module.exports.LUA_CDIR = LUA_CDIR; + + const LUA_SHRDIR = "!\\..\\share\\lua\\" + LUA_VDIR + "\\"; + module.exports.LUA_SHRDIR = LUA_SHRDIR; + + const LUA_PATH_DEFAULT = + LUA_LDIR + "?.lua;" + LUA_LDIR + "?\\init.lua;" + + LUA_CDIR + "?.lua;" + LUA_CDIR + "?\\init.lua;" + + LUA_SHRDIR + "?.lua;" + LUA_SHRDIR + "?\\init.lua;" + + ".\\?.lua;.\\?\\init.lua"; + module.exports.LUA_PATH_DEFAULT = LUA_PATH_DEFAULT; + + const LUA_CPATH_DEFAULT = + LUA_CDIR + "?.dll;" + + LUA_CDIR + "..\\lib\\lua\\" + LUA_VDIR + "\\?.dll;" + + LUA_CDIR + "loadall.dll;.\\?.dll"; + module.exports.LUA_CPATH_DEFAULT = LUA_CPATH_DEFAULT; +} else { + const LUA_ROOT = "/usr/local/"; + module.exports.LUA_ROOT = LUA_ROOT; + + const LUA_LDIR = LUA_ROOT + "share/lua/" + LUA_VDIR + "/"; + module.exports.LUA_LDIR = LUA_LDIR; + + const LUA_CDIR = LUA_ROOT + "lib/lua/" + LUA_VDIR + "/"; + module.exports.LUA_CDIR = LUA_CDIR; + + const LUA_PATH_DEFAULT = + LUA_LDIR + "?.lua;" + LUA_LDIR + "?/init.lua;" + + LUA_CDIR + "?.lua;" + LUA_CDIR + "?/init.lua;" + + "./?.lua;./?/init.lua"; + module.exports.LUA_PATH_DEFAULT = LUA_PATH_DEFAULT; + + const LUA_CPATH_DEFAULT = + LUA_CDIR + "?.so;" + LUA_CDIR + "loadall.so;./?.so"; + module.exports.LUA_CPATH_DEFAULT = LUA_CPATH_DEFAULT; +} + module.exports.CT = CT; module.exports.FENGARI_AUTHORS = FENGARI_AUTHORS; module.exports.FENGARI_COPYRIGHT = FENGARI_COPYRIGHT; diff --git a/src/lauxlib.js b/src/lauxlib.js index 9c4b98d..d5b69f9 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -4,8 +4,12 @@ const lua = require('./lua.js'); const linit = require('./linit.js'); +/* key, in the registry, for table of loaded modules */ const LUA_LOADED_TABLE = "_LOADED"; +/* key, in the registry, for table of preloaded loaders */ +const LUA_PRELOAD_TABLE = "_PRELOAD"; + const LUA_FILEHANDLE = lua.to_luastring("FILE*", true); @@ -541,6 +545,34 @@ const luaL_requiref = function(L, modname, openf, glb) { } }; +const find_subarray = function(arr, subarr, from_index) { + var i = from_index >>> 0, + sl = subarr.length, + l = arr.length + 1 - sl; + + loop: for (; i < l; i++) { + for (let j = 0; j < sl; j++) + if (arr[i+j] !== subarr[j]) + continue loop; + return i; + } + return -1; +}; + +const luaL_gsub = function(L, s, p, r) { + let wild; + let i = 0; + let b = []; + while ((wild = find_subarray(s, p)) >= 0) { + b.push(...s.slice(i, wild)); /* push prefix */ + b.push(...r); /* push replacement in place of pattern */ + i = wild + p.length; /* continue after 'p' */ + } + b.push(s.slice(i)); /* push last suffix */ + lua.lua_pushstring(L, b); + return lua.lua_tostring(L, -1); +}; + /* ** ensure that stack[idx][fname] has a table and push that table ** into the stack @@ -748,6 +780,7 @@ const lua_writestringerror = function(s) { module.exports.LUA_FILEHANDLE = LUA_FILEHANDLE; module.exports.LUA_LOADED_TABLE = LUA_LOADED_TABLE; +module.exports.LUA_PRELOAD_TABLE = LUA_PRELOAD_TABLE; module.exports.luaL_Buffer = luaL_Buffer; module.exports.luaL_addchar = luaL_addchar; module.exports.luaL_addlstring = luaL_addlstring; @@ -773,6 +806,7 @@ module.exports.luaL_fileresult = luaL_fileresult; module.exports.luaL_getmetafield = luaL_getmetafield; module.exports.luaL_getmetatable = luaL_getmetatable; module.exports.luaL_getsubtable = luaL_getsubtable; +module.exports.luaL_gsub = luaL_gsub; module.exports.luaL_len = luaL_len; module.exports.luaL_loadbuffer = luaL_loadbuffer; module.exports.luaL_loadbufferx = luaL_loadbufferx; diff --git a/src/linit.js b/src/linit.js index 325dfa1..13e60e3 100644 --- a/src/linit.js +++ b/src/linit.js @@ -11,6 +11,7 @@ const lutf8lib = require('./lutf8lib.js'); const ldblib = require('./ldblib.js'); const liolib = require('./liolib.js'); const loslib = require('./loslib.js'); +const loadlib = require('./loadlib.js'); const lualib = require('./lualib.js'); const loadedlibs = { @@ -25,6 +26,19 @@ const loadedlibs = { "_G": lbaselib.luaopen_base }; +// Only with Node +if (typeof require === "function") { + + let fs = false; + try { + fs = require('fs'); + } catch (e) {} + + if (fs) { + loadedlibs[lualib.LUA_LOADLIBNAME] = loadlib.luaopen_package; + } +} + const luaL_openlibs = function(L) { /* "require" functions from 'loadedlibs' and set results to global table */ for (let lib in loadedlibs) { diff --git a/src/loadlib.js b/src/loadlib.js new file mode 100644 index 0000000..c29bc37 --- /dev/null +++ b/src/loadlib.js @@ -0,0 +1,317 @@ +"use strict"; + +const lua = require('./lua.js'); +const lauxlib = require('./lauxlib.js'); + +const fs = require('fs'); + + +const LUA_IGMARK = ["-".charCodeAt(0)]; + +const CLIBS = lua.to_luastring("__CLIBS__", true); +const LUA_PATH_VAR = "LUA_PATH"; +const LUA_CPATH_VAR = "LUA_CPATH"; + +/* +** LUA_CSUBSEP is the character that replaces dots in submodule names +** when searching for a C loader. +** LUA_LSUBSEP is the character that replaces dots in submodule names +** when searching for a Lua loader. +*/ +const LUA_CSUBSEP = lua.LUA_DIRSEP; +const LUA_LSUBSEP = lua.LUA_DIRSEP; + +/* prefix for open functions in C libraries */ +const LUA_POF = "luaopen_"; + +/* separator for open functions in C libraries */ +const LUA_OFSEP = "_"; +// const LIB_FAIL = "open"; + +const AUXMARK = [1]; + + +const LIB_FAIL = "absent"; +const DLMSG = "dynamic libraries not enabled; check your Lua installation"; + + +const lsys_unloadlib = function(lib) { +}; + + +const lsys_load = function(L, path, seeglb) { + lua.lua_pushliteral(L, DLMSG); + return null; +}; + + +const lsys_sym = function(L, lib, sym) { + lua.lua_pushliteral(L, DLMSG); + return null; +}; + +/* +** return registry.LUA_NOENV as a boolean +*/ +const noenv = function(L) { + lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, "LUA_NOENV"); + let b = lua.lua_toboolean(L, -1); + lua.lua_pop(L, 1); /* remove value */ + return b; +}; + +const readable = function(filename) { + try { + fs.openSync(lua.to_jsstring(filename), 'r'); + } catch (e) { + return false; + } + + return true; +}; + +/* error codes for 'lookforfunc' */ +const ERRLIB = 1; +const ERRFUNC = 2; + +/* +** Look for a C function named 'sym' in a dynamically loaded library +** 'path'. +** First, check whether the library is already loaded; if not, try +** to load it. +** Then, if 'sym' is '*', return true (as library has been loaded). +** Otherwise, look for symbol 'sym' in the library and push a +** C function with that symbol. +** Return 0 and 'true' or a function in the stack; in case of +** errors, return an error code and an error message in the stack. +*/ +const lookforfunc = function(L, path, sym) { + let reg = checkclib(L, path); /* check loaded C libraries */ + if (reg === null) { /* must load library? */ + reg = lsys_load(L, path, sym[0] === '*'.charCodeAt(0)); /* a global symbols if 'sym'=='*' */ + if (reg === null) return ERRLIB; /* unable to load library */ + addtoclib(L, path, reg); + } + if (sym[0] === '*'.charCodeAt(0)) { /* loading only library (no function)? */ + lua.lua_pushboolean(L, 1); /* return 'true' */ + return 0; /* no errors */ + } + else { + let f = lsys_sym(L, reg, sym); + if (f === null) + return ERRFUNC; /* unable to find function */ + lua.lua_pushcfunction(L, f); /* else create new function */ + return 0; /* no errors */ + } +}; + +/* +** Set a path +*/ +const setpath = function(L, fieldname, envname, dft) { + let nver = lua.lua_pushstring(L, lua.to_luastring(`${envname}${lua.LUA_VERSUFFIX}`, true)); + let path = process.env[nver]; /* use versioned name */ + if (path === undefined) /* no environment variable? */ + path = process.env[envname]; /* try unversioned name */ + if (path === undefined || noenv(L)) /* no environment variable? */ + lua.lua_pushstring(L, lua.to_luastring(dft, true)); /* use default */ + else { + /* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */ + path = lauxlib.luaL_gsub( + L, + lua.to_luastring(path), + lua.to_luastring(lua.LUA_PATH_SEP + lua.LUA_PATH_SEP, true), + lua.to_luastring(lua.LUA_PATH_SEP, true) + .concat(AUXMARK) + .concat(lua.to_luastring(lua.LUA_PATH_SEP, true)) + ); + lauxlib.luaL_gsub(L, path, AUXMARK, dft); + lua.lua_remove(L, -2); /* remove result from 1st 'gsub' */ + } + lua.lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */ + lua.lua_pop(L, 1); /* pop versioned variable name */ +}; + +/* +** return registry.CLIBS[path] +*/ +const checkclib = function(L, path) { + lua.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, CLIBS); + lua.lua_getfield(L, -1, path); + let plib = lua.lua_touserdata(L, -1); /* plib = CLIBS[path] */ + lua.lua_pop(L, 2); /* pop CLIBS table and 'plib' */ + return plib; +}; + +/* +** registry.CLIBS[path] = plib -- for queries +** registry.CLIBS[#CLIBS + 1] = plib -- also keep a list of all libraries +*/ +const addtoclib = function(L, path, plib) { + lua.lua_rawgetp(L, lua.LUA_REGISTRYINDEX, CLIBS); + lua.lua_pushlightuserdata(L, plib); + lua.lua_pushvalue(L, -1); + lua.lua_setfield(L, -3, path); /* CLIBS[path] = plib */ + lua.lua_rawseti(L, -2, lauxlib.luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */ + lua.lua_pop(L, 1); /* pop CLIBS table */ +}; + +const pushnexttemplate = function(L, path) { + while (path[0] === lua.LUA_PATH_SEP.charCodeAt[0]) path = path.slice(1); /* skip separators */ + if (path.length === 0) return null; /* no more templates */ + let l = path.indexOf(lua.LUA_PATH_SEP.charCodeAt(0)); /* find next separator */ + if (l < 0) l = path.length; + lua.lua_pushlstring(L, path, l); /* template */ + return l; +}; + +const searchpath = function(L, name, path, sep, dirsep) { + let msg = []; /* to build error message */ + if (sep[0] !== 0) /* non-empty separator? */ + name = lauxlib.luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */ + while ((path = pushnexttemplate(L, path)) !== null) { + let filename = lauxlib.luaL_gsub(L, lua.lua_tostring(L, -1), lua.LUA_PATH_MARK, name); + lua.lua_remove(L, -2); /* remove path template */ + if (readable(filename)) /* does file exist and is readable? */ + return filename; /* return that file name */ + lua.lua_remove(L, -1); /* remove file name */ + msg.push(...lua.to_luastring(`\n\tno file '${lua.to_jsstring(filename)}'`)); + } + lua.lua_pushstring(msg); /* create error message */ + return null; /* not found */ +}; + +const findfile = function(L, name, pname, dirsep) { + lua.lua_getfield(L, lua.lua_upvalueindex(1), pname); + let path = lua.lua_tostring(L, -1); + if (path === null) + lauxlib.luaL_error(L, `'package.${lua.to_jsstring(pname)}' must be a string`); + return searchpath(L, name, path, ['.'.charCodeAt(0)], dirsep); +}; + +const checkload = function(L, stat, filename) { + if (stat) { /* module loaded successfully? */ + lua.lua_pushstring(L, filename); /* will be 2nd argument to module */ + return 2; /* return open function and file name */ + } else + return lauxlib.luaL_error(L, lua.to_luastring(`error loading module '${lua.lua_tojsstring(L, 1)}' from file '${lua.to_jsstring(filename)}':\n\t${lua.lua_tojsstring(L, 1)}`)); +}; + +const searcher_Lua = function(L) { + let name = lauxlib.luaL_checkstring(L, 1); + let filename = findfile(L, name, lua.to_luastring("path", true), LUA_LSUBSEP); + if (filename === null) return 1; /* module not found in this path */ + return checkload(L, lauxlib.luaL_loadfile(L, filename) === lua.LUA_OK, filename); +}; + +/* +** Try to find a load function for module 'modname' at file 'filename'. +** First, change '.' to '_' in 'modname'; then, if 'modname' has +** the form X-Y (that is, it has an "ignore mark"), build a function +** name "luaopen_X" and look for it. (For compatibility, if that +** fails, it also tries "luaopen_Y".) If there is no ignore mark, +** look for a function named "luaopen_modname". +*/ +const loadfunc = function(L, filename, modname) { + let openfunc; + modname = lauxlib.luaL_gsub(L, modname, [".".charCodeAt(0)], LUA_OFSEP); + let mark = modname.indexOf(LUA_IGMARK[0]); + if (mark >= 0) { + openfunc = lua.lua_pushlstring(L, modname, mark); + openfunc = lua.lua_pushstring(L, lua.to_luastring(`${LUA_POF}${openfunc}`)); + let stat = lookforfunc(L, filename, openfunc); + if (stat !== ERRFUNC) return stat; + modname = mark + 1; /* else go ahead and try old-style name */ + } + openfunc = lua.lua_pushstring(L, lua.to_luastring(`${LUA_POF}${modname}`)); + return lookforfunc(L, filename, openfunc); +}; + +const searcher_C = function(L) { + let name = lauxlib.luaL_checkstring(L, 1); + let filename = findfile(L, name, "cpath", LUA_CSUBSEP); + if (filename === null) return 1; /* module not found in this path */ + return checkload(L, (loadfunc(L, filename, name) === 0), filename); +}; + +const searcher_Croot = function(L) { + let name = lauxlib.luaL_checkstring(L, 1); + let p = name.indexOf('.'.charCodeAt(0)); + let stat; + if (p < 0) return 0; /* is root */ + lua.lua_pushlstring(L, name, p); + let filename = findfile(L, lua.lua_tostring(L, -1), lua.to_luastring("cpath", true), LUA_CSUBSEP); + if (filename === null) return 1; /* root not found */ + if ((stat = loadfunc(L, filename, name)) !== 0) { + if (stat != ERRFUNC) + return checkload(L, 0, filename); /* real error */ + else { /* open function not found */ + lua.lua_pushstring(L, lua.to_luastring(`\n\tno module '${lua.to_jsstring(name)}' in file '${lua.to_jsstring(filename)}'`)); + return 1; + } + } + lua.lua_pushstring(L, filename); /* will be 2nd argument to module */ + return 2; +}; + +const searcher_preload = function(L) { + let name = lauxlib.luaL_checkstring(L, 1); + lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lauxlib.LUA_PRELOAD_TABLE); + if (lua.lua_getfield(L, -1, name) === lua.LUA_TNIL) /* not found? */ + lua.lua_pushliteral(L, `\n\tno field package.preload['${lua.to_jsstring(name)}']`); + return 1; +}; + +const pk_funcs = {}; + +const ll_funcs = {}; + +const createsearcherstable = function(L) { + let searchers = {searcher_preload, searcher_Lua, searcher_C, searcher_Croot, null}; + /* create 'searchers' table */ + lua.lua_createtable(L); + /* fill it with predefined searchers */ + for (let i = 0; searchers[i]; i++) { + lua.lua_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */ + lua.lua_pushcclosure(L, searchers[i], 1); + lua.lua_rawseti(L, -2, i+1); + } + lua.lua_setfield(L, -2, lua.to_luastring("searchers", true)); /* put it in field 'searchers' */ +}; + +/* +** create table CLIBS to keep track of loaded C libraries, +** setting a finalizer to close all libraries when closing state. +*/ +const createclibstable = function(L) { + lua.lua_newtable(L); /* create CLIBS table */ + lua.lua_createtable(L, 0, 1); /* create metatable for CLIBS */ + lua.lua_setmetatable(L, -2); + lua.lua_rawsetp(L, lua.LUA_REGISTRYINDEX, CLIBS); /* set CLIBS table in registry */ +}; + +const luaopen_package = function(L) { + createclibstable(L); + lauxlib.luaL_newlib(L, pk_funcs); /* create 'package' table */ + createsearcherstable(L); + /* set paths */ + setpath(L, lua.to_luastring("path", true), LUA_PATH_VAR, lua.LUA_PATH_DEFAULT); + setpath(L, lua.to_luastring("cpath", true), LUA_CPATH_VAR, lua.LUA_CPATH_DEFAULT); + /* store config information */ + lua.lua_pushliteral(L, lua.LUA_DIRSEP + "\n" + lua.LUA_PATH_SEP + "\n" + lua.LUA_PATH_MARK + "\n" + + lua.LUA_EXEC_DIR + "\n" + LUA_IGMARK + "\n"); + lua.lua_setfield(L, -2, lua.to_luastring("config", true)); + /* set field 'loaded' */ + lauxlib.luaL_getsubtable(L, lua.LUA_REGISTRYINDEX, lua.to_luastring(lauxlib.LUA_LOADED_TABLE, true)); + lua.lua_setfield(L, -2, lua.to_luastring("loaded", true)); + /* set field 'preload' */ + lauxlib.luaL_getsubtable(L, lua.LUA_REGISTRYINDEX, lua.to_luastring(lauxlib.LUA_PRELOAD_TABLE, true)); + lua.lua_setfield(L, -2, lua.to_luastring("preload", true)); + lua.lua_pushglobaltable(L); + lua.lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */ + lauxlib.luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */ + lua.lua_pop(L, 1); /* pop global table */ + return 1; /* return 'package' table */ +}; + +module.exports.luaopen_package = luaopen_package; @@ -81,6 +81,17 @@ module.exports.lua_Debug = defs.lua_Debug; module.exports.lua_upvalueindex = defs.lua_upvalueindex; module.exports.to_jsstring = defs.to_jsstring; module.exports.to_luastring = defs.to_luastring; +module.exports.LUA_VDIR = defs.LUA_VDIR; +module.exports.LUA_LDIR = defs.LUA_LDIR; +module.exports.LUA_CDIR = defs.LUA_CDIR; +module.exports.LUA_SHRDIR = defs.LUA_SHRDIR; +module.exports.LUA_PATH_DEFAULT = defs.LUA_PATH_DEFAULT; +module.exports.LUA_CPATH_DEFAULT = defs.LUA_CPATH_DEFAULT; +module.exports.LUA_ROOT = defs.LUA_ROOT; +module.exports.LUA_LDIR = defs.LUA_LDIR; +module.exports.LUA_CDIR = defs.LUA_CDIR; +module.exports.LUA_PATH_DEFAULT = defs.LUA_PATH_DEFAULT; +module.exports.LUA_CPATH_DEFAULT = defs.LUA_CPATH_DEFAULT; module.exports.lua_absindex = lapi.lua_absindex; module.exports.lua_atpanic = lapi.lua_atpanic; diff --git a/src/lualib.js b/src/lualib.js index b0bd253..549ab28 100644 --- a/src/lualib.js +++ b/src/lualib.js @@ -44,4 +44,4 @@ module.exports[LUA_DBLIBNAME] = require("./ldblib.js").luaopen_debug; const LUA_LOADLIBNAME = "package"; module.exports.LUA_LOADLIBNAME = LUA_LOADLIBNAME; -// module.exports[LUA_LOADLIBNAME] = require("./loadlib.js").luaopen_package; +module.exports[LUA_LOADLIBNAME] = require("./loadlib.js").luaopen_package; |