@@ -241,6 +241,45 @@ struct IntrinsicJSFragment: Sendable {
241241
242242 // MARK: - Associated Value Payload Fragments
243243
244+ /// Fragment for pushing all associated value payloads for an entire enum case during enum lowering
245+ static func associatedValuePushPayload( enumCase: EnumCase ) -> IntrinsicJSFragment {
246+ return IntrinsicJSFragment (
247+ parameters: [ " value " , " enumName " , " caseName " ] ,
248+ printCode: { arguments, scope, printer, cleanup in
249+ let enumName = arguments [ 1 ]
250+ let caseName = arguments [ 2 ]
251+
252+ // If no associated values, return early
253+ if enumCase. associatedValues. isEmpty {
254+ printer. write ( " const cleanup = undefined; " )
255+ printer. write ( " return { caseId: \( enumName) .Tag. \( caseName) , cleanup }; " )
256+ return [ ]
257+ }
258+
259+ // Process associated values in reverse order (to match the order they'll be popped)
260+ let reversedValues = enumCase. associatedValues. enumerated ( ) . reversed ( )
261+
262+ for (associatedValueIndex, associatedValue) in reversedValues {
263+ let prop = associatedValue. label ?? " param \( associatedValueIndex) "
264+ let fragment = IntrinsicJSFragment . associatedValuePushPayload ( type: associatedValue. type)
265+
266+ _ = fragment. printCode ( [ " value. \( prop) " ] , scope, printer, cleanup)
267+ }
268+
269+ if cleanup. lines. isEmpty {
270+ printer. write ( " const cleanup = undefined; " )
271+ } else {
272+ printer. write ( " const cleanup = () => { " )
273+ printer. write ( contentsOf: cleanup)
274+ printer. write ( " }; " )
275+ }
276+ printer. write ( " return { caseId: \( enumName) .Tag. \( caseName) , cleanup }; " )
277+
278+ return [ ]
279+ }
280+ )
281+ }
282+
244283 /// Fragment for pushing associated value payloads during enum lowering
245284 static func associatedValuePushPayload( type: BridgeType ) -> IntrinsicJSFragment {
246285 switch type {
@@ -292,11 +331,9 @@ struct IntrinsicJSFragment: Sendable {
292331 }
293332 )
294333 default :
295- // For unsupported types, push a default value
296334 return IntrinsicJSFragment (
297- parameters: [ " value " ] ,
335+ parameters: [ ] ,
298336 printCode: { arguments, scope, printer, cleanup in
299- printer. write ( " \( JSGlueVariableScope . reservedTmpParamInts) .push(0); " )
300337 return [ ]
301338 }
302339 )
0 commit comments