Skip to content

Commit 411a135

Browse files
- Added runtime coverage for static @JSFunction imports: TS definitions and JS prelude now expose StaticBox static/instance API including jsName with dashes (Tests/BridgeJSRuntimeTests/bridge-js.d.ts, Tests/prelude.mjs).
- Regenerated BridgeJS bindings so `StaticBox` surfaces static functions plus instance `value` in the runtime module (`Tests/BridgeJSRuntimeTests/Generated/BridgeJS.Macros.swift`, `Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift`, `Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json`). - TS2Swift now emits `static` for class methods marked `static` in .d.ts to support the new import shape (`Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js`). - Added Swift runtime test validating static and jsName-static calls (`Tests/BridgeJSRuntimeTests/ImportAPITests.swift`). Tests: - `make unittest SWIFT_SDK_ID=DEVELOPMENT-SNAPSHOT-2025-11-03-a-wasm32-unknown-wasip1`
1 parent cc57cc4 commit 411a135

File tree

7 files changed

+251
-1
lines changed

7 files changed

+251
-1
lines changed

Plugins/BridgeJS/Sources/TS2Swift/JavaScript/src/processor.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,8 +821,12 @@ export class TypeProcessor {
821821
const returnType = this.visitType(signature.getReturnType(), node);
822822
const effects = this.renderEffects({ isAsync: false });
823823
const swiftMethodName = this.renderIdentifier(swiftName);
824+
const isStatic = node.modifiers?.some(
825+
(modifier) => modifier.kind === ts.SyntaxKind.StaticKeyword
826+
) ?? false;
827+
const staticKeyword = isStatic ? "static " : "";
824828

825-
this.swiftLines.push(` ${annotation} func ${swiftMethodName}(${params}) ${effects} -> ${returnType}`);
829+
this.swiftLines.push(` ${annotation} ${staticKeyword}func ${swiftMethodName}(${params}) ${effects} -> ${returnType}`);
826830
}
827831

