[λ] Re run SSA on function expr
Functions can capture variables declared after the definition of the function. This re runs SSA to map the captured identifiers to the new SSA identifiers if available.
Sathya Gunasekaran committed
Jan 23, 2023 at 13:20 UTC
3927281fe4da48f0d9eb78019f832fd404a7c089
3 files changed
+55
compiler/forget/src/SSA/EnterSSA.ts
+8
@@ -226,4 +226,12 @@ export default function enterSSA(func: HIRFunction) {
226
}
227
}
228
}
229
+
230
+ for (const [_, block] of func.body.blocks) {
231
+ for (const instr of block.instructions) {
232
+ if (instr.value.kind === "FunctionExpression") {
233
+ mapInstructionOperands(instr, (place) => builder.getPlace(place));
234
+ }
235
+ }
236
+ }
237
}
compiler/forget/src/__tests__/fixtures/hir/mutate-captured-arg-separately.expect.md
new
+38
@@ -0,0 +1,38 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component(a) {
6
+ let y = function () {
7
+ m(x);
8
+ };
9
+
10
+ let x = { a };
11
+ m(x);
12
+ return y;
13
+}
14
+
15
+```
16
+
17
+## Code
18
+
19
+```javascript
20
+function component(a) {
21
+ const $ = React.useMemoCache();
22
+ let y;
23
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
24
+ y = function () {
25
+ m(x);
26
+ };
27
+ $[0] = y;
28
+ } else {
29
+ y = $[0];
30
+ }
31
+
32
+ const x = { a: a };
33
+ m(x);
34
+ return y;
35
+}
36
+
37
+```
38
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/mutate-captured-arg-separately.js
new
+9
@@ -0,0 +1,9 @@
1
+function component(a) {
2
+ let y = function () {
3
+ m(x);
4
+ };
5
+
6
+ let x = { a };
7
+ m(x);
8
+ return y;
9
+}