Skip to content

Commit 3ffd723

Browse files
committed
BridgeJS: Improve test coverage for @js methods and properties in extensions
1 parent b5d873c commit 3ffd723

24 files changed

+652
-1
lines changed

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/StaticFunctions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,14 @@ extension MathUtils {
4343
@JS static func divide(a: Int, b: Int) -> Int {
4444
return a / b
4545
}
46+
47+
@JS static var pi: Double { 3.14159 }
4648
}
4749

4850
extension Calculator {
4951
@JS static func cube(value: Int) -> Int {
5052
return value * value * value
5153
}
54+
55+
@JS static var version: String { "1.0" }
5256
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/SwiftClass.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ extension Greeter {
1616
@JS func greetEnthusiastically() -> String {
1717
return "Hey, " + self.name + "!!!"
1818
}
19+
20+
@JS var nameCount: Int { name.count }
21+
22+
@JS static func greetAnonymously() -> String {
23+
return "Hello."
24+
}
25+
26+
@JS static var defaultGreeting: String { "Hello, world!" }
1927
}
2028

2129
@JS func takeGreeter(greeter: Greeter) {

Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/SwiftStruct.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ extension DataPoint {
6565
@JS func distanceFromOrigin() -> Double {
6666
return (x * x + y * y).squareRoot()
6767
}
68+
69+
@JS static func origin() -> DataPoint {
70+
return DataPoint(x: 0, y: 0, label: "origin", optCount: nil, optFlag: nil)
71+
}
72+
73+
@JS static var dimensions: Int { 2 }
6874
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StaticFunctions.Global.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,21 @@
168168
],
169169
"name" : "MathUtils",
170170
"properties" : [
171+
{
172+
"isReadonly" : true,
173+
"isStatic" : true,
174+
"name" : "pi",
175+
"staticContext" : {
176+
"className" : {
177+
"_0" : "MathUtils"
178+
}
179+
},
180+
"type" : {
181+
"double" : {
171182

183+
}
184+
}
185+
}
172186
],
173187
"swiftCallName" : "MathUtils"
174188
}
@@ -254,7 +268,21 @@
254268
}
255269
],
256270
"staticProperties" : [
271+
{
272+
"isReadonly" : true,
273+
"isStatic" : true,
274+
"name" : "version",
275+
"staticContext" : {
276+
"enumName" : {
277+
"_0" : "Calculator"
278+
}
279+
},
280+
"type" : {
281+
"string" : {
257282

283+
}
284+
}
285+
}
258286
],
259287
"swiftCallName" : "Calculator",
260288
"tsFullPath" : "Calculator"

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StaticFunctions.Global.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public func _bjs_Calculator_static_cube(_ value: Int32) -> Int32 {
5555
#endif
5656
}
5757

