BridgeJS: Support optional @JS struct in imported function signatures#755
Merged
Merged
Conversation
Optional @js structs could not be used as parameters or return values of imported (@JSFunction) signatures: the generator lowered Optional<Struct> using the non-optional object-id ABI ([isSome, objectId] / a single Int32 return), for which no Optional lowering exists, so the generated thunk did not compile. Bridge optional @js structs through the stack ABI instead - an isSome discriminator plus the struct fields - exactly like optional arrays and dictionaries. Structs already conform to the stack-based bridging protocols, so the existing _BridgedAsOptional/stack runtime extensions and the JS link's stack handling already support this; only the import-side lowering/lifting in the code generator needed to change. Adds a jsRoundTripOptionalPoint runtime round-trip (some + none) and a SwiftStructImports codegen snapshot.
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.
Overview
Optional
@JS structs could not be used as parameters or return values of imported (@JSFunction) signatures. While an optional primitive works, an optional@JS structdid not:The code generator lowered
Optional<Point>using the non-optional object-id ABI ([isSome, objectId]for parameters, a singleInt32word for returns), for which noOptionallowering exists in the runtime, so the generated thunk failed to compile.This change bridges optional
@JS structs through the stack ABI instead, anisSomediscriminator plus the struct fields, exactly like optional arrays and dictionaries.@JS structs already conform to the stack-based bridging protocols, so the runtime's_BridgedAsOptional/stack extensions and the JS link's stack handling already support this shape. Only the import-side lowering and lifting in the code generator needed to change:1. Parameter lowering.
.nullable(.swiftStruct)in the import context now lowers to a singleisSomeflag, with the struct transferred on the stack, rather than appending the non-optionalobjectIdword.2. Return lifting. The optional struct return is now lifted from the stack rather than from a single object-id word.
Generated thunk, before:
after:
The export side already used the stack ABI for optional structs and is unchanged. The change is guarded to the import context, so non-optional struct imports keep their existing object-id ABI.
Adds an end-to-end runtime round-trip test (
someandnone) and a codegen snapshot covering the new import shape.