From 31cacbc3d250b5eca1f65cabeb812df99e2a8f7f Mon Sep 17 00:00:00 2001 From: ledsun Date: Wed, 16 Aug 2023 21:00:39 +0900 Subject: [PATCH] Fall back to the existing WebAssembly.compile based implementation Since WebAssembly.compileStreaming is not available in iOS Safari 14 --- .../ruby-wasm-wasi/src/browser.script.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts b/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts index 9cce69a8f7..65d6364810 100644 --- a/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts +++ b/packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts @@ -4,7 +4,7 @@ export const main = async (pkg: { name: string; version: string }) => { const response = fetch( `https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`, ); - const module = await WebAssembly.compileStreaming(response); + const module = await compileWebAssemblyModule(response); const { vm } = await DefaultRubyVM(module); vm.printVersion(); @@ -78,3 +78,18 @@ const loadScriptAsync = async ( return Promise.resolve({ scriptContent: tag.innerHTML, evalStyle }); }; + +// WebAssembly.compileStreaming is a relatively new API. +// For example, it is not available in iOS Safari 14, +// so check whether WebAssembly.compileStreaming is available and +// fall back to the existing implementation using WebAssembly.compile if not. +const compileWebAssemblyModule = async function ( + response: Promise, +): Promise { + if (!WebAssembly.compileStreaming) { + const buffer = await (await response).arrayBuffer(); + return WebAssembly.compile(buffer); + } else { + return WebAssembly.compileStreaming(response); + } +};