58+
@_expose(wasm, "bjs_Calculator_static_version_get")
59+
@_cdecl("bjs_Calculator_static_version_get")
60+
public func _bjs_Calculator_static_version_get() -> Void {
61+
#if arch(wasm32)
62+
let ret = Calculator.version
63+
return ret.bridgeJSLowerReturn()
64+
#else
65+
fatalError("Only available on WebAssembly")
66+
#endif
67+
}
68+
5869
extension APIResult: _BridgedSwiftAssociatedValueEnum {
5970
@_spi(BridgeJS) @_transparent public static func bridgeJSStackPopPayload(_ caseId: Int32) -> APIResult {
6071
switch caseId {
@@ -156,6 +167,17 @@ public func _bjs_MathUtils_static_divide(_ a: Int32, _ b: Int32) -> Int32 {
156167
#endif
157168
}
158169

170+
@_expose(wasm, "bjs_MathUtils_static_pi_get")
171+
@_cdecl("bjs_MathUtils_static_pi_get")
172+
public func _bjs_MathUtils_static_pi_get() -> Float64 {
173+
#if arch(wasm32)
174+
let ret = MathUtils.pi
175+
return ret.bridgeJSLowerReturn()
176+
#else
177+
fatalError("Only available on WebAssembly")
178+
#endif
179+
}
180+
159181
@_expose(wasm, "bjs_MathUtils_deinit")
160182
@_cdecl("bjs_MathUtils_deinit")
161183
public func _bjs_MathUtils_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StaticFunctions.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,21 @@
168168
],
169169
"name" : "MathUtils",
170170
"properties" : [
171+
{
172+
"isReadonly" : true,
173+
"isStatic" : true,
174+
"name" : "pi",
175+
"staticContext" : {
176+
"className" : {
177+
"_0" : "MathUtils"
178+
}
179+
},
180+
"type" : {
181+
"double" : {
171182

183+
}
184+
}
185+
}
172186
],
173187
"swiftCallName" : "MathUtils"
174188
}
@@ -254,7 +268,21 @@
254268
}
255269
],
256270
"staticProperties" : [
271+
{
272+
"isReadonly" : true,
273+
"isStatic" : true,
274+
"name" : "version",
275+
"staticContext" : {
276+
"enumName" : {
277+
"_0" : "Calculator"
278+
}
279+
},
280+
"type" : {
281+
"string" : {
257282

283+
}
284+
}
285+
}
258286
],
259287
"swiftCallName" : "Calculator",
260288
"tsFullPath" : "Calculator"

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/StaticFunctions.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public func _bjs_Calculator_static_cube(_ value: Int32) -> Int32 {
5555
#endif
5656
}
5757

58+
@_expose(wasm, "bjs_Calculator_static_version_get")
59+
@_cdecl("bjs_Calculator_static_version_get")
60+
public func _bjs_Calculator_static_version_get() -> Void {
61+
#if arch(wasm32)
62+
let ret = Calculator.version
63+
return ret.bridgeJSLowerReturn()
64+
#else
65+
fatalError("Only available on WebAssembly")
66+
#endif
67+
}
68+
5869
extension APIResult: _BridgedSwiftAssociatedValueEnum {
5970
@_spi(BridgeJS) @_transparent public static func bridgeJSStackPopPayload(_ caseId: Int32) -> APIResult {
6071
switch caseId {
@@ -156,6 +167,17 @@ public func _bjs_MathUtils_static_divide(_ a: Int32, _ b: Int32) -> Int32 {
156167
#endif
157168
}
158169

170+
@_expose(wasm, "bjs_MathUtils_static_pi_get")
171+
@_cdecl("bjs_MathUtils_static_pi_get")
172+
public func _bjs_MathUtils_static_pi_get() -> Float64 {
173+
#if arch(wasm32)
174+
let ret = MathUtils.pi
175+
return ret.bridgeJSLowerReturn()
176+
#else
177+
fatalError("Only available on WebAssembly")
178+
#endif
179+
}
180+
159181
@_expose(wasm, "bjs_MathUtils_deinit")
160182
@_cdecl("bjs_MathUtils_deinit")
161183
public func _bjs_MathUtils_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClass.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,28 @@
8080

8181
}
8282
}
83+
},
84+
{
85+
"abiName" : "bjs_Greeter_static_greetAnonymously",
86+
"effects" : {
87+
"isAsync" : false,
88+
"isStatic" : true,
89+
"isThrows" : false
90+
},
91+
"name" : "greetAnonymously",
92+
"parameters" : [
93+
94+
],
95+
"returnType" : {
96+
"string" : {
97+
98+
}
99+
},
100+
"staticContext" : {
101+
"className" : {
102+
"_0" : "Greeter"
103+
}
104+
}
83105
}
84106
],
85107
"name" : "Greeter",
@@ -91,6 +113,31 @@
91113
"type" : {
92114
"string" : {
93115

116+
}
117+
}
118+
},
119+
{
120+
"isReadonly" : true,
121+
"isStatic" : false,
122+
"name" : "nameCount",
123+
"type" : {
124+
"int" : {
125+
126+
}
127+
}
128+
},
129+
{
130+
"isReadonly" : true,
131+
"isStatic" : true,
132+
"name" : "defaultGreeting",
133+
"staticContext" : {
134+
"className" : {
135+
"_0" : "Greeter"
136+
}
137+
},
138+
"type" : {
139+
"string" : {
140+
94141
}
95142
}
96143
}

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClass.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public func _bjs_Greeter_greetEnthusiastically(_ _self: UnsafeMutableRawPointer)
5151
#endif
5252
}
5353

