Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { c as _c } from "react/compiler-runtime";
export default function MyApp() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = <div>Hello World</div>;
$[0] = t0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { c as _c } from "react/compiler-runtime"; // @compilationMode:"
function nonReactFn() {
  const $ = _c(1);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
  if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
    t0 = {};
    $[0] = t0;
  } else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { c as _c } from "react/compiler-runtime";
function Foo() {
  const $ = _c(2);
  let t0;
  if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
  if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
    t0 = foo();
    $[0] = t0;
  } else {
    t0 = $[0];
  }
  const x = t0 as number;
  let t1;
  if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
  if ($[1] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
    t1 = <div>{x}</div>;
    $[1] = t1;
  } else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,18 @@ function shouldVisit(fn: BabelFn): boolean {
}

function buildTypeOfReactForget(): t.Statement {
// typeof globalThis[Symbol.for("react_forget")]
// typeof globalThis[globalThis.Symbol.for("react_forget")]
return t.expressionStatement(
t.unaryExpression(
'typeof',
t.memberExpression(
t.identifier('globalThis'),
t.callExpression(
t.memberExpression(
t.identifier('Symbol'),
t.memberExpression(
t.identifier('globalThis'),
t.identifier('Symbol'),
),
t.identifier('for'),
false,
false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,28 @@ function createSymbolProperty(
propertyName: string,
symbolName: string,
): ObjectProperty {
const globalThisPlace = createTemporaryPlace(fn.env, instr.value.loc);
const globalThisInstruction: Instruction = {
id: makeInstructionId(0),
lvalue: {...globalThisPlace, effect: Effect.Read},
value: {
kind: 'LoadGlobal',
binding: {kind: 'Global', name: 'globalThis'},
loc: instr.value.loc,
},
effects: null,
loc: instr.loc,
};
nextInstructions.push(globalThisInstruction);

const symbolPlace = createTemporaryPlace(fn.env, instr.value.loc);
const symbolInstruction: Instruction = {
id: makeInstructionId(0),
lvalue: {...symbolPlace, effect: Effect.Mutate},
lvalue: {...symbolPlace, effect: Effect.Read},
value: {
kind: 'LoadGlobal',
binding: {kind: 'Global', name: 'Symbol'},
kind: 'PropertyLoad',
object: {...globalThisInstruction.lvalue},
property: makePropertyLiteral('Symbol'),
loc: instr.value.loc,
},
effects: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,10 @@ export function codegenFunction(
),
t.callExpression(
t.memberExpression(
t.identifier('Symbol'),
t.memberExpression(
t.identifier('globalThis'),
t.identifier('Symbol'),
),
t.identifier('for'),
),
[t.stringLiteral(MEMO_CACHE_SENTINEL)],
Expand Down Expand Up @@ -748,7 +751,13 @@ function codegenReactiveScope(
true,
),
t.callExpression(
t.memberExpression(t.identifier('Symbol'), t.identifier('for')),
t.memberExpression(
t.memberExpression(
t.identifier('globalThis'),
t.identifier('Symbol'),
),
t.identifier('for'),
),
[t.stringLiteral(MEMO_CACHE_SENTINEL)],
),
);
Expand Down Expand Up @@ -968,7 +977,13 @@ function codegenReactiveScope(
'!==',
t.identifier(name),
t.callExpression(
t.memberExpression(t.identifier('Symbol'), t.identifier('for')),
t.memberExpression(
t.memberExpression(
t.identifier('globalThis'),
t.identifier('Symbol'),
),
t.identifier('for'),
),
[t.stringLiteral(EARLY_RETURN_SENTINEL)],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ import {ReactiveFunctionTransform, Transformed} from './visitors';
* ```
* let t0;
* if (props.cond !== $[0]) {
* t0 = Symbol.for('react.memo_cache_sentinel');
* t0 = globalThis.Symbol.for('react.memo_cache_sentinel');
* bb0: {
* let x = [];
* if (props.cond) {
Expand All @@ -77,7 +77,7 @@ import {ReactiveFunctionTransform, Transformed} from './visitors';
* break bb0;
* } else {
* let t1;
* if ($[1] === Symbol.for('react.memo_cache_sentinel')) {
* if ($[1] === globalThis.Symbol.for('react.memo_cache_sentinel')) {
* t1 = foo();
* $[1] = t1;
* } else {
Expand All @@ -93,7 +93,7 @@ import {ReactiveFunctionTransform, Transformed} from './visitors';
* t0 = $[2];
* }
* // This part added in CodegenReactiveScope:
* if (t0 !== Symbol.for('react.memo_cache_sentinel')) {
* if (t0 !== globalThis.Symbol.for('react.memo_cache_sentinel')) {
* return t0;
* }
* ```
Expand Down Expand Up @@ -161,6 +161,7 @@ class Transform extends ReactiveFunctionTransform<State> {
const instructions = scopeBlock.instructions;
const loc = earlyReturnValue.loc;
const sentinelTemp = createTemporaryPlace(this.env, loc);
const globalThisTemp = createTemporaryPlace(this.env, loc);
const symbolTemp = createTemporaryPlace(this.env, loc);
const forTemp = createTemporaryPlace(this.env, loc);
const argTemp = createTemporaryPlace(this.env, loc);
Expand All @@ -170,17 +171,31 @@ class Transform extends ReactiveFunctionTransform<State> {
instruction: {
id: makeInstructionId(0),
loc,
lvalue: {...symbolTemp},
lvalue: {...globalThisTemp},
value: {
kind: 'LoadGlobal',
binding: {
kind: 'Global',
name: 'Symbol',
name: 'globalThis',
},
loc,
},
},
},
{
kind: 'instruction',
instruction: {
id: makeInstructionId(0),
loc,
lvalue: {...symbolTemp},
value: {
kind: 'PropertyLoad',
object: {...globalThisTemp},
property: makePropertyLiteral('Symbol'),
loc,
},
},
},
{
kind: 'instruction',
instruction: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { makeObject_Primitives, mutate } from "shared-runtime";
function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
const a = makeObject_Primitives();
const x = [];
x.push(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import { c as _c } from "react/compiler-runtime";
function Component() {
const $ = _c(2);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = someObj();
$[0] = t0;
} else {
t0 = $[0];
}
const a = t0;
let t1;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
if ($[1] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
const x = [];
x.push(a);
t1 = [x, a];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { c as _c } from "react/compiler-runtime";
function component() {
const $ = _c(1);
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
const z = [];
const y = {};
y.z = z;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { c as _c } from "react/compiler-runtime";
function component() {
const $ = _c(1);
let x;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
const z = [];
const y = {};
y.z = z;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function useFoo(cond) {
let s;
let t0;
if ($[0] !== cond) {
t0 = Symbol.for("react.early_return_sentinel");
t0 = globalThis.Symbol.for("react.early_return_sentinel");
bb0: {
if (cond) {
s = {};
Expand All @@ -59,7 +59,7 @@ function useFoo(cond) {
t0 = $[1];
s = $[2];
}
if (t0 !== Symbol.for("react.early_return_sentinel")) {
if (t0 !== globalThis.Symbol.for("react.early_return_sentinel")) {
return t0;
}
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function useFoo(t0) {
let s;
let t1;
if ($[0] !== cond1 || $[1] !== cond2) {
t1 = Symbol.for("react.early_return_sentinel");
t1 = globalThis.Symbol.for("react.early_return_sentinel");
bb0: {
if (cond1) {
s = {};
Expand All @@ -142,7 +142,7 @@ function useFoo(t0) {
t1 = $[2];
s = $[3];
}
if (t1 !== Symbol.for("react.early_return_sentinel")) {
if (t1 !== globalThis.Symbol.for("react.early_return_sentinel")) {
return t1;
}
return s;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function useFoo(t0) {
const $ = _c(3);
const { cond } = t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t1 = {};
$[0] = t1;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Foo() {
const $ = _c(1);
try {
let thing;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
thing = null;
if (cond) {
thing = makeObject_Primitives();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function Component(props) {
const $ = _c(1);
const ref = useRef(props.value);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
const object = {};
object.foo = () => ref.current;
t0 = <Stringify object={object} shouldInvokeFns={true} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function Component() {
const $ = _c(1);
const foo = _temp;
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = <div {...foo} />;
$[0] = t0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function Component() {
const setGlobal = _temp;
let t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
setGlobal();
};
Expand All @@ -60,7 +60,7 @@ function Component() {
useEffect(t0, t1);
let t2;
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
if ($[2] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setState(someGlobal.value);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Component() {
const setGlobal = _temp;
let t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
setGlobal();
};
Expand All @@ -59,7 +59,7 @@ function Component() {
useEffect(t0, t1);
let t2;
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
if ($[2] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setState(someGlobal.value);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { useCallback, useEffect, useState } from "react";
function Component() {
const $ = _c(1);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = <div>Ok</div>;
$[0] = t0;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function Component() {
const setGlobal = _temp;
let t0;
let t1;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = () => {
setGlobal();
};
Expand All @@ -59,7 +59,7 @@ function Component() {
useEffect(t0, t1);
let t2;
let t3;
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
if ($[2] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t2 = () => {
setState(someGlobal);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function Component() {
const $ = _c(5);
const [state, setState] = useState(someGlobal);
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
if ($[0] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t0 = [];
$[0] = t0;
} else {
Expand All @@ -48,7 +48,7 @@ function Component() {
useEffect(_temp, t0);
let t1;
let t2;
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
if ($[1] === globalThis.Symbol.for("react.memo_cache_sentinel")) {
t1 = () => {
setState(someGlobal);
};
Expand Down
Loading