Skip to content

Commit ffe8e00

Browse files
committed
[compiler][ez] Add shape for global Object.keys
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
1 parent 22902de commit ffe8e00

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ const UNTYPED_GLOBALS: Set<string> = new Set([
8787
]);
8888

8989
const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
90+
[
91+
'Object',
92+
addObject(DEFAULT_SHAPES, 'Object', [
93+
[
94+
'keys',
95+
addFunction(DEFAULT_SHAPES, [], {
96+
positionalParams: [Effect.Read],
97+
restParam: null,
98+
returnType: {kind: 'Object', shapeId: BuiltInArrayId},
99+
calleeEffect: Effect.Read,
100+
returnValueKind: ValueKind.Mutable,
101+
}),
102+
],
103+
]),
104+
],
90105
[
91106
'Array',
92107
addObject(DEFAULT_SHAPES, 'Array', [
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
};

0 commit comments

Comments
 (0)