mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
feat(compiler): Compiler Logical Negation Constant Propagation (#29623)
## Summary Resolves #29622 ## How did you test this change? I verified the implementation using the test. Note: This PR was done without waiting for approval in #29622, so feel free to just close it.
This commit is contained in:
@@ -311,6 +311,25 @@ function evaluateInstruction(
|
||||
}
|
||||
return null;
|
||||
}
|
||||
case "UnaryExpression": {
|
||||
switch (value.operator) {
|
||||
case "!": {
|
||||
const operand = read(constants, value.value);
|
||||
if (operand !== null && operand.kind === "Primitive") {
|
||||
const result: Primitive = {
|
||||
kind: "Primitive",
|
||||
value: !operand.value,
|
||||
loc: value.loc,
|
||||
};
|
||||
instr.value = result;
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
case "BinaryExpression": {
|
||||
const lhsValue = read(constants, value.left);
|
||||
const rhsValue = read(constants, value.right);
|
||||
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
|
||||
## Input
|
||||
|
||||
```javascript
|
||||
import { Stringify } from "shared-runtime";
|
||||
|
||||
function foo() {
|
||||
let _b;
|
||||
const b = true;
|
||||
if (!b) {
|
||||
_b = "bar";
|
||||
} else {
|
||||
_b = "baz";
|
||||
}
|
||||
|
||||
return (
|
||||
<Stringify
|
||||
value={{
|
||||
_b,
|
||||
b0: !true,
|
||||
n0: !0,
|
||||
n1: !1,
|
||||
n2: !2,
|
||||
n3: !-1,
|
||||
s0: !"",
|
||||
s1: !"a",
|
||||
s2: !"ab",
|
||||
u: !undefined,
|
||||
n: !null,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: foo,
|
||||
params: [],
|
||||
isComponent: false,
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
## Code
|
||||
|
||||
```javascript
|
||||
import { c as _c } from "react/compiler-runtime";
|
||||
import { Stringify } from "shared-runtime";
|
||||
|
||||
function foo() {
|
||||
const $ = _c(1);
|
||||
let t0;
|
||||
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
||||
t0 = (
|
||||
<Stringify
|
||||
value={{
|
||||
_b: "baz",
|
||||
b0: false,
|
||||
n0: true,
|
||||
n1: false,
|
||||
n2: false,
|
||||
n3: !-1,
|
||||
s0: true,
|
||||
s1: false,
|
||||
s2: false,
|
||||
u: !undefined,
|
||||
n: true,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
$[0] = t0;
|
||||
} else {
|
||||
t0 = $[0];
|
||||
}
|
||||
return t0;
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: foo,
|
||||
params: [],
|
||||
isComponent: false,
|
||||
};
|
||||
|
||||
```
|
||||
|
||||
### Eval output
|
||||
(kind: ok) <div>{"value":{"_b":"baz","b0":false,"n0":true,"n1":false,"n2":false,"n3":false,"s0":true,"s1":false,"s2":false,"u":true,"n":true}}</div>
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import { Stringify } from "shared-runtime";
|
||||
|
||||
function foo() {
|
||||
let _b;
|
||||
const b = true;
|
||||
if (!b) {
|
||||
_b = "bar";
|
||||
} else {
|
||||
_b = "baz";
|
||||
}
|
||||
|
||||
return (
|
||||
<Stringify
|
||||
value={{
|
||||
_b,
|
||||
b0: !true,
|
||||
n0: !0,
|
||||
n1: !1,
|
||||
n2: !2,
|
||||
n3: !-1,
|
||||
s0: !"",
|
||||
s1: !"a",
|
||||
s2: !"ab",
|
||||
u: !undefined,
|
||||
n: !null,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
export const FIXTURE_ENTRYPOINT = {
|
||||
fn: foo,
|
||||
params: [],
|
||||
isComponent: false,
|
||||
};
|
||||
Reference in New Issue
Block a user