Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WebAssembly.compileStreaming #267

Merged
merged 2 commits into from
Oct 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/npm-packages/ruby-wasm-wasi/src/browser.script.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { DefaultRubyVM } from "./browser";

export const main = async (pkg: { name: string; version: string }) => {
const response = await fetch(
const response = fetch(
`https://cdn.jsdelivr.net/npm/${pkg.name}@${pkg.version}/dist/ruby+stdlib.wasm`,
);
const buffer = await response.arrayBuffer();
const module = await WebAssembly.compile(buffer);
const module = await compileWebAssemblyModule(response);
const { vm } = await DefaultRubyVM(module);

vm.printVersion();
Expand Down Expand Up @@ -79,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);
}
};