From 74dd47160960b3f0faa13dd1b61b64af69fde02e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Fri, 9 Jun 2017 13:01:21 +1000 Subject: Add is_luastring function instead of using Array.isArray directly --- src/defs.js | 9 +++++++-- src/lapi.js | 16 ++++++++-------- src/lstring.js | 2 +- src/lstrlib.js | 1 - src/lundump.js | 2 +- 5 files changed, 17 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/defs.js b/src/defs.js index 9e0fa5c..a125c34 100644 --- a/src/defs.js +++ b/src/defs.js @@ -132,8 +132,12 @@ class lua_Debug { } +const is_luastring = function(s) { + return Array.isArray(s); +}; + const to_jsstring = function(value, from, to) { - assert(Array.isArray(value), "jsstring expect a array of bytes"); + assert(is_luastring(value), "jsstring expect a array of bytes"); let u0, u1, u2, u3, u4, u5; let idx = 0; @@ -181,7 +185,7 @@ const to_luastring = function(str, cache) { if (cache) { let cached = to_luastring_cache[str]; - if (Array.isArray(cached)) return cached; + if (is_luastring(cached)) return cached; } let outU8Array = []; @@ -403,5 +407,6 @@ module.exports.constant_types = constant_types; module.exports.lua_Debug = lua_Debug; module.exports.lua_upvalueindex = lua_upvalueindex; module.exports.thread_status = thread_status; +module.exports.is_luastring = is_luastring; module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; diff --git a/src/lapi.js b/src/lapi.js index 4b5be04..1971cd7 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -236,7 +236,7 @@ const lua_pushlstring = function(L, s, len) { if (len === 0) { ts = lstring.luaS_bless(L, []); } else { - assert(Array.isArray(s), "lua_pushlstring expects array of byte"); + assert(defs.is_luastring(s), "lua_pushlstring expects array of byte"); ts = lstring.luaS_bless(L, s.slice(0, len)); } lobject.pushsvalue2s(L, ts); @@ -246,7 +246,7 @@ const lua_pushlstring = function(L, s, len) { }; const lua_pushstring = function (L, s) { - assert(Array.isArray(s) || s === undefined || s === null, "lua_pushstring expects array of byte"); + assert(defs.is_luastring(s) || s === undefined || s === null, "lua_pushstring expects array of byte"); if (s === undefined || s === null) { L.stack[L.top] = new TValue(CT.LUA_TNIL, null); @@ -262,12 +262,12 @@ const lua_pushstring = function (L, s) { }; const lua_pushvfstring = function (L, fmt, argp) { - assert(Array.isArray(fmt)); + assert(defs.is_luastring(fmt)); return lobject.luaO_pushvfstring(L, fmt, argp); }; const lua_pushfstring = function (L, fmt, ...argp) { - assert(Array.isArray(fmt)); + assert(defs.is_luastring(fmt)); return lobject.luaO_pushvfstring(L, fmt, argp); }; @@ -350,7 +350,7 @@ const lua_pushglobaltable = function(L) { ** t[k] = value at the top of the stack (where 'k' is a string) */ const auxsetstr = function(L, t, k) { - assert(Array.isArray(k), "key must be an array of bytes"); + assert(defs.is_luastring(k), "key must be an array of bytes"); let str = lstring.luaS_new(L, k); assert(1 < L.top - L.ci.funcOff, "not enough elements in the stack"); @@ -464,7 +464,7 @@ const lua_rawsetp = function(L, idx, p) { */ const auxgetstr = function(L, t, k) { - assert(Array.isArray(k), "key must be an array of bytes"); + assert(defs.is_luastring(k), "key must be an array of bytes"); let str = lstring.luaS_new(L, k); lobject.pushsvalue2s(L, str); assert(L.top <= L.ci.top, "stack overflow"); @@ -908,8 +908,8 @@ const lua_arith = function(L, op) { */ const lua_load = function(L, reader, data, chunkname, mode) { - assert(Array.isArray(chunkname), "lua_load expect an array of byte as chunkname"); - assert(mode ? Array.isArray(mode) : true, "lua_load expect an array of byte as mode"); + assert(defs.is_luastring(chunkname), "lua_load expect an array of byte as chunkname"); + assert(mode ? defs.is_luastring(mode) : true, "lua_load expect an array of byte as mode"); if (!chunkname) chunkname = [defs.char["?"]]; let z = new lzio.ZIO(L, reader, data); let status = ldo.luaD_protectedparser(L, z, chunkname, mode); diff --git a/src/lstring.js b/src/lstring.js index a5ee669..185582b 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -30,7 +30,7 @@ const luaS_eqlngstr = function(a, b) { /* converts strings (arrays) to a consistent map key make sure this doesn't conflict with any of the anti-collision strategies in ltable */ const luaS_hash = function(str) { - assert(Array.isArray(str)); + assert(defs.is_luastring(str)); return str.map(e => `${e}|`).join(''); }; diff --git a/src/lstrlib.js b/src/lstrlib.js index 56817a0..0fb8631 100644 --- a/src/lstrlib.js +++ b/src/lstrlib.js @@ -65,7 +65,6 @@ const str_char = function(L) { }; const writer = function(L, b, size, B) { - assert(Array.isArray(b)); B.push(...b.slice(0, size)); return 0; }; diff --git a/src/lundump.js b/src/lundump.js index 3f61cb6..946efa6 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -23,7 +23,7 @@ class BytecodeParser { this.numberSize = 8; assert(Z instanceof lzio.ZIO, "BytecodeParser only operates on a ZIO"); - assert(Array.isArray(name)); + assert(defs.is_luastring(name)); if (name[0] == defs.char["@"] || name[0] == defs.char["="]) this.name = name.slice(1); -- cgit v1.2.3-54-g00ecf