Skip to content

BridgeJS: Support generic functions on imported JS APIs - #799

Merged
krodak merged 6 commits into
swiftwasm:mainfrom
PassiveLogic:kr/stack-abi-generics-import
Aug 13, 2026
Merged

BridgeJS: Support generic functions on imported JS APIs#799
krodak merged 6 commits into
swiftwasm:mainfrom
PassiveLogic:kr/stack-abi-generics-import

Conversation

@krodak

@krodak krodak commented Aug 10, 2026

Copy link
Copy Markdown
Member

Summary

Import half of generic function support (#398), split out of #787 per review. An imported @JSFunction can take a type parameter, so one declaration covers every bridged type:

@JSFunction func parse<T: BridgedSwiftGenericBridgeable>(_ json: String) throws(JSException) -> T

let user: User = try parse(jsonString)   // T inferred from the call site

T can be any primitive, String, JSValue, or a @JS struct/enum/final class, bare or as T?, [T], [String: T]. Also works on @JSClass inits, 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

  • Values cross on the existing stack ABI. The only new thing on the wire is an i32 type ID per type parameter, so type-agnostic JS glue can pick the right lift/lower.
  • Type IDs are pointer-based, not name-based (per BridgeJS: Support generic functions at the Swift and JavaScript boundary #787 review): each type gets a BridgeJSTypeHandle, its address is the ID. Same-named types in different modules can't collide, and no existentials means it works under Embedded.
  • Each module exports bjs_<Module>_register_type_handles, which hands its IDs to JS; JS pairs them with its codec table by index. Runs eagerly via a new afterInitialize instantiator hook, or lazily on first generic call (worker threads).
  • Conformances are emitted for every @JS type, 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 #787 review).
  • JS containers get one shared codec each (__bjs_arrayCodec etc.); 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 #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 @JS types.

Test plan

  • Codegen + link snapshots: free functions, inits, methods, statics, mixed/multiple params, return-only, wrapped forms
  • Diagnostics: import constraints, export rejection
  • ImportGenericAPITests: runtime round-trip of every bridgeable type through real JS, including JSON.parse
  • Examples/Embedded builds a generic round-trip in CI
  • Cross-module registration + runtime gating link tests

@krodak krodak self-assigned this Aug 10, 2026
@krodak
krodak requested a review from kateinoigakukun August 10, 2026 14:13
Comment thread Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift Outdated
@krodak
krodak force-pushed the kr/stack-abi-generics-import branch 4 times, most recently from 1c68d9c to f3434b2 Compare August 13, 2026 09:50
@krodak
krodak force-pushed the kr/stack-abi-generics-import branch 2 times, most recently from 3841a35 to bcea48f Compare August 13, 2026 13:07
@krodak
krodak force-pushed the kr/stack-abi-generics-import branch from bcea48f to 4f89a87 Compare August 13, 2026 13:52

@kateinoigakukun kateinoigakukun left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Feel free to merge once CI passed

@krodak
krodak force-pushed the kr/stack-abi-generics-import branch from 4f89a87 to bf00750 Compare August 13, 2026 14:06
@krodak
krodak enabled auto-merge August 13, 2026 14:23
@krodak
krodak merged commit 3966530 into swiftwasm:main Aug 13, 2026
16 checks passed
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