@@ -16,6 +16,7 @@ public struct BridgeJSLink {
1616 let enableLifetimeTracking : Bool = false
1717 private let namespaceBuilder = NamespaceBuilder ( )
1818 private let intrinsicRegistry = JSIntrinsicRegistry ( )
19+ private let importedModuleRegistry = ImportedJSModuleRegistry ( )
1920
2021 public init (
2122 skeletons: [ BridgeJSSkeleton ] = [ ] ,
@@ -1077,6 +1078,11 @@ public struct BridgeJSLink {
10771078 let printer = CodeFragmentPrinter ( header: header)
10781079 printer. nextLine ( )
10791080
1081+ printer. write ( lines: importedModuleRegistry. importLines)
1082+ if !importedModuleRegistry. importLines. isEmpty {
1083+ printer. nextLine ( )
1084+ }
1085+
10801086 printer. write ( lines: data. topLevelTypeLines)
10811087
10821088 let exportedSkeletons = skeletons. compactMap ( \. exported)
@@ -1220,8 +1226,9 @@ public struct BridgeJSLink {
12201226 return printer. lines. joined ( separator: " \n " )
12211227 }
12221228
1223- public func link( ) throws -> ( outputJs : String , outputDts : String ) {
1229+ public func link( ) throws -> BridgeJSLinkOutput {
12241230 intrinsicRegistry. reset ( )
1231+ try importedModuleRegistry. configure ( skeletons: skeletons)
12251232 intrinsicRegistry. classNamespaces = skeletons. reduce ( into: [ : ] ) { result, unified in
12261233 guard let skeleton = unified. exported else { return }
12271234 for klass in skeleton. classes {
@@ -1233,7 +1240,11 @@ public struct BridgeJSLink {
12331240 let data = try collectLinkData ( )
12341241 let outputJs = try generateJavaScript ( data: data)
12351242 let outputDts = generateTypeScript ( data: data)
1236- return ( outputJs, outputDts)
1243+ return BridgeJSLinkOutput (
1244+ outputJs: outputJs,
1245+ outputDts: outputDts,
1246+ modules: importedModuleRegistry. artifacts
1247+ )
12371248 }
12381249
12391250 private func enumHelperAssignments( ) -> CodeFragmentPrinter {
@@ -1586,7 +1597,7 @@ public struct BridgeJSLink {
15861597 return " \" \( Self . escapeForJavaScriptStringLiteral ( name) ) \" "
15871598 }
15881599
1589- fileprivate static func escapeForJavaScriptStringLiteral( _ string: String ) -> String {
1600+ static func escapeForJavaScriptStringLiteral( _ string: String ) -> String {
15901601 string
15911602 . replacingOccurrences ( of: " \\ " , with: " \\ \\ " )
15921603 . replacingOccurrences ( of: " \" " , with: " \\ \" " )
@@ -3460,7 +3471,10 @@ extension BridgeJSLink {
34603471 try thunkBuilder. liftParameter ( param: param)
34613472 }
34623473 let jsName = function. jsName ?? function. name
3463- let importRootExpr = function. from == . global ? " globalThis " : " imports "
3474+ let importRootExpr = try importedModuleRegistry. namespaceExpression (
3475+ swiftModuleName: importObjectBuilder. moduleName,
3476+ from: function. from
3477+ )
34643478
34653479 try thunkBuilder. call ( name: jsName, fromObjectExpr: importRootExpr)
34663480 let funcLines = thunkBuilder. renderFunction ( name: function. abiName ( context: nil ) )
@@ -3484,7 +3498,10 @@ extension BridgeJSLink {
34843498 intrinsicRegistry: intrinsicRegistry
34853499 )
34863500 let jsName = getter. jsName ?? getter. name
3487- let importRootExpr = getter. from == . global ? " globalThis " : " imports "
3501+ let importRootExpr = try importedModuleRegistry. namespaceExpression (
3502+ swiftModuleName: importObjectBuilder. moduleName,
3503+ from: getter. from
3504+ )
34883505 try thunkBuilder. getImportProperty (
34893506 name: jsName,
34903507 fromObjectExpr: importRootExpr,
@@ -3539,7 +3556,11 @@ extension BridgeJSLink {
35393556 }
35403557 for method in type. staticMethods {
35413558 let abiName = method. abiName ( context: type, operation: " static " )
3542- let ( js, dts) = try renderImportedStaticMethod ( context: type, method: method)
3559+ let ( js, dts) = try renderImportedStaticMethod (
3560+ swiftModuleName: importObjectBuilder. moduleName,
3561+ context: type,
3562+ method: method
3563+ )
35433564 importObjectBuilder. assignToImportObject ( name: abiName, function: js)
35443565 importObjectBuilder. appendDts ( dts)
35453566 }
@@ -3583,7 +3604,10 @@ extension BridgeJSLink {
35833604 for param in constructor. parameters {
35843605 try thunkBuilder. liftParameter ( param: param)
35853606 }
3586- let importRootExpr = type. from == . global ? " globalThis " : " imports "
3607+ let importRootExpr = try importedModuleRegistry. namespaceExpression (
3608+ swiftModuleName: importObjectBuilder. moduleName,
3609+ from: type. from
3610+ )
35873611 try thunkBuilder. callConstructor (
35883612 jsName: type. jsName ?? type. name,
35893613 swiftTypeName: type. name,
@@ -3627,6 +3651,7 @@ extension BridgeJSLink {
36273651 }
36283652
36293653 func renderImportedStaticMethod(
3654+ swiftModuleName: String ,
36303655 context: ImportedTypeSkeleton ,
36313656 method: ImportedFunctionSkeleton
36323657 ) throws -> ( js: [ String ] , dts: [ String ] ) {
@@ -3638,7 +3663,10 @@ extension BridgeJSLink {
36383663 for param in method. parameters {
36393664 try thunkBuilder. liftParameter ( param: param)
36403665 }
3641- let importRootExpr = context. from == . global ? " globalThis " : " imports "
3666+ let importRootExpr = try importedModuleRegistry. namespaceExpression (
3667+ swiftModuleName: swiftModuleName,
3668+ from: context. from
3669+ )
36423670 let constructorExpr = ImportedThunkBuilder . propertyAccessExpr (
36433671 objectExpr: importRootExpr,
36443672 propertyName: context. jsName ?? context. name
0 commit comments