[compiler][ez] Add types for Math.min, recursive global
ghstack-source-id: 9940b8f96eec3fb2b411af8a7c92d93aae6da85e Pull Request resolved: https://github.com/facebook/react/pull/30641
Mofei Zhang committed
Aug 8, 2024 at 15:41 UTC
0fb03c4952195fb02980d3acd6797547a7ae6b23
4 files changed
+62
-14
compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts
+56
-1
@@ -136,6 +136,57 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
136
returnValueKind: ValueKind.Primitive,
137
}),
138
],
139
+ [
140
+ 'min',
141
+ // Math.min(value0, ..., valueN)
142
+ addFunction(DEFAULT_SHAPES, [], {
143
+ positionalParams: [],
144
+ restParam: Effect.Read,
145
+ returnType: {kind: 'Primitive'},
146
+ calleeEffect: Effect.Read,
147
+ returnValueKind: ValueKind.Primitive,
148
+ }),
149
+ ],
150
+ [
151
+ 'trunc',
152
+ addFunction(DEFAULT_SHAPES, [], {
153
+ positionalParams: [],
154
+ restParam: Effect.Read,
155
+ returnType: {kind: 'Primitive'},
156
+ calleeEffect: Effect.Read,
157
+ returnValueKind: ValueKind.Primitive,
158
+ }),
159
+ ],
160
+ [
161
+ 'ceil',
162
+ addFunction(DEFAULT_SHAPES, [], {
163
+ positionalParams: [],
164
+ restParam: Effect.Read,
165
+ returnType: {kind: 'Primitive'},
166
+ calleeEffect: Effect.Read,
167
+ returnValueKind: ValueKind.Primitive,
168
+ }),
169
+ ],
170
+ [
171
+ 'floor',
172
+ addFunction(DEFAULT_SHAPES, [], {
173
+ positionalParams: [],
174
+ restParam: Effect.Read,
175
+ returnType: {kind: 'Primitive'},
176
+ calleeEffect: Effect.Read,
177
+ returnValueKind: ValueKind.Primitive,
178
+ }),
179
+ ],
180
+ [
181
+ 'pow',
182
+ addFunction(DEFAULT_SHAPES, [], {
183
+ positionalParams: [],
184
+ restParam: Effect.Read,
185
+ returnType: {kind: 'Primitive'},
186
+ calleeEffect: Effect.Read,
187
+ returnValueKind: ValueKind.Primitive,
188
+ }),
189
+ ],
190
]),
191
],
192
['Infinity', {kind: 'Primitive'}],
@@ -455,11 +506,15 @@ for (const [name, type_] of TYPED_GLOBALS) {
506
DEFAULT_GLOBALS.set(name, type_);
507
}
508
458
-// Recursive global type
509
+// Recursive global types
510
DEFAULT_GLOBALS.set(
511
'globalThis',
512
addObject(DEFAULT_SHAPES, 'globalThis', TYPED_GLOBALS),
513
);
514
+DEFAULT_GLOBALS.set(
515
+ 'global',
516
+ addObject(DEFAULT_SHAPES, 'global', TYPED_GLOBALS),
517
+);
518
519
export function installReAnimatedTypes(
520
globals: GlobalRegistry,
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.expect.md
+3
-1
@@ -13,6 +13,7 @@ function Component(props) {
13
console.error(x);
14
console.trace(x);
15
console.table(x);
16
+ global.console.log(x);
17
return x;
18
}
19
@@ -48,6 +49,7 @@ function Component(props) {
49
console.error(x);
50
console.trace(x);
51
console.table(x);
52
+ global.console.log(x);
53
return x;
54
}
55
@@ -61,4 +63,4 @@ export const FIXTURE_ENTRYPOINT = {
63
64
### Eval output
65
(kind: ok) {"a":1,"b":2}
64
-logs: [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }]
\ No newline at end of file
66
+logs: [{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 },{ a: 1, b: 2 }]
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/console-readonly.js
+1
@@ -9,6 +9,7 @@ function Component(props) {
9
console.error(x);
10
console.trace(x);
11
console.table(x);
12
+ global.console.log(x);
13
return x;
14
}
15
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/preserve-memo-validation/useMemo-infer-nonallocating.expect.md
+2
-12
@@ -22,25 +22,15 @@ export const FIXTURE_ENTRYPOINT = {
22
## Code
23
24
```javascript
25
-import { c as _c } from "react/compiler-runtime"; // @validatePreserveExistingMemoizationGuarantees
25
+// @validatePreserveExistingMemoizationGuarantees
26
27
import { useMemo } from "react";
28
29
// It's correct to infer a useMemo value is non-allocating
30
// and not provide it with a reactive scope
31
function useFoo(num1, num2) {
32
- const $ = _c(3);
32
let t0;
34
- let t1;
35
- if ($[0] !== num1 || $[1] !== num2) {
36
- t1 = Math.min(num1, num2);
37
- $[0] = num1;
38
- $[1] = num2;
39
- $[2] = t1;
40
- } else {
41
- t1 = $[2];
42
- }
43
- t0 = t1;
33
+ t0 = Math.min(num1, num2);
34
return t0;
35
}
36