From 5812c42e7bb680592e60454b003f228dfe95869a Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 12 Nov 2017 16:01:51 +1100 Subject: Add internal function defs.luastring_cmp for string equality checks --- src/defs.js | 6 ++++++ src/ldebug.js | 2 +- src/lstring.js | 2 +- src/lundump.js | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/defs.js b/src/defs.js index 0802376..315364a 100644 --- a/src/defs.js +++ b/src/defs.js @@ -135,6 +135,11 @@ const is_luastring = function(s) { return Array.isArray(s); }; +/* test two lua strings for equality */ +const luastring_cmp = function(a, b) { + return a === b || (a.length === b.length && a.join() === b.join()); +}; + const to_jsstring = function(value, from, to) { assert(is_luastring(value), "jsstring expects an array of bytes"); @@ -397,5 +402,6 @@ 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.luastring_cmp = luastring_cmp; module.exports.to_jsstring = to_jsstring; module.exports.to_luastring = to_luastring; diff --git a/src/ldebug.js b/src/ldebug.js index 3637b5d..59520c3 100644 --- a/src/ldebug.js +++ b/src/ldebug.js @@ -565,7 +565,7 @@ const luaG_opinterror = function(L, p1, p2, msg) { const luaG_ordererror = function(L, p1, p2) { let t1 = ltm.luaT_objtypename(L, p1); let t2 = ltm.luaT_objtypename(L, p2); - if (t1.join() === t2.join()) + if (defs.luastring_cmp(t1, t2)) luaG_runerror(L, defs.to_luastring("attempt to compare two %s values", true), t1); else luaG_runerror(L, defs.to_luastring("attempt to compare %s with %s", true), t1, t2); diff --git a/src/lstring.js b/src/lstring.js index 185582b..c85e5b2 100644 --- a/src/lstring.js +++ b/src/lstring.js @@ -24,7 +24,7 @@ class TString { const luaS_eqlngstr = function(a, b) { assert(a instanceof TString); assert(b instanceof TString); - return a == b || (a.realstring.length == b.realstring.length && a.realstring.join() == b.realstring.join()); + return a == b || defs.luastring_cmp(a.realstring, b.realstring); }; /* converts strings (arrays) to a consistent map key diff --git a/src/lundump.js b/src/lundump.js index 61697b6..b25a955 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -214,7 +214,7 @@ class BytecodeParser { checkliteral(s, msg) { let buff = this.read(s.length); - if (buff.join() !== s.join()) + if (!defs.luastring_cmp(buff, s)) this.error(msg); } -- cgit v1.2.3-54-g00ecf