summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordaurnimator <quae@daurnimator.com>2018-01-06 23:00:05 +1100
committerdaurnimator <quae@daurnimator.com>2018-01-06 23:02:06 +1100
commitc89c132d2125950c49e9bf6b700eb489981f71b9 (patch)
treee57679a4eaa22d360f8cc14eb539269620cb8e97
parent970dbe61ce2e0c46455a89c8fbc75e6eb783d7c9 (diff)
downloadfengari-c89c132d2125950c49e9bf6b700eb489981f71b9.tar.gz
fengari-c89c132d2125950c49e9bf6b700eb489981f71b9.tar.bz2
fengari-c89c132d2125950c49e9bf6b700eb489981f71b9.zip
src/defs.js: Support environments with no String.codePointAt
-rw-r--r--src/defs.js36
1 files 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);