BridgeJS: Support generic functions on imported JS APIs - #18
Closed
krodak wants to merge 2 commits into
Closed
Conversation
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
2 times, most recently
from
August 10, 2026 13:04
c995dfb to
adbbb6f
Compare
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
from
August 10, 2026 13:33
adbbb6f to
3561938
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
Import half of generic function support (swiftwasm#398), split out of swiftwasm#787 per review. An imported
@JSFunctioncan take a type parameter, so one declaration covers every bridged type:Tcan be any primitive,String,JSValue, or a@JSstruct/enum/final class, bare or asT?,[T],[String: T]. Also works on@JSClassinits, methods and statics, and for return-only generics. Exported generics are rejected with a diagnostic and land separately on top of this ABI.How it works
i32type ID per type parameter, so type-agnostic JS glue can pick the right lift/lower.BridgeJSTypeHandle, its address is the ID. Same-named types in different modules can't collide, and no existentials means it works under Embedded.bjs_<Module>_register_type_handles, which hands its IDs to JS; JS pairs them with its codec table by index. Runs eagerly via a newafterInitializeinstantiator hook, or lazily on first generic call (worker threads).@JStype, not just in modules that declare generics, module B can pass module A's type to a generic function (per BridgeJS: Support generic functions at the Swift and JavaScript boundary swiftwasm/JavaScriptKit#787 review).__bjs_arrayCodecetc.); the non-generic array/dict/optional paths now reuse them instead of inlining a copy per thunk (per BridgeJS: Support generic functions at the Swift and JavaScript boundary swiftwasm/JavaScriptKit#787 review on the stack ABI clones).Builds without generics don't get the JS generic runtime, just a no-op registration hook per module with
@JStypes.Test plan
ImportGenericAPITests: runtime round-trip of every bridgeable type through real JS, includingJSON.parseExamples/Embeddedbuilds a generic round-trip in CI