54+
@_expose(wasm, "bjs_Greeter_static_greetAnonymously")
55+
@_cdecl("bjs_Greeter_static_greetAnonymously")
56+
public func _bjs_Greeter_static_greetAnonymously() -> Void {
57+
#if arch(wasm32)
58+
let ret = Greeter.greetAnonymously()
59+
return ret.bridgeJSLowerReturn()
60+
#else
61+
fatalError("Only available on WebAssembly")
62+
#endif
63+
}
64+
5465
@_expose(wasm, "bjs_Greeter_name_get")
5566
@_cdecl("bjs_Greeter_name_get")
5667
public func _bjs_Greeter_name_get(_ _self: UnsafeMutableRawPointer) -> Void {
@@ -72,6 +83,28 @@ public func _bjs_Greeter_name_set(_ _self: UnsafeMutableRawPointer, _ valueBytes
7283
#endif
7384
}
7485

86+
@_expose(wasm, "bjs_Greeter_nameCount_get")
87+
@_cdecl("bjs_Greeter_nameCount_get")
88+
public func _bjs_Greeter_nameCount_get(_ _self: UnsafeMutableRawPointer) -> Int32 {
89+
#if arch(wasm32)
90+
let ret = Greeter.bridgeJSLiftParameter(_self).nameCount
91+
return ret.bridgeJSLowerReturn()
92+
#else
93+
fatalError("Only available on WebAssembly")
94+
#endif
95+
}
96+
97+
@_expose(wasm, "bjs_Greeter_static_defaultGreeting_get")
98+
@_cdecl("bjs_Greeter_static_defaultGreeting_get")
99+
public func _bjs_Greeter_static_defaultGreeting_get() -> Void {
100+
#if arch(wasm32)
101+
let ret = Greeter.defaultGreeting
102+
return ret.bridgeJSLowerReturn()
103+
#else
104+
fatalError("Only available on WebAssembly")
105+
#endif
106+
}
107+
75108
@_expose(wasm, "bjs_Greeter_deinit")
76109
@_cdecl("bjs_Greeter_deinit")
77110
public func _bjs_Greeter_deinit(_ pointer: UnsafeMutableRawPointer) -> Void {

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftStruct.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,28 @@
227227

228228
}
229229
}
230+
},
231+
{
232+
"abiName" : "bjs_DataPoint_static_origin",
233+
"effects" : {
234+
"isAsync" : false,
235+
"isStatic" : true,
236+
"isThrows" : false
237+
},
238+
"name" : "origin",
239+
"parameters" : [
240+
241+
],
242+
"returnType" : {
243+
"swiftStruct" : {
244+
"_0" : "DataPoint"
245+
}
246+
},
247+
"staticContext" : {
248+
"structName" : {
249+
"_0" : "DataPoint"
250+
}
251+
}
230252
}
231253
],
232254
"name" : "DataPoint",
@@ -290,6 +312,21 @@
290312
"_1" : "null"
291313
}
292314
}
315+
},
316+
{
317+
"isReadonly" : true,
318+
"isStatic" : true,
319+
"name" : "dimensions",
320+
"staticContext" : {
321+
"structName" : {
322+
"_0" : "DataPoint"
323+
}
324+
},
325+
"type" : {
326+
"int" : {
327+
328+
}
329+
}
293330
}
294331
],
295332
"swiftCallName" : "DataPoint"

0 commit comments

Comments
 (0)