Skip to content

Commit 9acbb8c

Browse files
Test: Cover Swift class without @js init
1 parent dff6231 commit 9acbb8c

File tree

4 files changed

+214
-1
lines changed

4 files changed

+214
-1
lines changed

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,48 @@ struct TestError: Error {
7474
g.changeName(name: name)
7575
}
7676

77+
// Test class without @JS init constructor
78+
@JS class Calculator {
79+
nonisolated(unsafe) static var onDeinit: () -> Void = {}
80+
81+
@JS func square(value: Int) -> Int {
82+
return value * value
83+
}
84+
85+
@JS func add(a: Int, b: Int) -> Int {
86+
return a + b
87+
}
88+
89+
deinit {
90+
Self.onDeinit()
91+
}
92+
}
93+
94+
@JS func createCalculator() -> Calculator {
95+
return Calculator()
96+
}
97+
98+
@JS func useCalculator(calc: Calculator, x: Int, y: Int) -> Int {
99+
return calc.add(a: calc.square(value: x), b: y)
100+
}
101+
102+
77103
class ExportAPITests: XCTestCase {
78104
func testAll() {
79105
var hasDeinitGreeter = false
106+
var hasDeinitCalculator = false
107+
80108
Greeter.onDeinit = {
81109
hasDeinitGreeter = true
82110
}
111+
112+
Calculator.onDeinit = {
113+
hasDeinitCalculator = true
114+
}
115+
83116
runJsWorks()
84-
XCTAssertTrue(hasDeinitGreeter)
117+
118+
XCTAssertTrue(hasDeinitGreeter, "Greeter (with @JS init) should have been deinitialized")
119+
XCTAssertTrue(hasDeinitCalculator, "Calculator (without @JS init) should have been deinitialized")
85120
}
86121
}

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.ExportSwift.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,28 @@ public func _bjs_takeGreeter(g: UnsafeMutableRawPointer, nameBytes: Int32, nameL
314314
#endif
315315
}
316316

