BridgeJS: Simplify Stack ABI for Optional#674
Merged
kateinoigakukun merged 1 commit intomainfrom Feb 25, 2026
Merged
Conversation
7045664 to
c5ec962
Compare
c5ec962 to
8641276
Compare
Now `Optional` conforms to `_BridgedSwiftStackType` whenever `Wrapped`
conforms to `_BridgedSwiftStackType`. This makes generic programming
easier and reduces boilerplate in intrinsics and codegen.
For specialized handling of certain types like associated value enums,
where the presence of a value is encoded as a sentinel case ID (-1),
we implement `bridgeJSStack{Push,Pop}AsOptional` methods, which is on
the witness table of `_BridgedSwiftStackType`.
Also now some of optional types no longer pushes placeholders values for
`nil` cases when pushing in JS and popping in Swift.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR simplifies the Stack ABI for Optional types in BridgeJS by making Optional conform to _BridgedSwiftStackType whenever its Wrapped type does. This architectural change reduces boilerplate and makes generic programming easier.
Changes:
- Introduces specialization methods
bridgeJSStackPopAsOptionalandbridgeJSStackPushAsOptionalon the_BridgedSwiftStackTypeprotocol - Makes
Optionalconform to_BridgedSwiftStackTypewith a conditional conformance - Removes specialized
bridgeJSStackPopimplementations from various Optional extensions (for scalar types, strings, JSObject, heap objects, case enums, etc.) - Implements efficient optional handling for associated value enums using -1 as a sentinel instead of separate discriminator
- Removes emission of placeholder values for nil cases in JavaScript codegen
- Simplifies Swift codegen by removing optional-specific unwrapping boilerplate
Reviewed changes
Copilot reviewed 12 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Sources/JavaScriptKit/BridgeJSIntrinsics.swift | Adds specialization methods to _BridgedSwiftStackType, makes Optional conform conditionally, removes specialized Optional implementations, adds -1 sentinel for optional enums |
| Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift | Removes optional-specific codegen, simplifies to unified bridgeJSStackPush calls |
| Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift | Removes placeholder emission logic, adds specialized handling for optional associated value enums |
| Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift | Generated Swift test code showing simplified optional handling without placeholder boilerplate |
| Plugins/BridgeJS/Tests/BridgeJSToolTests/Snapshots/BridgeJSLinkTests/*.js | Generated JavaScript showing no placeholder emission for nil, conditional value pushing |
| Plugins/BridgeJS/Tests/BridgeJSToolTests/Snapshots/BridgeJSCodegenTests/*.swift | Generated Swift showing simplified bridgeJSStackPush calls without manual unwrapping |
| Benchmarks/Sources/Generated/BridgeJS.swift | Benchmark code updated with simplified optional handling |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
8641276 to
68e50f3
Compare
MaxDesiatov
reviewed
Feb 25, 2026
| @JSFunction static func jsRoundTripOptionalJSValueArrayNull( | ||
| _ v: Optional<[JSValue]> | ||
| ) throws(JSException) -> Optional<[JSValue]> | ||
| // @JSFunction static func jsRoundTripOptionalJSValueArrayUndefined( |
Member
There was a problem hiding this comment.
are these supposed to be commented out?
Member
Author
There was a problem hiding this comment.
Yes, they are not supported and will be followed up later PRs
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.
Now
Optionalconforms to_BridgedSwiftStackTypewheneverWrappedconforms to_BridgedSwiftStackType. This makes generic programming easier and reduces boilerplate in intrinsics and codegen.For specialized handling of certain types like associated value enums, where the presence of a value is encoded as a sentinel case ID (-1), we implement
bridgeJSStack{Push,Pop}AsOptionalmethods, which is on the witness table of_BridgedSwiftStackType.Also now some of optional types no longer pushes placeholders values for
nilcases when pushing in JS and popping in Swift.