Skip to content

Commit 4e1eb26

Browse files
Merge pull request #609 from swiftwasm/yt/intrinsic-fragment-context
BridgeJS: Add `IntrinsicJSFragment.PrintCodeContext`
2 parents c74f659 + a0b8448 commit 4e1eb26

File tree

2 files changed

+468
-256
lines changed

2 files changed

+468
-256
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,11 @@ public struct BridgeJSLink {
717717
functionName: String
718718
) throws -> [String] {
719719
let printer = CodeFragmentPrinter()
720-
let scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
721-
let cleanupCode = CodeFragmentPrinter()
720+
let context = IntrinsicJSFragment.PrintCodeContext(
721+
scope: JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry),
722+
printer: printer,
723+
cleanupCode: CodeFragmentPrinter()
724+
)
722725

723726
// Build parameter list for invoke function
724727
var invokeParams: [String] = ["callbackId"]
@@ -746,7 +749,7 @@ public struct BridgeJSLink {
746749
} else {
747750
args = ["param\(index)Id", "param\(index)"]
748751
}
749-
_ = try fragment.printCode(args, scope, printer, cleanupCode)
752+
_ = try fragment.printCode(args, context)
750753
}
751754

752755
let callbackParams = signature.parameters.indices.map { "param\($0)" }.joined(separator: ", ")
@@ -765,13 +768,13 @@ public struct BridgeJSLink {
765768
}
766769

767770
let returnFragment = try IntrinsicJSFragment.closureLowerReturn(type: signature.returnType)
768-
_ = try returnFragment.printCode(["result"], scope, printer, cleanupCode)
771+
_ = try returnFragment.printCode(["result"], context)
769772
}
770773
printer.write("} catch (error) {")
771774
try printer.indent {
772775
printer.write("\(JSGlueVariableScope.reservedSetException)?.(error);")
773776
let errorFragment = IntrinsicJSFragment.closureErrorReturn(type: signature.returnType)
774-
_ = try errorFragment.printCode([], scope, printer, cleanupCode)
777+
_ = try errorFragment.printCode([], context)
775778
}
776779
printer.write("}")
777780
}
@@ -786,8 +789,11 @@ public struct BridgeJSLink {
786789
functionName: String
787790
) throws -> [String] {
788791
let printer = CodeFragmentPrinter()
789-
let scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
790-
let cleanupCode = CodeFragmentPrinter()
792+
let context = IntrinsicJSFragment.PrintCodeContext(
793+
scope: JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry),
794+
printer: printer,
795+
cleanupCode: CodeFragmentPrinter()
796+
)
791797

