@@ -96,7 +96,6 @@ struct BridgeJSLink {
96
96
js [ 0 ] = " \( function. name) : " + js[ 0 ]
97
97
js [ js. count - 1 ] += " , "
98
98
exportsLines. append ( contentsOf: js)
99
-
100
99
dtsExportLines. append ( contentsOf: dts)
101
100
}
102
101
}
@@ -119,8 +118,8 @@ struct BridgeJSLink {
119
118
120
119
let exportsSection : String
121
120
if hasNamespacedFunctions {
122
- let setupLines = renderGlobalNamespace ( namespacedFunctions: namespacedFunctions)
123
- let namespaceSetupCode = setupLines . map { $0. indent ( count: 12 ) } . joined ( separator: " \n " )
121
+ let namespaceSetupCode = renderGlobalNamespace ( namespacedFunctions: namespacedFunctions)
122
+ . map { $0. indent ( count: 12 ) } . joined ( separator: " \n " )
124
123
exportsSection = """
125
124
\( classLines. map { $0. indent ( count: 12 ) } . joined ( separator: " \n " ) )
126
125
const exports = {
@@ -203,12 +202,41 @@ struct BridgeJSLink {
203
202
/** @param {WebAssembly.Instance} instance */
204
203
createExports: (instance) => {
205
204
const js = swift.memory.heap;
206
- \( exportsSection)
205
+ \( exportsSection)
207
206
}
208
207
}
209
208
"""
210
-
211
- // Collect namespace declarations for TypeScript
209
+
210
+ var dtsLines : [ String ] = [ ]
211
+ dtsLines. append ( contentsOf: namespaceDeclarations ( ) )
212
+ dtsLines. append ( contentsOf: dtsClassLines)
213
+ dtsLines. append ( " export type Exports = { " )
214
+ dtsLines. append ( contentsOf: dtsExportLines. map { $0. indent ( count: 4 ) } )
215
+ dtsLines. append ( " } " )
216
+ dtsLines. append ( " export type Imports = { " )
217
+ dtsLines. append ( contentsOf: importObjectBuilders. flatMap { $0. dtsImportLines } . map { $0. indent ( count: 4 ) } )
218
+ dtsLines. append ( " } " )
219
+ let outputDts = """
220
+ // NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
221
+ // DO NOT EDIT.
222
+ //
223
+ // To update this file, just rebuild your project or run
224
+ // `swift package bridge-js`.
225
+
226
+ \( dtsLines. joined ( separator: " \n " ) )
227
+ export function createInstantiator(options: {
228
+ imports: Imports;
229
+ }, swift: any): Promise<{
230
+ addImports: (importObject: WebAssembly.Imports) => void;
231
+ setInstance: (instance: WebAssembly.Instance) => void;
232
+ createExports: (instance: WebAssembly.Instance) => Exports;
233
+ }>;
234
+ """
235
+ return ( outputJs, outputDts)
236
+ }
237
+
238
+ private func namespaceDeclarations( ) -> [ String ] {
239
+ var dtsLines : [ String ] = [ ]
212
240
var namespaceDeclarations : [ String : [ ( name: String , parameters: [ Parameter ] , returnType: BridgeType ) ] ] = [ : ]
213
241
214
242
for skeleton in exportedSkeletons {
@@ -223,71 +251,34 @@ struct BridgeJSLink {
223
251
}
224
252
}
225
253
226
- // Generate namespace declarations in TypeScript
227
- var dtsLines : [ String ] = [ ]
254
+ guard !namespaceDeclarations. isEmpty else { return dtsLines }
228
255
229
- // Only add export {} and declare global block if we have namespace declarations
230
- let hasNamespaceDeclarations = !namespaceDeclarations. isEmpty
256
+ dtsLines. append ( " export {}; " )
257
+ dtsLines. append ( " " )
258
+ dtsLines. append ( " declare global { " )
231
259
232
- if hasNamespaceDeclarations {
233
- dtsLines. append ( " export {}; " )
234
- dtsLines. append ( " " )
235
- dtsLines. append ( " declare global { " )
236
- }
237
-
238
- // Generate namespace structure using nested declarations
260
+ let identBaseSize = 4
239
261
for (namespacePath, functions) in namespaceDeclarations. sorted ( by: { $0. key < $1. key } ) {
240
262
let parts = namespacePath. split ( separator: " . " ) . map ( String . init)
241
263
242
- // Open namespaces with proper indentation
243
264
for i in 0 ..< parts. count {
244
- dtsLines. append ( " namespace \( parts [ i] ) { " . indent ( count: 4 * ( hasNamespaceDeclarations ? i + 1 : 1 ) ) )
265
+ dtsLines. append ( " namespace \( parts [ i] ) { " . indent ( count: identBaseSize* ( i + 1 ) ) )
245
266
}
246
267
247
- // Add function signatures with proper indentation
248
- let functionIndentationLevel = hasNamespaceDeclarations ? parts. count + 1 : parts. count
249
268
for (name, parameters, returnType) in functions {
250
269
let signature = " function \( name) \( renderTSSignature ( parameters: parameters, returnType: returnType) ) ; "
251
- dtsLines. append ( " \( signature) " . indent ( count: 4 * functionIndentationLevel ) )
270
+ dtsLines. append ( " \( signature) " . indent ( count: identBaseSize* ( parts . count + 1 ) ) )
252
271
}
253
272
254
- // Close namespaces with proper indentation (in reverse order)
255
273
for i in ( 0 ..< parts. count) . reversed ( ) {
256
- let indentationLevel = hasNamespaceDeclarations ? i + 1 : i
257
- dtsLines. append ( " } " . indent ( count: 4 * indentationLevel) )
274
+ dtsLines. append ( " } " . indent ( count: identBaseSize* ( i+ 1 ) ) )
258
275
}
259
276
}
260
277
261
- if hasNamespaceDeclarations {
262
- dtsLines. append ( " } " )
263
- dtsLines. append ( " " )
264
- }
265
-
266
- // Add remaining class lines
267
- dtsLines. append ( contentsOf: dtsClassLines)
268
- dtsLines. append ( " export type Exports = { " )
269
- dtsLines. append ( contentsOf: dtsExportLines. map { $0. indent ( count: 4 ) } )
270
- dtsLines. append ( " } " )
271
- dtsLines. append ( " export type Imports = { " )
272
- dtsLines. append ( contentsOf: importObjectBuilders. flatMap { $0. dtsImportLines } . map { $0. indent ( count: 4 ) } )
273
278
dtsLines. append ( " } " )
274
- let outputDts = """
275
- // NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
276
- // DO NOT EDIT.
277
- //
278
- // To update this file, just rebuild your project or run
279
- // `swift package bridge-js`.
280
-
281
- \( dtsLines. joined ( separator: " \n " ) )
282
- export function createInstantiator(options: {
283
- imports: Imports;
284
- }, swift: any): Promise<{
285
- addImports: (importObject: WebAssembly.Imports) => void;
286
- setInstance: (instance: WebAssembly.Instance) => void;
287
- createExports: (instance: WebAssembly.Instance) => Exports;
288
- }>;
289
- """
290
- return ( outputJs, outputDts)
279
+ dtsLines. append ( " " )
280
+
281
+ return dtsLines
291
282
}
292
283
293
284
class ExportedThunkBuilder {
@@ -482,20 +473,11 @@ struct BridgeJSLink {
482
473
return ( jsLines, dtsTypeLines, dtsExportEntryLines)
483
474
}
484
475
485
- // __Swift.Foundation.UUID
486
-
487
- // [__Swift, Foundation, UUID]
488
- // [[__Swift, Foundation, UUID], [__Swift, Foundation]]
489
-
490
- // __Swift
491
- // __Swift.Foundation
492
- // __Swift.Foundation.UUID
493
-
494
476
func renderGlobalNamespace( namespacedFunctions: [ ExportedFunction ] ) -> [ String ] {
495
477
var lines : [ String ] = [ ]
496
478
var uniqueNamespaces : [ String ] = [ ]
497
479
498
- var namespacePaths : Set < [ String ] > = Set ( namespacedFunctions
480
+ let namespacePaths : Set < [ String ] > = Set ( namespacedFunctions
499
481
. compactMap { $0. namespace } )
500
482
501
483
namespacePaths. forEach { namespacePath in
@@ -507,7 +489,7 @@ struct BridgeJSLink {
507
489
}
508
490
}
509
491
510
- uniqueNamespaces. map { namespace in
492
+ uniqueNamespaces. forEach { namespace in
511
493
lines. append ( " if (typeof globalThis. \( namespace) === 'undefined') { " )
512
494
lines. append ( " globalThis. \( namespace) = {}; " )
513
495
lines. append ( " } " )
0 commit comments