Add standalone WASM component output for WasmGC backend#13157
Open
zacharywhitley wants to merge 2 commits intooracle:masterfrom
Open
Add standalone WASM component output for WasmGC backend#13157zacharywhitley wants to merge 2 commits intooracle:masterfrom
zacharywhitley wants to merge 2 commits intooracle:masterfrom
Conversation
|
Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (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. |
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
def29a0 to
8dc20be
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
-H:+StandaloneWasmoption 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/convertmodules). 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=WASMGCis passed:JSCallNodelowering routes to host imports (io.print-buffer,io.host-time-ms,wasi.proc-exit) or stubs instead of JS interopio.print-buffer(fd, ptr, num_chars)— avoids per-character overheadWasmExternstruct field replaced withi32;toExtern/wrapExterntemplates produce stubs; no externref in the modulegraalvm:standalone/io@0.1.0) with kebab-case function names via import remapping inWasmIdFactorywasm-tools parseused as default assembler for standalone (supports WasmGC natively)DisableStackTraces=true,ImageHeapObjectsPerFunction=1000Result
The output module has:
interop/jsbody/convert/externrefreferencescompat(math),io(print-buffer, host-time-ms),wasi(proc-exit)i32/f64typeswasm-tools validate --features allwasm-tools component embed+component newFiles changed (13)
WebImageOptions.javaStandaloneWasmhosted optionmx_web_image.pyNativeImageWasmGeneratorRunner.javaDisableStackTraces, defaultImageHeapObjectsPerFunctionWasmImports.javaprintChar,printBuffer, WASI imports,Componentinner classWasmIdFactory.javaWasmAssembler.javaWasmToolsassembler classverify-wasm-tools.watGCKnownIds.javastandalonePrintCharsTemplate, skip extern exportsWasmGCElementCreator.javai32instead ofexternreffor WasmExtern fieldWasmGCFunctionTemplates.javaStandalonePrintCharstemplate, stubToExtern/WrapExternWebImageWasmGCCodeGen.javaWebImageWasmGCNodeLowerer.javalowerJSCallStandalone, JSBody/JSValue/PROXY_CHAR_ARRAY stubsWebImageWasmGCProviders.javaTest plan
-H:+StandaloneWasm)wasm-tools validate --features allpasses on outputwasm-tools component embed+component newsucceeds without--skip-validation