Skip to content

Commit e34f382

Browse files
BridgeJS: add runtime coverage for public @js struct (#629)
BridgeJS: add runtime coverage for public JS structs
1 parent fcd2b53 commit e34f382

File tree

4 files changed

+167
-0
lines changed

4 files changed

+167
-0
lines changed

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,62 @@ public func _bjs_DataPoint_init(_ x: Float64, _ y: Float64, _ labelBytes: Int32,
28842884
#endif
28852885
}
28862886

2887+
extension PublicPoint: _BridgedSwiftStruct {
2888+
@_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> PublicPoint {
2889+
let y = Int.bridgeJSLiftParameter()
2890+
let x = Int.bridgeJSLiftParameter()
2891+
return PublicPoint(x: x, y: y)
2892+
}
2893+
2894+
@_spi(BridgeJS) @_transparent public consuming func bridgeJSLowerReturn() {
2895+
self.x.bridgeJSLowerStackReturn()
2896+
self.y.bridgeJSLowerStackReturn()
2897+
}
2898+
2899+
public init(unsafelyCopying jsObject: JSObject) {
2900+
let __bjs_cleanupId = _bjs_struct_lower_PublicPoint(jsObject.bridgeJSLowerParameter())
2901+
defer {
2902+
_swift_js_struct_cleanup(__bjs_cleanupId)
2903+
}
2904+
self = Self.bridgeJSLiftParameter()
2905+
}
2906+
2907+
public func toJSObject() -> JSObject {
2908+
let __bjs_self = self
2909+
__bjs_self.bridgeJSLowerReturn()
2910+
return JSObject(id: UInt32(bitPattern: _bjs_struct_lift_PublicPoint()))
2911+
}
2912+
}
2913+
2914+
#if arch(wasm32)
2915+
@_extern(wasm, module: "bjs", name: "swift_js_struct_lower_PublicPoint")
2916+
fileprivate func _bjs_struct_lower_PublicPoint(_ objectId: Int32) -> Int32
2917+
#else
2918+
fileprivate func _bjs_struct_lower_PublicPoint(_ objectId: Int32) -> Int32 {
2919+
fatalError("Only available on WebAssembly")
2920+
}
2921+
#endif
2922+
2923+
#if arch(wasm32)
2924+
@_extern(wasm, module: "bjs", name: "swift_js_struct_lift_PublicPoint")
2925+
fileprivate func _bjs_struct_lift_PublicPoint() -> Int32
2926+
#else
2927+
fileprivate func _bjs_struct_lift_PublicPoint() -> Int32 {
2928+
fatalError("Only available on WebAssembly")
2929+
}
2930+
#endif
2931+
2932+
@_expose(wasm, "bjs_PublicPoint_init")
2933+
@_cdecl("bjs_PublicPoint_init")
2934+
public func _bjs_PublicPoint_init(_ x: Int32, _ y: Int32) -> Void {
2935+
#if arch(wasm32)
2936+
let ret = PublicPoint(x: Int.bridgeJSLiftParameter(x), y: Int.bridgeJSLiftParameter(y))
2937+
return ret.bridgeJSLowerReturn()
2938+
#else
2939+
fatalError("Only available on WebAssembly")
2940+
#endif
2941+
}
2942+
28872943
extension Address: _BridgedSwiftStruct {
28882944
@_spi(BridgeJS) @_transparent public static func bridgeJSLiftParameter() -> Address {
28892945
let zipCode = Optional<Int>.bridgeJSLiftParameter()
@@ -5954,6 +6010,17 @@ public func _bjs_roundTripDataPoint() -> Void {
59546010
#endif
59556011
}
59566012

6013+
@_expose(wasm, "bjs_roundTripPublicPoint")
6014+
@_cdecl("bjs_roundTripPublicPoint")
6015+
public func _bjs_roundTripPublicPoint() -> Void {
6016+
#if arch(wasm32)
6017+
let ret = roundTripPublicPoint(_: PublicPoint.bridgeJSLiftParameter())
6018+
return ret.bridgeJSLowerReturn()
6019+
#else
6020+
fatalError("Only available on WebAssembly")
6021+
#endif
6022+
}
6023+
59576024
@_expose(wasm, "bjs_roundTripContact")
59586025
@_cdecl("bjs_roundTripContact")
59596026
public func _bjs_roundTripContact() -> Void {

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11131,6 +11131,31 @@
1113111131
}
1113211132
}
1113311133
},
11134+
{
11135+
"abiName" : "bjs_roundTripPublicPoint",
11136+
"effects" : {
11137+
"isAsync" : false,
11138+
"isStatic" : false,
11139+
"isThrows" : false
11140+
},
11141+
"name" : "roundTripPublicPoint",
11142+
"parameters" : [
11143+
{
11144+
"label" : "_",
11145+
"name" : "point",
11146+
"type" : {
11147+
"swiftStruct" : {
11148+
"_0" : "PublicPoint"
11149+
}
11150+
}
11151+
}
11152+
],
11153+
"returnType" : {
11154+
"swiftStruct" : {
11155+
"_0" : "PublicPoint"
11156+
}
11157+
}
11158+
},
1113411159
{
1113511160
"abiName" : "bjs_roundTripContact",
1113611161
"effects" : {
@@ -12196,6 +12221,64 @@
1219612221
],
1219712222
"swiftCallName" : "DataPoint"
1219812223
},
12224+
{
12225+
"constructor" : {
12226+
"abiName" : "bjs_PublicPoint_init",
12227+
"effects" : {
12228+
"isAsync" : false,
12229+
"isStatic" : false,
12230+
"isThrows" : false
12231+
},
12232+
"parameters" : [
12233+
{
12234+
"label" : "x",
12235+
"name" : "x",
12236+
"type" : {
12237+
"int" : {
12238+
12239+
}
12240+
}
12241+
},
12242+
{
12243+
"label" : "y",
12244+
"name" : "y",
12245+
"type" : {
12246+
"int" : {
12247+
12248+
}
12249+
}
12250+
}
12251+
]
12252+
},
12253+
"explicitAccessControl" : "public",
12254+
"methods" : [
12255+
12256+
],
12257+
"name" : "PublicPoint",
12258+
"properties" : [
12259+
{
12260+
"isReadonly" : true,
12261+
"isStatic" : false,
12262+
"name" : "x",
12263+
"type" : {
12264+
"int" : {
12265+
12266+
}
12267+
}
12268+
},
12269+
{
12270+
"isReadonly" : true,
12271+
"isStatic" : false,
12272+
"name" : "y",
12273+
"type" : {
12274+
"int" : {
12275+
12276+
}
12277+
}
12278+
}
12279+
],
12280+
"swiftCallName" : "PublicPoint"
12281+
},
1219912282
{
1220012283
"methods" : [
1220112284

Tests/BridgeJSRuntimeTests/StructAPIs.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@
4040
}
4141
}
4242

43+
@JS public struct PublicPoint {
44+
public var x: Int
45+
public var y: Int
46+
47+
@JS public init(x: Int, y: Int) {
48+
self.x = x
49+
self.y = y
50+
}
51+
}
52+
4353
@JS struct Address {
4454
var street: String
4555
var city: String
@@ -168,6 +178,10 @@
168178
return data
169179
}
170180

181+
@JS public func roundTripPublicPoint(_ point: PublicPoint) -> PublicPoint {
182+
point
183+
}
184+
171185
@JS func roundTripContact(_ contact: Contact) -> Contact {
172186
return contact
173187
}

Tests/prelude.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,9 @@ function testStructSupport(exports) {
890890
const data2 = { x: 0.0, y: 0.0, label: "", optCount: null, optFlag: null };
891891
assert.deepEqual(exports.roundTripDataPoint(data2), data2);
892892

893+
const publicPoint = { x: 9, y: -3 };
894+
assert.deepEqual(exports.roundTripPublicPoint(publicPoint), publicPoint);
895+
893896
const pointerFields1 = { raw: 1, mutRaw: 4, opaque: 1024, ptr: 65536, mutPtr: 2 };
894897
assert.deepEqual(exports.roundTripPointerFields(pointerFields1), pointerFields1);
895898

0 commit comments

Comments
 (0)