BridgeJS: Support generic functions on imported JS APIs - #799
Merged
krodak merged 6 commits intoAug 13, 2026
Merged
Conversation
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
4 times, most recently
from
August 13, 2026 09:50
1c68d9c to
f3434b2
Compare
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
2 times, most recently
from
August 13, 2026 13:07
3841a35 to
bcea48f
Compare
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
from
August 13, 2026 13:52
bcea48f to
4f89a87
Compare
kateinoigakukun
approved these changes
Aug 13, 2026
kateinoigakukun
left a comment
Member
There was a problem hiding this comment.
Thanks! Feel free to merge once CI passed
krodak
force-pushed
the
kr/stack-abi-generics-import
branch
from
August 13, 2026 14:06
4f89a87 to
bf00750
Compare
krodak
enabled auto-merge
August 13, 2026 14:23
krodak
added a commit
to PassiveLogic/JavaScriptKit
that referenced
this pull request
Sep 16, 2026
Export half of generic function support (swiftwasm#398); the import half landed in swiftwasm#799 and this reuses its ABI: values cross on each type's existing stack layout, and a trailing i32 type ID per generic parameter selects the concrete type, so the per-function glue stays type-agnostic. @js public func identity<T: BridgedSwiftGenericBridgeable>(_ value: T) -> T { value } const n = exports.identity(42, BridgeTypes.Int); // TS erases generics, const p = exports.identity(pt, BridgeTypes.Point); // so callers pass a token How it works: - The wasm entry point is a concrete @_expose thunk taking the trailing type IDs. Because a type ID is the address of the type's BridgeJSTypeHandle, the thunk recovers the concrete type directly via Unmanaged.fromOpaque and reifies T through an opened existential (nested opening chain for multiple parameters); the body then runs the same pop-call-push sequence a concrete export would. - Generic thunks always hoist stack-using arguments: the JS wrapper lowers self and every argument in declaration order, so the pops must run in reverse declaration order ahead of any self pop embedded in the callee expression. - The JS wrapper resolves the caller's BridgeTypes token through the map built during type-handle registration and throws a catchable TypeError for unknown tokens before anything is lowered, so the shared value stack stays balanced. A raw wasm caller passing a garbage type ID is undefined behavior; the generated wrapper is the only supported caller. - Tokens are unqualified type names, so linking two modules that define same-named @js types fails the build while generics are in use. - Generics work on top-level functions and on instance and static methods of @js classes, structs, and enums, with T bare or wrapped as [T], T?, or [String: T]. - Type recovery needs the handle's metatype storage (an existential), so exported generic thunks are fatalError stubs under Embedded Swift; imports remain Embedded-compatible. Unsupported forms (async or throws generics, where clauses, unused generic parameters, concrete non-Void returns, generic initializers, default parameter values, nested wrappings) are rejected with build-time diagnostics.
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 (#398), split out of #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 #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 #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