Skip to content

Commit

Permalink
misc/wasm: change "var" to "let", "const", add missing semicolons, pl…
Browse files Browse the repository at this point in the history
…ace polyfill before wasm_exec.js to fix crashing in Edge

Fixes golang#27295
  • Loading branch information
silbinarywolf committed Sep 2, 2018
1 parent faa0667 commit da7f55a
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions misc/wasm/wasm_exec.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,19 @@
</head>

<body>
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

// Add polyfill for TextEncoder and TextDecoder for Microsoft Edge 17/18 support
// https://caniuse.com/#feat=textencoder
if (!window.TextEncoder) {
TextEncoder = function(){}
window.TextEncoder = function(){}
TextEncoder.prototype.encode = function (string) {
var octets = [];
var length = string.length;
var i = 0;
const octets = [];
const length = string.length;
let i = 0;
while (i < length) {
var codePoint = string.codePointAt(i);
var c = 0;
var bits = 0;
let codePoint = string.codePointAt(i);
let c = 0;
let bits = 0;
if (codePoint <= 0x0000007F) {
c = 0;
bits = 0x00;
Expand All @@ -58,14 +50,14 @@
};
}
if (!window.TextDecoder) {
TextDecoder = function(){}
window.TextDecoder = function(){}
TextDecoder.prototype.decode = function (octets) {
var string = "";
var i = 0;
let string = "";
let i = 0;
while (i < octets.length) {
var octet = octets[i];
var bytesNeeded = 0;
var codePoint = 0;
const octet = octets[i];
let bytesNeeded = 0;
let codePoint = 0;
if (octet <= 0x7F) {
bytesNeeded = 0;
codePoint = octet & 0xFF;
Expand All @@ -80,7 +72,7 @@
codePoint = octet & 0x07;
}
if (octets.length - i - bytesNeeded > 0) {
var k = 0;
let k = 0;
while (k < bytesNeeded) {
octet = octets[i + k + 1];
codePoint = (codePoint << 6) | (octet & 0x3F);
Expand All @@ -93,7 +85,16 @@
string += String.fromCodePoint(codePoint);
i += bytesNeeded + 1;
}
return string
return string;
};
}
</script>
<script src="wasm_exec.js"></script>
<script>
if (!WebAssembly.instantiateStreaming) { // polyfill
WebAssembly.instantiateStreaming = async (resp, importObject) => {
const source = await (await resp).arrayBuffer();
return await WebAssembly.instantiate(source, importObject);
};
}

Expand Down

0 comments on commit da7f55a

Please sign in to comment.