Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions Benchmarks/Sources/Generated/JavaScript/BridgeJS.json
Original file line number Diff line number Diff line change
Expand Up @@ -2737,14 +2737,6 @@
},
"imported" : {
"children" : [
{
"functions" : [

],
"types" : [

]
},
{
"functions" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,6 @@
]
}
]
},
{
"functions" : [

],
"types" : [

]
}
]
},
Expand Down
16 changes: 9 additions & 7 deletions Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public final class SwiftToSkeleton {
)
}

importedFiles.append(
ImportedFileSkeleton(
functions: importCollector.importedFunctions,
types: importCollector.importedTypes,
globalGetters: importCollector.importedGlobalGetters
)
let importedFile = ImportedFileSkeleton(
functions: importCollector.importedFunctions,
types: importCollector.importedTypes,
globalGetters: importCollector.importedGlobalGetters
)
if !importedFile.isEmpty {
importedFiles.append(importedFile)
}
exportCollector.finalize(&exported)
}

Expand All @@ -90,7 +91,8 @@ public final class SwiftToSkeleton {
return module
}()

return BridgeJSSkeleton(moduleName: moduleName, exported: exported, imported: importedSkeleton)
let exportedSkeleton: ExportedSkeleton? = exported.isEmpty ? nil : exported
return BridgeJSSkeleton(moduleName: moduleName, exported: exportedSkeleton, imported: importedSkeleton)
}

func lookupType(for type: TypeSyntax, errors: inout [DiagnosticError]) -> BridgeType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,10 @@ public struct ExportedSkeleton: Codable {
self.protocols.append(contentsOf: other.protocols)
assert(self.exposeToGlobal == other.exposeToGlobal)
}

public var isEmpty: Bool {
functions.isEmpty && classes.isEmpty && enums.isEmpty && structs.isEmpty && protocols.isEmpty
}
}

// MARK: - Imported Skeleton
Expand Down Expand Up @@ -854,6 +858,10 @@ public struct ImportedFileSkeleton: Codable {
try container.encode(globalSetters, forKey: .globalSetters)
}
}

public var isEmpty: Bool {
functions.isEmpty && types.isEmpty && globalGetters.isEmpty && globalSetters.isEmpty
}
}

public struct ImportedModuleSkeleton: Codable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,22 @@ import Testing
let skeleton = try swiftAPI.finalize()
try snapshotCodegen(skeleton: skeleton, name: "CrossFileFunctionTypes.ReverseOrder")
}

@Test
func codegenSkipsEmptySkeletons() throws {
let swiftAPI = SwiftToSkeleton(progress: .silent, moduleName: "TestModule", exposeToGlobal: false)
let importedURL = Self.multifileInputsDirectory.appendingPathComponent("ImportedFunctions.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: importedURL, encoding: .utf8)),
inputFilePath: "ImportedFunctions.swift"
)
let exportedOnlyURL = Self.multifileInputsDirectory.appendingPathComponent("ExportedOnly.swift")
swiftAPI.addSourceFile(
Parser.parse(source: try String(contentsOf: exportedOnlyURL, encoding: .utf8)),
inputFilePath: "ExportedOnly.swift"
)
let skeleton = try swiftAPI.finalize()
#expect(skeleton.exported == nil, "Empty exported skeleton should be omitted")
try snapshotCodegen(skeleton: skeleton, name: "CrossFileSkipsEmptySkeletons")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct ExportedOnly {
let value: Int = 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@JSFunction func fetchNumber() throws(JSException) -> Int
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"imported" : {
"children" : [
{
"functions" : [
{
"name" : "fetchNumber",
"parameters" : [

],
"returnType" : {
"int" : {

}
}
}
],
"types" : [

]
}
]
},
"moduleName" : "TestModule"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#if arch(wasm32)
@_extern(wasm, module: "TestModule", name: "bjs_fetchNumber")
fileprivate func bjs_fetchNumber() -> Int32
#else
fileprivate func bjs_fetchNumber() -> Int32 {
fatalError("Only available on WebAssembly")
}
#endif

func _$fetchNumber() throws(JSException) -> Int {
let ret = bjs_fetchNumber()
if let error = _swift_js_take_exception() {
throw error
}
return Int.bridgeJSLiftReturn(ret)
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,9 @@
}
}
}
],
"staticMethods" : [

]
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"exported" : {
"classes" : [

],
"enums" : [

],
"exposeToGlobal" : false,
"functions" : [

],
"protocols" : [

],
"structs" : [

]
},
"imported" : {
"children" : [
{
Expand Down
32 changes: 0 additions & 32 deletions Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json
Original file line number Diff line number Diff line change
Expand Up @@ -12643,22 +12643,6 @@
}
]
},
{
"functions" : [

],
"types" : [

]
},
{
"functions" : [

],
"types" : [

]
},
{
"functions" : [
{
Expand Down Expand Up @@ -12904,22 +12888,6 @@
],
"types" : [

]
},
{
"functions" : [

],
"types" : [

]
},
{
"functions" : [

],
"types" : [

]
}
]
Expand Down