@@ -47,10 +47,8 @@ struct BridgeJSLink {
47
47
48
48
func link( ) throws -> ( outputJs: String , outputDts: String ) {
49
49
var exportsLines : [ String ] = [ ]
50
- var importedLines : [ String ] = [ ]
51
50
var classLines : [ String ] = [ ]
52
51
var dtsExportLines : [ String ] = [ ]
53
- var dtsImportLines : [ String ] = [ ]
54
52
var dtsClassLines : [ String ] = [ ]
55
53
56
54
if exportedSkeletons. contains ( where: { $0. classes. count > 0 } ) {
@@ -84,57 +82,18 @@ struct BridgeJSLink {
84
82
}
85
83
}
86
84
85
+ var importObjectBuilders : [ ImportObjectBuilder ] = [ ]
87
86
for skeletonSet in importedSkeletons {
88
- importedLines. append ( " const \( skeletonSet. moduleName) = importObject[ \" \( skeletonSet. moduleName) \" ] = {}; " )
89
- func assignToImportObject( name: String , function: [ String ] ) {
90
- var js = function
91
- js [ 0 ] = " \( skeletonSet. moduleName) [ \" \( name) \" ] = " + js[ 0 ]
92
- importedLines. append ( contentsOf: js)
93
- }
87
+ let importObjectBuilder = ImportObjectBuilder ( moduleName: skeletonSet. moduleName)
94
88
for fileSkeleton in skeletonSet. children {
95
89
for function in fileSkeleton. functions {
96
- let ( js, dts) = try renderImportedFunction ( function: function)
97
- assignToImportObject ( name: function. abiName ( context: nil ) , function: js)
98
- dtsImportLines. append ( contentsOf: dts)
90
+ try renderImportedFunction ( importObjectBuilder: importObjectBuilder, function: function)
99
91
}
100
92
for type in fileSkeleton. types {
101
- for property in type. properties {
102
- let getterAbiName = property. getterAbiName ( context: type)
103
- let ( js, dts) = try renderImportedProperty (
104
- property: property,
105
- abiName: getterAbiName,
106
- emitCall: { thunkBuilder in
107
- thunkBuilder. callPropertyGetter ( name: property. name, returnType: property. type)
108
- return try thunkBuilder. lowerReturnValue ( returnType: property. type)
109
- }
110
- )
111
- assignToImportObject ( name: getterAbiName, function: js)
112
- dtsImportLines. append ( contentsOf: dts)
113
-
114
- if !property. isReadonly {
115
- let setterAbiName = property. setterAbiName ( context: type)
116
- let ( js, dts) = try renderImportedProperty (
117
- property: property,
118
- abiName: setterAbiName,
119
- emitCall: { thunkBuilder in
120
- thunkBuilder. liftParameter (
121
- param: Parameter ( label: nil , name: " newValue " , type: property. type)
122
- )
123
- thunkBuilder. callPropertySetter ( name: property. name, returnType: property. type)
124
- return nil
125
- }
126
- )
127
- assignToImportObject ( name: setterAbiName, function: js)
128
- dtsImportLines. append ( contentsOf: dts)
129
- }
130
- }
131
- for method in type. methods {
132
- let ( js, dts) = try renderImportedMethod ( context: type, method: method)
133
- assignToImportObject ( name: method. abiName ( context: type) , function: js)
134
- dtsImportLines. append ( contentsOf: dts)
135
- }
93
+ try renderImportedType ( importObjectBuilder: importObjectBuilder, type: type)
136
94
}
137
95
}
96
+ importObjectBuilders. append ( importObjectBuilder)
138
97
}
139
98
140
99
let outputJs = """
@@ -175,7 +134,7 @@ struct BridgeJSLink {
175
134
target.set(tmpRetBytes);
176
135
tmpRetBytes = undefined;
177
136
}
178
- \( importedLines. map { $0. indent ( count: 12 ) } . joined ( separator: " \n " ) )
137
+ \( importObjectBuilders . flatMap { $0 . importedLines } . map { $0. indent ( count: 12 ) } . joined ( separator: " \n " ) )
179
138
},
180
139
setInstance: (i) => {
181
140
instance = i;
@@ -198,7 +157,7 @@ struct BridgeJSLink {
198
157
dtsLines. append ( contentsOf: dtsExportLines. map { $0. indent ( count: 4 ) } )
199
158
dtsLines. append ( " } " )
200
159
dtsLines. append ( " export type Imports = { " )
201
- dtsLines. append ( contentsOf: dtsImportLines. map { $0. indent ( count: 4 ) } )
160
+ dtsLines. append ( contentsOf: importObjectBuilders . flatMap { $0 . dtsImportLines } . map { $0. indent ( count: 4 ) } )
202
161
dtsLines. append ( " } " )
203
162
let outputDts = """
204
163
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
@@ -475,7 +434,31 @@ struct BridgeJSLink {
475
434
}
476
435
}
477
436
478
- func renderImportedFunction( function: ImportedFunctionSkeleton ) throws -> ( js: [ String ] , dts: [ String ] ) {
437
+ class ImportObjectBuilder {
438
+ var moduleName : String
439
+ var importedLines : [ String ] = [ ]
440
+ var dtsImportLines : [ String ] = [ ]
441
+
442
+ init ( moduleName: String ) {
443
+ self . moduleName = moduleName
444
+ importedLines. append ( " const \( moduleName) = importObject[ \" \( moduleName) \" ] = {}; " )
445
+ }
446
+
447
+ func assignToImportObject( name: String , function: [ String ] ) {
448
+ var js = function
449
+ js [ 0 ] = " \( moduleName) [ \" \( name) \" ] = " + js[ 0 ]
450
+ importedLines. append ( contentsOf: js)
451
+ }
452
+
453
+ func appendDts( _ lines: [ String ] ) {
454
+ dtsImportLines. append ( contentsOf: lines)
455
+ }
456
+ }
457
+
458
+ func renderImportedFunction(
459
+ importObjectBuilder: ImportObjectBuilder ,
460
+ function: ImportedFunctionSkeleton
461
+ ) throws {
479
462
let thunkBuilder = ImportedThunkBuilder ( )
480
463
for param in function. parameters {
481
464
thunkBuilder. liftParameter ( param: param)
@@ -486,11 +469,53 @@ struct BridgeJSLink {
486
469
name: function. abiName ( context: nil ) ,
487
470
returnExpr: returnExpr
488
471
)
489
- var dtsLines : [ String ] = [ ]
490
- dtsLines. append (
491
- " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
472
+ importObjectBuilder. appendDts (
473
+ [
474
+ " \( function. name) \( renderTSSignature ( parameters: function. parameters, returnType: function. returnType) ) ; "
475
+ ]
492
476
)
493
- return ( funcLines, dtsLines)
477
+ importObjectBuilder. assignToImportObject ( name: function. abiName ( context: nil ) , function: funcLines)
478
+ }
479
+
480
+ func renderImportedType(
481
+ importObjectBuilder: ImportObjectBuilder ,
482
+ type: ImportedTypeSkeleton
483
+ ) throws {
484
+ for property in type. properties {
485
+ let getterAbiName = property. getterAbiName ( context: type)
486
+ let ( js, dts) = try renderImportedProperty (
487
+ property: property,
488
+ abiName: getterAbiName,
489
+ emitCall: { thunkBuilder in
490
+ thunkBuilder. callPropertyGetter ( name: property. name, returnType: property. type)
491
+ return try thunkBuilder. lowerReturnValue ( returnType: property. type)
492
+ }
493
+ )
494
+ importObjectBuilder. assignToImportObject ( name: getterAbiName, function: js)
495
+ importObjectBuilder. appendDts ( dts)
496
+
497
+ if !property. isReadonly {
498
+ let setterAbiName = property. setterAbiName ( context: type)
499
+ let ( js, dts) = try renderImportedProperty (
500
+ property: property,
501
+ abiName: setterAbiName,
502
+ emitCall: { thunkBuilder in
503
+ thunkBuilder. liftParameter (
504
+ param: Parameter ( label: nil , name: " newValue " , type: property. type)
505
+ )
506
+ thunkBuilder. callPropertySetter ( name: property. name, returnType: property. type)
507
+ return nil
508
+ }
509
+ )
510
+ importObjectBuilder. assignToImportObject ( name: setterAbiName, function: js)
511
+ importObjectBuilder. appendDts ( dts)
512
+ }
513
+ }
514
+ for method in type. methods {
515
+ let ( js, dts) = try renderImportedMethod ( context: type, method: method)
516
+ importObjectBuilder. assignToImportObject ( name: method. abiName ( context: type) , function: js)
517
+ importObjectBuilder. appendDts ( dts)
518
+ }
494
519
}
495
520
496
521
func renderImportedProperty(
0 commit comments