Skip to content

Commit e0c9485

Browse files
committed
BridgeJS: Eliminate dead innerCleanup variables in nullable codegen and fix em-dashes
1 parent 1646876 commit e0c9485

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1957,19 +1957,28 @@ struct IntrinsicJSFragment: Sendable {
19571957
printer.write("const \(isSomeVar) = \(kind.presenceCheck(value: value));")
19581958

19591959
let localCleanup = CodeFragmentPrinter()
1960-
let innerCleanupVar = scope.variable("innerCleanup")
1961-
printer.write("let \(innerCleanupVar);")
1962-
printer.write("if (\(isSomeVar)) {")
1963-
try printer.indent {
1960+
let ifBodyPrinter = CodeFragmentPrinter()
1961+
try ifBodyPrinter.indent {
19641962
let innerFragment = try stackLowerFragment(elementType: wrappedType)
19651963
let _ = try innerFragment.printCode(
19661964
[value],
1967-
context.with(\.cleanupCode, localCleanup)
1965+
context.with(\.printer, ifBodyPrinter).with(\.cleanupCode, localCleanup)
19681966
)
1969-
let cleanupLines = localCleanup.lines.filter {
1970-
!$0.trimmingCharacters(in: .whitespaces).isEmpty
1971-
}
1972-
if !cleanupLines.isEmpty {
1967+
}
1968+
let cleanupLines = localCleanup.lines.filter {
1969+
!$0.trimmingCharacters(in: .whitespaces).isEmpty
1970+
}
1971+
let hasCleanup = !cleanupLines.isEmpty
1972+
let innerCleanupVar = hasCleanup ? scope.variable("innerCleanup") : nil
1973+
if let innerCleanupVar {
1974+
printer.write("let \(innerCleanupVar);")
1975+
}
1976+
printer.write("if (\(isSomeVar)) {")
1977+
for line in ifBodyPrinter.lines {
1978+
printer.write(line)
1979+
}
1980+
if let innerCleanupVar {
1981+
printer.indent {
19731982
printer.write("\(innerCleanupVar) = () => {")
19741983
printer.indent {
19751984
for line in cleanupLines {
@@ -1997,7 +2006,9 @@ struct IntrinsicJSFragment: Sendable {
19972006
printer.write("}")
19982007
}
19992008
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
2000-
cleanup.write("if (\(innerCleanupVar)) { \(innerCleanupVar)(); }")
2009+
if let innerCleanupVar {
2010+
cleanup.write("if (\(innerCleanupVar)) { \(innerCleanupVar)(); }")
2011+
}
20012012

20022013
return []
20032014
}
@@ -2855,23 +2866,32 @@ struct IntrinsicJSFragment: Sendable {
28552866
printer.write("const \(isSomeVar) = \(kind.presenceCheck(value: value));")
28562867

28572868
let localCleanup = CodeFragmentPrinter()
2858-
let innerCleanupVar = scope.variable("innerCleanup")
2859-
printer.write("let \(innerCleanupVar);")
2860-
printer.write("if (\(isSomeVar)) {")
2861-
try printer.indent {
2869+
let ifBodyPrinter = CodeFragmentPrinter()
2870+
try ifBodyPrinter.indent {
28622871
let innerFragment = try structFieldLowerFragment(
28632872
type: wrappedType,
28642873
fieldName: fieldName,
28652874
allStructs: allStructs
28662875
)
28672876
let _ = try innerFragment.printCode(
28682877
[value],
2869-
context.with(\.cleanupCode, localCleanup)
2878+
context.with(\.printer, ifBodyPrinter).with(\.cleanupCode, localCleanup)
28702879
)
2871-
let cleanupLines = localCleanup.lines.filter {
2872-
!$0.trimmingCharacters(in: .whitespaces).isEmpty
2873-
}
2874-
if !cleanupLines.isEmpty {
2880+
}
2881+
let cleanupLines = localCleanup.lines.filter {
2882+
!$0.trimmingCharacters(in: .whitespaces).isEmpty
2883+
}
2884+
let hasCleanup = !cleanupLines.isEmpty
2885+
let innerCleanupVar = hasCleanup ? scope.variable("innerCleanup") : nil
2886+
if let innerCleanupVar {
2887+
printer.write("let \(innerCleanupVar);")
2888+
}
2889+
printer.write("if (\(isSomeVar)) {")
2890+
for line in ifBodyPrinter.lines {
2891+
printer.write(line)
2892+
}
2893+
if let innerCleanupVar {
2894+
printer.indent {
28752895
printer.write("\(innerCleanupVar) = () => {")
28762896
printer.indent {
28772897
for line in cleanupLines {
@@ -2899,7 +2919,9 @@ struct IntrinsicJSFragment: Sendable {
28992919
printer.write("}")
29002920
}
29012921
scope.emitPushI32Parameter("\(isSomeVar) ? 1 : 0", printer: printer)
2902-
cleanup.write("if (\(innerCleanupVar)) { \(innerCleanupVar)(); }")
2922+
if let innerCleanupVar {
2923+
cleanup.write("if (\(innerCleanupVar)) { \(innerCleanupVar)(); }")
2924+
}
29032925
return []
29042926
}
29052927
)

Plugins/BridgeJS/Sources/BridgeJSSkeleton/BridgeJSSkeleton.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public struct BridgeTypeDescriptor: Sendable {
269269

270270
/// Describes how to transform a Swift accessor expression before passing it to bridge methods.
271271
///
272-
/// Most types use `.identity` the value is passed as-is. Two patterns require transformation:
272+
/// Most types use `.identity` - the value is passed as-is. Two patterns require transformation:
273273
/// - `@JSClass` types expose their underlying `JSObject` via a `.jsObject` member.
274274
/// - `@JS protocol` wrapper types require a downcast from the existential to the concrete wrapper.
275275
///
@@ -322,9 +322,9 @@ public enum AccessorTransform: Sendable, Equatable {
322322

323323
/// Which bridge protocol method the codegen calls to lower a value in the export direction.
324324
public enum LowerMethod: Sendable, Equatable {
325-
/// Calls `bridgeJSLowerStackReturn()` pushes a scalar onto the stack (used within composites like optionals, struct fields).
325+
/// Calls `bridgeJSLowerStackReturn()` - pushes a scalar onto the stack (used within composites like optionals, struct fields).
326326
case stackReturn
327-
/// Calls `bridgeJSLowerReturn()` serializes the entire value onto the stack (struct, array, dictionary).
327+
/// Calls `bridgeJSLowerReturn()` - serializes the entire value onto the stack (struct, array, dictionary).
328328
case fullReturn
329329
/// Calls `bridgeJSLowerParameter()` and pushes the i32 result. Used by associated value enums (tag + stack payload).
330330
case pushParameter

Plugins/BridgeJS/Tests/BridgeJSToolTests/__Snapshots__/BridgeJSLinkTests/EnumAssociatedValue.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,31 +740,25 @@ export async function createInstantiator(options, swift) {
740740
}
741741
case OptionalAllTypesResultValues.Tag.OptClass: {
742742
const isSome = value.param0 != null;
743-
let innerCleanup;
744743
if (isSome) {
745744
ptrStack.push(value.param0.pointer);
746745
} else {
747746
ptrStack.push(0);
748747
}
749748
i32Stack.push(isSome ? 1 : 0);
750-
const cleanup = () => {
751-
if (innerCleanup) { innerCleanup(); }
752-
};
749+
const cleanup = undefined;
753750
return { caseId: OptionalAllTypesResultValues.Tag.OptClass, cleanup };
754751
}
755752
case OptionalAllTypesResultValues.Tag.OptJSObject: {
756753
const isSome = value.param0 != null;
757-
let innerCleanup;
758754
if (isSome) {
759755
const objId = swift.memory.retain(value.param0);
760756
i32Stack.push(objId);
761757
} else {
762758
i32Stack.push(0);
763759
}
764760
i32Stack.push(isSome ? 1 : 0);
765-
const cleanup = () => {
766-
if (innerCleanup) { innerCleanup(); }
767-
};
761+
const cleanup = undefined;
768762
return { caseId: OptionalAllTypesResultValues.Tag.OptJSObject, cleanup };
769763
}
770764
case OptionalAllTypesResultValues.Tag.OptNestedEnum: {

0 commit comments

Comments
 (0)