Repository navigation
BridgeJS: Support Span<UInt8>/MutableSpan<UInt8> for @JSFunction parameters - #833
MaxDesiatov wants to merge 1 commit into
Conversation
|
cc @sliemeobn |
Span<UInt8>/MutableSpan<UInt8> for UInt8 parameters| @JSGetter(from: .global) var crypto: Crypto | ||
|
|
||
| var key = [UInt8](repeating: 0, count: 32) | ||
| var span = key.mutableSpan |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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 { | |||
There was a problem hiding this comment.
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: [ |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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
|
The macOS native job fails because Xcode 26.0.1 is still on Swift 6.2, which rejects mutating on ~Escapable self ( |
|
@MaxDesiatov I think it's great to have access to "zero-copy" BridgeJS tools like this. 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 |
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.
9c31e91 to
445bd73
Compare
A
@JSFunctionwith aSpan<UInt8>parameter failed to build, because BridgeJS dropped the parameter from the generated thunk. JavaScript now receives aUint8Arrayview of Wasm memory, valid until the call returns. Shared-memory builds pass a copy and write aMutableSpancopy back when the call returns or throws.