Skip to content

Commit c4c5083

Browse files
ledsunkateinoigakukun
authored andcommitted
Fall back to the existing WebAssembly.compile based implementation
Since WebAssembly.compileStreaming is not available in iOS Safari 14
1 parent d7bda26 commit c4c5083

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const main = async (pkg: { name: string; version: string }) => {
44
const response = fetch(
55
`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`,
66
);
7-
const module = await WebAssembly.compileStreaming(response);
7+
const module = await compileWebAssemblyModule(response);
88
const { vm } = await DefaultRubyVM(module);
99

1010
vm.printVersion();
@@ -78,3 +78,18 @@ const loadScriptAsync = async (
7878

7979
return Promise.resolve({ scriptContent: tag.innerHTML, evalStyle });
8080
};
81+
82+
// WebAssembly.compileStreaming is a relatively new API.
83+
// For example, it is not available in iOS Safari 14,
84+
// so check whether WebAssembly.compileStreaming is available and
85+
// fall back to the existing implementation using WebAssembly.compile if not.
86+
const compileWebAssemblyModule = async function (
87+
response: Promise<Response>,
88+
): Promise<WebAssembly.Module> {
89+
if (!WebAssembly.compileStreaming) {
90+
const buffer = await (await response).arrayBuffer();
91+
return WebAssembly.compile(buffer);
92+
} else {
93+
return WebAssembly.compileStreaming(response);
94+
}
95+
};

0 commit comments

Comments
 (0)