@@ -145,8 +145,6 @@ struct IntrinsicJSFragment: Sendable {
145145 var scope : JSGlueVariableScope
146146 /// The printer to print the main fragment code.
147147 var printer : CodeFragmentPrinter
148- /// The printer to print the code that is expected to be executed at the end of the caller of the fragment.
149- var cleanupCode : CodeFragmentPrinter
150148 /// Whether the fragment has direct access to the SwiftHeapObject classes.
151149 /// If false, the fragment needs to use `_exports["<class name>"]` to access the class.
152150 var hasDirectAccessToSwiftClass : Bool = true
@@ -236,7 +234,7 @@ struct IntrinsicJSFragment: Sendable {
236234 static let stringLowerParameter = IntrinsicJSFragment (
237235 parameters: [ " value " ] ,
238236 printCode: { arguments, context in
239- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
237+ let ( scope, printer) = ( context. scope, context. printer)
240238 let argument = arguments [ 0 ]
241239 let bytesLabel = scope. variable ( " \( argument) Bytes " )
242240 let bytesIdLabel = scope. variable ( " \( argument) Id " )
@@ -248,7 +246,7 @@ struct IntrinsicJSFragment: Sendable {
248246 static let stringLiftReturn = IntrinsicJSFragment (
249247 parameters: [ ] ,
250248 printCode: { arguments, context in
251- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
249+ let ( scope, printer) = ( context. scope, context. printer)
252250 let resultLabel = scope. variable ( " ret " )
253251 printer. write ( " const \( resultLabel) = \( JSGlueVariableScope . reservedStorageToReturnString) ; " )
254252 printer. write ( " \( JSGlueVariableScope . reservedStorageToReturnString) = undefined; " )
@@ -258,7 +256,7 @@ struct IntrinsicJSFragment: Sendable {
258256 static let stringLiftParameter = IntrinsicJSFragment (
259257 parameters: [ " objectId " ] ,
260258 printCode: { arguments, context in
261- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
259+ let ( scope, printer) = ( context. scope, context. printer)
262260 let objectId = arguments [ 0 ]
263261 let objectLabel = scope. variable ( " \( objectId) Object " )
264262 // TODO: Implement "take" operation
@@ -270,7 +268,7 @@ struct IntrinsicJSFragment: Sendable {
270268 static let stringLowerReturn = IntrinsicJSFragment (
271269 parameters: [ " value " ] ,
272270 printCode: { arguments, context in
273- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
271+ let ( scope, printer) = ( context. scope, context. printer)
274272 printer. write (
275273 " \( JSGlueVariableScope . reservedStorageToReturnBytes) = \( JSGlueVariableScope . reservedTextEncoder) .encode( \( arguments [ 0 ] ) ); "
276274 )
@@ -287,7 +285,7 @@ struct IntrinsicJSFragment: Sendable {
287285 static let jsObjectLiftReturn = IntrinsicJSFragment (
288286 parameters: [ " retId " ] ,
289287 printCode: { arguments, context in
290- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
288+ let ( scope, printer) = ( context. scope, context. printer)
291289 // TODO: Implement "take" operation
292290 let resultLabel = scope. variable ( " ret " )
293291 let retId = arguments [ 0 ]
@@ -299,7 +297,7 @@ struct IntrinsicJSFragment: Sendable {
299297 static let jsObjectLiftRetainedObjectId = IntrinsicJSFragment (
300298 parameters: [ " objectId " ] ,
301299 printCode: { arguments, context in
302- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
300+ let ( scope, printer) = ( context. scope, context. printer)
303301 let resultLabel = scope. variable ( " value " )
304302 let objectId = arguments [ 0 ]
305303 printer. write (
@@ -444,7 +442,7 @@ struct IntrinsicJSFragment: Sendable {
444442 static let jsValueLower = IntrinsicJSFragment (
445443 parameters: [ " value " ] ,
446444 printCode: { arguments, context in
447- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
445+ let ( scope, printer) = ( context. scope, context. printer)
448446 let value = arguments [ 0 ]
449447 let kindVar = scope. variable ( " \( value) Kind " )
450448 let payload1Var = scope. variable ( " \( value) Payload1 " )
@@ -570,7 +568,7 @@ struct IntrinsicJSFragment: Sendable {
570568 static let jsValueLift = IntrinsicJSFragment (
571569 parameters: [ ] ,
572570 printCode: { _, context in
573- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
571+ let ( scope, printer) = ( context. scope, context. printer)
574572 let payload2 = scope. variable ( " jsValuePayload2 " )
575573 let payload1 = scope. variable ( " jsValuePayload1 " )
576574 let kind = scope. variable ( " jsValueKind " )
@@ -588,7 +586,7 @@ struct IntrinsicJSFragment: Sendable {
588586 static let jsValueLiftParameter = IntrinsicJSFragment (
589587 parameters: [ " kind " , " payload1 " , " payload2 " ] ,
590588 printCode: { arguments, context in
591- let ( scope, printer, cleanupCode ) = ( context. scope, context. printer, context . cleanupCode )
589+ let ( scope, printer) = ( context. scope, context. printer)
592590 let resultVar = scope. variable ( " jsValue " )
593591 registerJSValueHelpers ( scope: scope)
594592 printer. write (
@@ -1128,16 +1126,7 @@ struct IntrinsicJSFragment: Sendable {
11281126 printer. write ( " if ( \( isSomeVar) ) { " )
11291127 try printer. indent {
11301128 let arrayLowerFragment = try arrayLower ( elementType: elementType)
1131- let arrayCleanup = CodeFragmentPrinter ( )
1132- let _ = try arrayLowerFragment. printCode (
1133- [ value] ,
1134- context. with ( \. cleanupCode, arrayCleanup)
1135- )
1136- if !arrayCleanup. lines. isEmpty {
1137- for line in arrayCleanup. lines {
1138- printer. write ( line)
1139- }
1140- }
1129+ let _ = try arrayLowerFragment. printCode ( [ value] , context)
11411130 }
11421131 printer. write ( " } " )
11431132 scope. emitPushI32Parameter ( " \( isSomeVar) ? 1 : 0 " , printer: printer)
@@ -1176,11 +1165,10 @@ struct IntrinsicJSFragment: Sendable {
11761165 case . associatedValueEnum( let fullName) :
11771166 let base = fullName. components ( separatedBy: " . " ) . last ?? fullName
11781167 let caseIdVar = scope. variable ( " caseId " )
1179- let cleanupVar = scope. variable ( " cleanup " )
11801168 printer. write ( " if ( \( isSomeVar) ) { " )
11811169 printer. indent {
11821170 printer. write (
1183- " const { caseId: \( caseIdVar) , cleanup: \( cleanupVar ) } = \( JSGlueVariableScope . reservedEnumHelpers) . \( base) .lower( \( value) ); "
1171+ " const { caseId: \( caseIdVar) } = \( JSGlueVariableScope . reservedEnumHelpers) . \( base) .lower( \( value) ); "
11841172 )
11851173 printer. write ( " return \( caseIdVar) ; " )
11861174 }
@@ -1529,9 +1517,8 @@ struct IntrinsicJSFragment: Sendable {
15291517 let ( scope, printer) = ( context. scope, context. printer)
15301518 let value = arguments [ 0 ]
15311519 let caseIdVar = scope. variable ( " caseId " )
1532- let cleanupVar = scope. variable ( " cleanup " )
15331520 printer. write (
1534- " const { caseId: \( caseIdVar) , cleanup: \( cleanupVar ) } = \( JSGlueVariableScope . reservedEnumHelpers) . \( base) .lower( \( value) ); "
1521+ " const { caseId: \( caseIdVar) } = \( JSGlueVariableScope . reservedEnumHelpers) . \( base) .lower( \( value) ); "
15351522 )
15361523 printer. write ( " return \( caseIdVar) ; " )
15371524 return [ ]
@@ -1593,15 +1580,10 @@ struct IntrinsicJSFragment: Sendable {
15931580 for enumCase in enumDefinition. cases {
15941581 let caseName = enumCase. name. capitalizedFirstLetter
15951582 let caseScope = scope. makeChildScope ( )
1596- let caseCleanup = CodeFragmentPrinter ( )
1597- caseCleanup. indent ( )
15981583 let fragment = IntrinsicJSFragment . associatedValuePushPayload ( enumCase: enumCase)
15991584 _ = try fragment. printCode (
16001585 [ " value " , enumName, caseName] ,
1601- context. with ( \. scope, caseScope) . with ( \. printer, lowerPrinter) . with (
1602- \. cleanupCode,
1603- caseCleanup
1604- )
1586+ context. with ( \. scope, caseScope) . with ( \. printer, lowerPrinter)
16051587 )
16061588 }
16071589
@@ -1627,15 +1609,11 @@ struct IntrinsicJSFragment: Sendable {
16271609 for enumCase in enumDefinition. cases {
16281610 let caseName = enumCase. name. capitalizedFirstLetter
16291611 let caseScope = scope. makeChildScope ( )
1630- let caseCleanup = CodeFragmentPrinter ( )
16311612
16321613 let fragment = IntrinsicJSFragment . associatedValuePopPayload ( enumCase: enumCase)
16331614 _ = try fragment. printCode (
16341615 [ enumName, caseName] ,
1635- context. with ( \. scope, caseScope) . with ( \. printer, liftPrinter) . with (
1636- \. cleanupCode,
1637- caseCleanup
1638- )
1616+ context. with ( \. scope, caseScope) . with ( \. printer, liftPrinter)
16391617 )
16401618 }
16411619
@@ -2012,12 +1990,9 @@ struct IntrinsicJSFragment: Sendable {
20121990 return IntrinsicJSFragment (
20131991 parameters: [ " value " ] ,
20141992 printCode: { arguments, context in
2015- let ( scope , printer) = ( context. scope , context . printer)
1993+ let printer = context. printer
20161994 let value = arguments [ 0 ]
2017- let cleanupVar = scope. variable ( " cleanup " )
2018- printer. write (
2019- " const { cleanup: \( cleanupVar) } = \( JSGlueVariableScope . reservedStructHelpers) . \( base) .lower( \( value) ); "
2020- )
1995+ printer. write ( " \( JSGlueVariableScope . reservedStructHelpers) . \( base) .lower( \( value) ); " )
20211996 return [ ]
20221997 }
20231998 )
@@ -2027,12 +2002,9 @@ struct IntrinsicJSFragment: Sendable {
20272002 return IntrinsicJSFragment (
20282003 parameters: [ " value " ] ,
20292004 printCode: { arguments, context in
2030- let ( scope , printer) = ( context. scope , context . printer)
2005+ let printer = context. printer
20312006 let value = arguments [ 0 ]
2032- let cleanupVar = scope. variable ( " cleanup " )
2033- printer. write (
2034- " const { cleanup: \( cleanupVar) } = \( JSGlueVariableScope . reservedStructHelpers) . \( structBase) .lower( \( value) ); "
2035- )
2007+ printer. write ( " \( JSGlueVariableScope . reservedStructHelpers) . \( structBase) .lower( \( value) ); " )
20362008 return [ ]
20372009 }
20382010 )
@@ -2614,22 +2586,10 @@ struct IntrinsicJSFragment: Sendable {
26142586
26152587 let presenceExpr = kind. presenceCheck ( value: value)
26162588 printer. write ( " const \( isSomeVar) = \( presenceExpr) ? 1 : 0; " )
2617- // Cleanup is written inside the if block so retained id is in scope
2618- let localCleanupWriter = CodeFragmentPrinter ( )
26192589 printer. write ( " if ( \( isSomeVar) ) { " )
26202590 try printer. indent {
26212591 let innerFragment = try stackLowerFragment ( elementType: wrappedType)
2622- let _ = try innerFragment. printCode (
2623- [ value] ,
2624- context. with ( \. cleanupCode, localCleanupWriter)
2625- )
2626- let localCleanupLines = localCleanupWriter. lines. filter {
2627- !$0. trimmingCharacters ( in: . whitespaces) . isEmpty
2628- }
2629- if !localCleanupLines. isEmpty {
2630- let localCleanupCode = localCleanupLines. joined ( separator: " " )
2631- printer. write ( " arrayCleanups.push(() => { \( localCleanupCode) }); " )
2632- }
2592+ let _ = try innerFragment. printCode ( [ value] , context)
26332593 }
26342594 printer. write ( " } else { " )
26352595 printer. indent {
0 commit comments