[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
This commit is contained in:
Mofei Zhang
2024-12-16 14:06:37 -05:00
parent 6240edf054
commit e68a6485e8
3 changed files with 75 additions and 0 deletions
@@ -87,6 +87,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', [
@@ -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}
@@ -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}],
};