Skip to content

BridgeJS: Change @JS init to generate static init() methods instead of constructor in JS #409

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

Merged
merged 10 commits into from
Aug 15, 2025

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Aug 15, 2025

Changes @JS init to generate static init() methods instead of direct constructors to separate two distinct Swift class instantiation patterns.

Problem

Previously, new SwiftClass() was used for both:

  1. Calling Swift @JS init constructors
  2. Lifting Swift class instances returned from exposed Swift functions (Swift side returns pointer, JS glue code lifts it into wrapper object)

These two operations should not share the same API as they have fundamentally different semantics.

Solution

  • @JS init now generates static init() methods for Swift constructor calls
  • new SwiftClass() is reserved for lifting Swift instances returned from Swift functions
  • Fixed handling of Swift classes without @JS init constructors

Changes on generated interface

// Before
export type Exports = {
    Greeter: {
        new(name: string): Greeter;
    }
}
const { exports } = await instantiate(...)
const greeter = new exports.Greeter("world");

// After
export type Exports = {
    Greeter: {
        init(name: string): Greeter;
    }
}
const { exports } = await instantiate(...)
const greeter = exports.Greeter.init("world");

@kateinoigakukun kateinoigakukun merged commit 57a6fa2 into main Aug 15, 2025
9 checks passed
@kateinoigakukun kateinoigakukun deleted the yt/bjs-fix-swift-class branch August 15, 2025 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant