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

Incomplete WebAssembly implementation #15930

Open
ProphetLamb opened this issue Dec 21, 2024 · 0 comments
Open

Incomplete WebAssembly implementation #15930

ProphetLamb opened this issue Dec 21, 2024 · 0 comments
Labels
bug Something isn't working needs triage

Comments

@ProphetLamb
Copy link

ProphetLamb commented Dec 21, 2024

What version of Bun is running?

1.1.42

What platform is your computer?

Linux 5.15.153.1-microsoft-standard-WSL2 x86_64 unknown, Microsoft Windows NT 10.0.19045.0 x64

What steps can reproduce the bug?

The WebAssembly class is missing the suggested method for interacting with Wasm. This causes compatibility issues compared to node.
Instead of WebAssembly.instantiateStreaming() I need to use WebAssembly.instantiate().

  1. Compile a trivial Wasm module, e.g. add.wasm
  2. Place the module in the directory of index.ts
  3. load the wasm module
const url = new URL('add.wasm', import.meta.url)
const rsp = await fetch(url)
const dat = await rsp.arrayBuffer()
const wsm = await WebAssembly.instantiate(dat);
const mod = wsm.instance.exports
console.log(mod.add(1,2))
3

Perfect, all working as intended. But we are using a WebAssembly.instantiate which MDN advises against. 4. Let's use the suggested method instead: WebAssembly.instantiateStreaming.

console.log(WebAssembly)
const wsm = await WebAssembly.instantiateStreaming(fetch(new URL('add.wasm', import.meta.url)));
WebAssembly {
  CompileError: [class CompileError extends Error],
  Exception: [class Exception],
  Global: [class Global],
  Instance: [class Instance],
  LinkError: [class LinkError extends Error],
  Memory: [class Memory],
  Module: [class Module],
  RuntimeError: [class RuntimeError extends Error],
  Table: [class Table],
  Tag: [class Tag],
  compile: [Function: compile],
  instantiate: [Function: instantiate],
  validate: [Function: validate],
}
4 | console.log(WebAssembly)
5 | const wsm = await WebAssembly.instantiateStreaming(dat);
                                  ^
TypeError: WebAssembly.instantiateStreaming is not a function. (In 'WebAssembly.instantiateStreaming(dat)', 'WebAssembly.instantiateStreaming' is undefined)
      at /com.docker.devenvironments.code/index.ts:5:30

Bun v1.1.42 (Linux x64)

This same example is reproducible on Windows native, as well as Linux native

What is the expected behavior?

The signature conforms to node, or the wasm compatibility for bun is corrected to "partially implemented"

🟢 Fully implemented.

What do you see instead?

I text patch my node_modules after installing. I add a simple implementation to the next best js file

WebAssembly.instantiateStreaming = async (
    source,
    importObject
) => {
const rsp = await source;
const buf = await rsp.arrayBuffer();
return WebAssembly.instantiate(buf, importObject);
};
  

Additional information

instantiateStreaming is declared in the namespace WebAssembly for main

function instantiateStreaming(

aswell as 1.1.42
https://github.com/oven-sh/bun/blob/bun-v1.1.42/packages/bun-types/wasm.d.ts#L263

@ProphetLamb ProphetLamb added bug Something isn't working needs triage labels Dec 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

1 participant