Skip to content

Commit ac02477

Browse files
Refactoring work before integrating to the main branch (vibe-kanban 09a10065)
Current issues - Generated BridgeJS.swift has two headers - Generated BridgeJS.json has three duplicate moduleName - Warning: ```javascript /home/katei/ghq/github.com/swiftwasm/JavaScriptKit/Plugins/BridgeJS/Sources/BridgeJSCore/ImportSwiftMacros.swift:553:20: warning: value 'enclosingTypeName' was defined but never used; consider replacing with boolean test [#no-usage] 551 | // Generate the setter function name 552 | let setterFunctionName: String 553 | if let enclosingTypeName { | `- warning: value 'enclosingTypeName' was defined but never used; consider replacing with boolean test [#no-usage] 554 | setterFunctionName = "\(baseName)_set" 555 | } else { ```
1 parent 63667d7 commit ac02477

File tree

90 files changed

+102
-571
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+102
-571
lines changed

Benchmarks/Sources/Generated/BridgeJS.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,15 +1175,6 @@ fileprivate func _bjs_ClassRoundtrip_wrap(_ pointer: UnsafeMutableRawPointer) ->
11751175
}
11761176
#endif
11771177

1178-
// bridge-js: skip
1179-
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
1180-
// DO NOT EDIT.
1181-
//
1182-
// To update this file, just rebuild your project or run
1183-
// `swift package bridge-js`.
1184-
1185-
@_spi(BridgeJS) import JavaScriptKit
1186-
11871178
#if arch(wasm32)
11881179
@_extern(wasm, module: "Benchmarks", name: "bjs_benchmarkHelperNoop")
11891180
fileprivate func bjs_benchmarkHelperNoop() -> Void

Benchmarks/Sources/Generated/JavaScript/BridgeJS.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,6 @@
13561356
}
13571357
}
13581358
],
1359-
"moduleName" : "Benchmarks",
13601359
"protocols" : [
13611360

13621361
],
@@ -1655,8 +1654,7 @@
16551654

16561655
]
16571656
}
1658-
],
1659-
"moduleName" : "Benchmarks"
1657+
]
16601658
},
16611659
"moduleName" : "Benchmarks"
16621660
}

Examples/PlayBridgeJS/Sources/PlayBridgeJS/Generated/BridgeJS.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,6 @@ fileprivate func _bjs_PlayBridgeJSOutput_wrap(_ pointer: UnsafeMutableRawPointer
137137
}
138138
#endif
139139

140-
// bridge-js: skip
141-
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
142-
// DO NOT EDIT.
143-
//
144-
// To update this file, just rebuild your project or run
145-
// `swift package bridge-js`.
146-
147-
@_spi(BridgeJS) import JavaScriptKit
148-
149140
#if arch(wasm32)
150141
@_extern(wasm, module: "PlayBridgeJS", name: "bjs_createTS2Swift")
151142
fileprivate func bjs_createTS2Swift() -> Int32

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
"functions" : [
141141

142142
],
143-
"moduleName" : "PlayBridgeJS",
144143
"protocols" : [
145144

146145
],
@@ -202,18 +201,9 @@
202201
],
203202
"types" : [
204203

205-
]
206-
},
207-
{
208-
"functions" : [
209-
210-
],
211-
"types" : [
212-
213204
]
214205
}
215-
],
216-
"moduleName" : "PlayBridgeJS"
206+
]
217207
},
218208
"moduleName" : "PlayBridgeJS"
219209
}

