Skip to content

Commit 64e7feb

Browse files
Implemented runtime coverage for jsName on @JSFunction and @JSClass.
- Added new import API + XCTest: `Tests/BridgeJSRuntimeTests/bridge-js.d.ts`, `Tests/BridgeJSRuntimeTests/ImportAPITests.swift` - Wired JS-side implementations: `Tests/prelude.mjs` - Regenerated pre-generated files: `./Utilities/bridge-js-generate.sh` (updated `Tests/BridgeJSRuntimeTests/Generated/*`) - Verified: `SWIFT_SDK_ID=DEVELOPMENT-SNAPSHOT-2025-11-03-a-wasm32-unknown-wasip1 make unittest` passes
1 parent e3a10d7 commit 64e7feb

File tree

6 files changed

+125
-0
lines changed

6 files changed

+125
-0
lines changed

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.Macros.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ extension FeatureFlag: _BridgedSwiftEnumNoPayload {}
4040
}
4141

4242
@JSFunction func runAsyncWorks() throws (JSException) -> JSPromise
43+
44+
@JSFunction(jsName: "$jsWeirdFunction") func _jsWeirdFunction() throws (JSException) -> Double
45+
46+
@JSClass(jsName: "$WeirdClass") struct _WeirdClass {
47+
@JSFunction init() throws (JSException)
48+
@JSFunction(jsName: "method-with-dashes") func method_with_dashes() throws (JSException) -> String
49+
}

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6449,6 +6449,23 @@ func _$runAsyncWorks() throws(JSException) -> JSPromise {
64496449
return JSPromise.bridgeJSLiftReturn(ret)
64506450
}
64516451

6452+
#if arch(wasm32)
6453+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs__jsWeirdFunction")
6454+
fileprivate func bjs__jsWeirdFunction() -> Float64
6455+
#else
6456+
fileprivate func bjs__jsWeirdFunction() -> Float64 {
6457+
fatalError("Only available on WebAssembly")
6458+
}
6459+
#endif
6460+
6461+
func _$_jsWeirdFunction() throws(JSException) -> Double {
6462+
let ret = bjs__jsWeirdFunction()
6463+
if let error = _swift_js_take_exception() {
6464+
throw error
6465+
}
6466+
return Double.bridgeJSLiftReturn(ret)
6467+
}
6468+
64526469
#if arch(wasm32)
64536470
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_JsGreeter_init")
64546471
fileprivate func bjs_JsGreeter_init(_ name: Int32, _ prefix: Int32) -> Int32
@@ -6558,6 +6575,41 @@ func _$JsGreeter_changeName(_ self: JSObject, _ name: String) throws(JSException
65586575
}
65596576
}
65606577

6578+
#if arch(wasm32)
6579+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs__WeirdClass_init")
6580+
fileprivate func bjs__WeirdClass_init() -> Int32
6581+
#else
6582+
fileprivate func bjs__WeirdClass_init() -> Int32 {
6583+
fatalError("Only available on WebAssembly")
6584+
}
6585+
#endif
6586+
6587+
#if arch(wasm32)
6588+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs__WeirdClass_method_with_dashes")
6589+
fileprivate func bjs__WeirdClass_method_with_dashes(_ self: Int32) -> Int32
6590+
#else
6591+
fileprivate func bjs__WeirdClass_method_with_dashes(_ self: Int32) -> Int32 {
6592+
fatalError("Only available on WebAssembly")
6593+
}
6594+
#endif
6595+
6596+
func _$_WeirdClass_init() throws(JSException) -> JSObject {
6597+
let ret = bjs__WeirdClass_init()
6598+
if let error = _swift_js_take_exception() {
6599+
throw error
6600+
}
6601+
return JSObject.bridgeJSLiftReturn(ret)
6602+
}
6603+
6604+
func _$_WeirdClass_method_with_dashes(_ self: JSObject) throws(JSException) -> String {
6605+
let selfValue = self.bridgeJSLowerParameter()
6606+
let ret = bjs__WeirdClass_method_with_dashes(selfValue)
6607+
if let error = _swift_js_take_exception() {
6608+
throw error
6609+
}
6610+
return String.bridgeJSLiftReturn(ret)
6611+
}
6612+
65616613
#if arch(wasm32)
65626614
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_jsApplyInt")
65636615
fileprivate func bjs_jsApplyInt(_ value: Int32, _ transform: UnsafeMutableRawPointer) -> Int32

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9155,6 +9155,18 @@
91559155
"_0" : "JSPromise"
91569156
}
91579157
}
9158+
},
9159+
{
9160+
"jsName" : "$jsWeirdFunction",
9161+
"name" : "_jsWeirdFunction",
9162+
"parameters" : [
9163+
9164+
],
9165+
"returnType" : {
9166+
"double" : {
9167+
9168+
}
9169+
}
91589170
}
91599171
],
91609172
"types" : [
@@ -9240,6 +9252,35 @@
92409252
}
92419253
}
92429254
]
9255+
},
9256+
{
9257+
"constructor" : {
9258+
"parameters" : [
9259+
9260+
]
9261+
},
9262+
"getters" : [
9263+
9264+
],
9265+
"jsName" : "$WeirdClass",
9266+
"methods" : [
9267+
{
9268+
"jsName" : "method-with-dashes",
9269+
"name" : "method_with_dashes",
9270+
"parameters" : [
9271+
9272+
],
9273+
"returnType" : {
9274+
"string" : {
9275+
9276+
}
9277+
}
9278+
}
9279+
],
9280+
"name" : "_WeirdClass",
9281+
"setters" : [
9282+
9283+
]
92439284
}
92449285
]
92459286
},

Tests/BridgeJSRuntimeTests/ImportAPITests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,11 @@ class ImportAPITests: XCTestCase {
120120
XCTAssertEqual(ret, 5)
121121
XCTAssertEqual(total, 10)
122122
}
123+
124+
func testJSNameFunctionAndClass() throws {
125+
XCTAssertEqual(try _jsWeirdFunction(), 42)
126+
127+
let obj = try _WeirdClass()
128+
XCTAssertEqual(try obj.method_with_dashes(), "ok")
129+
}
123130
}

Tests/BridgeJSRuntimeTests/bridge-js.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,11 @@ export class JsGreeter {
2323
}
2424

2525
export function runAsyncWorks(): Promise<void>;
26+
27+
// jsName tests
28+
export function $jsWeirdFunction(): number;
29+
30+
export class $WeirdClass {
31+
constructor();
32+
"method-with-dashes"(): string;
33+
}

Tests/prelude.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export async function setupOptions(options, context) {
5050
"jsRoundTripFeatureFlag": (flag) => {
5151
return flag;
5252
},
53+
"$jsWeirdFunction": () => {
54+
return 42;
55+
},
5356
JsGreeter: class {
5457
/**
5558
* @param {string} name
@@ -67,6 +70,13 @@ export async function setupOptions(options, context) {
6770
this.name = name;
6871
}
6972
},
73+
$WeirdClass: class {
74+
constructor() {
75+
}
76+
["method-with-dashes"]() {
77+
return "ok";
78+
}
79+
},
7080
Foo: ImportedFoo,
7181
runAsyncWorks: async () => {
7282
const exports = importsContext.getExports();

0 commit comments

Comments
 (0)