@@ -96,7 +96,6 @@ struct BridgeJSLink {
9696 js [ 0 ] = " \( function. name) : " + js[ 0 ]
9797 js [ js. count - 1 ] += " , "
9898 exportsLines. append ( contentsOf: js)
99-
10099 dtsExportLines. append ( contentsOf: dts)
101100 }
102101 }
@@ -119,8 +118,8 @@ struct BridgeJSLink {
119118
120119 let exportsSection : String
121120 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 " )
124123 exportsSection = """
125124 \( classLines. map { $0. indent ( count: 12 ) } . joined ( separator: " \n " ) )
126125 const exports = {
@@ -203,12 +202,41 @@ struct BridgeJSLink {
203202 /** @param {WebAssembly.Instance} instance */
204203 createExports: (instance) => {
205204 const js = swift.memory.heap;
206- \( exportsSection)
205+ \( exportsSection)
207206 }
208207 }
209208 """
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 ] = [ ]
212240 var namespaceDeclarations : [ String : [ ( name: String , parameters: [ Parameter ] , returnType: BridgeType ) ] ] = [ : ]
213241
214242 for skeleton in exportedSkeletons {
@@ -223,71 +251,34 @@ struct BridgeJSLink {
223251 }
224252 }
225253
226- // Generate namespace declarations in TypeScript
227- var dtsLines : [ String ] = [ ]
254+ guard !namespaceDeclarations. isEmpty else { return dtsLines }
228255
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 { " )
231259
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
239261 for (namespacePath, functions) in namespaceDeclarations. sorted ( by: { $0. key < $1. key } ) {
240262 let parts = namespacePath. split ( separator: " . " ) . map ( String . init)
241263
242- // Open namespaces with proper indentation
243264 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 ) ) )
245266 }
246267
247- // Add function signatures with proper indentation
248- let functionIndentationLevel = hasNamespaceDeclarations ? parts. count + 1 : parts. count
249268 for (name, parameters, returnType) in functions {
250269 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 ) ) )
252271 }
253272
254- // Close namespaces with proper indentation (in reverse order)
255273 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 ) ) )
258275 }
259276 }
260277
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 ) } )
273278 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
291282 }
292283
293284 class ExportedThunkBuilder {
@@ -482,20 +473,11 @@ struct BridgeJSLink {
482473 return ( jsLines, dtsTypeLines, dtsExportEntryLines)
483474 }
484475
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-
494476 func renderGlobalNamespace( namespacedFunctions: [ ExportedFunction ] ) -> [ String ] {
495477 var lines : [ String ] = [ ]
496478 var uniqueNamespaces : [ String ] = [ ]
497479
498- var namespacePaths : Set < [ String ] > = Set ( namespacedFunctions
480+ let namespacePaths : Set < [ String ] > = Set ( namespacedFunctions
499481 . compactMap { $0. namespace } )
500482
501483 namespacePaths. forEach { namespacePath in
@@ -507,7 +489,7 @@ struct BridgeJSLink {
507489 }
508490 }
509491
510- uniqueNamespaces. map { namespace in
492+ uniqueNamespaces. forEach { namespace in
511493 lines. append ( " if (typeof globalThis. \( namespace) === 'undefined') { " )
512494 lines. append ( " globalThis. \( namespace) = {}; " )
513495 lines. append ( " } " )
0 commit comments