Skip to content

Rust JSG: support WebAssembly.Module handles - #8616

Merged
bartlomieju merged 5 commits into
mainfrom
biwanczuk/rust-jsg-wasm-module-object
Nov 19, 2026
Merged

bartlomieju merged 5 commits into
mainfrom
biwanczuk/rust-jsg-wasm-module-object

Conversation

@bartlomieju

@bartlomieju bartlomieju commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Rust JSG had no handle type for WebAssembly.Module; v8.rs listed
WasmModuleObject as deliberately unimplemented because nothing
needed it. The Rust port of the Worker Loader binding does:
WorkerCode.modules accepts WebAssembly.Module values, and C++
hands their compiled code to the new isolate without recompiling. This
adds the WasmModuleObject handle type and
Local<WasmModuleObject>::compiled_module(), which returns an owned,
Send + Sync CompiledWasmModule.

The first commit fixes the jsg-test and jsg-bench harnesses, which
started V8 lazily on a test thread. That left the main thread without
access to V8's memory protection key, so V8::Dispose() crashed at
exit once a test had compiled WebAssembly (Linux x86-64 only).
V8System is now constructed at namespace scope, as in the C++ JSG
tests.

Both harnesses created their V8System lazily, on the first thread that
needed it. On Linux x86-64, V8 guards JIT memory such as the
WebAssembly code pointer table with a memory protection key, and only
the thread that creates the platform, and threads it later creates,
get access to that key. The Rust test harness runs tests on threads
spawned from the main thread, so the main thread, which disposes V8
from static destructors at exit, had no access. Once a test had
compiled WebAssembly, V8::Dispose() faulted with SEGV_PKUERR in
WasmCodePointerTable::SweepSegments.

Construct V8System at namespace scope instead, as the C++ JSG tests
and benchmarks do, so V8 starts and stops on the main thread.
Rust JSG had no handle type for WebAssembly.Module. Add the
WasmModuleObject marker type, accepted as an exact-type parameter, in
unions and in Global/TracedGlobal like the other handle types, and
Local<WasmModuleObject>::compiled_module(), which returns an owned
v8::CompiledWasmModule. It exposes the module's wire bytes and can be
handed to C++ to make a module object in another isolate without
recompiling. It is Send + Sync and must be dropped before V8 is
disposed.

The Rust port of the Worker Loader binding needs this for the
WebAssembly.Module values in WorkerCode.modules.
@bartlomieju
bartlomieju requested review from a team as code owners November 18, 2026 14:55

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 23196725.

This review, of f5d6967e

✅ Approved — No issues found. The new handle type and its compiled-module accessor are wired in consistently and well tested, and the harness change starts V8 on the main thread as the C++ JSG tests do.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.


📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

No defects found:

  • Harnesses: V8System is now constructed at namespace scope in both harnesses, and the lazy accessors are removed cleanly.
  • Handle type: WasmModuleObject is integrated throughout the handle machinery.
  • CompiledWasmModule: ownership crosses the FFI boundary as a kj::Own, and the empty wire-bytes case is handled.
  • Tests: coverage is thorough.

⚡ Performance: ✅ LGTM

No performance concerns.


📝 Documentation: ✅ LGTM

The README update matches the new handle type.


Reviewed commit: f5d6967e

@codecov-commenter

codecov-commenter commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 7.50000% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 67.01%. Comparing base (9471353) to head (b878706).
⚠️ Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/rust/jsg/v8.rs 10.34% 26 Missing ⚠️
src/rust/jsg/ffi.c++ 0.00% 11 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8616      +/-   ##
==========================================
- Coverage   67.03%   67.01%   -0.02%     
==========================================
  Files         799      799              
  Lines      206639   206765     +126     
  Branches    32062    32072      +10     
==========================================
+ Hits       138519   138569      +50     
- Misses      49323    49394      +71     
- Partials    18797    18802       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…sm-module-object

# Conflicts:
#	src/rust/jsg/wrappable.rs

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of 5cc510ef.

This review, of 23196725

✅ Approved — No issues found. The merge-resolved wrappable.rs registrations and the test harness's namespace-scope V8System are consistent with the rest of the change.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.

  • A value that is not a WebAssembly.Module is rejected by the type check before compiled_module() is reached.
  • The empty wire-bytes case is handled.
  • The harness change affects only the test and benchmark binaries.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates needed.


