Skip to content

Commit 95f6869

Browse files
committed
BridgeJS: Move whole push into Intrinsic fragment
1 parent 13e34bc commit 95f6869

File tree

3 files changed

+48
-34
lines changed

3 files changed

+48
-34
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/BridgeJSLink.swift

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -700,36 +700,14 @@ struct BridgeJSLink {
700700
} else {
701701
let scope = JSGlueVariableScope()
702702
let cleanup = CodeFragmentPrinter()
703-
cleanup.indent()
704-
cleanup.indent()
705-
cleanup.indent()
706-
cleanup.indent()
707-
cleanup.indent()
708-
cleanup.indent()
709703
let printer = CodeFragmentPrinter()
704+
cleanup.indent()
710705

711-
let reversedValues = enumCase.associatedValues.enumerated().reversed()
712-
713-
for (associatedValueIndex, associatedValue) in reversedValues {
714-
let prop = associatedValue.label ?? "param\(associatedValueIndex)"
715-
let fragment = IntrinsicJSFragment.associatedValuePushPayload(type: associatedValue.type)
716-
717-
_ = fragment.printCode(["value.\(prop)"], scope, printer, cleanup)
718-
}
706+
let fragment = IntrinsicJSFragment.associatedValuePushPayload(enumCase: enumCase)
707+
_ = fragment.printCode(["value", enumDefinition.name, caseName], scope, printer, cleanup)
719708

720709
jsLines.append("case \(enumDefinition.name).Tag.\(caseName): {".indent(count: 16))
721710
jsLines.append(contentsOf: printer.lines.map { $0.indent(count: 20) })
722-
if cleanup.lines.isEmpty {
723-
jsLines.append("const cleanup = undefined;".indent(count: 20))
724-
} else {
725-
jsLines.append("const cleanup = () => {".indent(count: 20))
726-
jsLines.append(contentsOf: cleanup.lines)
727-
jsLines.append("};".indent(count: 20))
728-
}
729-
jsLines.append(
730-
"return { caseId: \(enumDefinition.name).Tag.\(caseName), cleanup };"
731-
.indent(count: 20)
732-
)
733711
jsLines.append("}".indent(count: 16))
734712
}
735713
}

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// MARK: - Types
44

5-
public enum BridgeType: Codable, Equatable {
5+
public enum BridgeType: Codable, Equatable, Sendable {
66
case int, float, double, string, bool, jsObject(String?), swiftHeapObject(String), void
77
case caseEnum(String)
88
case rawValueEnum(String, SwiftEnumRawType)
@@ -14,7 +14,7 @@ public enum WasmCoreType: String, Codable, Sendable {
1414
case i32, i64, f32, f64, pointer
1515
}
1616

17-
public enum SwiftEnumRawType: String, CaseIterable, Codable {
17+
public enum SwiftEnumRawType: String, CaseIterable, Codable, Sendable {
1818
case string = "String"
1919
case bool = "Bool"
2020
case int = "Int"
@@ -70,7 +70,7 @@ public struct Effects: Codable {
7070

7171
// MARK: - Enum Skeleton
7272

73-
public struct AssociatedValue: Codable, Equatable {
73+
public struct AssociatedValue: Codable, Equatable, Sendable {
7474
public let label: String?
7575
public let type: BridgeType
7676

@@ -80,16 +80,15 @@ public struct AssociatedValue: Codable, Equatable {
8080
}
8181
}
8282

83-
public struct EnumCase: Codable, Equatable {
83+
public struct EnumCase: Codable, Equatable, Sendable {
8484
public let name: String
8585
public let rawValue: String?
8686
public let associatedValues: [AssociatedValue]
8787

8888
public var isSimple: Bool {
8989
associatedValues.isEmpty
9090
}
91-
92-
public init(name: String, rawValue: String?, associatedValues: [AssociatedValue]) {
91+
public init(name: String, rawValue: String?, associatedValues: [AssociatedValue] = []) {
9392
self.name = name
9493
self.rawValue = rawValue
9594
self.associatedValues = associatedValues

0 commit comments

Comments
 (0)