Skip to content

BridgeJS: Add JSTypedArray convenience typealiases as recognized types#9

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

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

Conversation

@krodak
Copy link
Copy Markdown
Collaborator

@krodak krodak commented May 12, 2026

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
@JS func processData(_ data: JSTypedArray<UInt8>) -> JSTypedArray<UInt8> { ... }

// Convenience typealias (equivalent)
@JS func 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)
  • TestsJSTypedArrayTypes.swift test input covering both typealias and generic forms

Type mapping

Swift TypeScript
JSTypedArray<Int8> / JSInt8Array Int8Array
JSTypedArray<UInt8> / JSUint8Array Uint8Array
JSTypedArray<Int16> / JSInt16Array Int16Array
JSTypedArray<UInt16> / JSUint16Array Uint16Array
JSTypedArray<Int32> / JSInt32Array Int32Array
JSTypedArray<UInt32> / JSUint32Array Uint32Array
JSTypedArray<Float> / JSFloat32Array Float32Array
JSTypedArray<Double> / JSFloat64Array Float64Array

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 krodak force-pushed the krodak/js-typed-array-bridgejs branch from 315d7be to 87d9404 Compare May 12, 2026 22:53
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