-
-
Notifications
You must be signed in to change notification settings - Fork 55
BridgeJS: Swift -> JS Optionals support #444
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BridgeJS: Swift -> JS Optionals support #444
Conversation
BridgeJS: Optional case enum, optional raw enum support BridgeJS: Simplify JS glue code as much as possible + basic docs
3f55206
to
8ecbbd4
Compare
8ecbbd4
to
6091dfc
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR adds comprehensive support for Swift Optional types when exporting Swift functionality to JavaScript/TypeScript using the BridgeJS plugin. Swift optionals are mapped to T | null
union types in TypeScript, using null
to represent the absence of a value which aligns with Swift's nil
semantics.
Key changes include:
- Support for all optional syntax forms:
T?
,Optional<T>
,Swift.Optional<T>
, and type aliases - Bidirectional optional handling for parameters, return values, and class properties
- New intrinsic functions for optional type bridging and lowering operations
Reviewed Changes
Copilot reviewed 57 out of 65 changed files in this pull request and generated 6 comments.
Show a summary per file
File | Description |
---|---|
Sources/JavaScriptKit/BridgeJSInstrincics.swift | Adds extensive optional bridging support with new protocols and extension methods |
Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Optional.md | Comprehensive documentation for optional types usage |
Tests/BridgeJSRuntimeTests/ExportAPITests.swift | Test functions for all optional type scenarios |
Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift | Updates type system to include optional case |
Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift | Core optional type resolution and code generation logic |
Comments suppressed due to low confidence (1)
Sources/JavaScriptKit/BridgeJSInstrincics.swift:1
- The function should be marked as
consuming
like other similar functions in this file to match the pattern used elsewhere for bridge lowering functions.
/// BridgeJS Intrinsics
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
@JS func test(name: String?) -> String? { return name } | ||
@JS func test(name: Optional<String>) -> Optional<String> { return name } | ||
@JS func test(name: Swift.Optional<String>) -> Swift.Optional<String> { return name } | ||
@JS func test(name: Swift.Optional< String >) -> Swift.Optional< String. > { return name } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a typo with an extra period before the closing angle bracket: String. >
should be String >
.
@JS func test(name: Swift.Optional< String >) -> Swift.Optional< String. > { return name } | |
@JS func test(name: Swift.Optional< String >) -> Swift.Optional< String > { return name } |
Copilot uses AI. Check for mistakes.
tmpRetString = null; | ||
} else { | ||
tmpRetString = swift.memory.getObject(objectId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrong variable assignment: should assign to tmpRetString
for object return storage, but this should likely be a different variable for optional object returns.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job 👏
Introduction
This PR adds support for Swift Optionals when exporting Swift funcionality to JS / TS using BridgeJS plugin.
Overview
T | null
union, asnull
explicitly represents intentional absent of value, which aligns with Swift's nil.extension Optionals
were added toBridgeJSIntrinsics
to keep generated Swift glue code sparse and reuse existing patterns. This required new protocols for case and associated enum value types, which_swift_js_return_optional_<type>
Examples
Generated TypeScript:
Testing
Added tests for different scenarios for all supported optional data types, including parameters, properties and return value usage.
Documentation
Extended current documentation with new
Exporting-Swift-Optional.md