Skip to content

feat: add bindings for v8::WasmModuleCompilation#1908

Merged
bartlomieju merged 6 commits intomainfrom
add-wasm-module-compilation-bindings
Feb 19, 2026
Merged

feat: add bindings for v8::WasmModuleCompilation#1908
bartlomieju merged 6 commits intomainfrom
add-wasm-module-compilation-bindings

Conversation

@bartlomieju
Copy link
Member

@bartlomieju bartlomieju commented Feb 16, 2026

Summary

Add Rust bindings for the new experimental v8::WasmModuleCompilation class from v8/include/v8-wasm.h, providing an interface for asynchronous WebAssembly module compilation (e.g. for source phase imports).

Bindings provided

  • WasmModuleCompilation::new() / Default — create a new compilation
  • on_bytes_received(&mut self, data: &[u8]) — feed wasm bytes (callable from any thread)
  • finish(self, scope, caching_callback, resolution_callback) — finalize compilation on the main thread; the resolution callback receives Result<Local<WasmModuleObject>, Local<Value>> along with &Isolate for creating Global handles
  • abort(self) — cancel compilation
  • set_has_compiled_module_bytes(&mut self) — signal cached module bytes are available
  • set_more_functions_can_be_serialized_callback(...) — register a serialization callback
  • set_url(&mut self, url: &str) — set source URL before finishing
  • Drop cleans up the C++ allocation; Send is implemented (V8 allows cross-thread usage)

Implementation details

  • Resolution callback lifetime is managed via shared_ptr<void> with a custom deleter on the C++ side, which correctly handles std::function copy semantics when V8 internally copies the callback into its Resolver
  • Rust side uses Option<Box<dyn FnOnce>> so the trampoline can .take() the closure (FnOnce semantics) without freeing the outer allocation, which is ref-counted by shared_ptr
  • Serialization callback uses the same shared_ptr<void> pattern
  • Isolate pointer is captured in the Rust closure rather than threaded through C++
  • Caching callback function pointer is passed directly to V8 without wrapping

Test plan

  • wasm_module_compilation — compiles a minimal wasm module, finishes, asserts the result via Global handle
  • wasm_module_compilation_abort — compiles and aborts without finishing

🤖 Generated with Claude Code

Expose the new experimental WasmModuleCompilation API from V8 14.6,
which provides asynchronous WebAssembly module compilation without
requiring a Promise-based streaming interface. This is intended for
use cases like source phase imports.

Bindings include: new, on_bytes_received, finish, abort,
set_has_compiled_module_bytes, set_more_functions_can_be_serialized_callback,
and set_url.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
bartlomieju and others added 3 commits February 17, 2026 15:26
- Pass caching_callback directly to Finish instead of wrapping in a lambda
- Capture isolate pointer in the Rust closure instead of threading it
  through the C++ resolution callback
- Use a rule-of-five RustCallable class to track Box lifetime through
  std::function, preventing leaks if the callback is never invoked

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…pilation

Replace RustCallable class with shared_ptr<void> to properly handle
std::function copy semantics in V8's Finish method. The previous approach
caused heap-use-after-free because V8 copies the std::function internally,
and the RustCallable copy/destroy semantics couldn't coordinate ownership
of the raw pointer across copies.

Now the Rust closure data is reference-counted through shared_ptr copies,
and the Rust side uses Option<Box<dyn FnOnce>> so the trampoline can
.take() the closure without freeing the outer allocation.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@bartlomieju bartlomieju changed the title Add Rust bindings for v8::WasmModuleCompilation feat: add bindings for v8::WasmModuleCompilation Feb 19, 2026
@bartlomieju bartlomieju merged commit 87082f0 into main Feb 19, 2026
20 checks passed
@bartlomieju bartlomieju deleted the add-wasm-module-compilation-bindings branch February 19, 2026 14:56
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.

2 participants