Skip to content

BridgeJS: Support JSUniqueRef in function signatures - #845

Open
kateinoigakukun wants to merge 1 commit into
mainfrom
katei/bridgejs-jsuniqueref
Open

kateinoigakukun wants to merge 1 commit into
mainfrom
katei/bridgejs-jsuniqueref

Conversation

@kateinoigakukun

Copy link
Copy Markdown
Member

Overview

This PR lets BridgeJS signatures use JSUniqueRef and JSUniqueRef?. It is the next step in landing #829 incrementally, after #830.

@JSFunction func makeNode() throws(JSException) -> JSUniqueRef
@JSFunction func appendNode(_ node: borrowing JSUniqueRef) throws(JSException)

@JS func identity(_ node: consuming JSUniqueRef) -> JSUniqueRef { node }

1. Supported positions

  • Imports: parameters of @JSFunction (including async functions and initializers) and @JSSetter, and synchronous returns.
  • Exports: parameters and returns of synchronous @JS functions and methods, and parameters of class initializers.

2. ABI

JSUniqueRef uses the same ABI as JSObject, so the JS runtime needs no changes. Generated import thunks always take it as borrowing, which works for both borrowing and consuming user declarations, so the skeleton does not record ownership. Exported returns hand the reference over to JS without the extra retain and release JSObject needs.

3. Diagnostics

Other positions are rejected with an error: properties, struct fields, enum payloads, arrays, dictionaries, JSUndefinedOr, closures, protocols, @JS(as:), async exports and async import returns. @JSGetter is among them because Swift does not allow a throwing getter of a noncopyable type.

@JSClass struct C: ~Copyable, TS2Swift support and UniqueArray are left out of this PR.

Testing

Added codegen and link snapshots, diagnostics tests, and runtime tests that also check the JS object heap for leaked references. The BridgeJS tests and make unittest pass locally with Swift 6.4. The runtime tests have not been run with Swift 6.3 locally.

Bridge `JSUniqueRef` and `JSUniqueRef?` parameters and synchronous returns with the same ABI as `JSObject`.
Diagnose positions that cannot hold a noncopyable type yet, such as properties, containers, closures and async returns.
let parameters = parseParameters(
from: node.signature.parameterClause,
allowDefaults: true,
allowsNoncopyable: false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@JS struct S { @JS func f(_ r: consuming JSUniqueRef) -> JSUniqueRef } is accepted and gives the expected thunk. The class initializer path uses allowsNoncopyable: asyncSpecifier == nil, but the struct initializer path here uses allowsNoncopyable: false, so @JS init(_ ref: consuming JSUniqueRef) on a struct is diagnosed. A struct can't store a JSUniqueRef field yet, but an initializer could still consume one. The DocC says "class initializers", so this may be deliberate and I may be reading the intent wrong.

If it isn't deliberate:

let parameters = parseParameters(
    from: node.signature.parameterClause,
    allowDefaults: true,
    allowsNoncopyable: node.signature.effectSpecifiers?.asyncSpecifier == nil
)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants