summaryrefslogtreecommitdiff
path: root/src/lauxlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann008@gmail.com>2017-05-02 15:01:54 +0200
committerBenoit Giannangeli <giann008@gmail.com>2017-05-03 13:28:11 +0200
commitd231ed14d0816eb26856e948a9ab72f64d46c1eb (patch)
tree4e85b5237878770195aedb362b6bd446f7f2596c /src/lauxlib.js
parent5ac211f983cd628dfb95a6add7e5386071f4dada (diff)
downloadfengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.tar.gz
fengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.tar.bz2
fengari-d231ed14d0816eb26856e948a9ab72f64d46c1eb.zip
loadlib.js
Diffstat (limited to 'src/lauxlib.js')
-rw-r--r--src/lauxlib.js34
1 files changed, 34 insertions, 0 deletions
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;