Skip to content

Commit 4f26c00

Browse files
Added a runtime regression test for “exported API returns an imported @JSClass type”.
- Swift runtime test case: `Tests/BridgeJSRuntimeTests/ExportAPITests.swift` - JS assertions + imports wiring: `Tests/prelude.mjs` - Regenerated fixtures: `Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift`, `Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json` (via `./Utilities/bridge-js-generate.sh`) To run: `make unittest SWIFT_SDK_ID=<matching wasm sdk>` (I couldn’t run it here because the available WASM SDKs don’t match the installed Swift 6.3-dev compiler: 6.2 is too old, and the available dev snapshots are too new).
1 parent c5fb1af commit 4f26c00

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

Tests/BridgeJSRuntimeTests/ExportAPITests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ func runJsWorks() -> Void
3333
return v
3434
}
3535

36+
@JSClass class Foo {
37+
@JSGetter var value: String
38+
@JSFunction init(_ value: String) throws(JSException)
39+
}
40+
41+
@JS func makeImportedFoo(value: String) throws(JSException) -> Foo {
42+
return try Foo(value)
43+
}
44+
3645
struct TestError: Error {
3746
let message: String
3847
}

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,31 @@ public func _bjs_roundTripJSObject(_ v: Int32) -> Int32 {
25602560
#endif
25612561
}
25622562

2563+
@_expose(wasm, "bjs_makeImportedFoo")
2564+
@_cdecl("bjs_makeImportedFoo")
2565+
public func _bjs_makeImportedFoo(_ valueBytes: Int32, _ valueLength: Int32) -> Int32 {
2566+
#if arch(wasm32)
2567+
do {
2568+
let ret = try makeImportedFoo(value: String.bridgeJSLiftParameter(valueBytes, valueLength))
2569+
return ret.bridgeJSLowerReturn()
2570+
} catch let error {
2571+
if let error = error.thrownValue.object {
2572+
withExtendedLifetime(error) {
2573+
_swift_js_throw(Int32(bitPattern: $0.id))
2574+
}
2575+
} else {
2576+
let jsError = JSError(message: String(describing: error))
2577+
withExtendedLifetime(jsError.jsObject) {
2578+
_swift_js_throw(Int32(bitPattern: $0.id))
2579+
}
2580+
}
2581+
return 0
2582+
}
2583+
#else
2584+
fatalError("Only available on WebAssembly")
2585+
#endif
2586+
}
2587+
25632588
@_expose(wasm, "bjs_throwsSwiftError")
25642589
@_cdecl("bjs_throwsSwiftError")
25652590
public func _bjs_throwsSwiftError(_ shouldThrow: Int32) -> Void {
@@ -6166,6 +6191,42 @@ fileprivate func _bjs_Container_wrap(_ pointer: UnsafeMutableRawPointer) -> Int3
61666191
}
61676192
#endif
61686193

6194+
#if arch(wasm32)
6195+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Foo_init")
6196+
fileprivate func bjs_Foo_init(_ value: Int32) -> Int32
6197+
#else
6198+
fileprivate func bjs_Foo_init(_ value: Int32) -> Int32 {
6199+
fatalError("Only available on WebAssembly")
6200+
}
6201+
#endif
6202+
6203+
#if arch(wasm32)
6204+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Foo_value_get")
6205+
fileprivate func bjs_Foo_value_get(_ self: Int32) -> Int32
6206+
#else
6207+
fileprivate func bjs_Foo_value_get(_ self: Int32) -> Int32 {
6208+
fatalError("Only available on WebAssembly")
6209+
}
6210+
#endif
6211+
6212+
func _$Foo_init(_ value: String) throws(JSException) -> JSObject {
6213+
let valueValue = value.bridgeJSLowerParameter()
6214+
let ret = bjs_Foo_init(valueValue)
6215+
if let error = _swift_js_take_exception() {
6216+
throw error
6217+
}
6218+
return JSObject.bridgeJSLiftReturn(ret)
6219+
}
6220+
6221+
func _$Foo_value_get(_ self: JSObject) throws(JSException) -> String {
6222+
let selfValue = self.bridgeJSLowerParameter()
6223+
let ret = bjs_Foo_value_get(selfValue)
6224+
if let error = _swift_js_take_exception() {
6225+
throw error
6226+
}
6227+
return String.bridgeJSLiftReturn(ret)
6228+
}
6229+
61696230
#if arch(wasm32)
61706231
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsRoundTripVoid")
61716232
fileprivate func bjs_jsRoundTripVoid() -> Void

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4729,6 +4729,31 @@
47294729
}
47304730
}
47314731
},
4732+
{
4733+
"abiName" : "bjs_makeImportedFoo",
4734+
"effects" : {
4735+
"isAsync" : false,
4736+
"isStatic" : false,
4737+
"isThrows" : true
4738+
},
4739+
"name" : "makeImportedFoo",
4740+
"parameters" : [
4741+
{
4742+
"label" : "value",
4743+
"name" : "value",
4744+
"type" : {
4745+
"string" : {
4746+
4747+
}
4748+
}
4749+
}
4750+
],
4751+
"returnType" : {
4752+
"jsObject" : {
4753+
"_0" : "Foo"
4754+
}
4755+
}
4756+
},
47324757
{
47334758
"abiName" : "bjs_throwsSwiftError",
47344759
"effects" : {
@@ -8928,7 +8953,37 @@
89288953

89298954
],
89308955
"types" : [
8956+
{
8957+
"constructor" : {
8958+
"parameters" : [
8959+
{
8960+
"name" : "value",
8961+
"type" : {
8962+
"string" : {
89318963

8964+
}
8965+
}
8966+
}
8967+
]
8968+
},
8969+
"getters" : [
8970+
{
8971+
"name" : "value",
8972+
"type" : {
8973+
"string" : {
8974+
8975+
}
8976+
}
8977+
}
8978+
],
8979+
"methods" : [
8980+
8981+
],
8982+
"name" : "Foo",
8983+
"setters" : [
8984+
8985+
]
8986+
}
89328987
]
89338988
},
89348989
{

Tests/prelude.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export async function setupOptions(options, context) {
6464
this.name = name;
6565
}
6666
},
67+
Foo: ImportedFoo,
6768
runAsyncWorks: async () => {
6869
const exports = importsContext.getExports();
6970
if (!exports) {
@@ -100,6 +101,13 @@ export async function setupOptions(options, context) {
100101

101102
import assert from "node:assert";
102103

104+
class ImportedFoo {
105+
/** @param {string} value */
106+
constructor(value) {
107+
this.value = value;
108+
}
109+
}
110+
103111
/** @param {import('./../.build/plugins/PackageToJS/outputs/PackageTests/bridge-js.d.ts').Exports} exports */
104112
function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
105113
exports.roundTripVoid();
@@ -173,6 +181,10 @@ function BridgeJSRuntimeTests_runJsWorks(instance, exports) {
173181

174182
g.release();
175183

184+
const foo = exports.makeImportedFoo("hello");
185+
assert.ok(foo instanceof ImportedFoo);
186+
assert.equal(foo.value, "hello");
187+
176188
// Test PropertyHolder with various types
177189
const testObj = { testProp: "test" };
178190
const sibling = new exports.SimplePropertyHolder(999);

0 commit comments

Comments
 (0)