Skip to content

Commit cf6c745

Browse files
BridgeJS: Simplify static method naming
1 parent 0c3cb25 commit cf6c745

File tree

6 files changed

+93
-109
lines changed

6 files changed

+93
-109
lines changed

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,7 +2198,7 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
21982198

21992199
private func handleTopLevelFunction(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
22002200
if let jsFunction = AttributeChecker.firstJSFunctionAttribute(node.attributes),
2201-
let function = parseFunction(jsFunction, node, enclosingTypeName: nil, isStaticMember: true)
2201+
let function = parseFunction(jsFunction, node)
22022202
{
22032203
importedFunctions.append(function)
22042204
return .skipChildren
@@ -2223,19 +2223,11 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
22232223
type: inout CurrentType
22242224
) -> Bool {
22252225
if let jsFunction = AttributeChecker.firstJSFunctionAttribute(node.attributes) {
2226-
if isStaticMember {
2227-
parseFunction(
2228-
jsFunction,
2229-
node,
2230-
enclosingTypeName: typeName,
2231-
isStaticMember: true,
2232-
includeTypeNameForStatic: false
2233-
).map {
2234-
type.staticMethods.append($0)
2235-
}
2236-
} else {
2237-
parseFunction(jsFunction, node, enclosingTypeName: typeName, isStaticMember: false).map {
2238-
type.methods.append($0)
2226+
if let method = parseFunction(jsFunction, node) {
2227+
if isStaticMember {
2228+
type.staticMethods.append(method)
2229+
} else {
2230+
type.methods.append(method)
22392231
}
22402232
}
22412233
return true
@@ -2332,30 +2324,12 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
23322324
for member in members {
23332325
if let function = member.decl.as(FunctionDeclSyntax.self) {
23342326
if let jsFunction = AttributeChecker.firstJSFunctionAttribute(function.attributes),
2335-
let parsed = parseFunction(
2336-
jsFunction,
2337-
function,
2338-
enclosingTypeName: typeName,
2339-
isStaticMember: true,
2340-
includeTypeNameForStatic: !jsClassNames.contains(typeName)
2341-
)
2342-
{
2327+
let parsed = parseFunction(jsFunction, function) {
23432328
if jsClassNames.contains(typeName) {
23442329
if let index = importedTypes.firstIndex(where: { $0.name == typeName }) {
2345-
let existing = importedTypes[index]
2346-
importedTypes[index] = ImportedTypeSkeleton(
2347-
name: existing.name,
2348-
jsName: existing.jsName,
2349-
from: existing.from,
2350-
constructor: existing.constructor,
2351-
methods: existing.methods,
2352-
staticMethods: existing.staticMethods + [parsed],
2353-
getters: existing.getters,
2354-
setters: existing.setters,
2355-
documentation: existing.documentation
2356-
)
2330+
importedTypes[index].staticMethods.append(parsed)
23572331
} else {
2358-
staticMethodsByType[typeName, default: []].append(parsed)
2332+
importedTypes.append(ImportedTypeSkeleton(name: typeName, staticMethods: [parsed]))
23592333
}
23602334
} else {
23612335
importedFunctions.append(parsed)
@@ -2403,9 +2377,6 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
24032377
private func parseFunction(
24042378
_ jsFunction: AttributeSyntax,
24052379
_ node: FunctionDeclSyntax,
2406-
enclosingTypeName: String?,
2407-
isStaticMember: Bool,
2408-
includeTypeNameForStatic: Bool = true
24092380
) -> ImportedFunctionSkeleton? {
24102381
guard validateEffects(node.signature.effectSpecifiers, node: node, attributeName: "JSFunction") != nil
24112382
else {
@@ -2415,12 +2386,7 @@ private final class ImportSwiftMacrosAPICollector: SyntaxAnyVisitor {
24152386
let baseName = SwiftToSkeleton.normalizeIdentifier(node.name.text)
24162387
let jsName = AttributeChecker.extractJSName(from: jsFunction)
24172388
let from = AttributeChecker.extractJSImportFrom(from: jsFunction)
2418-
let name: String
2419-
if isStaticMember, includeTypeNameForStatic, let enclosingTypeName {
2420-
name = "\(enclosingTypeName)_\(baseName)"
2421-
} else {
2422-
name = baseName
2423-
}
2389+
let name = baseName
24242390

24252391
let parameters = parseParameters(from: node.signature.parameterClause)
24262392
let returnType: BridgeType

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ public struct ImportedTypeSkeleton: Codable {
758758
public let constructor: ImportedConstructorSkeleton?
759759
public let methods: [ImportedFunctionSkeleton]
760760
/// Static methods available on the JavaScript constructor.
761-
public var staticMethods: [ImportedFunctionSkeleton] = []
761+
public var staticMethods: [ImportedFunctionSkeleton]
762762
public let getters: [ImportedGetterSkeleton]
763763
public let setters: [ImportedSetterSkeleton]
764764
public let documentation: String?
@@ -768,7 +768,7 @@ public struct ImportedTypeSkeleton: Codable {
768768
jsName: String? = nil,
769769
from: JSImportFrom? = nil,
770770
constructor: ImportedConstructorSkeleton? = nil,
771-
methods: [ImportedFunctionSkeleton],
771+
methods: [ImportedFunctionSkeleton] = [],
772772
staticMethods: [ImportedFunctionSkeleton] = [],
773773
getters: [ImportedGetterSkeleton] = [],
774774
setters: [ImportedSetterSkeleton] = [],

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

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,43 @@
2424

2525
],
2626
"types" : [
27+
{
28+
"getters" : [
29+
30+
],
31+
"methods" : [
32+
33+
],
34+
"name" : "StaticBox",
35+
"setters" : [
36+
37+
],
38+
"staticMethods" : [
39+
{
40+
"name" : "makeDefault",
41+
"parameters" : [
42+
43+
],
44+
"returnType" : {
45+
"jsObject" : {
46+
"_0" : "StaticBox"
47+
}
48+
}
49+
},
50+
{
51+
"jsName" : "with-dashes",
52+
"name" : "dashed",
53+
"parameters" : [
54+
55+
],
56+
"returnType" : {
57+
"jsObject" : {
58+
"_0" : "StaticBox"
59+
}
60+
}
61+
}
62+
]
63+
},
2764
{
2865
"getters" : [
2966

@@ -74,29 +111,6 @@
74111

75112
}
76113
}
77-
},
78-
{
79-
"name" : "makeDefault",
80-
"parameters" : [
81-
82-
],
83-
"returnType" : {
84-
"jsObject" : {
85-
"_0" : "StaticBox"
86-
}
87-
}
88-
},
89-
{
90-
"jsName" : "with-dashes",
91-
"name" : "dashed",
92-
"parameters" : [
93-
94-
],
95-
"returnType" : {
96-
"jsObject" : {
97-
"_0" : "StaticBox"
98-
}
99-
}
100114
}
101115
]
102116
},

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

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,51 @@
11
#if arch(wasm32)
2-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_create_static")
3-
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32
2+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_makeDefault_static")
3+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32
44
#else
5-
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32 {
5+
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32 {
66
fatalError("Only available on WebAssembly")
77
}
88
#endif
99

1010
#if arch(wasm32)
11-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_value_static")
12-
fileprivate func bjs_StaticBox_value_static() -> Float64
11+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_dashed_static")
12+
fileprivate func bjs_StaticBox_dashed_static() -> Int32
1313
#else
14-
fileprivate func bjs_StaticBox_value_static() -> Float64 {
14+
fileprivate func bjs_StaticBox_dashed_static() -> Int32 {
1515
fatalError("Only available on WebAssembly")
1616
}
1717
#endif
1818

19+
func _$StaticBox_makeDefault() throws(JSException) -> StaticBox {
20+
let ret = bjs_StaticBox_makeDefault_static()
21+
if let error = _swift_js_take_exception() {
22+
throw error
23+
}
24+
return StaticBox.bridgeJSLiftReturn(ret)
25+
}
26+
27+
func _$StaticBox_dashed() throws(JSException) -> StaticBox {
28+
let ret = bjs_StaticBox_dashed_static()
29+
if let error = _swift_js_take_exception() {
30+
throw error
31+
}
32+
return StaticBox.bridgeJSLiftReturn(ret)
33+
}
34+
1935
#if arch(wasm32)
20-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_makeDefault_static")
21-
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32
36+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_create_static")
37+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32
2238
#else
23-
fileprivate func bjs_StaticBox_makeDefault_static() -> Int32 {
39+
fileprivate func bjs_StaticBox_create_static(_ value: Float64) -> Int32 {
2440
fatalError("Only available on WebAssembly")
2541
}
2642
#endif
2743

2844
#if arch(wasm32)
29-
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_dashed_static")
30-
fileprivate func bjs_StaticBox_dashed_static() -> Int32
45+
@_extern(wasm, module: "TestModule", name: "bjs_StaticBox_value_static")
46+
fileprivate func bjs_StaticBox_value_static() -> Float64
3147
#else
32-
fileprivate func bjs_StaticBox_dashed_static() -> Int32 {
48+
fileprivate func bjs_StaticBox_value_static() -> Float64 {
3349
fatalError("Only available on WebAssembly")
3450
}
3551
#endif
@@ -60,22 +76,6 @@ func _$StaticBox_value() throws(JSException) -> Double {
6076
return Double.bridgeJSLiftReturn(ret)
6177
}
6278

63-
func _$StaticBox_makeDefault() throws(JSException) -> StaticBox {
64-
let ret = bjs_StaticBox_makeDefault_static()
65-
if let error = _swift_js_take_exception() {
66-
throw error
67-
}
68-
return StaticBox.bridgeJSLiftReturn(ret)
69-
}
70-
71-
func _$StaticBox_dashed() throws(JSException) -> StaticBox {
72-
let ret = bjs_StaticBox_dashed_static()
73-
if let error = _swift_js_take_exception() {
74-
throw error
75-
}
76-
return StaticBox.bridgeJSLiftReturn(ret)
77-
}
78-
7979
func _$StaticBox_value(_ self: JSObject) throws(JSException) -> Double {
8080
let selfValue = self.bridgeJSLowerParameter()
8181
let ret = bjs_StaticBox_value(selfValue)

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// To update this file, just rebuild your project or run
55
// `swift package bridge-js`.
66

7+
export interface StaticBox {
8+
}
79
export interface StaticBox {
810
value(): number;
911
}
@@ -13,11 +15,13 @@ export type Exports = {
1315
}
1416
export type Imports = {
1517
StaticBox: {
16-
create(value: number): StaticBox;
17-
value(): number;
1818
makeDefault(): StaticBox;
1919
"with-dashes"(): StaticBox;
2020
}
21+
StaticBox: {
22+
create(value: number): StaticBox;
23+
value(): number;
24+
}
2125
WithCtor: {
2226
new(value: number): WithCtor;
2327
create(value: number): WithCtor;

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/JSClassStaticFunctions.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,37 +203,37 @@ export async function createInstantiator(options, swift) {
203203
return pointer || 0;
204204
}
205205
const TestModule = importObject["TestModule"] = importObject["TestModule"] || {};
206-
TestModule["bjs_StaticBox_create_static"] = function bjs_StaticBox_create_static(value) {
206+
TestModule["bjs_StaticBox_makeDefault_static"] = function bjs_StaticBox_makeDefault_static() {
207207
try {
208-
let ret = imports.StaticBox.create(value);
208+
let ret = imports.StaticBox.makeDefault();
209209
return swift.memory.retain(ret);
210210
} catch (error) {
211211
setException(error);
212212
return 0
213213
}
214214
}
215-
TestModule["bjs_StaticBox_value_static"] = function bjs_StaticBox_value_static() {
215+
TestModule["bjs_StaticBox_dashed_static"] = function bjs_StaticBox_dashed_static() {
216216
try {
217-
let ret = imports.StaticBox.value();
218-
return ret;
217+
let ret = imports.StaticBox["with-dashes"]();
218+
return swift.memory.retain(ret);
219219
} catch (error) {
220220
setException(error);
221221
return 0
222222
}
223223
}
224-
TestModule["bjs_StaticBox_makeDefault_static"] = function bjs_StaticBox_makeDefault_static() {
224+
TestModule["bjs_StaticBox_create_static"] = function bjs_StaticBox_create_static(value) {
225225
try {
226-
let ret = imports.StaticBox.makeDefault();
226+
let ret = imports.StaticBox.create(value);
227227
return swift.memory.retain(ret);
228228
} catch (error) {
229229
setException(error);
230230
return 0
231231
}
232232
}
233-
TestModule["bjs_StaticBox_dashed_static"] = function bjs_StaticBox_dashed_static() {
233+
TestModule["bjs_StaticBox_value_static"] = function bjs_StaticBox_value_static() {
234234
try {
235-
let ret = imports.StaticBox["with-dashes"]();
236-
return swift.memory.retain(ret);
235+
let ret = imports.StaticBox.value();
236+
return ret;
237237
} catch (error) {
238238
setException(error);
239239
return 0

0 commit comments

Comments
 (0)