Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ public final class SwiftToSkeleton {
self.typeDeclResolver.addSourceFile(
"""
@JSClass struct JSPromise {}
@JSClass struct JSInt8Array {}
@JSClass struct JSUint8Array {}
@JSClass struct JSInt16Array {}
@JSClass struct JSUint16Array {}
@JSClass struct JSInt32Array {}
@JSClass struct JSUint32Array {}
@JSClass struct JSFloat32Array {}
@JSClass struct JSFloat64Array {}
"""
)
}
Expand Down Expand Up @@ -128,11 +136,35 @@ public final class SwiftToSkeleton {
)
}

private static let jsTypedArrayTypealiasNames: [String: String] = [
"Int8": "JSInt8Array",
"UInt8": "JSUint8Array",
"Int16": "JSInt16Array",
"UInt16": "JSUint16Array",
"Int32": "JSInt32Array",
"UInt32": "JSUint32Array",
"Float": "JSFloat32Array",
"Float32": "JSFloat32Array",
"Double": "JSFloat64Array",
"Float64": "JSFloat64Array",
]

func lookupType(for type: TypeSyntax, errors: inout [DiagnosticError]) -> BridgeType? {
if let attributedType = type.as(AttributedTypeSyntax.self) {
return lookupType(for: attributedType.baseType, errors: &errors)
}

// JSTypedArray<T>
if let identifierType = type.as(IdentifierTypeSyntax.self),
identifierType.name.text == "JSTypedArray",
let genericArgs = identifierType.genericArgumentClause?.arguments,
genericArgs.count == 1,
let elementName = genericArgs.first?.argument.as(IdentifierTypeSyntax.self)?.name.text,
let typealiasName = Self.jsTypedArrayTypealiasNames[elementName]
{
return .jsObject(typealiasName)
}

if let identifierType = type.as(IdentifierTypeSyntax.self),
identifierType.name.text == "JSTypedClosure",
let genericArgs = identifierType.genericArgumentClause?.arguments,
Expand Down
15 changes: 15 additions & 0 deletions Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3594,6 +3594,9 @@ extension BridgeType {
case .bool:
return "boolean"
case .jsObject(let name):
if let name, let tsName = Self.jsTypedArrayTSNames[name] {
return tsName
}
return name ?? "any"
case .jsValue:
return "any"
Expand Down Expand Up @@ -3630,6 +3633,18 @@ extension BridgeType {
return "Record<string, \(valueType.tsType)>"
}
}

/// Maps JSTypedArray Swift typealias names to their JavaScript TypedArray constructor names.
private static let jsTypedArrayTSNames: [String: String] = [
"JSInt8Array": "Int8Array",
"JSUint8Array": "Uint8Array",
"JSInt16Array": "Int16Array",
"JSUint16Array": "Uint16Array",
"JSInt32Array": "Int32Array",
"JSUint32Array": "Uint32Array",
"JSFloat32Array": "Float32Array",
"JSFloat64Array": "Float64Array",
]
}

extension WasmCoreType {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import JavaScriptKit

// Using typealiases
@JS func processBytes(_ data: JSUint8Array) -> JSUint8Array {
return data
}

@JS func processFloats(_ data: JSFloat32Array) -> JSFloat32Array {
return data
}

// Using generic form directly
@JS func processGenericDoubles(_ data: JSTypedArray<Double>) -> JSTypedArray<Double> {
return data
}

@JS func processGenericInts(_ data: JSTypedArray<Int32>) -> JSTypedArray<Int32> {
return data
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [
{
"abiName" : "bjs_processBytes",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processBytes",
"parameters" : [
{
"label" : "_",
"name" : "data",
"type" : {
"jsObject" : {
"_0" : "JSUint8Array"
}
}
}
],
"returnType" : {
"jsObject" : {
"_0" : "JSUint8Array"
}
}
},
{
"abiName" : "bjs_processFloats",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processFloats",
"parameters" : [
{
"label" : "_",
"name" : "data",
"type" : {
"jsObject" : {
"_0" : "JSFloat32Array"
}
}
}
],
"returnType" : {
"jsObject" : {
"_0" : "JSFloat32Array"
}
}
},
{
"abiName" : "bjs_processGenericDoubles",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processGenericDoubles",
"parameters" : [
{
"label" : "_",
"name" : "data",
"type" : {
"jsObject" : {
"_0" : "JSFloat64Array"
}
}
}
],
"returnType" : {
"jsObject" : {
"_0" : "JSFloat64Array"
}
}
},
{
"abiName" : "bjs_processGenericInts",
"effects" : {
"isAsync" : false,
"isStatic" : false,
"isThrows" : false
},
"name" : "processGenericInts",
"parameters" : [
{
"label" : "_",
"name" : "data",
"type" : {
"jsObject" : {
"_0" : "JSInt32Array"
}
}
}
],
"returnType" : {
"jsObject" : {
"_0" : "JSInt32Array"
}
}
}
],
"protocols" : [

],
"structs" : [

]
},
"moduleName" : "TestModule",
"usedExternalModules" : [

]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@_expose(wasm, "bjs_processBytes")
@_cdecl("bjs_processBytes")
public func _bjs_processBytes(_ data: Int32) -> Int32 {
#if arch(wasm32)
let ret = processBytes(_: JSUint8Array.bridgeJSLiftParameter(data))
return ret.bridgeJSLowerReturn()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_processFloats")
@_cdecl("bjs_processFloats")
public func _bjs_processFloats(_ data: Int32) -> Int32 {
#if arch(wasm32)
let ret = processFloats(_: JSFloat32Array.bridgeJSLiftParameter(data))
return ret.bridgeJSLowerReturn()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_processGenericDoubles")
@_cdecl("bjs_processGenericDoubles")
public func _bjs_processGenericDoubles(_ data: Int32) -> Int32 {
#if arch(wasm32)
let ret = processGenericDoubles(_: JSFloat64Array.bridgeJSLiftParameter(data))
return ret.bridgeJSLowerReturn()
#else
fatalError("Only available on WebAssembly")
#endif
}

@_expose(wasm, "bjs_processGenericInts")
@_cdecl("bjs_processGenericInts")
public func _bjs_processGenericInts(_ data: Int32) -> Int32 {
#if arch(wasm32)
let ret = processGenericInts(_: JSInt32Array.bridgeJSLiftParameter(data))
return ret.bridgeJSLowerReturn()
#else
fatalError("Only available on WebAssembly")
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
// DO NOT EDIT.
//
// To update this file, just rebuild your project or run
// `swift package bridge-js`.

export type Exports = {
processBytes(data: Uint8Array): Uint8Array;
processFloats(data: Float32Array): Float32Array;
processGenericDoubles(data: Float64Array): Float64Array;
processGenericInts(data: Int32Array): Int32Array;
}
export type Imports = {
}
export function createInstantiator(options: {
imports: Imports;
}, swift: any): Promise<{
addImports: (importObject: WebAssembly.Imports) => void;
setInstance: (instance: WebAssembly.Instance) => void;
createExports: (instance: WebAssembly.Instance) => Exports;
}>;
Loading
Loading