Plugins/BridgeJS/Sources/BridgeJSCore/ExportSwift.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ public class ExportSwift {
8181
return (
8282
outputSwift: outputSwift,
8383
outputSkeleton: ExportedSkeleton(
84-
moduleName: moduleName,
8584
functions: exportedFunctions,
8685
classes: exportedClasses,
8786
enums: exportedEnums,
@@ -1659,17 +1658,6 @@ public class ExportSwift {
16591658
return .swiftHeapObject(swiftCallName)
16601659
}
16611660

1662-
static let prelude: DeclSyntax = """
1663-
// bridge-js: skip
1664-
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
1665-
// DO NOT EDIT.
1666-
//
1667-
// To update this file, just rebuild your project or run
1668-
// `swift package bridge-js`.
1669-
1670-
@_spi(BridgeJS) import JavaScriptKit
1671-
"""
1672-
16731661
func renderSwiftGlue() throws -> String? {
16741662
var decls: [DeclSyntax] = []
16751663
guard
@@ -1678,7 +1666,6 @@ public class ExportSwift {
16781666
else {
16791667
return nil
16801668
}
1681-
decls.append(Self.prelude)
16821669

16831670
let closureCodegen = ClosureCodegen()
16841671
var closureSignatures: Set<ClosureSignature> = []

Plugins/BridgeJS/Sources/BridgeJSCore/ImportSwiftMacros.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class ImportSwiftMacros {
5959
throw BridgeJSCoreError(allErrors.joined(separator: "\n"))
6060
}
6161

62-
let moduleSkeleton = ImportedModuleSkeleton(moduleName: moduleName, children: importedFiles)
62+
let moduleSkeleton = ImportedModuleSkeleton(children: importedFiles)
6363

6464
var importer = ImportTS(progress: progress, moduleName: moduleName)
6565
for skeleton in importedFiles {
@@ -549,12 +549,7 @@ public final class ImportSwiftMacros {
549549
let valueType = parseType(paramType, enclosingTypeName: enclosingTypeName)
550550

551551
// Generate the setter function name
552-
let setterFunctionName: String
553-
if let enclosingTypeName {
554-
setterFunctionName = "\(baseName)_set"
555-
} else {
556-
setterFunctionName = "\(baseName)_set"
557-
}
552+
let setterFunctionName = "\(baseName)_set"
558553

559554
return ImportedSetterSkeleton(
560555
name: baseName,

Plugins/BridgeJS/Sources/BridgeJSCore/ImportTS.swift

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,12 @@ import BridgeJSUtilities
1919
public struct ImportTS {
2020
public let progress: ProgressReporting
2121
public private(set) var skeleton: ImportedModuleSkeleton
22-
private var moduleName: String {
23-
skeleton.moduleName
24-
}
22+
private let moduleName: String
2523

2624
public init(progress: ProgressReporting, moduleName: String) {
2725
self.progress = progress
28-
self.skeleton = ImportedModuleSkeleton(moduleName: moduleName, children: [])
26+
self.moduleName = moduleName
27+
self.skeleton = ImportedModuleSkeleton(children: [])
2928
}
3029

3130
/// Adds a skeleton to the importer's state
@@ -52,8 +51,7 @@ public struct ImportTS {
5251
}
5352

5453
let format = BasicFormat()
55-
let allDecls: [DeclSyntax] = [Self.prelude] + decls
56-
return allDecls.map { $0.formatted(using: format).description }.joined(separator: "\n\n")
54+
return decls.map { $0.formatted(using: format).description }.joined(separator: "\n\n")
5755
}
5856

5957
class CallJSEmission {
@@ -302,17 +300,6 @@ public struct ImportTS {
302300
}
303301
}
304302

305-
static let prelude: DeclSyntax = """
306-
// bridge-js: skip
307-
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
308-
// DO NOT EDIT.
309-
//
310-
// To update this file, just rebuild your project or run
311-
// `swift package bridge-js`.
312-
313-
@_spi(BridgeJS) import JavaScriptKit
314-
"""
315-
316303
private static func thunkName(function: ImportedFunctionSkeleton) -> String {
317304
"_$\(function.name)"
318305
}

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,22 @@ import BridgeJSUtilities
88
#endif
99

1010
struct BridgeJSLink {
11-
/// The exported skeletons
12-
var exportedSkeletons: [ExportedSkeleton] = []
13-
var importedSkeletons: [ImportedModuleSkeleton] = []
11+
var skeletons: [BridgeJSSkeleton] = []
1412
let sharedMemory: Bool
1513
private let namespaceBuilder = NamespaceBuilder()
1614

1715
init(
18-
exportedSkeletons: [ExportedSkeleton] = [],
19-
importedSkeletons: [ImportedModuleSkeleton] = [],
16+
skeletons: [BridgeJSSkeleton] = [],
2017
sharedMemory: Bool
2118
) {
22-
self.exportedSkeletons = exportedSkeletons
23-
self.importedSkeletons = importedSkeletons
19+
self.skeletons = skeletons
2420
self.sharedMemory = sharedMemory
2521
}
2622

2723
mutating func addSkeletonFile(data: Data) throws {
2824
do {
2925
let unified = try JSONDecoder().decode(BridgeJSSkeleton.self, from: data)
30-
if let exported = unified.exported {
31-
exportedSkeletons.append(exported)
32-
}
33-
if let imported = unified.imported {
34-
importedSkeletons.append(imported)
35-
}
26+
skeletons.append(unified)
3627
} catch {
3728
struct SkeletonDecodingError: Error, CustomStringConvertible {
3829
let description: String
@@ -95,7 +86,7 @@ struct BridgeJSLink {
9586
var data = LinkData()
9687

9788
// Swift heap object class definitions
98-
if exportedSkeletons.contains(where: { $0.classes.count > 0 }) {
89+
if skeletons.contains(where: { $0.exported?.classes.isEmpty == false }) {
9990
data.classLines.append(
10091
contentsOf: swiftHeapObjectClassJs.split(separator: "\n", omittingEmptySubsequences: false).map {
10192
String($0)
@@ -109,7 +100,8 @@ struct BridgeJSLink {
109100
}
110101

111102
// Process exported skeletons
112-
for skeleton in exportedSkeletons {
103+
for unified in skeletons {
104+
guard let skeleton = unified.exported else { continue }
113105
// Process classes
114106
for klass in skeleton.classes {
115107
let (jsType, dtsType, dtsExportEntry) = try renderExportedClass(klass)
@@ -176,9 +168,10 @@ struct BridgeJSLink {
176168
}
177169

178170
// Process imported skeletons
179-
for skeletonSet in importedSkeletons {
180-
let importObjectBuilder = ImportObjectBuilder(moduleName: skeletonSet.moduleName)
181-
for fileSkeleton in skeletonSet.children {
171+
for unified in skeletons {
172+
guard let imported = unified.imported else { continue }
173+
let importObjectBuilder = ImportObjectBuilder(moduleName: unified.moduleName)
174+
for fileSkeleton in imported.children {
182175
for function in fileSkeleton.functions {
183176
try renderImportedFunction(importObjectBuilder: importObjectBuilder, function: function)
184177
}
@@ -189,14 +182,16 @@ struct BridgeJSLink {
189182
data.importObjectBuilders.append(importObjectBuilder)
190183
}
191184

192-
for skeleton in exportedSkeletons {
185+
for unified in skeletons {
186+
guard let skeleton = unified.exported else { continue }
187+
let moduleName = unified.moduleName
193188
if !skeleton.protocols.isEmpty {
194189
let importObjectBuilder: ImportObjectBuilder
195-
if let existingBuilder = data.importObjectBuilders.first(where: { $0.moduleName == skeleton.moduleName }
190+
if let existingBuilder = data.importObjectBuilders.first(where: { $0.moduleName == moduleName }
196191
) {
197192
importObjectBuilder = existingBuilder
198193
} else {
199-
importObjectBuilder = ImportObjectBuilder(moduleName: skeleton.moduleName)
194+
importObjectBuilder = ImportObjectBuilder(moduleName: moduleName)
200195
data.importObjectBuilders.append(importObjectBuilder)
201196
}
202197

@@ -311,7 +306,7 @@ struct BridgeJSLink {
311306
"bjs = {};",
312307
"importObject[\"bjs\"] = bjs;",
313308
])
314-
if self.importedSkeletons.count > 0 {
309+
if skeletons.contains(where: { $0.imported != nil }) {
315310
printer.write(lines: [
316311
"const imports = options.getImports(importsContext);"
317312
])
@@ -590,22 +585,24 @@ struct BridgeJSLink {
590585
}
591586
printer.write("}")
592587

593-
for skeleton in exportedSkeletons {
588+
for unified in skeletons {
589+
guard let skeleton = unified.exported else { continue }
590+
let moduleName = unified.moduleName
594591
var closureSignatures: Set<ClosureSignature> = []
595592
collectClosureSignatures(from: skeleton, into: &closureSignatures)
596593

597594
guard !closureSignatures.isEmpty else { continue }
598595

599596
for signature in closureSignatures.sorted(by: { $0.mangleName < $1.mangleName }) {
600-
let invokeFuncName = "invoke_js_callback_\(skeleton.moduleName)_\(signature.mangleName)"
597+
let invokeFuncName = "invoke_js_callback_\(moduleName)_\(signature.mangleName)"
601598
printer.write(
602599
lines: generateInvokeFunction(
603600
signature: signature,
604601
functionName: invokeFuncName
605602
)
606603
)
607604

608-
let lowerFuncName = "lower_closure_\(skeleton.moduleName)_\(signature.mangleName)"
605+
let lowerFuncName = "lower_closure_\(moduleName)_\(signature.mangleName)"
609606
printer.write(
610607
lines: generateLowerClosureFunction(
611608
signature: signature,
@@ -793,6 +790,8 @@ struct BridgeJSLink {
793790
let printer = CodeFragmentPrinter(header: header)
794791
printer.nextLine()
795792

793+
let exportedSkeletons = skeletons.compactMap(\.exported)
794+
796795
for skeleton in exportedSkeletons {
797796
for proto in skeleton.protocols {
798797
printer.write("export interface \(proto.name) {")
@@ -936,6 +935,7 @@ struct BridgeJSLink {
936935

937936
printer.write(lines: data.topLevelTypeLines)
938937

938+
let exportedSkeletons = skeletons.compactMap(\.exported)
939939
let topLevelNamespaceCode = namespaceBuilder.buildTopLevelNamespaceInitialization(
940940
exportedSkeletons: exportedSkeletons
941941
)
@@ -1042,7 +1042,7 @@ struct BridgeJSLink {
10421042
private func enumHelperAssignments() -> CodeFragmentPrinter {
10431043
let printer = CodeFragmentPrinter()
10441044

1045-
for skeleton in exportedSkeletons {
1045+
for skeleton in skeletons.compactMap(\.exported) {
10461046
for enumDef in skeleton.enums where enumDef.enumType == .associatedValue {
10471047
printer.write(
10481048
"const \(enumDef.name)Helpers = __bjs_create\(enumDef.valuesName)Helpers()(\(JSGlueVariableScope.reservedTmpParamInts), \(JSGlueVariableScope.reservedTmpParamF32s), \(JSGlueVariableScope.reservedTmpParamF64s), \(JSGlueVariableScope.reservedTextEncoder), \(JSGlueVariableScope.reservedSwift));"
@@ -1058,7 +1058,7 @@ struct BridgeJSLink {
10581058
private func structHelperAssignments() -> CodeFragmentPrinter {
10591059
let printer = CodeFragmentPrinter()
10601060

1061-
for skeleton in exportedSkeletons {
1061+
for skeleton in skeletons.compactMap(\.exported) {
10621062
for structDef in skeleton.structs {
10631063
printer.write(
10641064
"const \(structDef.name)Helpers = __bjs_create\(structDef.name)Helpers()(\(JSGlueVariableScope.reservedTmpParamInts), \(JSGlueVariableScope.reservedTmpParamF32s), \(JSGlueVariableScope.reservedTmpParamF64s), \(JSGlueVariableScope.reservedTmpParamPointers), \(JSGlueVariableScope.reservedTmpRetPointers), \(JSGlueVariableScope.reservedTextEncoder), \(JSGlueVariableScope.reservedSwift), \(JSGlueVariableScope.reservedEnumHelpers));"
@@ -1078,13 +1078,15 @@ struct BridgeJSLink {
10781078
var modulesByName: [String: [ExportedClass]] = [:]
10791079

10801080
// Group classes by their module name
1081-
for skeleton in exportedSkeletons {
1081+
for unified in skeletons {
1082+
guard let skeleton = unified.exported else { continue }
10821083
if skeleton.classes.isEmpty { continue }
1084+
let moduleName = unified.moduleName
10831085

1084-
if modulesByName[skeleton.moduleName] == nil {
1085-
modulesByName[skeleton.moduleName] = []
1086+
if modulesByName[moduleName] == nil {
1087+
modulesByName[moduleName] = []
10861088
}
1087-
modulesByName[skeleton.moduleName]?.append(contentsOf: skeleton.classes)
1089+
modulesByName[moduleName]?.append(contentsOf: skeleton.classes)
10881090
}
10891091

10901092
// Generate wrapper functions for each module
@@ -1146,7 +1148,8 @@ struct BridgeJSLink {
11461148
private func generateImportedTypeDefinitions() -> [String] {
11471149
let printer = CodeFragmentPrinter()
11481150

1149-
for skeletonSet in importedSkeletons {
1151+
for unified in skeletons {
1152+
guard let skeletonSet = unified.imported else { continue }
11501153
for fileSkeleton in skeletonSet.children {
11511154
for type in fileSkeleton.types {
11521155
printer.write("export interface \(type.name) {")
@@ -1300,7 +1303,7 @@ struct BridgeJSLink {
13001303
/// If the type is an enum, looks up the ExportedEnum and uses its tsFullPath
13011304
/// Otherwise, uses the default tsType property
13021305
private func resolveTypeScriptType(_ type: BridgeType) -> String {
1303-
return Self.resolveTypeScriptType(type, exportedSkeletons: exportedSkeletons)
1306+
return Self.resolveTypeScriptType(type, exportedSkeletons: skeletons.compactMap(\.exported))
13041307
}
13051308

13061309
/// Static helper for resolving TypeScript types with full enum paths

0 commit comments

Comments
 (0)