Skip to content

BridgeJS: Add JSTypedArray convenience typealiases as recognized types - #9

Closed
krodak wants to merge 1 commit into
mainfrom
krodak/js-typed-array-bridgejs
Closed

BridgeJS: Add JSTypedArray convenience typealiases as recognized types#9
krodak wants to merge 1 commit into
mainfrom
krodak/js-typed-array-bridgejs

Conversation

@krodak

@krodakkrodak commented May 12, 2026

Copy link
Copy Markdown
Collaborator

Overview

Add JSTypedArray<T> as a recognized BridgeJS type, so users can use typed arrays directly in @JS function signatures. Both the generic form and convenience typealiases are supported:

// Generic form
@JSfunc processData(_ data:JSTypedArray<UInt8>)->JSTypedArray<UInt8>{...}
// Convenience typealias (equivalent)
@JSfunc processData(_ data:JSUint8Array)->JSUint8Array{...}

Both generate identical TypeScript:

processData(data: Uint8Array): Uint8Array;

Bridging is reference-based — passes the JSObject ID across the boundary with no data copying. The TypedArray lives on the JS heap.

This complements the separate numeric array optimization PR (#8) which makes [Int]/[Double] faster internally while keeping number[] in TypeScript. This PR gives users the option to use actual TypedArray types when they need them (e.g., for fetch body, WebGPU).

What changed

  • JSTypedArray.swift — 8 convenience typealiases: JSInt8Array, JSUint8Array, JSInt16Array, JSUint16Array, JSInt32Array, JSUint32Array, JSFloat32Array, JSFloat64Array
  • SwiftToSkeleton.swift — Pre-seed typed array types in the type resolver (same pattern as JSPromise). Recognize JSTypedArray<T> generic form and map to the corresponding typealias.
  • BridgeJSLink.swift — Map Swift typealias names to JS TypedArray constructor names in tsType (e.g., JSUint8ArrayUint8Array)
  • TS2Swift/processor.js — Map TypeScript TypedArray types to Swift typealias names in the TS importer (e.g., Uint8ArrayJSUint8Array), preventing import of full TypedArray API surface
  • Snapshot testsJSTypedArrayTypes.swift test input covering both typealias and generic forms
  • E2e runtime testsJSTypedArrayTests.swift + JSTypedArrayTests.mjs with round-trip tests for Uint8Array, Float32Array, Float64Array, Int32Array (both Swift→JS and JS→Swift directions, including empty arrays)

Type mapping

SwiftTypeScript
JSTypedArray<Int8> / JSInt8ArrayInt8Array
JSTypedArray<UInt8> / JSUint8ArrayUint8Array
JSTypedArray<Int16> / JSInt16ArrayInt16Array
JSTypedArray<UInt16> / JSUint16ArrayUint16Array
JSTypedArray<Int32> / JSInt32ArrayInt32Array
JSTypedArray<UInt32> / JSUint32ArrayUint32Array
JSTypedArray<Float> / JSFloat32ArrayFloat32Array
JSTypedArray<Double> / JSFloat64ArrayFloat64Array

Test coverage

  • 134 snapshot tests pass
  • 170 XCTest + 11 swift-testing e2e tests pass
  • Round-trip verified for all 4 TypedArray types from both Swift and JS sides

@krodak
krodakforce-pushed the krodak/js-typed-array-bridgejs branch from 315d7be to 87d9404CompareMay 12, 2026 22:53
Add JSInt8Array, JSUint8Array, JSInt16Array, JSUint16Array, JSInt32Array,
JSUint32Array, JSFloat32Array, JSFloat64Array typealiases and pre-seed
them in SwiftToSkeleton so BridgeJS recognizes them in @js signatures.
Users can now write:
@js func processData(_ data: JSUint8Array) -> JSUint8Array
Generated TypeScript uses native typed array names:
processData(data: Uint8Array): Uint8Array
Bridging is reference-based (passes JSObject ID, no data copying).
Follows the existing JSPromise pre-seeding pattern.
@krodak

Copy link
Copy Markdown
CollaboratorAuthor

Tracked in new PR against main repo

@krodakkrodak closed this May 13, 2026
Sign up for freeto 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

@krodak