Skip to content

Commit 315d7be

Browse files
committed
BridgeJS: Add JSTypedArray convenience typealiases as recognized types
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.
1 parent 5e96639 commit 315d7be

8 files changed

Lines changed: 424 additions & 0 deletions

File tree

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public final class SwiftToSkeleton {
4242
self.typeDeclResolver.addSourceFile(
4343
"""
4444
@JSClass struct JSPromise {}
45+
@JSClass struct JSInt8Array {}
46+
@JSClass struct JSUint8Array {}
47+
@JSClass struct JSInt16Array {}
48+
@JSClass struct JSUint16Array {}
49+
@JSClass struct JSInt32Array {}
50+
@JSClass struct JSUint32Array {}
51+
@JSClass struct JSFloat32Array {}
52+
@JSClass struct JSFloat64Array {}
4553
"""
4654
)
4755
}

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3594,6 +3594,9 @@ extension BridgeType {
35943594
case .bool:
35953595
return "boolean"
35963596
case .jsObject(let name):
3597+
if let name, let tsName = Self.jsTypedArrayTSNames[name] {
3598+
return tsName
3599+
}
35973600
return name ?? "any"
35983601
case .jsValue:
35993602
return "any"
@@ -3630,6 +3633,18 @@ extension BridgeType {
36303633
return "Record<string, \(valueType.tsType)>"
36313634
}
36323635
}
3636+
3637+
/// Maps JSTypedArray Swift typealias names to their JavaScript TypedArray constructor names.
3638+
private static let jsTypedArrayTSNames: [String: String] = [
3639+
"JSInt8Array": "Int8Array",
3640+
"JSUint8Array": "Uint8Array",
3641+
"JSInt16Array": "Int16Array",
3642+
"JSUint16Array": "Uint16Array",
3643+
"JSInt32Array": "Int32Array",
3644+
"JSUint32Array": "Uint32Array",
3645+
"JSFloat32Array": "Float32Array",
3646+
"JSFloat64Array": "Float64Array",
3647+
]
36333648
}
36343649

36353650
extension WasmCoreType {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import JavaScriptKit
2+
3+
@JS func processBytes(_ data: JSUint8Array) -> JSUint8Array {
4+
return data
5+
}
6+
7+
@JS func processFloats(_ data: JSFloat32Array) -> JSFloat32Array {
8+
return data
9+
}
10+
11+
@JS func processDoubles(_ data: JSFloat64Array) -> JSFloat64Array {
12+
return data
13+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
{
2+
"exported" : {
3+
"classes" : [
4+
5+
],
6+
"enums" : [
7+
8+
],
9+
"exposeToGlobal" : false,
10+
"functions" : [
11+
{
12+
"abiName" : "bjs_processBytes",
13+
"effects" : {
14+
"isAsync" : false,
15+
"isStatic" : false,
16+
"isThrows" : false
17+
},
18+
"name" : "processBytes",
19+
"parameters" : [
20+
{
21+
"label" : "_",
22+
"name" : "data",
23+
"type" : {
24+
"jsObject" : {
25+
"_0" : "JSUint8Array"
26+
}
27+
}
28+
}
29+
],
30+
"returnType" : {
31+
"jsObject" : {
32+
"_0" : "JSUint8Array"
33+
}
34+
}
35+
},
36+
{
37+
"abiName" : "bjs_processFloats",
38+
"effects" : {
39+
"isAsync" : false,
40+
"isStatic" : false,
41+
"isThrows" : false
42+
},
43+
"name" : "processFloats",
44+
"parameters" : [
45+
{
46+
"label" : "_",
47+
"name" : "data",
48+
"type" : {
49+
"jsObject" : {
50+
"_0" : "JSFloat32Array"
51+
}
52+
}
53+
}
54+
],
55+
"returnType" : {
56+
"jsObject" : {
57+
"_0" : "JSFloat32Array"
58+
}
59+
}
60+
},
61+
{
62+
"abiName" : "bjs_processDoubles",
63+
"effects" : {
64+
"isAsync" : false,
65+
"isStatic" : false,
66+
"isThrows" : false
67+
},
68+
"name" : "processDoubles",
69+
"parameters" : [
70+
{
71+
"label" : "_",
72+
"name" : "data",
73+
"type" : {
74+
"jsObject" : {
75+
"_0" : "JSFloat64Array"
76+
}
77+
}
78+
}
79+
],
80+
"returnType" : {
81+
"jsObject" : {
82+
"_0" : "JSFloat64Array"
83+
}
84+
}
85+
}
86+
],
87+
"protocols" : [
88+
89+
],
90+
"structs" : [
91+
92+
]
93+
},
94+
"moduleName" : "TestModule",
95+
"usedExternalModules" : [
96+
97+
]
98+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@_expose(wasm, "bjs_processBytes")
2+
@_cdecl("bjs_processBytes")
3+
public func _bjs_processBytes(_ data: Int32) -> Int32 {
4+
#if arch(wasm32)
5+
let ret = processBytes(_: JSUint8Array.bridgeJSLiftParameter(data))
6+
return ret.bridgeJSLowerReturn()
7+
#else
8+
fatalError("Only available on WebAssembly")
9+
#endif
10+
}
11+
12+
@_expose(wasm, "bjs_processFloats")
13+
@_cdecl("bjs_processFloats")
14+
public func _bjs_processFloats(_ data: Int32) -> Int32 {
15+
#if arch(wasm32)
16+
let ret = processFloats(_: JSFloat32Array.bridgeJSLiftParameter(data))
17+
return ret.bridgeJSLowerReturn()
18+
#else
19+
fatalError("Only available on WebAssembly")
20+
#endif
21+
}
22+
23+
@_expose(wasm, "bjs_processDoubles")
24+
@_cdecl("bjs_processDoubles")
25+
public func _bjs_processDoubles(_ data: Int32) -> Int32 {
26+
#if arch(wasm32)
27+
let ret = processDoubles(_: JSFloat64Array.bridgeJSLiftParameter(data))
28+
return ret.bridgeJSLowerReturn()
29+
#else
30+
fatalError("Only available on WebAssembly")
31+
#endif
32+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export type Exports = {
8+
processBytes(data: Uint8Array): Uint8Array;
9+
processFloats(data: Float32Array): Float32Array;
10+
processDoubles(data: Float64Array): Float64Array;
11+
}
12+
export type Imports = {
13+
}
14+
export function createInstantiator(options: {
15+
imports: Imports;
16+
}, swift: any): Promise<{
17+
addImports: (importObject: WebAssembly.Imports) => void;
18+
setInstance: (instance: WebAssembly.Instance) => void;
19+
createExports: (instance: WebAssembly.Instance) => Exports;
20+
}>;

0 commit comments

Comments
 (0)