Skip to content

Add JSRemote<JSObject>#711

Merged
kateinoigakukun merged 1 commit intomainfrom
issue-704
Apr 2, 2026
Merged

Add JSRemote<JSObject>#711
kateinoigakukun merged 1 commit intomainfrom
issue-704

Conversation

@kateinoigakukun
Copy link
Copy Markdown
Member

@kateinoigakukun kateinoigakukun commented Mar 31, 2026

#704

Add JSRemote<JSObject> API for accessing JSObjects without transfer

The new API allows creating a handle for a JSObject that remains
on its original JavaScript thread and hopping back to that thread
to access the object when needed. This is useful for cases where the
object cannot be transferred to another thread, but occasional access
is still required or when we want to guarantee that an object is always
accessed on the same thread for safety (it should be statically
guaranteed with strict Sendable checking, but modules with language mode
5 don't have that).

Example:

let document = JSObject.global.document.object!
let remoteDocument = JSRemote(document)

let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
let title = try await Task(executorPreference: executor) {
    try await remoteDocument.withJSObject { document in
        document.title.string ?? ""
    }
}.value

@kateinoigakukun kateinoigakukun changed the title Add JSRemote<JSObject> with typed-throws owner-thread access Add JSRemote<JSObject> Mar 31, 2026
@kateinoigakukun kateinoigakukun force-pushed the issue-704 branch 3 times, most recently from c82cab3 to e032560 Compare April 2, 2026 15:58
@kateinoigakukun kateinoigakukun marked this pull request as ready for review April 2, 2026 15:58
The new API allows creating a handle for a `JSObject` that remains
on its original JavaScript thread and hopping back to that thread
to access the object when needed. This is useful for cases where the
object cannot be transferred to another thread, but occasional access
is still required or when we want to guarantee that an object is always
accessed on the same thread for safety (it should be statically
guaranteed with strict Sendable checking, but modules with language mode
5 don't have that).

Example:

```swift
let document = JSObject.global.document.object!
let remoteDocument = JSRemote(document)

let executor = try await WebWorkerTaskExecutor(numberOfThreads: 1)
let title = try await Task(executorPreference: executor) {
    try await remoteDocument.withJSObject { document in
        document.title.string ?? ""
    }
}.value
```
@kateinoigakukun kateinoigakukun merged commit df32a66 into main Apr 2, 2026
12 checks passed
@kateinoigakukun kateinoigakukun deleted the issue-704 branch April 2, 2026 16:27
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