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.
Joe Savona committed
May 18, 2023 at 15:52 UTC
a325eed4727af4f2d567fecc8f33926e2c0fc055
7 files changed
+168
compiler/forget/src/HIR/Globals.ts
+27
@@ -187,6 +187,33 @@ const TYPED_GLOBALS: Array<[string, BuiltInType]> = [
187
],
188
]),
189
],
190
+ [
191
+ "Boolean",
192
+ addFunction(DEFAULT_SHAPES, [], {
193
+ positionalParams: [],
194
+ restParam: Effect.Read,
195
+ returnType: { kind: "Primitive" },
196
+ calleeEffect: Effect.Read,
197
+ }),
198
+ ],
199
+ [
200
+ "Number",
201
+ addFunction(DEFAULT_SHAPES, [], {
202
+ positionalParams: [],
203
+ restParam: Effect.Read,
204
+ returnType: { kind: "Primitive" },
205
+ calleeEffect: Effect.Read,
206
+ }),
207
+ ],
208
+ [
209
+ "String",
210
+ addFunction(DEFAULT_SHAPES, [], {
211
+ positionalParams: [],
212
+ restParam: Effect.Read,
213
+ returnType: { kind: "Primitive" },
214
+ calleeEffect: Effect.Read,
215
+ }),
216
+ ],
217
// TODO: rest of Global objects
218
];
219
compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const x = {};
7
+ const y = Boolean(x);
8
+ return [x, y];
9
+}
10
+
11
+```
12
+
13
+## Code
14
+
15
+```javascript
16
+import { unstable_useMemoCache as useMemoCache } from "react";
17
+function Component(props) {
18
+ const $ = useMemoCache(3);
19
+ let t0;
20
+ let x;
21
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
22
+ x = {};
23
+ t0 = Boolean(x);
24
+ $[0] = t0;
25
+ $[1] = x;
26
+ } else {
27
+ t0 = $[0];
28
+ x = $[1];
29
+ }
30
+ const y = t0;
31
+ let t1;
32
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
33
+ t1 = [x, y];
34
+ $[2] = t1;
35
+ } else {
36
+ t1 = $[2];
37
+ }
38
+ return t1;
39
+}
40
+
41
+```
42
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.js
new
+5
@@ -0,0 +1,5 @@
1
+function Component(props) {
2
+ const x = {};
3
+ const y = Boolean(x);
4
+ return [x, y];
5
+}
compiler/forget/src/__tests__/fixtures/compiler/globals-Number.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const x = {};
7
+ const y = Number(x);
8
+ return [x, y];
9
+}
10
+
11
+```
12
+
13
+## Code
14
+
15
+```javascript
16
+import { unstable_useMemoCache as useMemoCache } from "react";
17
+function Component(props) {
18
+ const $ = useMemoCache(3);
19
+ let t0;
20
+ let x;
21
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
22
+ x = {};
23
+ t0 = Number(x);
24
+ $[0] = t0;
25
+ $[1] = x;
26
+ } else {
27
+ t0 = $[0];
28
+ x = $[1];
29
+ }
30
+ const y = t0;
31
+ let t1;
32
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
33
+ t1 = [x, y];
34
+ $[2] = t1;
35
+ } else {
36
+ t1 = $[2];
37
+ }
38
+ return t1;
39
+}
40
+
41
+```
42
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/globals-Number.js
new
+5
@@ -0,0 +1,5 @@
1
+function Component(props) {
2
+ const x = {};
3
+ const y = Number(x);
4
+ return [x, y];
5
+}
compiler/forget/src/__tests__/fixtures/compiler/globals-String.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(props) {
6
+ const x = {};
7
+ const y = String(x);
8
+ return [x, y];
9
+}
10
+
11
+```
12
+
13
+## Code
14
+
15
+```javascript
16
+import { unstable_useMemoCache as useMemoCache } from "react";
17
+function Component(props) {
18
+ const $ = useMemoCache(3);
19
+ let t0;
20
+ let x;
21
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
22
+ x = {};
23
+ t0 = String(x);
24
+ $[0] = t0;
25
+ $[1] = x;
26
+ } else {
27
+ t0 = $[0];
28
+ x = $[1];
29
+ }
30
+ const y = t0;
31
+ let t1;
32
+ if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
33
+ t1 = [x, y];
34
+ $[2] = t1;
35
+ } else {
36
+ t1 = $[2];
37
+ }
38
+ return t1;
39
+}
40
+
41
+```
42
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/globals-String.js
new
+5
@@ -0,0 +1,5 @@
1
+function Component(props) {
2
+ const x = {};
3
+ const y = String(x);
4
+ return [x, y];
5
+}