Skip to content

Commit 4e3f02b

Browse files
BridgeJS: Fix d.ts rendering of static methods
1 parent a2aa6e1 commit 4e3f02b

File tree

6 files changed

+422
-13
lines changed

6 files changed

+422
-13
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,25 @@ extension BridgeJSLink {
31443144
importObjectBuilder.assignToImportObject(name: abiName, function: js)
31453145
importObjectBuilder.appendDts(dts)
31463146
}
3147+
if type.from == nil, type.constructor != nil || !type.staticMethods.isEmpty {
3148+
let dtsPrinter = CodeFragmentPrinter()
3149+
dtsPrinter.write("\(type.name): {")
3150+
dtsPrinter.indent {
3151+
if let constructor = type.constructor {
3152+
let returnType = BridgeType.jsObject(type.name)
3153+
dtsPrinter.write(
3154+
"new\(renderTSSignature(parameters: constructor.parameters, returnType: returnType, effects: Effects(isAsync: false, isThrows: false)));"
3155+
)
3156+
}
3157+
for method in type.staticMethods {
3158+
let methodName = method.jsName ?? method.name
3159+
let signature = "\(renderTSPropertyName(methodName))\(renderTSSignature(parameters: method.parameters, returnType: method.returnType, effects: Effects(isAsync: false, isThrows: false)));"
3160+
dtsPrinter.write(signature)
3161+
}
3162+
}
3163+
dtsPrinter.write("}")
3164+
importObjectBuilder.appendDts(dtsPrinter.lines)
3165+
}
31473166
for method in type.methods {
31483167
let (js, dts) = try renderImportedMethod(context: type, method: method)
31493168
importObjectBuilder.assignToImportObject(name: method.abiName(context: type), function: js)
@@ -3174,19 +3193,6 @@ extension BridgeJSLink {
31743193
returnType: returnType
31753194
)
31763195
importObjectBuilder.assignToImportObject(name: abiName, function: funcLines)
3177-
3178-
if type.from == nil {
3179-
let dtsPrinter = CodeFragmentPrinter()
3180-
dtsPrinter.write("\(type.name): {")
3181-
dtsPrinter.indent {
3182-
dtsPrinter.write(
3183-
"new\(renderTSSignature(parameters: constructor.parameters, returnType: returnType, effects: Effects(isAsync: false, isThrows: false)));"
3184-
)
3185-
}
3186-
dtsPrinter.write("}")
3187-
3188-
importObjectBuilder.appendDts(dtsPrinter.lines)
3189-
}
31903196
}
31913197

31923198
func renderImportedGetter(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,8 @@ extension StaticBox {
1111
extension StaticBox {
1212
@JSFunction(jsName: "with-dashes") static func dashed() throws(JSException) -> StaticBox
1313
}
14+
15+
@JSClass struct WithCtor {
16+
@JSFunction init(_ value: Double) throws(JSException)
17+
@JSFunction static func create(_ value: Double) throws(JSException) -> WithCtor
18+
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,50 @@
9999
}
100100
}
101101
]
102+
},
103+
{
104+
"constructor" : {
105+
"parameters" : [
106+
{
107+
"name" : "value",
108+
"type" : {
109+
"double" : {
110+
111+
}
112+
}
113+
}
114+
]
115+
},
116+
"getters" : [
117+
118+
],
119+
"methods" : [
120+
121+
],
122+
"name" : "WithCtor",
123+
"setters" : [
124+
125+
],
126+
"staticMethods" : [
127+
{
128+
"name" : "create",
129+
"parameters" : [
130+
{
131+
"name" : "value",
132+
"type" : {
133+
"double" : {
134+
135+
}
136+
}
137+
}
138+
],
139+
"returnType" : {
140+
"jsObject" : {
141+
"_0" : "WithCtor"
142+
}
143+
}
144+
}
145+
]
102146
}
103147
]
104148
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,40 @@ func _$StaticBox_value(_ self: JSObject) throws(JSException) -> Double {
8383
throw error
8484
}
8585
return Double.bridgeJSLiftReturn(ret)
86+
}
87+
88+
#if arch(wasm32)
89+
@_extern(wasm, module: "TestModule", name: "bjs_WithCtor_init")
90+
fileprivate func bjs_WithCtor_init(_ value: Float64) -> Int32
91+
#else
92+
fileprivate func bjs_WithCtor_init(_ value: Float64) -> Int32 {
93+
fatalError("Only available on WebAssembly")
94+
}
95+
#endif
96+
97+
#if arch(wasm32)
98+
@_extern(wasm, module: "TestModule", name: "bjs_WithCtor_create_static")
99+
fileprivate func bjs_WithCtor_create_static(_ value: Float64) -> Int32
100+
#else
101+
fileprivate func bjs_WithCtor_create_static(_ value: Float64) -> Int32 {
102+
fatalError("Only available on WebAssembly")
103+
}
104+
#endif
105+
106+
func _$WithCtor_init(_ value: Double) throws(JSException) -> JSObject {
107+
let valueValue = value.bridgeJSLowerParameter()
108+
let ret = bjs_WithCtor_init(valueValue)
109+
if let error = _swift_js_take_exception() {
110+
throw error
111+
}
112+
return JSObject.bridgeJSLiftReturn(ret)
113+
}
114+
115+
func _$WithCtor_create(_ value: Double) throws(JSException) -> WithCtor {
116+
let valueValue = value.bridgeJSLowerParameter()
117+
let ret = bjs_WithCtor_create_static(valueValue)
118+
if let error = _swift_js_take_exception() {
119+
throw error
120+
}
121+
return WithCtor.bridgeJSLiftReturn(ret)
86122
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
2+
// DO NOT EDIT.
3+
//
4+
// To update this file, just rebuild your project or run
5+
// `swift package bridge-js`.
6+
7+
export interface StaticBox {
8+
value(): number;
9+
}
10+
export interface WithCtor {
11+
}
12+
export type Exports = {
13+
}
14+
export type Imports = {
15+
StaticBox: {
16+
create(value: number): StaticBox;
17+
value(): number;
18+
makeDefault(): StaticBox;
19+
"with-dashes"(): StaticBox;
20+
}
21+
WithCtor: {
22+
new(value: number): WithCtor;
23+
create(value: number): WithCtor;
24+
}
25+
}
26+
export function createInstantiator(options: {
27+
imports: Imports;
28+
}, swift: any): Promise<{
29+
addImports: (importObject: WebAssembly.Imports) => void;
30+
setInstance: (instance: WebAssembly.Instance) => void;
31+
createExports: (instance: WebAssembly.Instance) => Exports;
32+
}>;

0 commit comments

Comments
 (0)