828832
/**

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.Macros.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ extension FeatureFlag: _BridgedSwiftEnumNoPayload, _BridgedSwiftRawValueEnum {}
4848
@JSFunction(jsName: "method-with-dashes") func method_with_dashes() throws(JSException) -> String
4949
}
5050

51+
@JSClass struct StaticBox {
52+
@JSFunction init(_ value: Double) throws(JSException)
53+
@JSFunction func value() throws(JSException) -> Double
54+
@JSFunction static func create(_ value: Double) throws(JSException) -> StaticBox
55+
@JSFunction static func value() throws(JSException) -> Double
56+
@JSFunction static func makeDefault() throws(JSException) -> StaticBox
57+
@JSFunction(jsName: "with-dashes") static func with_dashes() throws(JSException) -> StaticBox
58+
}
59+
5160
@JSFunction(from: .global) func parseInt(_ string: String) throws(JSException) -> Double
5261

5362
@JSClass(from: .global) struct Animal {

Tests/BridgeJSRuntimeTests/Generated/BridgeJS.swift

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8140,6 +8140,111 @@ func _$_WeirdClass_method_with_dashes(_ self: JSObject) throws(JSException) -> S
81408140
return String.bridgeJSLiftReturn(ret)
81418141
}
81428142

8143+
#if arch(wasm32)
8144+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_init")
8145+
fileprivate func bjs_StaticBox_init(_ value: Float64) -> Int32
8146+
#else
8147+
fileprivate func bjs_StaticBox_init(_ value: Float64) -> Int32 {
8148+
fatalError("Only available on WebAssembly")
8149+
}
8150+
#endif
8151+
8152+
#if arch(wasm32)
8153+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_create_static")
8154+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32
8155+
#else
8156+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32 {
8157+
fatalError("Only available on WebAssembly")
8158+
}
8159+
#endif
8160+
8161+
#if arch(wasm32)
8162+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_value_static")
8163+
fileprivate func bjs_StaticBox_value_static() -> Float64
8164+
#else
8165+
fileprivate func bjs_StaticBox_value_static() -> Float64 {
8166+
fatalError("Only available on WebAssembly")
8167+
}
8168+
#endif
8169+
8170+
#if arch(wasm32)
8171+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_makeDefault_static")
8172+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32
8173+
#else
8174+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32 {
8175+
fatalError("Only available on WebAssembly")
8176+
}
8177+
#endif
8178+
8179+
#if arch(wasm32)
8180+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_with_dashes_static")
8181+
fileprivate func bjs_StaticBox_with_dashes_static() -> Int32
8182+
#else
8183+
fileprivate func bjs_StaticBox_with_dashes_static() -> Int32 {
8184+
fatalError("Only available on WebAssembly")
8185+
}
8186+
#endif
8187+
8188+
#if arch(wasm32)
8189+
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_StaticBox_value")
8190+
fileprivate func bjs_StaticBox_value(_ self: Int32) -> Float64
8191+
#else
8192+
fileprivate func bjs_StaticBox_value(_ self: Int32) -> Float64 {
8193+
fatalError("Only available on WebAssembly")
8194+
}
8195+
#endif
8196+
8197+
func _$StaticBox_init(_ value: Double) throws(JSException) -> JSObject {
8198+
let valueValue = value.bridgeJSLowerParameter()
8199+
let ret = bjs_StaticBox_init(valueValue)
8200+
if let error = _swift_js_take_exception() {
8201+
throw error
8202+
}
8203+
return JSObject.bridgeJSLiftReturn(ret)
8204+
}
8205+
8206+
func _$StaticBox_create(_ value: Double) throws(JSException) -> StaticBox {
8207+
let valueValue = value.bridgeJSLowerParameter()
8208+
let ret = bjs_StaticBox_create_static(valueValue)
8209+
if let error = _swift_js_take_exception() {
8210+
throw error
8211+
}
8212+
return StaticBox.bridgeJSLiftReturn(ret)
8213+
}
8214+
8215+
func _$StaticBox_value() throws(JSException) -> Double {
8216+
let ret = bjs_StaticBox_value_static()
8217+
if let error = _swift_js_take_exception() {
8218+
throw error
8219+
}
8220+
return Double.bridgeJSLiftReturn(ret)
8221+
}
8222+
8223+
func _$StaticBox_makeDefault() throws(JSException) -> StaticBox {
8224+
let ret = bjs_StaticBox_makeDefault_static()
8225+
if let error = _swift_js_take_exception() {
8226+
throw error
8227+
}
8228+
return StaticBox.bridgeJSLiftReturn(ret)
8229+
}
8230+
8231+
func _$StaticBox_with_dashes() throws(JSException) -> StaticBox {
8232+
let ret = bjs_StaticBox_with_dashes_static()
8233+
if let error = _swift_js_take_exception() {
8234+
throw error
8235+
}
8236+
return StaticBox.bridgeJSLiftReturn(ret)
8237+
}
8238+
8239+
func _$StaticBox_value(_ self: JSObject) throws(JSException) -> Double {
8240+
let selfValue = self.bridgeJSLowerParameter()
8241+
let ret = bjs_StaticBox_value(selfValue)
8242+
if let error = _swift_js_take_exception() {
8243+
throw error
8244+
}
8245+
return Double.bridgeJSLiftReturn(ret)
8246+
}
8247+
81438248
#if arch(wasm32)
81448249
@_extern(wasm, module: "BridgeJSRuntimeTests", name: "bjs_Animal_init")
81458250
fileprivate func bjs_Animal_init(_ name: Int32, _ age: Float64, _ isCat: Int32) -> Int32

Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11998,6 +11998,94 @@
1199811998

1199911999
]
1200012000
},
12001+
{
12002+
"constructor" : {
12003+
"parameters" : [
12004+
{
12005+
"name" : "value",
12006+
"type" : {
12007+
"double" : {
12008+
12009+
}
12010+
}
12011+
}
12012+
]
12013+
},
12014+
"getters" : [
12015+
12016+
],
12017+
"methods" : [
12018+
{
12019+
"name" : "value",
12020+
"parameters" : [
12021+
12022+
],
12023+
"returnType" : {
12024+
"double" : {
12025+
12026+
}
12027+
}
12028+
}
12029+
],
12030+
"name" : "StaticBox",
12031+
"setters" : [
12032+
12033+
],
12034+
"staticMethods" : [
12035+
{
12036+
"name" : "create",
12037+
"parameters" : [
12038+
{
12039+
"name" : "value",
12040+
"type" : {
12041+
"double" : {
12042+
12043+
}
12044+
}
12045+
}
12046+
],
12047+
"returnType" : {
12048+
"jsObject" : {
12049+
"_0" : "StaticBox"
12050+
}
12051+
}
12052+
},
12053+
{
12054+
"name" : "value",
12055+
"parameters" : [
12056+
12057+
],
12058+
"returnType" : {
12059+
"double" : {
12060+
12061+
}
12062+
}
12063+
},
12064+
{
12065+
"name" : "makeDefault",
12066+
"parameters" : [
12067+
12068+
],
12069+
"returnType" : {
12070+
"jsObject" : {
12071+
"_0" : "StaticBox"
12072+
}
12073+
}
12074+
},
12075+
{
12076+
"jsName" : "with-dashes",
12077+
"name" : "with_dashes",
12078+
"parameters" : [
12079+
12080+
],
12081+
"returnType" : {
12082+
"jsObject" : {
12083+
"_0" : "StaticBox"
12084+
}
12085+
}
12086+
}
12087+
]
12088+
},
1200112089
{
1200212090
"constructor" : {
1200312091
"parameters" : [

Tests/BridgeJSRuntimeTests/ImportAPITests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,17 @@ class ImportAPITests: XCTestCase {
127127
let obj = try _WeirdClass()
128128
XCTAssertEqual(try obj.method_with_dashes(), "ok")
129129
}
130+
131+
func testJSClassStaticFunctions() throws {
132+
let created = try StaticBox.create(10)
133+
XCTAssertEqual(try created.value(), 10)
134+
135+
let defaultBox = try StaticBox.makeDefault()
136+
XCTAssertEqual(try defaultBox.value(), 0)
137+
138+
XCTAssertEqual(try StaticBox.value(), 99)
139+
140+
let dashed = try StaticBox.with_dashes()
141+
XCTAssertEqual(try dashed.value(), 7)
142+
}
130143
}

Tests/BridgeJSRuntimeTests/bridge-js.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,12 @@ export class $WeirdClass {
3131
constructor();
3232
"method-with-dashes"(): string;
3333
}
34+
35+
export class StaticBox {
36+
constructor(value: number);
37+
value(): number;
38+
static create(value: number): StaticBox;
39+
static value(): number;
40+
static makeDefault(): StaticBox;
41+
static "with-dashes"(): StaticBox;
42+
}

Tests/prelude.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ export async function setupOptions(options, context) {
99
Error.stackTraceLimit = 100;
1010
setupTestGlobals(globalThis);
1111

12+
class StaticBox {
13+
constructor(value) {
14+
this._value = value;
15+
}
16+
value() {
17+
return this._value;
18+
}
19+
static create(value) {
20+
return new StaticBox(value);
21+
}
22+
static value() {
23+
return 99;
24+
}
25+
static makeDefault() {
26+
return new StaticBox(0);
27+
}
28+
static ["with-dashes"]() {
29+
return new StaticBox(7);
30+
}
31+
}
32+
1233
return {
1334
...options,
1435
getImports: (importsContext) => {
@@ -78,6 +99,7 @@ export async function setupOptions(options, context) {
7899
return "ok";
79100
}
80101
},
102+
StaticBox,
81103
Foo: ImportedFoo,
82104
runAsyncWorks: async () => {
83105
const exports = importsContext.getExports();

0 commit comments

Comments
 (0)