summaryrefslogtreecommitdiff
path: root/docs/demo/wat2wasm/demo.js
diff options
context:
space:
mode:
authorThomas Tay <thomastayac@gmail.com>2022-12-19 04:22:02 -0800
committerGitHub <noreply@github.com>2022-12-19 04:22:02 -0800
commit7e0b5c9def65a64717a6d337d418afd23dabcd49 (patch)
treefc3f0090134102eec7da2c763e781e4bf4468676 /docs/demo/wat2wasm/demo.js
parented0b720c97ec6cc70b1dc33151b5dcadf12c31f4 (diff)
downloadwabt-7e0b5c9def65a64717a6d337d418afd23dabcd49.tar.gz
wabt-7e0b5c9def65a64717a6d337d418afd23dabcd49.tar.bz2
wabt-7e0b5c9def65a64717a6d337d418afd23dabcd49.zip
Base 64 output is incorrect (#2103)
For simple, it decodes to: MCw5NywxMTUsMTA5LDEsMCwwLDAsMSw2LDEsOTYsMSwxMjQsMSwxMjQsMywyLDEsMCw3LDcsMSwzLDEwMiw5Nyw5OSwwLDAsMTAsNDYsMSw0NCwwLDMyLDAsNjgsMCwwLDAsMCwwLDAsMjQwLDYzLDk5LDQsMTI0LDY4LDAsMCwwLDAsMCwwLDI0MCw2Myw1LDMyLDAsMzIsMCw2OCwwLDAsMCwwLDAsMCwyNDAsNjMsMTYxLDE2LDAsMTYyLDExLDExLDAsMTgsNCwxMTAsOTcsMTA5LDEwMSwxLDYsMSwwLDMsMTAyLDk3LDk5LDIsMywxLDAsMA== which is base64 for the literal string: '0,97,115,109,1,0,0,0,1,7,1,96,2,127,127,1,127,3,2,1,0,7,10,1,6,97,100,100,84,119,111,0,0,10,9,1,7,0,32,0,32,1,106,11,0,10,4,110,97,109,101,2,3,1,0,0' This is the Uint8 array encoded as a string, which is not what we want! We want the numbers as bytes encoded in base64. This answer changes the base64 to: AGFzbQEAAAABBgFgAXwBfAMCAQAHBwEDZmFjAAAKLgEsACAARAAAAAAAAPA/YwR8RAAAAAAAAPA/BSAAIABEAAAAAAAA8D+hEACiCwsAEgRuYW1lAQYBAANmYWMCAwEAAA== Which decodes properly. In Node: ``` > Buffer.from("AGFzbQEAAAABBgFgAXwBfAMCAQAHBwEDZmFjAAAKLgEsACAARAAAAAAAAPA/YwR8RAAAAAAAAPA/BSAAIABEAAAAAAAA8D+hEACiCwsAEgRuYW1lAQYBAANmYWMCAwEAAA==", 'base64') <Buffer 00 61 73 6d 01 00 00 00 01 06 01 60 01 7c 01 7c 03 02 01 00 07 07 01 03 66 61 63 00 00 0a 2e 01 2c 00 20 00 44 00 00 00 00 00 00 f0 3f 63 04 7c 44 00 ... 47 more bytes> ```
Diffstat (limited to 'docs/demo/wat2wasm/demo.js')
-rw-r--r--docs/demo/wat2wasm/demo.js4
1 files changed, 3 insertions, 1 deletions
diff --git a/docs/demo/wat2wasm/demo.js b/docs/demo/wat2wasm/demo.js
index 1300022b..1fcd42cb 100644
--- a/docs/demo/wat2wasm/demo.js
+++ b/docs/demo/wat2wasm/demo.js
@@ -117,7 +117,9 @@ function compile() {
var binaryOutput = module.toBinary({log: true, write_debug_names:true});
outputLog = binaryOutput.log;
binaryBuffer = binaryOutput.buffer;
- outputBase64 = btoa(binaryBuffer);
+ // binaryBuffer is a Uint8Array, so we need to convert it to a string to use btoa
+ // https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string
+ outputBase64 = btoa((String.fromCharCode.apply(null, binaryBuffer));
var blob = new Blob([binaryOutput.buffer]);
if (binaryBlobUrl) {