792798
printer.write(
793799
"const \(functionName) = function(\(signature.parameters.indices.map { "param\($0)" }.joined(separator: ", "))) {"
@@ -798,7 +804,7 @@ public struct BridgeJSLink {
798804
for (index, paramType) in signature.parameters.enumerated() {
799805
let paramName = "param\(index)"
800806
let fragment = try IntrinsicJSFragment.lowerParameter(type: paramType)
801-
let lowered = try fragment.printCode([paramName], scope, printer, cleanupCode)
807+
let lowered = try fragment.printCode([paramName], context)
802808
invokeArgs.append(contentsOf: lowered)
803809
}
804810

@@ -822,7 +828,7 @@ public struct BridgeJSLink {
822828
printer.write("}")
823829

824830
let returnFragment = try IntrinsicJSFragment.closureLiftReturn(type: signature.returnType)
825-
_ = try returnFragment.printCode([invokeResultName], scope, printer, cleanupCode)
831+
_ = try returnFragment.printCode([invokeResultName], context)
826832
}
827833
printer.write("};")
828834

@@ -1010,7 +1016,14 @@ public struct BridgeJSLink {
10101016
let structScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
10111017
let structCleanup = CodeFragmentPrinter()
10121018
let fragment = IntrinsicJSFragment.structHelper(structDefinition: structDef, allStructs: allStructs)
1013-
_ = try fragment.printCode([structDef.name], structScope, structPrinter, structCleanup)
1019+
_ = try fragment.printCode(
1020+
[structDef.name],
1021+
IntrinsicJSFragment.PrintCodeContext(
1022+
scope: structScope,
1023+
printer: structPrinter,
1024+
cleanupCode: structCleanup
1025+
)
1026+
)
10141027
bodyPrinter.write(lines: structPrinter.lines)
10151028
}
10161029

@@ -1022,7 +1035,14 @@ public struct BridgeJSLink {
10221035
let enumScope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
10231036
let enumCleanup = CodeFragmentPrinter()
10241037
let fragment = IntrinsicJSFragment.associatedValueEnumHelperFactory(enumDefinition: enumDef)
1025-
_ = try fragment.printCode([enumDef.valuesName], enumScope, enumPrinter, enumCleanup)
1038+
_ = try fragment.printCode(
1039+
[enumDef.valuesName],
1040+
IntrinsicJSFragment.PrintCodeContext(
1041+
scope: enumScope,
1042+
printer: enumPrinter,
1043+
cleanupCode: enumCleanup
1044+
)
1045+
)
10261046
bodyPrinter.write(lines: enumPrinter.lines)
10271047
}
10281048
bodyPrinter.nextLine()
@@ -1269,12 +1289,18 @@ public struct BridgeJSLink {
12691289
var parameterForwardings: [String] = []
12701290
let effects: Effects
12711291
let scope: JSGlueVariableScope
1292+
let context: IntrinsicJSFragment.PrintCodeContext
12721293

12731294
init(effects: Effects, intrinsicRegistry: JSIntrinsicRegistry) {
12741295
self.effects = effects
12751296
self.scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
12761297
self.body = CodeFragmentPrinter()
12771298
self.cleanupCode = CodeFragmentPrinter()
1299+
self.context = IntrinsicJSFragment.PrintCodeContext(
1300+
scope: scope,
1301+
printer: body,
1302+
cleanupCode: cleanupCode
1303+
)
12781304
}
12791305

12801306
func lowerParameter(param: Parameter) throws {
@@ -1283,7 +1309,7 @@ public struct BridgeJSLink {
12831309
loweringFragment.parameters.count == 1,
12841310
"Lowering fragment should have exactly one parameter to lower"
12851311
)
1286-
let loweredValues = try loweringFragment.printCode([param.name], scope, body, cleanupCode)
1312+
let loweredValues = try loweringFragment.printCode([param.name], context)
12871313
parameterForwardings.append(contentsOf: loweredValues)
12881314
}
12891315

@@ -1315,7 +1341,7 @@ public struct BridgeJSLink {
13151341
body.write("const \(returnVariable) = \(call);")
13161342
fragmentArguments = [returnVariable]
13171343
}
1318-
let liftedValues = try liftingFragment.printCode(fragmentArguments, scope, body, cleanupCode)
1344+
let liftedValues = try liftingFragment.printCode(fragmentArguments, context)
13191345
assert(liftedValues.count <= 1, "Lifting fragment should produce at most one value")
13201346
return liftedValues.first
13211347
}
@@ -1566,26 +1592,30 @@ public struct BridgeJSLink {
15661592
var jsTopLevelLines: [String] = []
15671593
var dtsTypeLines: [String] = []
15681594
let scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
1569-
let cleanup = CodeFragmentPrinter()
15701595
let printer = CodeFragmentPrinter()
1596+
let context = IntrinsicJSFragment.PrintCodeContext(
1597+
scope: scope,
1598+
printer: printer,
1599+
cleanupCode: CodeFragmentPrinter()
1600+
)
15711601
let enumValuesName = enumDefinition.valuesName
15721602

15731603
switch enumDefinition.enumType {
15741604
case .simple:
15751605
let fragment = IntrinsicJSFragment.simpleEnumHelper(enumDefinition: enumDefinition)
1576-
_ = try fragment.printCode([enumValuesName], scope, printer, cleanup)
1606+
_ = try fragment.printCode([enumValuesName], context)
15771607
jsTopLevelLines.append(contentsOf: printer.lines)
15781608
case .rawValue:
15791609
guard enumDefinition.rawType != nil else {
15801610
throw BridgeJSLinkError(message: "Raw value enum \(enumDefinition.name) is missing rawType")
15811611
}
15821612

15831613
let fragment = IntrinsicJSFragment.rawValueEnumHelper(enumDefinition: enumDefinition)
1584-
_ = try fragment.printCode([enumValuesName], scope, printer, cleanup)
1614+
_ = try fragment.printCode([enumValuesName], context)
15851615
jsTopLevelLines.append(contentsOf: printer.lines)
15861616
case .associatedValue:
15871617
let fragment = IntrinsicJSFragment.associatedValueEnumValues(enumDefinition: enumDefinition)
1588-
_ = try fragment.printCode([enumValuesName], scope, printer, cleanup)
1618+
_ = try fragment.printCode([enumValuesName], context)
15891619
jsTopLevelLines.append(contentsOf: printer.lines)
15901620
case .namespace:
15911621
break
@@ -2155,12 +2185,18 @@ extension BridgeJSLink {
21552185
let context: BridgeContext
21562186
var parameterNames: [String] = []
21572187
var parameterForwardings: [String] = []
2188+
let printContext: IntrinsicJSFragment.PrintCodeContext
21582189

21592190
init(context: BridgeContext = .importTS, intrinsicRegistry: JSIntrinsicRegistry) {
21602191
self.body = CodeFragmentPrinter()
21612192
self.scope = JSGlueVariableScope(intrinsicRegistry: intrinsicRegistry)
21622193
self.cleanupCode = CodeFragmentPrinter()
21632194
self.context = context
2195+
self.printContext = IntrinsicJSFragment.PrintCodeContext(
2196+
scope: scope,
2197+
printer: body,
2198+
cleanupCode: cleanupCode
2199+
)
21642200
}
21652201

21662202
func liftSelf() {
@@ -2179,7 +2215,7 @@ extension BridgeJSLink {
21792215
valuesToLift = liftingFragment.parameters.map { scope.variable(param.name + $0.capitalizedFirstLetter) }
21802216
parameterNames.append(contentsOf: valuesToLift)
21812217
}
2182-
let liftedValues = try liftingFragment.printCode(valuesToLift, scope, body, cleanupCode)
2218+
let liftedValues = try liftingFragment.printCode(valuesToLift, printContext)
21832219
assert(liftedValues.count == 1, "Lifting fragment should produce exactly one value")
21842220
parameterForwardings.append(contentsOf: liftedValues)
21852221
}
@@ -2289,7 +2325,7 @@ extension BridgeJSLink {
22892325
)
22902326

22912327
let fragment = try IntrinsicJSFragment.protocolPropertyOptionalToSideChannel(wrappedType: wrappedType)
2292-
_ = try fragment.printCode([resultVar], scope, body, cleanupCode)
2328+
_ = try fragment.printCode([resultVar], printContext)
22932329

22942330
return nil // Side-channel types return nil (no direct return value)
22952331
}
@@ -2342,7 +2378,10 @@ extension BridgeJSLink {
23422378
loweringFragment: IntrinsicJSFragment
23432379
) throws -> String? {
23442380
assert(loweringFragment.parameters.count <= 1, "Lowering fragment should have at most one parameter")
2345-
let loweredValues = try loweringFragment.printCode(returnExpr.map { [$0] } ?? [], scope, body, cleanupCode)
2381+
let loweredValues = try loweringFragment.printCode(
2382+
returnExpr.map { [$0] } ?? [],
2383+
printContext
2384+
)
23462385
assert(loweredValues.count <= 1, "Lowering fragment should produce at most one value")
23472386
return loweredValues.first
23482387
}

0 commit comments

Comments
 (0)