Skip to content

Add standalone WASM component output for WasmGC backend#13157

Open
zacharywhitley wants to merge 2 commits intooracle:masterfrom
tegmentum:feat/standalone-wasmgc
Open

Add standalone WASM component output for WasmGC backend#13157
zacharywhitley wants to merge 2 commits intooracle:masterfrom
tegmentum:feat/standalone-wasmgc

Conversation

@zacharywhitley
Copy link

Summary

Add -H:+StandaloneWasm option that produces WasmGC modules without any JS interop dependencies, suitable for wrapping as WebAssembly Components.

Currently the WasmGC backend always produces WASM that requires a JS host runtime (27+ imports from interop/jsbody/convert modules). This PR adds a standalone mode that eliminates all JS dependencies, producing modules that import only simple math, I/O, and process control functions using primitive types.

Depends on: #13156 (declarative element segments fix)

What standalone mode does

When -H:+StandaloneWasm -H:Backend=WASMGC is passed:

  • JS call stubbing: JSCallNode lowering routes to host imports (io.print-buffer, io.host-time-ms, wasi.proc-exit) or stubs instead of JS interop
  • Batch printing: Characters are copied from GC arrays to a 1-page linear memory buffer, then flushed to the host via io.print-buffer(fd, ptr, num_chars) — avoids per-character overhead
  • externref elimination: WasmExtern struct field replaced with i32; toExtern/wrapExtern templates produce stubs; no externref in the module
  • Component-model naming: Import names use fully-qualified WIT paths (graalvm:standalone/io@0.1.0) with kebab-case function names via import remapping in WasmIdFactory
  • wasm-tools assembler: wasm-tools parse used as default assembler for standalone (supports WasmGC natively)
  • JS code suppression: Bootstrap definitions, JSBody imports, and JS companion file all skipped
  • Defaults: DisableStackTraces=true, ImageHeapObjectsPerFunction=1000

Result

The output module has:

  • Zero interop/jsbody/convert/externref references
  • 7 imports from compat (math), io (print-buffer, host-time-ms), wasi (proc-exit)
  • All imports use simple i32/f64 types
  • Exported linear memory for host I/O
  • Passes wasm-tools validate --features all
  • Wraps cleanly with wasm-tools component embed + component new

Files changed (13)

File Change
WebImageOptions.java StandaloneWasm hosted option
mx_web_image.py Register option in hosted options list
NativeImageWasmGeneratorRunner.java Force DisableStackTraces, default ImageHeapObjectsPerFunction
WasmImports.java printChar, printBuffer, WASI imports, Component inner class
WasmIdFactory.java Import remapping infrastructure
WasmAssembler.java WasmTools assembler class
verify-wasm-tools.wat Verification file for wasm-tools
GCKnownIds.java standalonePrintCharsTemplate, skip extern exports
WasmGCElementCreator.java i32 instead of externref for WasmExtern field
WasmGCFunctionTemplates.java StandalonePrintChars template, stub ToExtern/WrapExtern
WebImageWasmGCCodeGen.java Skip JS emissions, add linear memory, metrics init
WebImageWasmGCNodeLowerer.java lowerJSCallStandalone, JSBody/JSValue/PROXY_CHAR_ARRAY stubs
WebImageWasmGCProviders.java Register component import remappings

Test plan

  • Existing WasmGC tests pass (all changes gated behind -H:+StandaloneWasm)
  • Compile HelloWorld/Calculator with standalone flag, verify zero JS imports
  • wasm-tools validate --features all passes on output
  • wasm-tools component embed + component new succeeds without --skip-validation
  • Output loads and runs correctly in wasmtime with WasmGC enabled

@oracle-contributor-agreement
Copy link

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Mar 17, 2026
Zachary Whitley added 2 commits March 16, 2026 20:47
The WebAssembly spec requires that functions referenced by ref.func
outside of active/passive element segments must be declared in a
declarative element segment (elem declare func ...).

Add a RefFuncCollector visitor that scans all function bodies and
global initializers for ref.func instructions, then emits a single
declarative element segment in the WAT output.

This fixes validation errors from wasm-tools validate and wasmtime
("undeclared function reference") that occurred because the WasmGC
heap initialization code uses ref.func for DynamicHub newInstance
and clone function pointers without declaring them.
Add -H:+StandaloneWasm option that produces WasmGC modules without
any JS interop dependencies, suitable for wrapping as WebAssembly
Components via wasm-tools.

When enabled, the WasmGC backend:
- Routes JSCallNode/JSBody to standalone stubs or host imports
- Prints via a linear memory transfer buffer (io.print-buffer)
  instead of JS interop, using a 1-page buffer for batch I/O
- Uses io.host-time-ms for time, wasi.proc-exit for exit
- Replaces externref with i32 in WasmExtern struct and stubs
  toExtern/wrapExtern templates to avoid externref in the module
- Skips JS bootstrap code emission and JS companion file
- Uses component-model-compatible import names
  (graalvm:standalone/io@0.1.0, etc.) via import remapping
- Defaults ImageHeapObjectsPerFunction=1000 to stay within
  Cranelift's function size limit
- Uses wasm-tools as the default assembler (supports WasmGC)
- Forces DisableStackTraces (stack traces require JS runtime)

The resulting module:
- Has zero interop/jsbody/convert/externref imports
- Imports only from compat (math), io (print-buffer, host-time-ms),
  and wasi (proc-exit) using simple i32/f64 types
- Exports a linear memory for host I/O access
- Validates with wasm-tools validate
- Wraps cleanly with wasm-tools component embed + component new

Usage:
  mx web-image -H:+StandaloneWasm -H:Backend=WASMGC -cp classes App
@zacharywhitley zacharywhitley force-pushed the feat/standalone-wasmgc branch from def29a0 to 8dc20be Compare March 17, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Required At least one contributor does not have an approved Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant