From a325eed4727af4f2d567fecc8f33926e2c0fc055 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Thu, 18 May 2023 15:52:03 -0700 Subject: [PATCH] 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. --- compiler/forget/src/HIR/Globals.ts | 27 ++++++++++++ .../compiler/globals-Boolean.expect.md | 42 +++++++++++++++++++ .../fixtures/compiler/globals-Boolean.js | 5 +++ .../compiler/globals-Number.expect.md | 42 +++++++++++++++++++ .../fixtures/compiler/globals-Number.js | 5 +++ .../compiler/globals-String.expect.md | 42 +++++++++++++++++++ .../fixtures/compiler/globals-String.js | 5 +++ 7 files changed, 168 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.js create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-Number.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-Number.js create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-String.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/globals-String.js diff --git a/compiler/forget/src/HIR/Globals.ts b/compiler/forget/src/HIR/Globals.ts index d1511fa5bb..f6d600772f 100644 --- a/compiler/forget/src/HIR/Globals.ts +++ b/compiler/forget/src/HIR/Globals.ts @@ -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 ]; diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.expect.md new file mode 100644 index 0000000000..642b273630 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.js b/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.js new file mode 100644 index 0000000000..b19c8ff7a0 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-Boolean.js @@ -0,0 +1,5 @@ +function Component(props) { + const x = {}; + const y = Boolean(x); + return [x, y]; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.expect.md new file mode 100644 index 0000000000..a8ce51aae2 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.js b/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.js new file mode 100644 index 0000000000..8831bb4a33 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-Number.js @@ -0,0 +1,5 @@ +function Component(props) { + const x = {}; + const y = Number(x); + return [x, y]; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-String.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/globals-String.expect.md new file mode 100644 index 0000000000..8955b4b326 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-String.expect.md @@ -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; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/globals-String.js b/compiler/forget/src/__tests__/fixtures/compiler/globals-String.js new file mode 100644 index 0000000000..d5e769807e --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/globals-String.js @@ -0,0 +1,5 @@ +function Component(props) { + const x = {}; + const y = String(x); + return [x, y]; +}