317+
@_expose(wasm, "bjs_createCalculator")
318+
@_cdecl("bjs_createCalculator")
319+
public func _bjs_createCalculator() -> UnsafeMutableRawPointer {
320+
#if arch(wasm32)
321+
let ret = createCalculator()
322+
return Unmanaged.passRetained(ret).toOpaque()
323+
#else
324+
fatalError("Only available on WebAssembly")
325+
#endif
326+
}
327+
328+
@_expose(wasm, "bjs_useCalculator")
329+
@_cdecl("bjs_useCalculator")
330+
public func _bjs_useCalculator(calc: UnsafeMutableRawPointer, x: Int32, y: Int32) -> Int32 {
331+
#if arch(wasm32)
332+
let ret = useCalculator(calc: Unmanaged<Calculator>.fromOpaque(calc).takeUnretainedValue(), x: Int(x), y: Int(y))
333+
return Int32(ret)
334+
#else
335+
fatalError("Only available on WebAssembly")
336+
#endif
337+
}
338+
317339
@_expose(wasm, "bjs_Greeter_init")
318340
@_cdecl("bjs_Greeter_init")
319341
public func _bjs_Greeter_init(nameBytes: Int32, nameLen: Int32) -> UnsafeMutableRawPointer {
@@ -360,4 +382,32 @@ public func _bjs_Greeter_changeName(_self: UnsafeMutableRawPointer, nameBytes: I
360382
@_cdecl("bjs_Greeter_deinit")
361383
public func _bjs_Greeter_deinit(pointer: UnsafeMutableRawPointer) {
362384
Unmanaged<Greeter>.fromOpaque(pointer).release()
385+
}
386+
387+
@_expose(wasm, "bjs_Calculator_square")
388+
@_cdecl("bjs_Calculator_square")
389+
public func _bjs_Calculator_square(_self: UnsafeMutableRawPointer, value: Int32) -> Int32 {
390+
#if arch(wasm32)
391+
let ret = Unmanaged<Calculator>.fromOpaque(_self).takeUnretainedValue().square(value: Int(value))
392+
return Int32(ret)
393+
#else
394+
fatalError("Only available on WebAssembly")
395+
#endif
396+
}
397+
398+
@_expose(wasm, "bjs_Calculator_add")
399+
@_cdecl("bjs_Calculator_add")
400+
public func _bjs_Calculator_add(_self: UnsafeMutableRawPointer, a: Int32, b: Int32) -> Int32 {
401+
#if arch(wasm32)
402+
let ret = Unmanaged<Calculator>.fromOpaque(_self).takeUnretainedValue().add(a: Int(a), b: Int(b))
403+
return Int32(ret)
404+
#else
405+
fatalError("Only available on WebAssembly")
406+
#endif
407+
}
408+
409+
@_expose(wasm, "bjs_Calculator_deinit")
410+
@_cdecl("bjs_Calculator_deinit")
411+
public func _bjs_Calculator_deinit(pointer: UnsafeMutableRawPointer) {
412+
Unmanaged<Calculator>.fromOpaque(pointer).release()
363413
}

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.ExportSwift.json

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,68 @@
6262
}
6363
],
6464
"name" : "Greeter"
65+
},
66+
{
67+
"methods" : [
68+
{
69+
"abiName" : "bjs_Calculator_square",
70+
"effects" : {
71+
"isAsync" : false,
72+
"isThrows" : false
73+
},
74+
"name" : "square",
75+
"parameters" : [
76+
{
77+
"label" : "value",
78+
"name" : "value",
79+
"type" : {
80+
"int" : {
81+
82+
}
83+
}
84+
}
85+
],
86+
"returnType" : {
87+
"int" : {
88+
89+
}
90+
}
91+
},
92+
{
93+
"abiName" : "bjs_Calculator_add",
94+
"effects" : {
95+
"isAsync" : false,
96+
"isThrows" : false
97+
},
98+
"name" : "add",
99+
"parameters" : [
100+
{
101+
"label" : "a",
102+
"name" : "a",
103+
"type" : {
104+
"int" : {
105+
106+
}
107+
}
108+
},
109+
{
110+
"label" : "b",
111+
"name" : "b",
112+
"type" : {
113+
"int" : {
114+
115+
}
116+
}
117+
}
118+
],
119+
"returnType" : {
120+
"int" : {
121+
122+
}
123+
}
124+
}
125+
],
126+
"name" : "Calculator"
65127
}
66128
],
67129
"functions" : [
@@ -415,6 +477,64 @@
415477
"returnType" : {
416478
"void" : {
417479

480+
}
481+
}
482+
},
483+
{
484+
"abiName" : "bjs_createCalculator",
485+
"effects" : {
486+
"isAsync" : false,
487+
"isThrows" : false
488+
},
489+
"name" : "createCalculator",
490+
"parameters" : [
491+
492+
],
493+
"returnType" : {
494+
"swiftHeapObject" : {
495+
"_0" : "Calculator"
496+
}
497+
}
498+
},
499+
{
500+
"abiName" : "bjs_useCalculator",
501+
"effects" : {
502+
"isAsync" : false,
503+
"isThrows" : false
504+
},
505+
"name" : "useCalculator",
506+
"parameters" : [
507+
{
508+
"label" : "calc",
509+
"name" : "calc",
510+
"type" : {
511+
"swiftHeapObject" : {
512+
"_0" : "Calculator"
513+
}
514+
}
515+
},
516+
{
517+
"label" : "x",
518+
"name" : "x",
519+
"type" : {
520+
"int" : {
521+
522+
}
523+
}
524+
},
525+
{
526+
"label" : "y",
527+
"name" : "y",
528+
"type" : {
529+
"int" : {
530+
531+
}
532+
}
533+
}
534+
],
535+
"returnType" : {
536+
"int" : {
537+
418538
}
419539
}
420540
}

Tests/prelude.mjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
128128

129129
g.release();
130130

131+
// Test class without @JS init constructor
132+
const calc = exports.createCalculator();
133+
assert.equal(calc.square(5), 25);
134+
assert.equal(calc.add(3, 4), 7);
135+
assert.equal(exports.useCalculator(calc, 3, 10), 19); // 3^2 + 10 = 19
136+
137+
calc.release();
138+
131139
const anyObject = {};
132140
assert.equal(exports.roundTripJSObject(anyObject), anyObject);
133141

0 commit comments

Comments
 (0)