[compiler][ez] Add shape for global Object.keys (#31583)
Add shape / type for global Object.keys. This is useful because - it has an Effect.Read (not an Effect.Capture) as it cannot alias its argument. - Object.keys return an array --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/facebook/react/pull/31583). * __->__ #31583 * #31582
mofeiZ committed
Dec 16, 2024 at 16:45 UTC
8a7b30669aaa6f020494ba143d40fb396521e7a7
3 files changed
+75
compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts
+15
@@ -88,6 +88,21 @@ const UNTYPED_GLOBALS: Set<string> = new Set([
88
]);
89
90
const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
91
+ [
92
+ 'Object',
93
+ addObject(DEFAULT_SHAPES, 'Object', [
94
+ [
95
+ 'keys',
96
+ addFunction(DEFAULT_SHAPES, [], {
97
+ positionalParams: [Effect.Read],
98
+ restParam: null,
99
+ returnType: {kind: 'Object', shapeId: BuiltInArrayId},
100
+ calleeEffect: Effect.Read,
101
+ returnValueKind: ValueKind.Mutable,
102
+ }),
103
+ ],
104
+ ]),
105
+ ],
106
[
107
'Array',
108
addObject(DEFAULT_SHAPES, 'Array', [
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/shapes-object-key.expect.md
new
+49
@@ -0,0 +1,49 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import {arrayPush} from 'shared-runtime';
6
+
7
+function useFoo({a, b}) {
8
+ const obj = {a};
9
+ arrayPush(Object.keys(obj), b);
10
+ return obj;
11
+}
12
+export const FIXTURE_ENTRYPOINT = {
13
+ fn: useFoo,
14
+ params: [{a: 2, b: 3}],
15
+};
16
+
17
+```
18
+
19
+## Code
20
+
21
+```javascript
22
+import { c as _c } from "react/compiler-runtime";
23
+import { arrayPush } from "shared-runtime";
24
+
25
+function useFoo(t0) {
26
+ const $ = _c(2);
27
+ const { a, b } = t0;
28
+ let t1;
29
+ if ($[0] !== a) {
30
+ t1 = { a };
31
+ $[0] = a;
32
+ $[1] = t1;
33
+ } else {
34
+ t1 = $[1];
35
+ }
36
+ const obj = t1;
37
+ arrayPush(Object.keys(obj), b);
38
+ return obj;
39
+}
40
+
41
+export const FIXTURE_ENTRYPOINT = {
42
+ fn: useFoo,
43
+ params: [{ a: 2, b: 3 }],
44
+};
45
+
46
+```
47
+
48
+### Eval output
49
+(kind: ok) {"a":2}
\ No newline at end of file
compiler/packages/babel-plugin-react-compiler/src/__tests__/fixtures/compiler/shapes-object-key.ts
new
+11
@@ -0,0 +1,11 @@
1
+import {arrayPush} from 'shared-runtime';
2
+
3
+function useFoo({a, b}) {
4
+ const obj = {a};
5
+ arrayPush(Object.keys(obj), b);
6
+ return obj;
7
+}
8
+export const FIXTURE_ENTRYPOINT = {
9
+ fn: useFoo,
10
+ params: [{a: 2, b: 3}],
11
+};