From c89c132d2125950c49e9bf6b700eb489981f71b9 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 6 Jan 2018 23:00:05 +1100 Subject: src/defs.js: Support environments with no String.codePointAt --- src/defs.js | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/defs.js b/src/defs.js index 629e0af..0c4ccd8 100644 --- a/src/defs.js +++ b/src/defs.js @@ -240,25 +240,37 @@ const to_luastring = function(str, cache) { if (is_luastring(cached)) return cached; } - let outU8Array = Array(str.length); /* array is at *least* going to be length of string */ + let len = str.length; + let outU8Array = Array(len); /* array is at *least* going to be length of string */ let outIdx = 0; - for (let i = 0; i < str.length; ++i) { - let u = str.codePointAt(i); + for (let i = 0; i < len; ++i) { + let u = str.charCodeAt(i); if (u <= 0x7F) { outU8Array[outIdx++] = u; } else if (u <= 0x7FF) { outU8Array[outIdx++] = 0xC0 | (u >> 6); outU8Array[outIdx++] = 0x80 | (u & 63); - } else if (u <= 0xFFFF) { - outU8Array[outIdx++] = 0xE0 | (u >> 12); - outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); - outU8Array[outIdx++] = 0x80 | (u & 63); } else { - i++; /* It was a surrogate pair and hence used up two javascript chars */ - outU8Array[outIdx++] = 0xF0 | (u >> 18); - outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); - outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); - outU8Array[outIdx++] = 0x80 | (u & 63); + /* This part is to work around possible lack of String.codePointAt */ + if (u >= 0xD800 && u <= 0xDBFF && (i+1) < len) { + /* is first half of surrogate pair */ + let v = str.charCodeAt(i+1); + if (v >= 0xDC00 && v <= 0xDFFF) { + /* is valid low surrogate */ + i++; + u = (u - 0xD800) * 0x400 + v + 0x2400; + } + } + if (u <= 0xFFFF) { + outU8Array[outIdx++] = 0xE0 | (u >> 12); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } else { + outU8Array[outIdx++] = 0xF0 | (u >> 18); + outU8Array[outIdx++] = 0x80 | ((u >> 12) & 63); + outU8Array[outIdx++] = 0x80 | ((u >> 6) & 63); + outU8Array[outIdx++] = 0x80 | (u & 63); + } } } outU8Array = luastring_from(outU8Array); -- cgit v1.2.3-70-g09d2