summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-07 02:52:40 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-07 03:34:56 +1100
commite46d3e7d8d7226d49aad05f5a9c639c77d53bb04 (patch)
treefe3e99cbfce31d7d3495c0f861cbdc0aad67167a
parent6ffe07fc5d16ee9acdcd6651d433ce13b193cd15 (diff)
downloadfengari-e46d3e7d8d7226d49aad05f5a9c639c77d53bb04.tar.gz
fengari-e46d3e7d8d7226d49aad05f5a9c639c77d53bb04.tar.bz2
fengari-e46d3e7d8d7226d49aad05f5a9c639c77d53bb04.zip
Introduce luastring_indexOf as Uint8Array.indexOf doesn't exist everywhere
-rw-r--r--src/defs.js15
-rw-r--r--src/ldblib.js20
-rw-r--r--src/ldebug.js4
-rw-r--r--src/ldo.js2
-rw-r--r--src/loadlib.js6
-rw-r--r--src/lobject.js6
-rw-r--r--src/loslib.js2
-rw-r--r--src/lstrlib.js16
-rw-r--r--src/lua.js1
-rw-r--r--tests/test-suite/ltests.js6
10 files changed, 47 insertions, 31 deletions
diff --git a/src/defs.js b/src/defs.js
index c0b3bda..fc18707 100644
--- a/src/defs.js
+++ b/src/defs.js
@@ -144,6 +144,20 @@ if (typeof Uint8Array.from === "function") {
};
}
+let luastring_indexOf;
+if (typeof (new Uint8Array().indexOf) === "function") {
+ luastring_indexOf = function(s, v, i) {
+ return s.indexOf(v, i);
+ };
+} else {
+ /* Browsers that don't support Uint8Array.indexOf seem to allow using Array.indexOf on Uint8Array objects e.g. IE11 */
+ let array_indexOf = [].indexOf;
+ if (array_indexOf.call(new Uint8Array(1), 0) !== 0) throw Error("missing .indexOf");
+ luastring_indexOf = function(s, v, i) {
+ return array_indexOf.call(s, v, i);
+ };
+}
+
let luastring_of;
if (typeof Uint8Array.of === "function") {
luastring_of = Uint8Array.of.bind(Uint8Array);
@@ -482,6 +496,7 @@ module.exports.thread_status = thread_status;
module.exports.is_luastring = is_luastring;
module.exports.luastring_eq = luastring_eq;
module.exports.luastring_from = luastring_from;
+module.exports.luastring_indexOf = luastring_indexOf;
module.exports.luastring_of = luastring_of;
module.exports.to_jsstring = to_jsstring;
module.exports.to_luastring = to_luastring;
diff --git a/src/ldblib.js b/src/ldblib.js
index e687ec1..7e08dc5 100644
--- a/src/ldblib.js
+++ b/src/ldblib.js
@@ -136,29 +136,29 @@ const db_getinfo = function(L) {
if (!lua.lua_getinfo(L1, options, ar))
lauxlib.luaL_argerror(L, arg + 2, lua.to_luastring("invalid option", true));
lua.lua_newtable(L); /* table to collect results */
- if (options.indexOf('S'.charCodeAt(0)) > -1) {
+ if (lua.luastring_indexOf(options, 'S'.charCodeAt(0)) > -1) {
settabss(L, lua.to_luastring("source", true), ar.source);
settabss(L, lua.to_luastring("short_src", true), ar.short_src);
settabsi(L, lua.to_luastring("linedefined", true), ar.linedefined);
settabsi(L, lua.to_luastring("lastlinedefined", true), ar.lastlinedefined);
settabss(L, lua.to_luastring("what", true), ar.what);
}
- if (options.indexOf('l'.charCodeAt(0)) > -1)
+ if (lua.luastring_indexOf(options, 'l'.charCodeAt(0)) > -1)
settabsi(L, lua.to_luastring("currentline", true), ar.currentline);
- if (options.indexOf('u'.charCodeAt(0)) > -1) {
+ if (lua.luastring_indexOf(options, 'u'.charCodeAt(0)) > -1) {
settabsi(L, lua.to_luastring("nups", true), ar.nups);
settabsi(L, lua.to_luastring("nparams", true), ar.nparams);
settabsb(L, lua.to_luastring("isvararg", true), ar.isvararg);
}
- if (options.indexOf('n'.charCodeAt(0)) > - 1) {
+ if (lua.luastring_indexOf(options, 'n'.charCodeAt(0)) > -1) {
settabss(L, lua.to_luastring("name", true), ar.name);
settabss(L, lua.to_luastring("namewhat", true), ar.namewhat);
}
- if (options.indexOf('t'.charCodeAt(0)) > - 1)
+ if (lua.luastring_indexOf(options, 't'.charCodeAt(0)) > -1)
settabsb(L, lua.to_luastring("istailcall", true), ar.istailcall);
- if (options.indexOf('L'.charCodeAt(0)) > - 1)
+ if (lua.luastring_indexOf(options, 'L'.charCodeAt(0)) > -1)
treatstackoption(L, L1, lua.to_luastring("activelines", true));
- if (options.indexOf('f'.charCodeAt(0)) > - 1)
+ if (lua.luastring_indexOf(options, 'f'.charCodeAt(0)) > -1)
treatstackoption(L, L1, lua.to_luastring("func", true));
return 1; /* return table */
};
@@ -291,9 +291,9 @@ const hookf = function(L, ar) {
*/
const makemask = function(smask, count) {
let mask = 0;
- if (smask.indexOf("c".charCodeAt(0)) > -1) mask |= lua.LUA_MASKCALL;
- if (smask.indexOf("r".charCodeAt(0)) > -1) mask |= lua.LUA_MASKRET;
- if (smask.indexOf("l".charCodeAt(0)) > -1) mask |= lua.LUA_MASKLINE;
+ if (lua.luastring_indexOf(smask, "c".charCodeAt(0)) > -1) mask |= lua.LUA_MASKCALL;
+ if (lua.luastring_indexOf(smask, "r".charCodeAt(0)) > -1) mask |= lua.LUA_MASKRET;
+ if (lua.luastring_indexOf(smask, "l".charCodeAt(0)) > -1) mask |= lua.LUA_MASKLINE;
if (count > 0) mask |= lua.LUA_MASKCOUNT;
return mask;
};
diff --git a/src/ldebug.js b/src/ldebug.js
index 3d722e1..d7a58e0 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -283,13 +283,13 @@ const lua_getinfo = function(L, what, ar) {
cl = func.ttisclosure() ? func.value : null;
status = auxgetinfo(L, what, ar, cl, ci);
- if (what.indexOf('f'.charCodeAt(0)) >= 0) {
+ if (defs.luastring_indexOf(what, 'f'.charCodeAt(0)) >= 0) {
lobject.pushobj2s(L, func);
assert(L.top <= L.ci.top, "stack overflow");
}
swapextra(L);
- if (what.indexOf('L'.charCodeAt(0)) >= 0)
+ if (defs.luastring_indexOf(what, 'L'.charCodeAt(0)) >= 0)
collectvalidlines(L, cl);
return status;
diff --git a/src/ldo.js b/src/ldo.js
index 3adedd4..88608ce 100644
--- a/src/ldo.js
+++ b/src/ldo.js
@@ -675,7 +675,7 @@ class SParser {
}
const checkmode = function(L, mode, x) {
- if (mode && mode.indexOf(x[0]) === -1) {
+ if (mode && defs.luastring_indexOf(mode, x[0]) === -1) {
lobject.luaO_pushfstring(L,
defs.to_luastring("attempt to load a %s chunk (mode is '%s')"), x, mode);
luaD_throw(L, TS.LUA_ERRSYNTAX);
diff --git a/src/loadlib.js b/src/loadlib.js
index 0be1742..c859798 100644
--- a/src/loadlib.js
+++ b/src/loadlib.js
@@ -265,7 +265,7 @@ const addtoclib = function(L, path, plib) {
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 */
+ let l = lua.luastring_indexOf(path, lua.LUA_PATH_SEP.charCodeAt(0)); /* find next separator */
if (l < 0) l = path.length;
lua.lua_pushlstring(L, path, l); /* template */
return path.slice(l);
@@ -340,7 +340,7 @@ const searcher_Lua = function(L) {
const loadfunc = function(L, filename, modname) {
let openfunc;
modname = lauxlib.luaL_gsub(L, modname, lua.to_luastring("."), LUA_OFSEP);
- let mark = modname.indexOf(LUA_IGMARK.charCodeAt(0));
+ let mark = lua.luastring_indexOf(modname, LUA_IGMARK.charCodeAt(0));
if (mark >= 0) {
openfunc = lua.lua_pushlstring(L, modname, mark);
openfunc = lua.lua_pushfstring(L, lua.to_luastring("%s%s"), LUA_POF, openfunc);
@@ -361,7 +361,7 @@ const searcher_C = function(L) {
const searcher_Croot = function(L) {
let name = lauxlib.luaL_checkstring(L, 1);
- let p = name.indexOf('.'.charCodeAt(0));
+ let p = lua.luastring_indexOf(name, '.'.charCodeAt(0));
let stat;
if (p < 0) return 0; /* is root */
lua.lua_pushlstring(L, name, p);
diff --git a/src/lobject.js b/src/lobject.js
index b5d4a96..384facd 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -318,7 +318,7 @@ const luaO_chunkid = function(source, bufflen) {
}
} else { /* string; format as [string "source"] */
out = new Uint8Array(bufflen);
- let nli = source.indexOf(char['\n']); /* find first new line (if any) */
+ let nli = defs.luastring_indexOf(source, char['\n']); /* find first new line (if any) */
out.set(PRE); /* add prefix */
let out_i = PRE.length;
bufflen -= PRE.length + RETS.length + POS.length; /* save space for prefix+suffix */
@@ -539,7 +539,7 @@ const luaO_pushvfstring = function(L, fmt, argp) {
let a = 0;
let e;
for (;;) {
- e = fmt.indexOf(char['%'], i);
+ e = defs.luastring_indexOf(fmt, char['%'], i);
if (e == -1) break;
pushstr(L, fmt.subarray(i, e));
switch(fmt[e+1]) {
@@ -549,7 +549,7 @@ const luaO_pushvfstring = function(L, fmt, argp) {
else {
s = defs.from_userstring(s);
/* respect null terminator */
- let i = s.indexOf(0);
+ let i = defs.luastring_indexOf(s, 0);
if (i !== -1)
s = s.subarray(0, i);
}
diff --git a/src/loslib.js b/src/loslib.js
index ecd42f1..ee9ddca 100644
--- a/src/loslib.js
+++ b/src/loslib.js
@@ -112,7 +112,7 @@ const os_date = function(L) {
} else {
i++; /* skip '%' */
i = checkoption(L, s, i, cc.subarray(1)); /* copy specifier to 'cc' */
- let len = cc.indexOf(0);
+ let len = lua.luastring_indexOf(cc, 0);
if (len !== -1)
cc = cc.subarray(0, len);
let buff = strftime(lua.to_jsstring(cc), stm);
diff --git a/src/lstrlib.js b/src/lstrlib.js
index bd26eba..73dcfb7 100644
--- a/src/lstrlib.js
+++ b/src/lstrlib.js
@@ -22,7 +22,7 @@ const MAXSIZE = 2147483647;
/* Give natural (i.e. strings end at the first \0) length of a string represented by an array of bytes */
const strlen = function(s) {
- let len = s.indexOf(0);
+ let len = lua.luastring_indexOf(s, 0);
return len > -1 ? len : s.length;
};
@@ -181,9 +181,9 @@ const addquoted = function(b, s, len) {
** Ensures the 'buff' string uses a dot as the radix character.
*/
const checkdp = function(buff) {
- if (buff.indexOf('.'.charCodeAt(0)) < 0) { /* no dot? */
+ if (lua.luastring_indexOf(buff, '.'.charCodeAt(0)) < 0) { /* no dot? */
let point = luaconf.lua_getlocaledecpoint().charCodeAt(0); /* try locale point */
- let ppoint = buff.indexOf(point);
+ let ppoint = lua.luastring_indexOf(buff, point);
if (ppoint) buff[ppoint] = '.'; /* change it to a dot */
}
};
@@ -224,7 +224,7 @@ const addliteral = function(L, b, arg) {
const scanformat = function(L, strfrmt, i, form) {
let p = i;
- while (strfrmt[p] !== 0 && FLAGS.indexOf(strfrmt[p]) >= 0) p++; /* skip flags */
+ while (strfrmt[p] !== 0 && lua.luastring_indexOf(FLAGS, strfrmt[p]) >= 0) p++; /* skip flags */
if (p - i >= FLAGS.length)
lauxlib.luaL_error(L, lua.to_luastring("invalid format (repeated flags)", true));
if (isdigit(strfrmt[p])) p++; /* skip width */
@@ -307,7 +307,7 @@ const str_format = function(L) {
lauxlib.luaL_addvalue(b); /* keep entire string */
} else {
lauxlib.luaL_argcheck(L, s.length === strlen(s), arg, lua.to_luastring("string contains zeros", true));
- if (form.indexOf('.'.charCodeAt(0)) < 0 && s.length >= 100) {
+ if (lua.luastring_indexOf(form, '.'.charCodeAt(0)) < 0 && s.length >= 100) {
/* no precision and string is too long to be formatted */
lauxlib.luaL_addvalue(b); /* keep entire string */
} else { /* format the string into 'buff' */
@@ -578,7 +578,7 @@ const str_pack = function(L) {
case KOption.Kzstr: { /* zero-terminated string */
let s = lauxlib.luaL_checkstring(L, arg);
let len = s.length;
- lauxlib.luaL_argcheck(L, s.indexOf(0) < 0, arg, lua.to_luastring("strings contains zeros", true));
+ lauxlib.luaL_argcheck(L, lua.luastring_indexOf(s, 0) < 0, arg, lua.to_luastring("strings contains zeros", true));
lauxlib.luaL_addlstring(b, s, len);
lauxlib.luaL_addchar(b, 0); /* add zero at the end */
totalsize += len + 1;
@@ -774,7 +774,7 @@ const str_unpack = function(L) {
break;
}
case KOption.Kzstr: {
- let len = data.slice(pos).indexOf(0);
+ let len = lua.luastring_indexOf(data.slice(pos), 0);
lua.lua_pushstring(L, data.slice(pos, pos + len));
pos += len + 1; /* skip string plus final '\0' */
break;
@@ -969,7 +969,7 @@ const array_cmp = function(a, ai, b, bi, len) {
return true;
let aj = ai+len;
loop: for (;;) {
- ai = a.indexOf(b[bi], ai);
+ ai = lua.luastring_indexOf(a, b[bi], ai);
if (ai === -1 || ai >= aj)
return false;
for (let j = 1; j < len; j++) {
diff --git a/src/lua.js b/src/lua.js
index ccdafd1..a5b290c 100644
--- a/src/lua.js
+++ b/src/lua.js
@@ -79,6 +79,7 @@ module.exports.LUA_YIELD = defs.thread_status.LUA_YIELD;
module.exports.lua_Debug = defs.lua_Debug;
module.exports.lua_upvalueindex = defs.lua_upvalueindex;
module.exports.luastring_eq = defs.luastring_eq;
+module.exports.luastring_indexOf = defs.luastring_indexOf;
module.exports.luastring_of = defs.luastring_of;
module.exports.to_jsstring = defs.to_jsstring;
module.exports.to_luastring = defs.to_luastring;
diff --git a/tests/test-suite/ltests.js b/tests/test-suite/ltests.js
index 08bb603..5847746 100644
--- a/tests/test-suite/ltests.js
+++ b/tests/test-suite/ltests.js
@@ -721,9 +721,9 @@ const sethook = function(L) {
const smask = lauxlib.luaL_checkstring(L, 2);
let count = lauxlib.luaL_optinteger(L, 3, 0);
let mask = 0;
- if (smask.indexOf('c'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKCALL;
- if (smask.indexOf('r'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKRET;
- if (smask.indexOf('l'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKLINE;
+ if (lua.luastring_indexOf(smask, 'c'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKCALL;
+ if (lua.luastring_indexOf(smask, 'r'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKRET;
+ if (lua.luastring_indexOf(smask, 'l'.charCodeAt(0)) >= 0) mask |= lua.LUA_MASKLINE;
if (count > 0) mask |= lua.LUA_MASKCOUNT;
sethookaux(L, mask, count, scpt);
}