Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

@krodak
Copy link
Member

Introduction

This PR adds support for Swift Optionals when exporting Swift funcionality to JS / TS using BridgeJS plugin.

Overview

  • Swift Optionals are represented byT | null union, asnull explicitly represents intentional absent of value, which aligns with Swift's nil.
  • for each parameter additional "isSome" flag is used to represent optionality.
  • new sharedextension 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
  • for support of return values, new bjs module methods were added for JS lowering_swift_js_return_optional_<type>
  • support for different Optional syntax was added (including typealiasing), so all of the below should work identically:
@JSfunc test(name:String?)->String?@JS func test(name:Optional<String>)->Optional<String>@JSfunc test(name:Swift.Optional<String>)->Swift.Optional<String>@JSfunc test(name:Swift.Optional<String>)->Swift.Optional<String.>typealias OptionalName= String?@JSfunc test(name: OptionalName)->OptionalName

Examples

@JSclassUserService{varuserId:Int?func findUser(id:Int)->User?func process(data:String?)->String}

Generated TypeScript:

exportinterfaceUserServiceextendsSwiftHeapObject{userId:number|null;findUser(id:number|null):User|null;process(data:string|null):string;}

Testing

Added tests for different scenarios for all supported optional data types, including parameters, properties and return value usage.

Documentation

Extended current documentation with newExporting-Swift-Optional.md

BridgeJS: Optional case enum, optional raw enum supportBridgeJS: Simplify JS glue code as much as possible + basic docs
@krodakkrodak self-assigned thisSep 10, 2025
@krodakkrodakforce-pushed thefeat/optionals-support branch 2 times, most recently from3f55206 to8ecbbd4CompareSeptember 10, 2025 12:56
Copy link

CopilotAI left a 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 toT | null union types in TypeScript, usingnull to represent the absence of a value which aligns with Swift'snil 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
FileDescription
Sources/JavaScriptKit/BridgeJSInstrincics.swiftAdds extensive optional bridging support with new protocols and extension methods
Sources/JavaScriptKit/Documentation.docc/Articles/BridgeJS/Exporting-Swift/Exporting-Swift-Optional.mdComprehensive documentation for optional types usage
Tests/BridgeJSRuntimeTests/ExportAPITests.swiftTest functions for all optional type scenarios
Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swiftUpdates type system to include optional case
Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swiftCore optional type resolution and code generation logic
Comments suppressed due to low confidence (1)

Sources/JavaScriptKit/BridgeJSInstrincics.swift:1

  • The function should be marked asconsuming 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 orlearn how to get started.

@JSfunctest(name:String?)->String? {return name }
@JSfunctest(name:Optional<String>)->Optional<String> {return name }
@JSfunctest(name: Swift.Optional<String>)-> Swift.Optional<String> {return name }
@JSfunctest(name: Swift.Optional<String >)-> Swift.Optional<String. > {return name }

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 beString >.

Suggested change
@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.
Comment on lines +129 to +131
tmpRetString=null;
}else{
tmpRetString=swift.memory.getObject(objectId);

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 totmpRetString for object return storage, but this should likely be a different variable for optional object returns.

Copilot uses AI. Check for mistakes.
Copy link
Member

@kateinoigakukunkateinoigakukun left a comment

Choose a reason for hiding this comment

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

Great job 👏

@kateinoigakukunkateinoigakukun merged commit79a0a4e intoswiftwasm:mainSep 10, 2025
9 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@kateinoigakukunkateinoigakukunkateinoigakukun approved these changes

Assignees

@krodakkrodak

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@krodak@kateinoigakukun

[8]ページ先頭

©2009-2025 Movatter.jp