diff --git a/Benchmarks/Sources/Generated/JavaScript/BridgeJS.json b/Benchmarks/Sources/Generated/JavaScript/BridgeJS.json index 4dff97132..eacd18cb3 100644 --- a/Benchmarks/Sources/Generated/JavaScript/BridgeJS.json +++ b/Benchmarks/Sources/Generated/JavaScript/BridgeJS.json @@ -2737,14 +2737,6 @@ }, "imported" : { "children" : [ - { - "functions" : [ - - ], - "types" : [ - - ] - }, { "functions" : [ { diff --git a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json index 2952e2157..743925c5f 100644 --- a/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json +++ b/Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/JavaScript/BridgeJS.json @@ -197,14 +197,6 @@ ] } ] - }, - { - "functions" : [ - - ], - "types" : [ - - ] } ] }, diff --git a/Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift b/Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift index f52aa7008..32b9f6418 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSCore/SwiftToSkeleton.swift @@ -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) } @@ -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? { diff --git a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift index 3e2d51f8f..a9812e2e0 100644 --- a/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift +++ b/Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift @@ -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 @@ -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 { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift index ed83bc074..9754fbced 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/BridgeJSCodegenTests.swift @@ -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") + } } diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ExportedOnly.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ExportedOnly.swift new file mode 100644 index 000000000..d7887d2bd --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ExportedOnly.swift @@ -0,0 +1,3 @@ +struct ExportedOnly { + let value: Int = 0 +} diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ImportedFunctions.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ImportedFunctions.swift new file mode 100644 index 000000000..451424777 --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/Inputs/MacroSwift/Multifile/ImportedFunctions.swift @@ -0,0 +1 @@ +@JSFunction func fetchNumber() throws(JSException) -> Int diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.json new file mode 100644 index 000000000..10c079b27 --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.json @@ -0,0 +1,25 @@ +{ + "imported" : { + "children" : [ + { + "functions" : [ + { + "name" : "fetchNumber", + "parameters" : [ + + ], + "returnType" : { + "int" : { + + } + } + } + ], + "types" : [ + + ] + } + ] + }, + "moduleName" : "TestModule" +} \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.swift b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.swift new file mode 100644 index 000000000..21e4e939a --- /dev/null +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/CrossFileSkipsEmptySkeletons.swift @@ -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) +} \ No newline at end of file diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.json index 031870e57..55ac7dd70 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalGetter.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.json index c75d9e011..5e002e34f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/GlobalThisImports.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.json index b983f27a4..935f7a7f2 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/InvalidPropertyNames.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.json index be1d0f4cd..689e86150 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClass.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.json index df3d3829e..a8b64558f 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/JSClassStaticFunctions.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.json index f74b3cdf7..9c99bb8c4 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/Optionals.json @@ -1220,6 +1220,9 @@ } } } + ], + "staticMethods" : [ + ] } ] diff --git a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosureImports.json b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosureImports.json index 12f76a912..9e0e0a491 100644 --- a/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosureImports.json +++ b/Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSCodegenTests/SwiftClosureImports.json @@ -1,22 +1,4 @@ { - "exported" : { - "classes" : [ - - ], - "enums" : [ - - ], - "exposeToGlobal" : false, - "functions" : [ - - ], - "protocols" : [ - - ], - "structs" : [ - - ] - }, "imported" : { "children" : [ { diff --git a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json index 476f60e34..0cb32079a 100644 --- a/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json +++ b/Tests/BridgeJSRuntimeTests/Generated/JavaScript/BridgeJS.json @@ -12643,22 +12643,6 @@ } ] }, - { - "functions" : [ - - ], - "types" : [ - - ] - }, - { - "functions" : [ - - ], - "types" : [ - - ] - }, { "functions" : [ { @@ -12904,22 +12888,6 @@ ], "types" : [ - ] - }, - { - "functions" : [ - - ], - "types" : [ - - ] - }, - { - "functions" : [ - - ], - "types" : [ - ] } ]