Skip to content

Commit

Permalink
Fall back to the existing WebAssembly.compile based implementation
Browse files Browse the repository at this point in the history
Since WebAssembly.compileStreaming is not available in iOS Safari 14
  • Loading branch information
ledsun authored and kateinoigakukun committed Oct 29, 2023
1 parent d7bda26 commit c4c5083
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<Response>,
): Promise<WebAssembly.Module> {
if (!WebAssembly.compileStreaming) {
const buffer = await (await response).arrayBuffer();
return WebAssembly.compile(buffer);
} else {
return WebAssembly.compileStreaming(response);
}
};

0 comments on commit c4c5083

Please sign in to comment.