Skip to content

Commit 63b7b72

Browse files
[NFC] BridgeJS: Remove explicit stack storage captures from enum/struct helpers (#588)
1 parent 2aceeb9 commit 63b7b72

File tree

12 files changed

+85
-85
lines changed

12 files changed

+85
-85
lines changed

Plugins/BridgeJS/Sources/BridgeJSLink/JSGlueGen.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ struct IntrinsicJSFragment: Sendable {
618618
printCode: { _, scope, printer, _ in
619619
let retName = scope.variable("ret")
620620
printer.write(
621-
"const \(retName) = \(JSGlueVariableScope.reservedEnumHelpers).\(enumBase).lift(\(scope.popTagReturn()), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
621+
"const \(retName) = \(JSGlueVariableScope.reservedEnumHelpers).\(enumBase).lift(\(scope.popTagReturn()));"
622622
)
623623
return [retName]
624624
}
@@ -699,7 +699,7 @@ struct IntrinsicJSFragment: Sendable {
699699
printer.write("if (\(isSome)) {")
700700
printer.indent {
701701
printer.write(
702-
"\(enumVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(wrappedValue), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
702+
"\(enumVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(wrappedValue));"
703703
)
704704
}
705705
printer.write("}")
@@ -711,7 +711,7 @@ struct IntrinsicJSFragment: Sendable {
711711
printer.write("if (\(isSome)) {")
712712
printer.indent {
713713
printer.write(
714-
"\(structVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
714+
"\(structVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift();"
715715
)
716716
}
717717
printer.write("} else {")
@@ -944,7 +944,7 @@ struct IntrinsicJSFragment: Sendable {
944944
printer.write("} else {")
945945
printer.indent {
946946
printer.write(
947-
"\(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(tagVar), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
947+
"\(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(tagVar));"
948948
)
949949
}
950950
printer.write("}")
@@ -956,7 +956,7 @@ struct IntrinsicJSFragment: Sendable {
956956
printer.write("if (\(isSomeVar)) {")
957957
printer.indent {
958958
printer.write(
959-
"\(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
959+
"\(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift();"
960960
)
961961
}
962962
printer.write("} else {")
@@ -1283,7 +1283,7 @@ struct IntrinsicJSFragment: Sendable {
12831283
let targetVar = arguments[1]
12841284
let base = fullName.components(separatedBy: ".").last ?? fullName
12851285
printer.write(
1286-
"let \(targetVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseId), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
1286+
"let \(targetVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseId));"
12871287
)
12881288
return []
12891289
}
@@ -1354,7 +1354,7 @@ struct IntrinsicJSFragment: Sendable {
13541354
case .associatedValueEnum(let fullName):
13551355
let base = fullName.components(separatedBy: ".").last ?? fullName
13561356
printer.write(
1357-
"\(targetVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(value), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
1357+
"\(targetVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(value));"
13581358
)
13591359
default:
13601360
fatalError("Unsupported optional wrapped type in closure parameter lifting: \(wrappedType)")
@@ -1682,7 +1682,7 @@ struct IntrinsicJSFragment: Sendable {
16821682
let base = fullName.components(separatedBy: ".").last ?? fullName
16831683
let resultVar = scope.variable("result")
16841684
printer.write(
1685-
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(scope.popTagReturn()), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
1685+
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(scope.popTagReturn()));"
16861686
)
16871687
printer.write("return \(resultVar);")
16881688
return []
@@ -1907,7 +1907,7 @@ struct IntrinsicJSFragment: Sendable {
19071907
let caseId = arguments[0]
19081908
let resultVar = scope.variable("enumValue")
19091909
printer.write(
1910-
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseId), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
1910+
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseId));"
19111911
)
19121912
return [resultVar]
19131913
}
@@ -1924,7 +1924,7 @@ struct IntrinsicJSFragment: Sendable {
19241924
printCode: { arguments, scope, printer, cleanupCode in
19251925
let resultVar = scope.variable("structValue")
19261926
printer.write(
1927-
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
1927+
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(base).lift();"
19281928
)
19291929
return [resultVar]
19301930
}
@@ -2107,7 +2107,7 @@ struct IntrinsicJSFragment: Sendable {
21072107

21082108
// Generate lift function
21092109
printer.write(
2110-
"lift: (tag, \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers)) => {"
2110+
"lift: (tag) => {"
21112111
)
21122112
printer.indent {
21132113
printer.write("tag = tag | 0;")
@@ -2505,7 +2505,7 @@ struct IntrinsicJSFragment: Sendable {
25052505
let caseIdVar = scope.variable("caseId")
25062506
printer.write("const \(caseIdVar) = \(scope.popI32Return());")
25072507
printer.write(
2508-
"\(optVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
2508+
"\(optVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar));"
25092509
)
25102510
} else {
25112511
let wrappedFragment = associatedValuePopPayload(type: wrappedType)
@@ -2565,7 +2565,7 @@ struct IntrinsicJSFragment: Sendable {
25652565
printCode: { arguments, scope, printer, cleanupCode in
25662566
let resultVar = scope.variable("structValue")
25672567
printer.write(
2568-
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(structBase).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
2568+
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(structBase).lift();"
25692569
)
25702570
return [resultVar]
25712571
}
@@ -2705,7 +2705,7 @@ struct IntrinsicJSFragment: Sendable {
27052705
printCode: { arguments, scope, printer, cleanup in
27062706
let resultVar = scope.variable("struct")
27072707
printer.write(
2708-
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(structBase).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
2708+
"const \(resultVar) = \(JSGlueVariableScope.reservedStructHelpers).\(structBase).lift();"
27092709
)
27102710
return [resultVar]
27112711
}
@@ -2765,7 +2765,7 @@ struct IntrinsicJSFragment: Sendable {
27652765
printCode: { arguments, scope, printer, cleanup in
27662766
let resultVar = scope.variable("enumValue")
27672767
printer.write(
2768-
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(scope.popTagReturn()), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
2768+
"const \(resultVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(scope.popTagReturn()), );"
27692769
)
27702770
return [resultVar]
27712771
}
@@ -3125,7 +3125,7 @@ struct IntrinsicJSFragment: Sendable {
31253125
printer.write("},")
31263126

31273127
printer.write(
3128-
"lift: (\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers)) => {"
3128+
"lift: () => {"
31293129
)
31303130
printer.indent {
31313131
generateStructLiftCode(
@@ -3678,7 +3678,7 @@ struct IntrinsicJSFragment: Sendable {
36783678
let caseIdVar = scope.variable("enumCaseId")
36793679
printer.write("const \(caseIdVar) = \(scope.popI32Return());")
36803680
printer.write(
3681-
"\(optVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar), \(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
3681+
"\(optVar) = \(JSGlueVariableScope.reservedEnumHelpers).\(base).lift(\(caseIdVar), );"
36823682
)
36833683
} else {
36843684
let wrappedFragment = structFieldLiftFragment(
@@ -3712,7 +3712,7 @@ struct IntrinsicJSFragment: Sendable {
37123712
printCode: { arguments, scope, printer, cleanup in
37133713
let structVar = scope.variable("struct")
37143714
printer.write(
3715-
"const \(structVar) = \(JSGlueVariableScope.reservedStructHelpers).\(nestedName).lift(\(JSGlueVariableScope.reservedTmpRetStrings), \(JSGlueVariableScope.reservedTmpRetInts), \(JSGlueVariableScope.reservedTmpRetF32s), \(JSGlueVariableScope.reservedTmpRetF64s), \(JSGlueVariableScope.reservedTmpRetPointers));"
3715+
"const \(structVar) = \(JSGlueVariableScope.reservedStructHelpers).\(nestedName).lift();"
37163716
)
37173717
return [structVar]
37183718
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function createInstantiator(options, swift) {
5454
tmpParamF64s.push(value.y);
5555
return { cleanup: undefined };
5656
},
57-
lift: (tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers) => {
57+
lift: () => {
5858
const f64 = tmpRetF64s.pop();
5959
const f641 = tmpRetF64s.pop();
6060
return { x: f641, y: f64 };
@@ -481,7 +481,7 @@ export async function createInstantiator(options, swift) {
481481
const arrayLen = tmpRetInts.pop();
482482
const arrayResult = [];
483483
for (let i = 0; i < arrayLen; i++) {
484-
const struct = structHelpers.Point.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
484+
const struct = structHelpers.Point.lift();
485485
arrayResult.push(struct);
486486
}
487487
arrayResult.reverse();
@@ -544,7 +544,7 @@ export async function createInstantiator(options, swift) {
544544
const matchingBytes = textEncoder.encode(matching);
545545
const matchingId = swift.memory.retain(matchingBytes);
546546
instance.exports.bjs_findFirstPoint(matchingId, matchingBytes.length);
547-
const structValue = structHelpers.Point.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
547+
const structValue = structHelpers.Point.lift();
548548
for (const cleanup of arrayCleanups) { cleanup(); }
549549
swift.memory.release(matchingId);
550550
return structValue;
@@ -715,7 +715,7 @@ export async function createInstantiator(options, swift) {
715715
if (isSome1 === 0) {
716716
optValue = null;
717717
} else {
718-
const struct = structHelpers.Point.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
718+
const struct = structHelpers.Point.lift();
719719
optValue = struct;
720720
}
721721
arrayResult.push(optValue);
@@ -873,7 +873,7 @@ export async function createInstantiator(options, swift) {
873873
const arrayLen1 = tmpRetInts.pop();
874874
const arrayResult1 = [];
875875
for (let i1 = 0; i1 < arrayLen1; i1++) {
876-
const struct = structHelpers.Point.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
876+
const struct = structHelpers.Point.lift();
877877
arrayResult1.push(struct);
878878
}
879879
arrayResult1.reverse();

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function createInstantiator(options, swift) {
5454
};
5555
return { cleanup };
5656
},
57-
lift: (tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers) => {
57+
lift: () => {
5858
const bool = tmpRetInts.pop() !== 0;
5959
const int = tmpRetInts.pop();
6060
const string = tmpRetStrings.pop();
@@ -68,7 +68,7 @@ export async function createInstantiator(options, swift) {
6868
tmpParamF64s.push(value.baseValue);
6969
return { cleanup: undefined };
7070
},
71-
lift: (tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers) => {
71+
lift: () => {
7272
const f64 = tmpRetF64s.pop();
7373
const instance1 = { baseValue: f64 };
7474
instance1.add = function(a, b = 10.0) {
@@ -528,7 +528,7 @@ export async function createInstantiator(options, swift) {
528528
const isSome1 = tmpRetInts.pop();
529529
let optResult;
530530
if (isSome1) {
531-
optResult = structHelpers.Config.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
531+
optResult = structHelpers.Config.lift();
532532
} else {
533533
optResult = null;
534534
}
@@ -546,7 +546,7 @@ export async function createInstantiator(options, swift) {
546546
const isSome1 = tmpRetInts.pop();
547547
let optResult;
548548
if (isSome1) {
549-
optResult = structHelpers.Config.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
549+
optResult = structHelpers.Config.lift();
550550
} else {
551551
optResult = null;
552552
}
@@ -663,7 +663,7 @@ export async function createInstantiator(options, swift) {
663663
MathOperations: {
664664
init: function(baseValue = 0.0) {
665665
instance.exports.bjs_MathOperations_init(baseValue);
666-
const structValue = structHelpers.MathOperations.lift(tmpRetStrings, tmpRetInts, tmpRetF32s, tmpRetF64s, tmpRetPointers);
666+
const structValue = structHelpers.MathOperations.lift();
667667
return structValue;
668668
},
669669
subtract: function(a, b) {

0 commit comments

Comments
 (0)