From ca4b033417397ae8a9e4ac3d18945fe8ea821831 Mon Sep 17 00:00:00 2001 From: Jake B Date: Tue, 28 Aug 2018 17:22:56 +1000 Subject: [PATCH] misc/wasm: add polyfill for TextEncoder/TextDecoder for Edge support Fixes #27295 --- misc/wasm/wasm_exec.js | 76 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/misc/wasm/wasm_exec.js b/misc/wasm/wasm_exec.js index 94b9552c59606..6fde803611158 100644 --- a/misc/wasm/wasm_exec.js +++ b/misc/wasm/wasm_exec.js @@ -27,6 +27,82 @@ global.TextEncoder = util.TextEncoder; global.TextDecoder = util.TextDecoder; } else { + // Add polyfill for TextEncoder and TextDecoder for Microsoft Edge 17/18 support + // https://caniuse.com/#feat=textencoder + if (!window.TextEncoder) { + TextEncoder = function(){} + TextEncoder.prototype.encode = function (string) { + var octets = []; + var length = string.length; + var i = 0; + while (i < length) { + var codePoint = string.codePointAt(i); + var c = 0; + var bits = 0; + if (codePoint <= 0x0000007F) { + c = 0; + bits = 0x00; + } else if (codePoint <= 0x000007FF) { + c = 6; + bits = 0xC0; + } else if (codePoint <= 0x0000FFFF) { + c = 12; + bits = 0xE0; + } else if (codePoint <= 0x001FFFFF) { + c = 18; + bits = 0xF0; + } + octets.push(bits | (codePoint >> c)); + c -= 6; + while (c >= 0) { + octets.push(0x80 | ((codePoint >> c) & 0x3F)); + c -= 6; + } + i += codePoint >= 0x10000 ? 2 : 1; + } + return octets; + }; + } + if (!window.TextDecoder) { + TextDecoder = function(){} + TextDecoder.prototype.decode = function (octets) { + var string = ""; + var i = 0; + while (i < octets.length) { + var octet = octets[i]; + var bytesNeeded = 0; + var codePoint = 0; + if (octet <= 0x7F) { + bytesNeeded = 0; + codePoint = octet & 0xFF; + } else if (octet <= 0xDF) { + bytesNeeded = 1; + codePoint = octet & 0x1F; + } else if (octet <= 0xEF) { + bytesNeeded = 2; + codePoint = octet & 0x0F; + } else if (octet <= 0xF4) { + bytesNeeded = 3; + codePoint = octet & 0x07; + } + if (octets.length - i - bytesNeeded > 0) { + var k = 0; + while (k < bytesNeeded) { + octet = octets[i + k + 1]; + codePoint = (codePoint << 6) | (octet & 0x3F); + k += 1; + } + } else { + codePoint = 0xFFFD; + bytesNeeded = octets.length - i; + } + string += String.fromCodePoint(codePoint); + i += bytesNeeded + 1; + } + return string + }; + } + if (typeof window !== "undefined") { window.global = window; } else if (typeof self !== "undefined") {