summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-22 14:19:14 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-22 14:20:56 +1100
commit52b43b164478278a6227a1db7f15c1367573655d (patch)
tree5d619ee23a0a6c21e3b6666a6efb36f4080c4f9b /src
parentd4b858c2d5e80d2c621e71fbae6046dc0f146103 (diff)
downloadfengari-52b43b164478278a6227a1db7f15c1367573655d.tar.gz
fengari-52b43b164478278a6227a1db7f15c1367573655d.tar.bz2
fengari-52b43b164478278a6227a1db7f15c1367573655d.zip
src/loadlib.js: Use destructuring requires
Diffstat (limited to 'src')
-rw-r--r--src/loadlib.js341
1 files changed, 203 insertions, 138 deletions
diff --git a/src/loadlib.js b/src/loadlib.js
index 2dc6d73..3fcc7c5 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -1,9 +1,74 @@
"use strict";
+const {
+ LUA_CPATH_DEFAULT,
+ LUA_DIRSEP,
+ LUA_EXEC_DIR,
+ LUA_OK,
+ LUA_PATH_DEFAULT,
+ LUA_PATH_MARK,
+ LUA_PATH_SEP,
+ LUA_REGISTRYINDEX,
+ LUA_TNIL,
+ LUA_TTABLE,
+ LUA_VERSUFFIX,
+ lua_callk,
+ lua_createtable,
+ lua_getfield,
+ lua_insert,
+ lua_isfunction,
+ lua_isnil,
+ lua_isstring,
+ lua_newtable,
+ lua_pop,
+ lua_pushboolean,
+ lua_pushcclosure,
+ lua_pushcfunction,
+ lua_pushfstring,
+ lua_pushglobaltable,
+ lua_pushlightuserdata,
+ lua_pushliteral,
+ lua_pushlstring,
+ lua_pushnil,
+ lua_pushstring,
+ lua_pushvalue,
+ lua_rawgeti,
+ lua_rawgetp,
+ lua_rawseti,
+ lua_rawsetp,
+ lua_remove,
+ lua_setfield,
+ lua_setmetatable,
+ lua_settop,
+ lua_toboolean,
+ lua_tostring,
+ lua_touserdata,
+ lua_upvalueindex
+} = require('./lua.js');
+const {
+ LUA_LOADED_TABLE,
+ LUA_PRELOAD_TABLE,
+ luaL_Buffer,
+ luaL_addvalue,
+ luaL_buffinit,
+ luaL_checkstring,
+ luaL_error,
+ luaL_getsubtable,
+ luaL_gsub,
+ luaL_len,
+ luaL_loadfile,
+ luaL_newlib,
+ luaL_optstring,
+ luaL_pushresult,
+ luaL_setfuncs
+} = require('./lauxlib.js');
+const {
+ luastring_indexOf,
+ to_jsstring,
+ to_luastring,
+ to_uristring
+} = require("./fengaricore.js");
const fengari = require('./fengari.js');
-const lua = require('./lua.js');
-const lauxlib = require('./lauxlib.js');
-const {luastring_indexOf, to_jsstring, to_luastring, to_uristring} = require("./fengaricore.js");
const global_env = (function() {
/* global WorkerGlobalScope */ /* see https://github.com/sindresorhus/globals/issues/127 */
@@ -34,8 +99,8 @@ const LUA_IGMARK = "-";
** 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;
+const LUA_CSUBSEP = LUA_DIRSEP;
+const LUA_LSUBSEP = LUA_DIRSEP;
/* prefix for open functions in C libraries */
const LUA_POF = to_luastring("luaopen_");
@@ -62,7 +127,7 @@ if (typeof process === "undefined") {
xhr.send();
if (xhr.status < 200 || xhr.status >= 300) {
- lua.lua_pushstring(L, to_luastring(`${xhr.status}: ${xhr.statusText}`));
+ lua_pushstring(L, to_luastring(`${xhr.status}: ${xhr.statusText}`));
return null;
}
@@ -74,7 +139,7 @@ if (typeof process === "undefined") {
try {
func = Function("fengari", code);
} catch (e) {
- lua.lua_pushstring(L, to_luastring(`${e.name}: ${e.message}`));
+ lua_pushstring(L, to_luastring(`${e.name}: ${e.message}`));
return null;
}
let res = func(fengari);
@@ -83,7 +148,7 @@ if (typeof process === "undefined") {
} else if (res === void 0) { /* assume library added symbols to global environment */
return global_env;
} else {
- lua.lua_pushstring(L, to_luastring(`library returned unexpected type (${typeof res})`));
+ lua_pushstring(L, to_luastring(`library returned unexpected type (${typeof res})`));
return null;
}
};
@@ -96,7 +161,7 @@ if (typeof process === "undefined") {
try {
return require(path);
} catch (e) {
- lua.lua_pushstring(L, to_luastring(e.message));
+ lua_pushstring(L, to_luastring(e.message));
return null;
}
};
@@ -113,7 +178,7 @@ const lsys_sym = function(L, lib, sym) {
if (f && typeof f === 'function')
return f;
else {
- lua.lua_pushfstring(L, to_luastring("undefined symbol: %s"), sym);
+ lua_pushfstring(L, to_luastring("undefined symbol: %s"), sym);
return null;
}
};
@@ -122,9 +187,9 @@ const lsys_sym = function(L, lib, sym) {
** return registry.LUA_NOENV as a boolean
*/
const noenv = function(L) {
- lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, to_luastring("LUA_NOENV"));
- let b = lua.lua_toboolean(L, -1);
- lua.lua_pop(L, 1); /* remove value */
+ lua_getfield(L, LUA_REGISTRYINDEX, to_luastring("LUA_NOENV"));
+ let b = lua_toboolean(L, -1);
+ lua_pop(L, 1); /* remove value */
return b;
};
@@ -178,28 +243,28 @@ const lookforfunc = function(L, path, sym) {
addtoclib(L, path, reg);
}
if (sym[0] === '*'.charCodeAt(0)) { /* loading only library (no function)? */
- lua.lua_pushboolean(L, 1); /* return 'true' */
+ 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 */
+ lua_pushcfunction(L, f); /* else create new function */
return 0; /* no errors */
}
};
const ll_loadlib = function(L) {
- let path = lauxlib.luaL_checkstring(L, 1);
- let init = lauxlib.luaL_checkstring(L, 2);
+ let path = luaL_checkstring(L, 1);
+ let init = luaL_checkstring(L, 2);
let stat = lookforfunc(L, path, init);
if (stat === 0) /* no errors? */
return 1; /* return the loaded function */
else { /* error; error message is on stack top */
- lua.lua_pushnil(L);
- lua.lua_insert(L, -2);
- lua.lua_pushliteral(L, (stat === ERRLIB) ? LIB_FAIL : "init");
+ lua_pushnil(L);
+ lua_insert(L, -2);
+ lua_pushliteral(L, (stat === ERRLIB) ? LIB_FAIL : "init");
return 3; /* return nil, error message, and where */
}
};
@@ -217,36 +282,36 @@ const env = (function() {
** Set a path
*/
const setpath = function(L, fieldname, envname, dft) {
- let nver = `${envname}${lua.LUA_VERSUFFIX}`;
- lua.lua_pushstring(L, to_luastring(nver));
+ let nver = `${envname}${LUA_VERSUFFIX}`;
+ lua_pushstring(L, to_luastring(nver));
let path = env[nver]; /* use versioned name */
if (path === undefined) /* no environment variable? */
path = env[envname]; /* try unversioned name */
if (path === undefined || noenv(L)) /* no environment variable? */
- lua.lua_pushstring(L, to_luastring(dft)); /* use default */
+ lua_pushstring(L, to_luastring(dft)); /* use default */
else {
/* replace ";;" by ";AUXMARK;" and then AUXMARK by default path */
- path = lauxlib.luaL_gsub(
+ path = luaL_gsub(
L,
to_luastring(path),
- to_luastring(lua.LUA_PATH_SEP + lua.LUA_PATH_SEP, true),
- to_luastring(lua.LUA_PATH_SEP + to_jsstring(AUXMARK) + lua.LUA_PATH_SEP, true)
+ to_luastring(LUA_PATH_SEP + LUA_PATH_SEP, true),
+ to_luastring(LUA_PATH_SEP + to_jsstring(AUXMARK) + LUA_PATH_SEP, true)
);
- lauxlib.luaL_gsub(L, path, AUXMARK, to_luastring(dft));
- lua.lua_remove(L, -2); /* remove result from 1st 'gsub' */
+ luaL_gsub(L, path, AUXMARK, to_luastring(dft));
+ 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 */
+ lua_setfield(L, -3, fieldname); /* package[fieldname] = path value */
+ 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' */
+ lua_rawgetp(L, LUA_REGISTRYINDEX, CLIBS);
+ lua_getfield(L, -1, path);
+ let plib = lua_touserdata(L, -1); /* plib = CLIBS[path] */
+ lua_pop(L, 2); /* pop CLIBS table and 'plib' */
return plib;
};
@@ -255,79 +320,79 @@ const checkclib = function(L, path) {
** 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 */
+ lua_rawgetp(L, LUA_REGISTRYINDEX, CLIBS);
+ lua_pushlightuserdata(L, plib);
+ lua_pushvalue(L, -1);
+ lua_setfield(L, -3, path); /* CLIBS[path] = plib */
+ lua_rawseti(L, -2, luaL_len(L, -2) + 1); /* CLIBS[#CLIBS + 1] = plib */
+ lua_pop(L, 1); /* pop CLIBS table */
};
const pushnexttemplate = function(L, path) {
- while (path[0] === lua.LUA_PATH_SEP.charCodeAt(0)) path = path.subarray(1); /* skip separators */
+ while (path[0] === LUA_PATH_SEP.charCodeAt(0)) path = path.subarray(1); /* skip separators */
if (path.length === 0) return null; /* no more templates */
- let l = luastring_indexOf(path, lua.LUA_PATH_SEP.charCodeAt(0)); /* find next separator */
+ let l = luastring_indexOf(path, LUA_PATH_SEP.charCodeAt(0)); /* find next separator */
if (l < 0) l = path.length;
- lua.lua_pushlstring(L, path, l); /* template */
+ lua_pushlstring(L, path, l); /* template */
return path.subarray(l);
};
const searchpath = function(L, name, path, sep, dirsep) {
- let msg = new lauxlib.luaL_Buffer(); /* to build error message */
- lauxlib.luaL_buffinit(L, msg);
+ let msg = new luaL_Buffer(); /* to build error message */
+ luaL_buffinit(L, msg);
if (sep[0] !== 0) /* non-empty separator? */
- name = lauxlib.luaL_gsub(L, name, sep, dirsep); /* replace it by 'dirsep' */
+ name = 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), to_luastring(lua.LUA_PATH_MARK, true), name);
- lua.lua_remove(L, -2); /* remove path template */
+ let filename = luaL_gsub(L, lua_tostring(L, -1), to_luastring(LUA_PATH_MARK, true), name);
+ lua_remove(L, -2); /* remove path template */
if (readable(filename)) /* does file exist and is readable? */
return filename; /* return that file name */
- lua.lua_pushfstring(L, to_luastring("\n\tno file '%s'"), filename);
- lua.lua_remove(L, -2); /* remove file name */
- lauxlib.luaL_addvalue(msg);
+ lua_pushfstring(L, to_luastring("\n\tno file '%s'"), filename);
+ lua_remove(L, -2); /* remove file name */
+ luaL_addvalue(msg);
}
- lauxlib.luaL_pushresult(msg); /* create error message */
+ luaL_pushresult(msg); /* create error message */
return null; /* not found */
};
const ll_searchpath = function(L) {
let f = searchpath(
L,
- lauxlib.luaL_checkstring(L, 1),
- lauxlib.luaL_checkstring(L, 2),
- lauxlib.luaL_optstring(L, 3, to_luastring(".")),
- lauxlib.luaL_optstring(L, 4, to_luastring(lua.LUA_DIRSEP))
+ luaL_checkstring(L, 1),
+ luaL_checkstring(L, 2),
+ luaL_optstring(L, 3, to_luastring(".")),
+ luaL_optstring(L, 4, to_luastring(LUA_DIRSEP))
);
if (f !== null) return 1;
else { /* error message is on top of the stack */
- lua.lua_pushnil(L);
- lua.lua_insert(L, -2);
+ lua_pushnil(L);
+ lua_insert(L, -2);
return 2; /* return nil + error message */
}
};
const findfile = function(L, name, pname, dirsep) {
- lua.lua_getfield(L, lua.lua_upvalueindex(1), pname);
- let path = lua.lua_tostring(L, -1);
+ lua_getfield(L, lua_upvalueindex(1), pname);
+ let path = lua_tostring(L, -1);
if (path === null)
- lauxlib.luaL_error(L, to_luastring("'package.%s' must be a string"), pname);
+ luaL_error(L, to_luastring("'package.%s' must be a string"), pname);
return searchpath(L, name, path, to_luastring("."), dirsep);
};
const checkload = function(L, stat, filename) {
if (stat) { /* module loaded successfully? */
- lua.lua_pushstring(L, filename); /* will be 2nd argument to module */
+ lua_pushstring(L, filename); /* will be 2nd argument to module */
return 2; /* return open function and file name */
} else
- return lauxlib.luaL_error(L, to_luastring("error loading module '%s' from file '%s':\n\t%s"),
- lua.lua_tostring(L, 1), filename, lua.lua_tostring(L, -1));
+ return luaL_error(L, to_luastring("error loading module '%s' from file '%s':\n\t%s"),
+ lua_tostring(L, 1), filename, lua_tostring(L, -1));
};
const searcher_Lua = function(L) {
- let name = lauxlib.luaL_checkstring(L, 1);
+ let name = luaL_checkstring(L, 1);
let filename = findfile(L, name, to_luastring("path", true), to_luastring(LUA_LSUBSEP, true));
if (filename === null) return 1; /* module not found in this path */
- return checkload(L, lauxlib.luaL_loadfile(L, filename) === lua.LUA_OK, filename);
+ return checkload(L, luaL_loadfile(L, filename) === LUA_OK, filename);
};
/*
@@ -340,119 +405,119 @@ const searcher_Lua = function(L) {
*/
const loadfunc = function(L, filename, modname) {
let openfunc;
- modname = lauxlib.luaL_gsub(L, modname, to_luastring("."), LUA_OFSEP);
+ modname = luaL_gsub(L, modname, to_luastring("."), LUA_OFSEP);
let mark = luastring_indexOf(modname, LUA_IGMARK.charCodeAt(0));
if (mark >= 0) {
- openfunc = lua.lua_pushlstring(L, modname, mark);
- openfunc = lua.lua_pushfstring(L, to_luastring("%s%s"), LUA_POF, openfunc);
+ openfunc = lua_pushlstring(L, modname, mark);
+ openfunc = lua_pushfstring(L, to_luastring("%s%s"), 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_pushfstring(L, to_luastring("%s%s"), LUA_POF, modname);
+ openfunc = lua_pushfstring(L, to_luastring("%s%s"), LUA_POF, modname);
return lookforfunc(L, filename, openfunc);
};
const searcher_C = function(L) {
- let name = lauxlib.luaL_checkstring(L, 1);
+ let name = luaL_checkstring(L, 1);
let filename = findfile(L, name, to_luastring("cpath", true), to_luastring(LUA_CSUBSEP, true));
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 name = luaL_checkstring(L, 1);
let p = luastring_indexOf(name, '.'.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), to_luastring("cpath", true), to_luastring(LUA_CSUBSEP, true));
+ lua_pushlstring(L, name, p);
+ let filename = findfile(L, lua_tostring(L, -1), to_luastring("cpath", true), to_luastring(LUA_CSUBSEP, true));
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, to_luastring("\n\tno module '%s' in file '%s'"), name, filename);
+ lua_pushstring(L, to_luastring("\n\tno module '%s' in file '%s'"), name, filename);
return 1;
}
}
- lua.lua_pushstring(L, filename); /* will be 2nd argument to module */
+ 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_pushfstring(L, to_luastring("\n\tno field package.preload['%s']"), name);
+ let name = luaL_checkstring(L, 1);
+ lua_getfield(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
+ if (lua_getfield(L, -1, name) === LUA_TNIL) /* not found? */
+ lua_pushfstring(L, to_luastring("\n\tno field package.preload['%s']"), name);
return 1;
};
const findloader = function(L, name, ctx, k) {
- let msg = new lauxlib.luaL_Buffer(); /* to build error message */
- lauxlib.luaL_buffinit(L, msg);
+ let msg = new luaL_Buffer(); /* to build error message */
+ luaL_buffinit(L, msg);
/* push 'package.searchers' to index 3 in the stack */
- if (lua.lua_getfield(L, lua.lua_upvalueindex(1), to_luastring("searchers", true)) !== lua.LUA_TTABLE)
- lauxlib.luaL_error(L, to_luastring("'package.searchers' must be a table"));
+ if (lua_getfield(L, lua_upvalueindex(1), to_luastring("searchers", true)) !== LUA_TTABLE)
+ luaL_error(L, to_luastring("'package.searchers' must be a table"));
let ctx2 = {name: name, i: 1, msg: msg, ctx: ctx, k: k};
- return findloader_cont(L, lua.LUA_OK, ctx2);
+ return findloader_cont(L, LUA_OK, ctx2);
};
const findloader_cont = function(L, status, ctx) {
/* iterate over available searchers to find a loader */
for (; ; ctx.i++) {
- if (status === lua.LUA_OK) {
- if (lua.lua_rawgeti(L, 3, ctx.i) === lua.LUA_TNIL) { /* no more searchers? */
- lua.lua_pop(L, 1); /* remove nil */
- lauxlib.luaL_pushresult(ctx.msg); /* create error message */
- lauxlib.luaL_error(L, to_luastring("module '%s' not found:%s"), ctx.name, lua.lua_tostring(L, -1));
+ if (status === LUA_OK) {
+ if (lua_rawgeti(L, 3, ctx.i) === LUA_TNIL) { /* no more searchers? */
+ lua_pop(L, 1); /* remove nil */
+ luaL_pushresult(ctx.msg); /* create error message */
+ luaL_error(L, to_luastring("module '%s' not found:%s"), ctx.name, lua_tostring(L, -1));
}
- lua.lua_pushstring(L, ctx.name);
- lua.lua_callk(L, 1, 2, ctx, findloader_cont); /* call it */
+ lua_pushstring(L, ctx.name);
+ lua_callk(L, 1, 2, ctx, findloader_cont); /* call it */
} else {
- status = lua.LUA_OK;
+ status = LUA_OK;
}
- if (lua.lua_isfunction(L, -2)) /* did it find a loader? */
+ if (lua_isfunction(L, -2)) /* did it find a loader? */
break; /* module loader found */
- else if (lua.lua_isstring(L, -2)) { /* searcher returned error message? */
- lua.lua_pop(L, 1); /* remove extra return */
- lauxlib.luaL_addvalue(ctx.msg); /* concatenate error message */
+ else if (lua_isstring(L, -2)) { /* searcher returned error message? */
+ lua_pop(L, 1); /* remove extra return */
+ luaL_addvalue(ctx.msg); /* concatenate error message */
}
else
- lua.lua_pop(L, 2); /* remove both returns */
+ lua_pop(L, 2); /* remove both returns */
}
- return ctx.k(L, lua.LUA_OK, ctx.ctx);
+ return ctx.k(L, LUA_OK, ctx.ctx);
};
const ll_require = function(L) {
- let name = lauxlib.luaL_checkstring(L, 1);
- lua.lua_settop(L, 1); /* LOADED table will be at index 2 */
- lua.lua_getfield(L, lua.LUA_REGISTRYINDEX, lauxlib.LUA_LOADED_TABLE);
- lua.lua_getfield(L, 2, name); /* LOADED[name] */
- if (lua.lua_toboolean(L, -1)) /* is it there? */
+ let name = luaL_checkstring(L, 1);
+ lua_settop(L, 1); /* LOADED table will be at index 2 */
+ lua_getfield(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
+ lua_getfield(L, 2, name); /* LOADED[name] */
+ if (lua_toboolean(L, -1)) /* is it there? */
return 1; /* package is already loaded */
/* else must load package */
- lua.lua_pop(L, 1); /* remove 'getfield' result */
+ lua_pop(L, 1); /* remove 'getfield' result */
let ctx = name;
return findloader(L, name, ctx, ll_require_cont);
};
const ll_require_cont = function(L, status, ctx) {
let name = ctx;
- lua.lua_pushstring(L, name); /* pass name as argument to module loader */
- lua.lua_insert(L, -2); /* name is 1st argument (before search data) */
- lua.lua_callk(L, 2, 1, ctx, ll_require_cont2);
- return ll_require_cont2(L, lua.LUA_OK, ctx); /* run loader to load module */
+ lua_pushstring(L, name); /* pass name as argument to module loader */
+ lua_insert(L, -2); /* name is 1st argument (before search data) */
+ lua_callk(L, 2, 1, ctx, ll_require_cont2);
+ return ll_require_cont2(L, LUA_OK, ctx); /* run loader to load module */
};
const ll_require_cont2 = function(L, status, ctx) {
let name = ctx;
- if (!lua.lua_isnil(L, -1)) /* non-nil return? */
- lua.lua_setfield(L, 2, name); /* LOADED[name] = returned value */
- if (lua.lua_getfield(L, 2, name) == lua.LUA_TNIL) { /* module set no value? */
- lua.lua_pushboolean(L, 1); /* use true as result */
- lua.lua_pushvalue(L, -1); /* extra copy to be returned */
- lua.lua_setfield(L, 2, name); /* LOADED[name] = true */
+ if (!lua_isnil(L, -1)) /* non-nil return? */
+ lua_setfield(L, 2, name); /* LOADED[name] = returned value */
+ if (lua_getfield(L, 2, name) == LUA_TNIL) { /* module set no value? */
+ lua_pushboolean(L, 1); /* use true as result */
+ lua_pushvalue(L, -1); /* extra copy to be returned */
+ lua_setfield(L, 2, name); /* LOADED[name] = true */
}
return 1;
};
@@ -469,14 +534,14 @@ 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);
+ 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_pushvalue(L, -2); /* set 'package' as upvalue for all searchers */
+ lua_pushcclosure(L, searchers[i], 1);
+ lua_rawseti(L, -2, i+1);
}
- lua.lua_setfield(L, -2, to_luastring("searchers", true)); /* put it in field 'searchers' */
+ lua_setfield(L, -2, to_luastring("searchers", true)); /* put it in field 'searchers' */
};
/*
@@ -484,33 +549,33 @@ const createsearcherstable = function(L) {
** 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 */
+ lua_newtable(L); /* create CLIBS table */
+ lua_createtable(L, 0, 1); /* create metatable for CLIBS */
+ lua_setmetatable(L, -2);
+ lua_rawsetp(L, LUA_REGISTRYINDEX, CLIBS); /* set CLIBS table in registry */
};
const luaopen_package = function(L) {
createclibstable(L);
- lauxlib.luaL_newlib(L, pk_funcs); /* create 'package' table */
+ luaL_newlib(L, pk_funcs); /* create 'package' table */
createsearcherstable(L);
/* set paths */
- setpath(L, to_luastring("path", true), LUA_PATH_VAR, lua.LUA_PATH_DEFAULT);
- setpath(L, to_luastring("cpath", true), LUA_CPATH_VAR, lua.LUA_CPATH_DEFAULT);
+ setpath(L, to_luastring("path", true), LUA_PATH_VAR, LUA_PATH_DEFAULT);
+ setpath(L, to_luastring("cpath", true), LUA_CPATH_VAR, 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, to_luastring("config", true));
+ lua_pushliteral(L, LUA_DIRSEP + "\n" + LUA_PATH_SEP + "\n" + LUA_PATH_MARK + "\n" +
+ LUA_EXEC_DIR + "\n" + LUA_IGMARK + "\n");
+ lua_setfield(L, -2, to_luastring("config", true));
/* set field 'loaded' */
- lauxlib.luaL_getsubtable(L, lua.LUA_REGISTRYINDEX, lauxlib.LUA_LOADED_TABLE);
- lua.lua_setfield(L, -2, to_luastring("loaded", true));
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
+ lua_setfield(L, -2, to_luastring("loaded", true));
/* set field 'preload' */
- lauxlib.luaL_getsubtable(L, lua.LUA_REGISTRYINDEX, lauxlib.LUA_PRELOAD_TABLE);
- lua.lua_setfield(L, -2, 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 */
+ luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_PRELOAD_TABLE);
+ lua_setfield(L, -2, to_luastring("preload", true));
+ lua_pushglobaltable(L);
+ lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
+ luaL_setfuncs(L, ll_funcs, 1); /* open lib into global table */
+ lua_pop(L, 1); /* pop global table */
return 1; /* return 'package' table */
};