Skip to content

BridgeJS: Support Span<UInt8>/MutableSpan<UInt8> for @JSFunction parameters - #833

Open
MaxDesiatov wants to merge 1 commit into
mainfrom
maxd/bridgejs-span
Open

MaxDesiatov wants to merge 1 commit into
mainfrom
maxd/bridgejs-span

Conversation

@MaxDesiatov

Copy link
Copy Markdown
Member

A @JSFunction with a Span<UInt8> parameter failed to build, because BridgeJS dropped the parameter from the generated thunk. JavaScript now receives a Uint8Array view of Wasm memory, valid until the call returns. Shared-memory builds pass a copy and write a MutableSpan copy back when the call returns or throws.

@MaxDesiatov

Copy link
Copy Markdown
Member Author
@MaxDesiatov MaxDesiatov added the enhancement New feature or request label Nov 24, 2026
@MaxDesiatov MaxDesiatov changed the title BridgeJS: Support Span<UInt8>/MutableSpan<UInt8> for UInt8 parameters Nov 24, 2026
@JSGetter(from: .global) var crypto: Crypto

var key = [UInt8](repeating: 0, count: 32)
var span = key.mutableSpan

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.

The example looks like top-level code, but var span = key.mutableSpan doesn't compile there. With Swift 6.4 I get "lifetime-dependent value escapes its scope". It works inside a function or a do block, same as the runtime tests do it, so wrapping it could save some confusion for people copying it:

var key = [UInt8](repeating: 0, count: 32)
do {
    var span = key.mutableSpan
    try crypto.getRandomValues(&span)
}

UTY

@testable import BridgeJSMacros
@testable import BridgeJSSkeleton

@Suite struct SpanParameterMacroAgreementTests {

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.

The new JSFunctionMacroTests cases already check the same things as this suite (user type named MutableSpan, MyModule.MutableSpan, plain inout). Dropping it would also remove the extra BridgeJSMacros dependency on the tool tests in Package.swift ✂️

UTY

@@ -0,0 +1,12 @@
import Testing

@Suite struct SpanParameterGlueTests {

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.

Between the SpanParameters.SharedMemory snapshot and the runtime tests, which also run on the threads target in CI, I think the shared and unshared glue is already covered. Without this script and Inputs/SpanParameterGlueTests.mjs, the StringDecoderTests refactor could go back to how it was as well.

UTY

#expect(function.parameters.map(\.type) == [.jsObject(typeName)])
}

@Test(arguments: [

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.

These two go further on spellings than we usually do for other types, e.g. Swift . UInt8, ((Swift.UInt8)) or Swift.Swift.UInt8. A case or two in each would probably do.

UTY

)
}

@Test func thunkQualifiesStandardLibrarySpans() throws {

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.

The SpanParameters.swift snapshot already has Swift.Span<UInt8> and Swift.MutableSpan<UInt8> in the thunks, and userTypeNamedSpanKeepsItsType covers user types named Span, so I'm not sure this one adds much ✂️

UTY

@krodak

krodak commented Nov 24, 2026

Copy link
Copy Markdown
Member

The macOS native job fails because Xcode 26.0.1 is still on Swift 6.2, which rejects mutating on ~Escapable self (BridgeJSIntrinsics.swift:1847), I'll open MR with a bump

@sliemeobn

Copy link
Copy Markdown
Contributor

@MaxDesiatov I think it's great to have access to "zero-copy" BridgeJS tools like this.
For things like crypto.getRandomValues or printing a Swift-owned line buffer to the console this should work great. maybe mutable spans can be used to improve String transfers (JS -> Swift) somehow (save JS allocations at least)?

imho there is a real downside to the rather long fine print for using these on the JS side (mutability, lifetime, risk of detachment, shared/non-shared memory, ...), but ultimately there is no way around these JS/wasm memory quirks I think.

do you see this in addition to a hypothetical ExchangeBuffer/(Mutable)ExchangeSpan type, or do you think support for spans is all we need?

@MaxDesiatov

Copy link
Copy Markdown
Member Author

do you see this in addition to a hypothetical ExchangeBuffer/(Mutable)ExchangeSpan type, or do you think support for spans is all we need?

IIUC these would only help to avoid copies in multi-threaded builds through dedicated non-shared memory. And it remains to be seen how much effort we need in terms of toolchain support for these to pay off. So far I haven't found these hypothetical types bringing any benefits in single-threaded builds.

…meters

A `@JSFunction` with a `Span<UInt8>` parameter failed to build, because BridgeJS dropped the parameter from the generated thunk. JavaScript now receives a `Uint8Array` view of Wasm memory, valid until the call returns. Shared-memory builds pass a copy and write a `MutableSpan` copy back when the call returns or throws.

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

enhancement New feature or request

3 participants