Skip to content

Commit 3e77198

Browse files
[NFC] BridgeJS: Avoid emitting empty skeletons
1 parent e55d927 commit 3e77198

File tree

17 files changed

+83
-163
lines changed

17 files changed

+83
-163
lines changed

Benchmarks/Sources/Generated/JavaScript/BridgeJS.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2737,14 +2737,6 @@
27372737
},
27382738
"imported" : {
27392739
"children" : [
2740-
{
2741-
"functions" : [
2742-
2743-
],
2744-
"types" : [
2745-
2746-
]
2747-
},
27482740
{
27492741
"functions" : [
27502742
{

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,6 @@
197197
]
198198
}
199199
]
200-
},
201-
{
202-
"functions" : [
203-
204-
],
205-
"types" : [
206-
207-
]
208200
}
209201
]
210202
},

Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,14 @@ public final class SwiftToSkeleton {
6666
)
6767
}
6868

69-
importedFiles.append(
70-
ImportedFileSkeleton(
71-
functions: importCollector.importedFunctions,
72-
types: importCollector.importedTypes,
73-
globalGetters: importCollector.importedGlobalGetters
74-
)
69+
let importedFile = ImportedFileSkeleton(
70+
functions: importCollector.importedFunctions,
71+
types: importCollector.importedTypes,
72+
globalGetters: importCollector.importedGlobalGetters
7573
)
74+
if !importedFile.isEmpty {
75+
importedFiles.append(importedFile)
76+
}
7677
exportCollector.finalize(&exported)
7778
}
7879

@@ -90,7 +91,8 @@ public final class SwiftToSkeleton {
9091
return module
9192
}()
9293

93-
return BridgeJSSkeleton(moduleName: moduleName, exported: exported, imported: importedSkeleton)
94+
let exportedSkeleton: ExportedSkeleton? = exported.isEmpty ? nil : exported
95+
return BridgeJSSkeleton(moduleName: moduleName, exported: exportedSkeleton, imported: importedSkeleton)
9496
}
9597

9698
func lookupType(for type: TypeSyntax, errors: inout [DiagnosticError]) -> BridgeType? {

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,10 @@ public struct ExportedSkeleton: Codable {
624624
self.protocols.append(contentsOf: other.protocols)
625625
assert(self.exposeToGlobal == other.exposeToGlobal)
626626
}
627+
628+
public var isEmpty: Bool {
629+
functions.isEmpty && classes.isEmpty && enums.isEmpty && structs.isEmpty && protocols.isEmpty
630+
}
627631
}
628632

629633
// MARK: - Imported Skeleton
@@ -854,6 +858,10 @@ public struct ImportedFileSkeleton: Codable {
854858
try container.encode(globalSetters, forKey: .globalSetters)
855859
}
856860
}
861+
862+
public var isEmpty: Bool {
863+
functions.isEmpty && types.isEmpty && globalGetters.isEmpty && globalSetters.isEmpty
864+
}
857865
}
858866

859867
public struct ImportedModuleSkeleton: Codable {

Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,22 @@ import Testing
166166
let skeleton = try swiftAPI.finalize()
167167
try snapshotCodegen(skeleton: skeleton, name: "CrossFileFunctionTypes.ReverseOrder")
168168
}
169+
170+
@Test
171+
func codegenSkipsEmptySkeletons() throws {
172+
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
173+
let importedURL = Self.multifileInputsDirectory.appendingPathComponent("ImportedFunctions.swift")
174+
swiftAPI.addSourceFile(
175+
Parser.parse(source: try String(contentsOf: importedURL, encoding: .utf8)),
176+
inputFilePath: "ImportedFunctions.swift"
177+
)
178+
let exportedOnlyURL = Self.multifileInputsDirectory.appendingPathComponent("ExportedOnly.swift")
179+
swiftAPI.addSourceFile(
180+
Parser.parse(source: try String(contentsOf: exportedOnlyURL, encoding: .utf8)),
181+
inputFilePath: "ExportedOnly.swift"
182+
)
183+
let skeleton = try swiftAPI.finalize()
184+
#expect(skeleton.exported == nil, "Empty exported skeleton should be omitted")
185+
try snapshotCodegen(skeleton: skeleton, name: "CrossFileSkipsEmptySkeletons")
186+
}
169187
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct ExportedOnly {
2+
let value: Int = 0
3+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@JSFunction func fetchNumber() throws(JSException) -> Int
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"imported" : {
3+
"children" : [
4+
{
5+
"functions" : [
6+
{
7+
"name" : "fetchNumber",
8+
"parameters" : [
9+
10+
],
11+
"returnType" : {
12+
"int" : {
13+
14+
}
15+
}
16+
}
17+
],
18+
"types" : [
19+
20+
]
21+
}
22+
]
23+
},
24+
"moduleName" : "TestModule"
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#if arch(wasm32)
2+
@_extern(wasm, module: "TestModule", name: "bjs_fetchNumber")
3+
fileprivate func bjs_fetchNumber() -> Int32
4+
#else
5+
fileprivate func bjs_fetchNumber() -> Int32 {
6+
fatalError("Only available on WebAssembly")
7+
}
8+
#endif
9+
10+
func _$fetchNumber() throws(JSException) -> Int {
11+
let ret = bjs_fetchNumber()
12+
if let error = _swift_js_take_exception() {
13+
throw error
14+
}
15+
return Int.bridgeJSLiftReturn(ret)
16+
}

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
{
2-
"exported" : {
3-
"classes" : [
4-
5-
],
6-
"enums" : [
7-
8-
],
9-
"exposeToGlobal" : false,
10-
"functions" : [
11-
12-
],
13-
"protocols" : [
14-
15-
],
16-
"structs" : [
17-
18-
]
19-
},
202
"imported" : {
213
"children" : [
224
{

0 commit comments

Comments
 (0)