Skip to content

Commit 328a5b7

Browse files
BridgeJS: Factor out import object builder
1 parent bfa4854 commit 328a5b7

File tree

1 file changed

+78
-53
lines changed

1 file changed

+78
-53
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,8 @@ struct BridgeJSLink {
4747

4848
func link() throws -> (outputJs: String, outputDts: String) {
4949
var exportsLines: [String] = []
50-
var importedLines: [String] = []
5150
var classLines: [String] = []
5251
var dtsExportLines: [String] = []
53-
var dtsImportLines: [String] = []
5452
var dtsClassLines: [String] = []
5553

5654
if exportedSkeletons.contains(where: { $0.classes.count > 0 }) {
@@ -84,57 +82,18 @@ struct BridgeJSLink {
8482
}
8583
}
8684

85+
var importObjectBuilders: [ImportObjectBuilder] = []
8786
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)
9488
for fileSkeleton in skeletonSet.children {
9589
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)
9991
}
10092
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)
13694
}
13795
}
96+
importObjectBuilders.append(importObjectBuilder)
13897
}
13998

14099
let outputJs = """
@@ -175,7 +134,7 @@ struct BridgeJSLink {
175134
target.set(tmpRetBytes);
176135
tmpRetBytes = undefined;
177136
}
178-
\(importedLines.map { $0.indent(count: 12) }.joined(separator: "\n"))
137+
\(importObjectBuilders.flatMap { $0.importedLines }.map { $0.indent(count: 12) }.joined(separator: "\n"))
179138
},
180139
setInstance: (i) => {
181140
instance = i;
@@ -198,7 +157,7 @@ struct BridgeJSLink {
198157
dtsLines.append(contentsOf: dtsExportLines.map { $0.indent(count: 4) })
199158
dtsLines.append("}")
200159
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) })
202161
dtsLines.append("}")
203162
let outputDts = """
204163
// NOTICE: This is auto-generated code by BridgeJS from JavaScriptKit,
@@ -475,7 +434,31 @@ struct BridgeJSLink {
475434
}
476435
}
477436

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 {
479462
let thunkBuilder = ImportedThunkBuilder()
480463
for param in function.parameters {
481464
thunkBuilder.liftParameter(param: param)
@@ -486,11 +469,53 @@ struct BridgeJSLink {
486469
name: function.abiName(context: nil),
487470
returnExpr: returnExpr
488471
)
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+
]
492476
)
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+
}
494519
}
495520

496521
func renderImportedProperty(

0 commit comments

Comments
 (0)