More React API coverage

Redo of #2248 in its own stack
This commit is contained in:
Joe Savona
2023-11-06 16:27:11 -08:00
parent 72c27b6893
commit 337c17a66f
3 changed files with 127 additions and 1 deletions
@@ -312,9 +312,55 @@ const BUILTIN_HOOKS: Array<[string, BuiltInType]> = [
returnValueKind: ValueKind.Frozen,
}),
],
[
"useInsertionEffect",
addHook(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
hookKind: "useLayoutEffect",
returnValueKind: ValueKind.Frozen,
}),
],
];
TYPED_GLOBALS.push(["React", addObject(DEFAULT_SHAPES, null, BUILTIN_HOOKS)]);
TYPED_GLOBALS.push([
"React",
addObject(DEFAULT_SHAPES, null, [
...BUILTIN_HOOKS,
[
"createElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"cloneElement",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Freeze,
returnType: { kind: "Poly" },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Frozen,
}),
],
[
"createRef",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Capture, // createRef takes no paramters
returnType: { kind: "Object", shapeId: BuiltInUseRefId },
calleeEffect: Effect.Read,
returnValueKind: ValueKind.Mutable,
}),
],
]),
]);
export type Global = BuiltInType | PolyType;
export type GlobalRegistry = Map<string, Global>;
@@ -0,0 +1,66 @@
## Input
```javascript
import React from "react";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const childProps = { style: { width: props.width } };
const element = React.createElement("div", childProps, ["hello world"]);
shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
import React from "react";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const $ = useMemoCache(5);
let t0;
if ($[0] !== props.width) {
t0 = { style: { width: props.width } };
$[0] = props.width;
$[1] = t0;
} else {
t0 = $[1];
}
const childProps = t0;
let t2;
if ($[2] !== childProps) {
let t1;
if ($[4] === Symbol.for("react.memo_cache_sentinel")) {
t1 = ["hello world"];
$[4] = t1;
} else {
t1 = $[4];
}
t2 = React.createElement("div", childProps, t1);
$[2] = childProps;
$[3] = t2;
} else {
t2 = $[3];
}
const element = t2;
shallowCopy(childProps);
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};
```
@@ -0,0 +1,14 @@
import React from "react";
import { shallowCopy } from "shared-runtime";
function Component(props) {
const childProps = { style: { width: props.width } };
const element = React.createElement("div", childProps, ["hello world"]);
shallowCopy(childProps); // function that in theory could mutate, we assume not bc createElement freezes
return element;
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};