Skip to content

Commit

Permalink
[compiler][ez] Add shape for global Object.keys (facebook#31583)
Browse files Browse the repository at this point in the history
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).
* __->__ facebook#31583
* facebook#31582
  • Loading branch information
mofeiZ authored Dec 16, 2024
1 parent a78bbf9 commit 8a7b306
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
15 changes: 15 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/src/HIR/Globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ const UNTYPED_GLOBALS: Set<string> = new Set([
]);

const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
[
'Object',
addObject(DEFAULT_SHAPES, 'Object', [
[
'keys',
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [Effect.Read],
restParam: null,
returnType: {kind: 'Object', shapeId: BuiltInArrayId},
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Mutable,
}),
],
]),
],
[
'Array',
addObject(DEFAULT_SHAPES, 'Array', [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

## Input

```javascript
import {arrayPush} from 'shared-runtime';

function useFoo({a, b}) {
const obj = {a};
arrayPush(Object.keys(obj), b);
return obj;
}
export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [{a: 2, b: 3}],
};

```

## Code

```javascript
import { c as _c } from "react/compiler-runtime";
import { arrayPush } from "shared-runtime";

function useFoo(t0) {
const $ = _c(2);
const { a, b } = t0;
let t1;
if ($[0] !== a) {
t1 = { a };
$[0] = a;
$[1] = t1;
} else {
t1 = $[1];
}
const obj = t1;
arrayPush(Object.keys(obj), b);
return obj;
}

export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [{ a: 2, b: 3 }],
};

```
### Eval output
(kind: ok) {"a":2}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {arrayPush} from 'shared-runtime';

function useFoo({a, b}) {
const obj = {a};
arrayPush(Object.keys(obj), b);
return obj;
}
export const FIXTURE_ENTRYPOINT = {
fn: useFoo,
params: [{a: 2, b: 3}],
};

0 comments on commit 8a7b306

Please sign in to comment.