Treat function expression deps as conditional

This commit is contained in:
Sathya Gunasekaran
2024-01-15 12:44:22 +00:00
parent 121e72a342
commit 38d3423970
3 changed files with 36 additions and 8 deletions
@@ -63,7 +63,11 @@ export function propagateScopeDependencies(fn: ReactiveFunction): void {
});
}
}
visitReactiveFunction(fn, new PropagationVisitor(), context);
visitReactiveFunction(
fn,
new PropagationVisitor(fn.env.config.enableTreatFunctionDepsAsConditional),
context
);
}
type TemporariesUsedOutsideDefiningScope = {
@@ -466,6 +470,14 @@ class Context {
}
class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
enableTreatFunctionDepsAsConditional = false;
constructor(enableTreatFunctionDepsAsConditional: boolean) {
super();
this.enableTreatFunctionDepsAsConditional =
enableTreatFunctionDepsAsConditional;
}
override visitScope(scope: ReactiveScopeBlock, context: Context): void {
const scopeDependencies = context.enter(scope.scope, () => {
this.visitBlock(scope.instructions, context);
@@ -547,6 +559,20 @@ class PropagationVisitor extends ReactiveFunctionVisitor<Context> {
this.visitInstructionValue(context, id, value.value, null);
break;
}
case "FunctionExpression": {
if (this.enableTreatFunctionDepsAsConditional) {
context.enterConditional(() => {
for (const operand of eachInstructionValueOperand(value)) {
context.visitOperand(operand);
}
});
} else {
for (const operand of eachInstructionValueOperand(value)) {
context.visitOperand(operand);
}
}
break;
}
default: {
for (const operand of eachInstructionValueOperand(value)) {
context.visitOperand(operand);
@@ -2,6 +2,7 @@
## Input
```javascript
// @enableTreatFunctionDepsAsConditional
function Component(props) {
function getLength() {
return props.bar.length;
@@ -12,7 +13,7 @@ function Component(props) {
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ bar: [] }],
params: [{ bar: null }],
};
```
@@ -20,15 +21,15 @@ export const FIXTURE_ENTRYPOINT = {
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import { unstable_useMemoCache as useMemoCache } from "react"; // @enableTreatFunctionDepsAsConditional
function Component(props) {
const $ = useMemoCache(5);
let t0;
if ($[0] !== props.bar.length) {
if ($[0] !== props) {
t0 = function getLength() {
return props.bar.length;
};
$[0] = props.bar.length;
$[0] = props;
$[1] = t0;
} else {
t0 = $[1];
@@ -48,10 +49,10 @@ function Component(props) {
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ bar: [] }],
params: [{ bar: null }],
};
```
### Eval output
(kind: ok) 0
(kind: ok) null
@@ -1,3 +1,4 @@
// @enableTreatFunctionDepsAsConditional
function Component(props) {
function getLength() {
return props.bar.length;
@@ -8,5 +9,5 @@ function Component(props) {
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{ bar: [] }],
params: [{ bar: null }],
};