Repository navigation
BridgeJS: Support JSUniqueRef in function signatures - #845
Open
kateinoigakukun wants to merge 1 commit into
Open
kateinoigakukun wants to merge 1 commit into
kateinoigakukun wants to merge 1 commit into
Conversation
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.
krodak
approved these changes
Dec 2, 2026
| let parameters = parseParameters( | ||
| from: node.signature.parameterClause, | ||
| allowDefaults: true, | ||
| allowsNoncopyable: false |
Member
There was a problem hiding this comment.
@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
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.
Overview
This PR lets BridgeJS signatures use
JSUniqueRefandJSUniqueRef?. It is the next step in landing #829 incrementally, after #830.1. Supported positions
@JSFunction(includingasyncfunctions and initializers) and@JSSetter, and synchronous returns.@JSfunctions and methods, and parameters of class initializers.2. ABI
JSUniqueRefuses the same ABI asJSObject, so the JS runtime needs no changes. Generated import thunks always take it asborrowing, which works for bothborrowingandconsuminguser declarations, so the skeleton does not record ownership. Exported returns hand the reference over to JS without the extra retain and releaseJSObjectneeds.3. Diagnostics
Other positions are rejected with an error: properties, struct fields, enum payloads, arrays, dictionaries,
JSUndefinedOr, closures, protocols,@JS(as:),asyncexports andasyncimport returns.@JSGetteris among them because Swift does not allow a throwing getter of a noncopyable type.@JSClass struct C: ~Copyable, TS2Swift support andUniqueArrayare 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 unittestpass locally with Swift 6.4. The runtime tests have not been run with Swift 6.3 locally.