Skip to content

Commit 3c12186

Browse files
committed
BridgeJS: Fixed support for UnsafePointer<T> and UnsafeMutablePointer<T>
1 parent d0be4a4 commit 3c12186

File tree

5 files changed

+110
-2
lines changed

5 files changed

+110
-2
lines changed

Sources/JavaScriptKit/BridgeJSIntrinsics.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ extension OpaquePointer: _BridgedSwiftStackType {
901901
}
902902
}
903903

904-
extension UnsafePointer {
904+
extension UnsafePointer: _BridgedSwiftStackType {
905905
// MARK: ImportTS
906906
@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> UnsafeMutableRawPointer {
907907
UnsafeMutableRawPointer(mutating: UnsafeRawPointer(self))
@@ -933,7 +933,7 @@ extension UnsafePointer {
933933
}
934934
}
935935

936-
extension UnsafeMutablePointer {
936+
extension UnsafeMutablePointer: _BridgedSwiftStackType {
937937
// MARK: ImportTS
938938
@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerParameter() -> UnsafeMutableRawPointer {
939939
UnsafeMutableRawPointer(self)

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,6 +1407,12 @@ enum APIOptionalResult {
14071407
@JS func roundTripOpaquePointerArray(_ values: [OpaquePointer]) -> [OpaquePointer] {
14081408
return values
14091409
}
1410+
@JS func roundTripUnsafePointerArray(_ values: [UnsafePointer<UInt8>]) -> [UnsafePointer<UInt8>] {
1411+
return values
1412+
}
1413+
@JS func roundTripUnsafeMutablePointerArray(_ values: [UnsafeMutablePointer<UInt8>]) -> [UnsafeMutablePointer<UInt8>] {
1414+
return values
1415+
}
14101416

14111417
@JS func consumeDataProcessorArrayType(_ processors: [DataProcessor]) -> Int {
14121418
return processors.count

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4990,6 +4990,28 @@ public func _bjs_roundTripOpaquePointerArray() -> Void {
49904990
#endif
49914991
}
49924992

4993+
@_expose(wasm, "bjs_roundTripUnsafePointerArray")
4994+
@_cdecl("bjs_roundTripUnsafePointerArray")
4995+
public func _bjs_roundTripUnsafePointerArray() -> Void {
4996+
#if arch(wasm32)
4997+
let ret = roundTripUnsafePointerArray(_: [UnsafePointer<UInt8>].bridgeJSLiftParameter())
4998+
ret.bridgeJSLowerReturn()
4999+
#else
5000+
fatalError("Only available on WebAssembly")
5001+
#endif
5002+
}
5003+
5004+
@_expose(wasm, "bjs_roundTripUnsafeMutablePointerArray")
5005+
@_cdecl("bjs_roundTripUnsafeMutablePointerArray")
5006+
public func _bjs_roundTripUnsafeMutablePointerArray() -> Void {
5007+
#if arch(wasm32)
5008+
let ret = roundTripUnsafeMutablePointerArray(_: [UnsafeMutablePointer<UInt8>].bridgeJSLiftParameter())
5009+
ret.bridgeJSLowerReturn()
5010+
#else
5011+
fatalError("Only available on WebAssembly")
5012+
#endif
5013+
}
5014+
49935015
@_expose(wasm, "bjs_consumeDataProcessorArrayType")
49945016
@_cdecl("bjs_consumeDataProcessorArrayType")
49955017
public func _bjs_consumeDataProcessorArrayType() -> Int32 {

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9137,6 +9137,84 @@
91379137
}
91389138
}
91399139
},
9140+
{
9141+
"abiName" : "bjs_roundTripUnsafePointerArray",
9142+
"effects" : {
9143+
"isAsync" : false,
9144+
"isStatic" : false,
9145+
"isThrows" : false
9146+
},
9147+
"name" : "roundTripUnsafePointerArray",
9148+
"parameters" : [
9149+
{
9150+
"label" : "_",
9151+
"name" : "values",
9152+
"type" : {
9153+
"array" : {
9154+
"_0" : {
9155+
"unsafePointer" : {
9156+
"_0" : {
9157+
"kind" : "unsafePointer",
9158+
"pointee" : "UInt8"
9159+
}
9160+
}
9161+
}
9162+
}
9163+
}
9164+
}
9165+
],
9166+
"returnType" : {
9167+
"array" : {
9168+
"_0" : {
9169+
"unsafePointer" : {
9170+
"_0" : {
9171+
"kind" : "unsafePointer",
9172+
"pointee" : "UInt8"
9173+
}
9174+
}
9175+
}
9176+
}
9177+
}
9178+
},
9179+
{
9180+
"abiName" : "bjs_roundTripUnsafeMutablePointerArray",
9181+
"effects" : {
9182+
"isAsync" : false,
9183+
"isStatic" : false,
9184+
"isThrows" : false
9185+
},
9186+
"name" : "roundTripUnsafeMutablePointerArray",
9187+
"parameters" : [
9188+
{
9189+
"label" : "_",
9190+
"name" : "values",
9191+
"type" : {
9192+
"array" : {
9193+
"_0" : {
9194+
"unsafePointer" : {
9195+
"_0" : {
9196+
"kind" : "unsafeMutablePointer",
9197+
"pointee" : "UInt8"
9198+
}
9199+
}
9200+
}
9201+
}
9202+
}
9203+
}
9204+
],
9205+
"returnType" : {
9206+
"array" : {
9207+
"_0" : {
9208+
"unsafePointer" : {
9209+
"_0" : {
9210+
"kind" : "unsafeMutablePointer",
9211+
"pointee" : "UInt8"
9212+
}
9213+
}
9214+
}
9215+
}
9216+
}
9217+
},
91409218
{
91419219
"abiName" : "bjs_consumeDataProcessorArrayType",
91429220
"effects" : {

Tests/prelude.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,6 +1385,8 @@ function testArraySupport(exports) {
13851385
assert.deepEqual(exports.roundTripUnsafeRawPointerArray(pointerValues), pointerValues);
13861386
assert.deepEqual(exports.roundTripUnsafeMutableRawPointerArray(pointerValues), pointerValues);
13871387
assert.deepEqual(exports.roundTripOpaquePointerArray(pointerValues), pointerValues);
1388+
assert.deepEqual(exports.roundTripUnsafePointerArray(pointerValues), pointerValues);
1389+
assert.deepEqual(exports.roundTripUnsafeMutablePointerArray(pointerValues), pointerValues);
13881390
assert.deepEqual(exports.roundTripUnsafeRawPointerArray([]), []);
13891391

13901392
// Default values

0 commit comments

Comments
 (0)