Define Boolean/Number/String globals

Defines the `Boolean`, `String`, and `Number` global functions. This will be 
useful for allowing developers to wrap statements that produce a primitive in a 
way that Forget knows about in order to optimize better.
This commit is contained in:
Joe Savona
2023-05-18 15:52:03 -07:00
parent 46417a4ed7
commit a325eed472
7 changed files with 168 additions and 0 deletions
+27
View File
@@ -187,6 +187,33 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
],
]),
],
[
"Boolean",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: { kind: "Primitive" },
calleeEffect: Effect.Read,
}),
],
[
"Number",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: { kind: "Primitive" },
calleeEffect: Effect.Read,
}),
],
[
"String",
addFunction(DEFAULT_SHAPES, [], {
positionalParams: [],
restParam: Effect.Read,
returnType: { kind: "Primitive" },
calleeEffect: Effect.Read,
}),
],
// TODO: rest of Global objects
];
@@ -0,0 +1,42 @@
## Input
```javascript
function Component(props) {
const x = {};
const y = Boolean(x);
return [x, y];
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
const $ = useMemoCache(3);
let t0;
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
x = {};
t0 = Boolean(x);
$[0] = t0;
$[1] = x;
} else {
t0 = $[0];
x = $[1];
}
const y = t0;
let t1;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t1 = [x, y];
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
```
@@ -0,0 +1,5 @@
function Component(props) {
const x = {};
const y = Boolean(x);
return [x, y];
}
@@ -0,0 +1,42 @@
## Input
```javascript
function Component(props) {
const x = {};
const y = Number(x);
return [x, y];
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
const $ = useMemoCache(3);
let t0;
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
x = {};
t0 = Number(x);
$[0] = t0;
$[1] = x;
} else {
t0 = $[0];
x = $[1];
}
const y = t0;
let t1;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t1 = [x, y];
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
```
@@ -0,0 +1,5 @@
function Component(props) {
const x = {};
const y = Number(x);
return [x, y];
}
@@ -0,0 +1,42 @@
## Input
```javascript
function Component(props) {
const x = {};
const y = String(x);
return [x, y];
}
```
## Code
```javascript
import { unstable_useMemoCache as useMemoCache } from "react";
function Component(props) {
const $ = useMemoCache(3);
let t0;
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
x = {};
t0 = String(x);
$[0] = t0;
$[1] = x;
} else {
t0 = $[0];
x = $[1];
}
const y = t0;
let t1;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
t1 = [x, y];
$[2] = t1;
} else {
t1 = $[2];
}
return t1;
}
```
@@ -0,0 +1,5 @@
function Component(props) {
const x = {};
const y = String(x);
return [x, y];
}