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

feat: accept WebAssembly.Module, Response inputs #32

Merged
merged 1 commit into from
Nov 28, 2023

Conversation

chrisdickinson
Copy link
Contributor

createPlugin now accepts two additional manifest types (response and module) as well as Response and WebAssembly.Module. There are four goals here:

  1. Allow us to target the Cloudflare Workers platform. CF Workers only support loading Wasm via import statements; these resolve to WebAssembly.Module objects, which means we need to allow users to pass Modules in addition to our other types.
  2. Play nicely with V8's Wasm caching; in particular V8 will use metadata from the Response to build a key for caching the results of Wasm compilation.
  3. This sets us up to implement Wasm linking by allowing us to introspect plugin modules imports and exports before instantiation.
  4. And finally, resolving to modules instead of arraybuffers allows us to add hooks for observe-sdk (especially in advance of adding thread pooling).

Because Bun lacks support for WebAssembly.compileStreaming and Response.clone(), we provide an alternate implementation for converting a response to a module and its metadata.

One caveat is that there's no way to get the source bytes of a WebAssembly.Module, so {module} cannot be used with {hash} in a Manifest.

Fixes #9

@@ -394,7 +394,7 @@ export async function createBackgroundPlugin(
});
});

worker.postMessage(message, modules);
worker.postMessage(message);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't transfer WebAssembly.Module instances between threads, unlike ArrayBuffers.

However, that's not necessarily a bad thing! The modules are structurally cloned between threads, which means we can send them to multiple threads in the future – which will be necessary when we go to implement pooling.

`createPlugin` now accepts two additional manifest types (`response` and `module`) as well
as `Response` and `WebAssembly.Module`. There are four goals here:

1. Allow us to target the Cloudflare Workers platform. CF Workers only support
   loading Wasm via `import` statements; these resolve to `WebAssembly.Module`
   objects, which means we need to allow users to pass `Module`s in addition
   to our other types.
2. Play nicely with V8's [Wasm caching][1]; in particular V8 will use metadata
   from the `Response` to build a key for caching the results of Wasm compilation.
3. This sets us up to implement [Wasm linking][2] by allowing us to introspect
   plugin modules imports and exports before instantiation.
4. And finally, resolving to modules instead of arraybuffers allows us to add
   [hooks for observe-sdk][3] (especially in advance of adding [thread pooling][4]).

Because Bun lacks support for `WebAssembly.compileStreaming` and
`Response.clone()`, we provide an alternate implementation for converting a
response to a module and its metadata.

One caveat is that there's no way to get the source bytes of a
`WebAssembly.Module`, so `{module}` cannot be used with `{hash}` in a
`Manifest`.

Fixes #9

[1]: https://v8.dev/blog/wasm-code-caching#stream
[2]: #29
[3]: #3
[4]: #31
@chrisdickinson chrisdickinson force-pushed the chris/20231127-allow-webassembly-modules branch from 07b6aa7 to 43153f7 Compare November 27, 2023 23:31
Copy link
Collaborator

@mhmd-azeez mhmd-azeez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@chrisdickinson chrisdickinson merged commit 630fcd6 into main Nov 28, 2023
3 checks passed
@chrisdickinson chrisdickinson deleted the chris/20231127-allow-webassembly-modules branch November 28, 2023 19:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Initialization of Plugin from WebAssembly.Module
2 participants