From ca8c8b33706abd7f882ee8a8f3272eba2326bcf9 Mon Sep 17 00:00:00 2001 From: Zhuowei Zhang Date: Mon, 6 May 2019 18:08:33 -0700 Subject: [PATCH] js-polyfill: support Safari, which doesn't have instantiateStreaming --- wasmtime-wasi/js-polyfill/polyfill.c | 2 +- wasmtime-wasi/js-polyfill/wasi.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/wasmtime-wasi/js-polyfill/polyfill.c b/wasmtime-wasi/js-polyfill/polyfill.c index a615225ead..d4c63cebf2 100644 --- a/wasmtime-wasi/js-polyfill/polyfill.c +++ b/wasmtime-wasi/js-polyfill/polyfill.c @@ -26,7 +26,7 @@ void handleFiles(void) { let file = document.getElementById('input').files[0]; \ let file_with_mime_type = file.slice(0, file.size, 'application/wasm'); \ let response = new Response(file_with_mime_type); \ - WebAssembly.instantiateStreaming(response, imports) \ + wasi_instantiateStreaming(response, imports) \ .then(obj => { \ setInstance(obj.instance); \ try { \ diff --git a/wasmtime-wasi/js-polyfill/wasi.js b/wasmtime-wasi/js-polyfill/wasi.js index ea11a92e97..02e6e2f0e4 100644 --- a/wasmtime-wasi/js-polyfill/wasi.js +++ b/wasmtime-wasi/js-polyfill/wasi.js @@ -31,6 +31,17 @@ function handleWASIExit(e) { } } +// Safari doesn't have instantiateStreaming +function wasi_instantiateStreaming(response, imports) { + if (WebAssembly && WebAssembly.instantiateStreaming) { + return WebAssembly.instantiateStreaming(response, imports); + } + return response.arrayBuffer() + .then(function(buffer) { + return WebAssembly.instantiate(buffer, imports); + }); +} + // The current guest wasm instance. var currentInstance;