📋 Code Quality: ✅ LGTM

No defects found in the code changed since the last review.

  • src/rust/jsg-test/ffi.c++: V8System now lives at namespace scope, and every TestIsolate construction site uses it. The new shims only call into V8 or Lock.
  • src/rust/jsg/wrappable.rs: CompiledWasmModule and WasmModuleObject are registered the same way as the neighbouring handle types.

⚡ Performance: ✅ LGTM

No performance concerns.

  • Getting the compiled module makes one small allocation per call.
  • Reading the wire bytes returns a view into the existing bytes without copying them.

📝 Documentation: ✅ LGTM

The README update matches the new handle type.


Reviewed commit: 23196725

On Linux x86-64, V8 grants access to the memory protection key that
guards its JIT memory only to the thread that starts it and the
threads that thread creates, and dropping the last reference to a
module's compiled code frees entries in the protected code pointer
table. Say so instead of claiming any thread will do, and keep the
test harness comment to the requirement it states.

@clanky-the-clanker clanky-the-clanker Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Superseded by the review of b8787062.

This review, of 5cc510ef

✅ Approved — No issues found. The changes since the last review (the V8System setup in the harnesses, the new test shims and the narrowed comment on where a CompiledWasmModule may be dropped) are correct and consistent with the rest of the change.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.

  • compiled_module() can only be reached after the WebAssembly.Module type check, so a value of another type never gets there.
  • Reading the wire bytes returns a view into bytes the module owns.
  • The harness changes affect only the test and benchmark binaries.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates are needed.


📋 Code Quality: ✅ LGTM

I found no defects in src/rust/jsg-test/ffi.c++, src/rust/jsg/ffi.h or src/rust/jsg/v8.rs.

  • Harness startup: V8System is built at namespace scope in both harnesses, and every isolate constructor passes it in.
  • KjOwn<CompiledWasmModule> drop: the bridge in v8.rs generates the drop function.
  • Empty wire bytes: they return an empty slice instead of a slice built from a possibly null pointer.
  • Tests: they cover conversion, rejection of other values, unions, struct fields, wire bytes, and a compiled module that outlives its isolate.

⚡ Performance: ✅ LGTM

No performance concerns.

  • compiled_module() makes one small allocation per call.
  • wire_bytes() returns a view into the existing bytes without copying them.

📝 Documentation: ✅ LGTM

The README and the doc comments match the new handle type and the thread requirement for dropping a CompiledWasmModule.


Reviewed commit: 5cc510ef

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved — No issues found. The test update since the last review is consistent with the error format in wrappable.rs.

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


✅ All checks passed

🔒 Security: ✅ LGTM

No security concerns.

  • Type check: compiled_module() can only be reached after the WebAssembly.Module type check.
  • Wire bytes: wire_bytes() returns a view into bytes the module owns, and an empty slice when there are no bytes.
  • Harness changes: These affect only the test and benchmark binaries.

📖 AGENTS.md: ✅ LGTM

No AGENTS.md updates are needed.


📋 Code Quality: ✅ LGTM

I found no defects in src/rust/jsg-test/tests/wasm_module_object.rs.

  • Helpers: The helper signatures match how the tests call them.
  • Error message: The expected error message matches the "Expected {} but got {type_name}" format in wrappable.rs.
  • Drop thread: The test where a compiled module outlives its isolate drops the last CompiledWasmModule on a thread allowed by the drop-thread contract documented in v8.rs.

⚡ Performance: ✅ LGTM

No performance concerns.

  • compiled_module() makes one small allocation per call.
  • wire_bytes() returns a view into the module's bytes without copying them.
  • The harness now starts V8 once, so it adds no per-test cost.

📝 Documentation: ✅ LGTM

The README and the doc comments match the new handle type and the thread requirement for dropping a CompiledWasmModule.


Reviewed commit: b8787062

@bartlomieju
bartlomieju merged commit 7c45e5d into main Nov 19, 2026
36 of 38 checks passed
@bartlomieju
bartlomieju deleted the biwanczuk/rust-jsg-wasm-module-object branch November 19, 2026 14:18
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.

3 participants