Skip to content

Commit

Permalink
feat: save code
Browse files Browse the repository at this point in the history
  • Loading branch information
GrinZero committed Jun 28, 2024
1 parent 27e9476 commit 689ff4f
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export function addImportsToProgram(
): void {
const identifiers: Set<string> = new Set();
const sortedImports: Map<string, Array<string>> = new Map();

console.log('importList',importList)
for (const { importSpecifierName, source } of importList) {
/*
* Codegen currently does not rename import specifiers, so we do additional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,65 +156,86 @@ export function codegenFunction(
// HMR detection is enabled, emit code to reset the memo cache on source changes
const index = cx.synthesizeName("$i");
preface.push(
t.ifStatement(
t.binaryExpression(
"!==",
t.memberExpression(
t.identifier(cx.synthesizeName("$")),
t.numericLiteral(fastRefreshState.cacheIndex),
true
),
t.stringLiteral(fastRefreshState.hash)
),
t.blockStatement([
t.forStatement(
t.variableDeclaration("let", [
t.variableDeclarator(t.identifier(index), t.numericLiteral(0)),
t.variableDeclaration("const", [
t.variableDeclarator(
t.identifier(index),
t.logicalExpression(
"||",
t.callExpression(t.identifier("u"), [
t.identifier(cx.synthesizeName("$")),
t.arrayExpression([]),
t.arrayExpression([]),
t.arrayExpression([]),
]),
t.binaryExpression(
"<",
t.identifier(index),
t.numericLiteral(cacheCount)
),
t.assignmentExpression(
"+=",
t.identifier(index),
t.numericLiteral(1)
),
t.blockStatement([
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(
t.identifier(cx.synthesizeName("$")),
t.identifier(index),
true
),
t.callExpression(
t.memberExpression(
t.identifier("Symbol"),
t.identifier("for")
),
[t.stringLiteral(MEMO_CACHE_SENTINEL)]
)
)
),
])
),
t.expressionStatement(
t.assignmentExpression(
"=",
t.memberExpression(
t.identifier(cx.synthesizeName("$")),
t.numericLiteral(fastRefreshState.cacheIndex),
true
),
t.stringLiteral(fastRefreshState.hash)
t.memberExpression(
t.identifier(cx.synthesizeName("$")),
t.numericLiteral(fastRefreshState.cacheIndex),
true
)
),
])
)
)
),
])
);
// preface.push(

Check failure on line 179 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Expected a block comment instead of consecutive line comments
// t.ifStatement(
// t.binaryExpression(
// "!==",
// t.memberExpression(
// t.identifier(cx.synthesizeName("$")),
// t.numericLiteral(fastRefreshState.cacheIndex),
// true
// ),
// t.stringLiteral(fastRefreshState.hash)
// ),
// t.blockStatement([
// t.forStatement(
// t.variableDeclaration("let", [
// t.variableDeclarator(t.identifier(index), t.numericLiteral(0)),
// ]),
// t.binaryExpression(
// "<",
// t.identifier(index),
// t.numericLiteral(cacheCount)
// ),
// t.assignmentExpression(
// "+=",
// t.identifier(index),
// t.numericLiteral(1)
// ),
// t.blockStatement([
// t.expressionStatement(
// t.assignmentExpression(
// "=",
// t.memberExpression(
// t.identifier(cx.synthesizeName("$")),
// t.identifier(index),
// true
// ),
// t.callExpression(
// t.memberExpression(
// t.identifier("Symbol"),
// t.identifier("for")
// ),
// [t.stringLiteral(MEMO_CACHE_SENTINEL)]
// )
// )
// ),
// ])
// ),
// t.expressionStatement(
// t.assignmentExpression(
// "=",
// t.memberExpression(
// t.identifier(cx.synthesizeName("$")),
// t.numericLiteral(fastRefreshState.cacheIndex),
// true
// ),
// t.stringLiteral(fastRefreshState.hash)
// )
// ),
// ])
// )
// );
}
compiled.body.body.unshift(...preface);
}
Expand Down Expand Up @@ -274,7 +295,7 @@ function codegenReactiveFunction(
}

const params = fn.params.map((param) => convertParameter(param));
const body: t.BlockStatement = codegenBlock(cx, fn.body);
const body: t.BlockStatement = codegenBlock(cx, fn.body); // 生成块
body.directives = fn.directives.map((d) =>
t.directive(t.directiveLiteral(d))
);
Expand All @@ -291,6 +312,7 @@ function codegenReactiveFunction(
}

const countMemoBlockVisitor = new CountMemoBlockVisitor(fn.env);

visitReactiveFunction(fn, countMemoBlockVisitor, undefined);

return Ok({
Expand Down Expand Up @@ -400,6 +422,7 @@ class Context {
function codegenBlock(cx: Context, block: ReactiveBlock): t.BlockStatement {
const temp = new Map(cx.temp);
const result = codegenBlockNoReset(cx, block);

/*
* Check that the block only added new temporaries and did not update the
* value of any existing temporary
Expand Down Expand Up @@ -431,9 +454,11 @@ function codegenBlockNoReset(
block: ReactiveBlock
): t.BlockStatement {
const statements: Array<t.Statement> = [];
//TODO: hack to generate
for (const item of block) {
switch (item.kind) {
case "instruction": {
// like a = b + c
const statement = codegenInstructionNullable(cx, item.instruction);
if (statement !== null) {
statements.push(statement);
Expand All @@ -446,12 +471,14 @@ function codegenBlockNoReset(
break;
}
case "scope": {
// like if, for, while
const temp = new Map(cx.temp);
codegenReactiveScope(cx, statements, item.scope, item.instructions);
cx.temp = temp;
break;
}
case "terminal": {
// like break, continue, return
const statement = codegenTerminal(cx, item.terminal);
if (statement === null) {
break;
Expand Down Expand Up @@ -498,6 +525,7 @@ function wrapCacheDep(cx: Context, value: t.Expression): t.Expression {
}
}

const count: 0 | 1 = 1;
function codegenReactiveScope(
cx: Context,
statements: Array<t.Statement>,
Expand All @@ -514,6 +542,77 @@ function codegenReactiveScope(
const changeExpressions: Array<t.Expression> = [];
const changeExpressionComments: Array<string> = [];
const outputComments: Array<string> = [];

// #region taking

if (count === 1) {
const indices: t.NumericLiteral[] = [];

Check failure on line 549 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 't.NumericLiteral[]' is forbidden. Use 'Array<t.NumericLiteral>' instead
const newValues: t.Expression[] = [];

Check failure on line 550 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 't.Expression[]' is forbidden. Use 'Array<t.Expression>' instead
const todo: (t.Expression | null | undefined)[] = [];

Check failure on line 551 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 'T[]' is forbidden. Use 'Array<T>' instead

for (const dep of scope.dependencies) {
const index = cx.nextCacheIndex;
indices.push(t.numericLiteral(index));
newValues.push(codegenDependency(cx, dep));

todo.push(codegenDependency(cx, dep));
}

const arr: t.Identifier[] = [];

Check failure on line 561 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 't.Identifier[]' is forbidden. Use 'Array<t.Identifier>' instead
const index = cx.nextCacheIndex;
for (const [, { identifier }] of scope.declarations) {
const name = convertIdentifier(identifier);
arr.push(name);
cacheLoads.push({ name, index, value: wrapCacheDep(cx, name) });
}
for (const reassignment of scope.reassignments) {
const index = cx.nextCacheIndex;
const name = convertIdentifier(reassignment);
cacheLoads.push({ name, index, value: wrapCacheDep(cx, name) });
}
// qucik
let computationBlock = codegenBlock(cx, block);

const [variableDeclaration] = computationBlock.body;
if ("declarations" in variableDeclaration) {
todo.push(...variableDeclaration.declarations.map((item) => item.init));
}

const back: t.MemberExpression[] = [];

Check failure on line 581 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 't.MemberExpression[]' is forbidden. Use 'Array<t.MemberExpression>' instead
for (const { index, value } of cacheLoads) {
todo.push(value);
back.push(
t.memberExpression(
t.identifier(cx.synthesizeName("$")),
t.numericLiteral(index),
true
)
);
}

const elements = t.variableDeclaration("const", [
t.variableDeclarator(
t.arrayPattern(arr),
t.logicalExpression(
"||",
t.callExpression(t.identifier("u"), [
t.identifier(cx.synthesizeName("$")),
t.arrayExpression(indices),
t.arrayExpression(newValues),
t.arrayExpression(
todo.slice(0, -1).filter(Boolean) as t.Expression[]

Check failure on line 603 in compiler/packages/babel-plugin-react-compiler/src/ReactiveScopes/CodegenReactiveFunction.ts

View workflow job for this annotation

GitHub Actions / Lint babel-plugin-react-compiler

Array type using 't.Expression[]' is forbidden. Use 'Array<t.Expression>' instead
),
]),
t.arrayExpression(back)
)
),
]);

statements.push(elements);
return;
}
// #endregion

for (const dep of scope.dependencies) {
const index = cx.nextCacheIndex;
changeExpressionComments.push(printDependencyComment(dep));
Expand Down Expand Up @@ -556,6 +655,7 @@ function codegenReactiveScope(
)
);
}

let firstOutputIndex: number | null = null;
for (const [, { identifier }] of scope.declarations) {
const index = cx.nextCacheIndex;
Expand All @@ -574,6 +674,7 @@ function codegenReactiveScope(

const name = convertIdentifier(identifier);
outputComments.push(name.name);
// !!! const t4
if (!cx.hasDeclared(identifier)) {
statements.push(
t.variableDeclaration("let", [t.variableDeclarator(name)])
Expand Down Expand Up @@ -637,8 +738,8 @@ function codegenReactiveScope(
t.booleanLiteral(true)
);
}
let computationBlock = codegenBlock(cx, block);

let computationBlock = codegenBlock(cx, block);
let memoStatement;
if (
cx.env.config.enableChangeDetectionForDebugging != null &&
Expand Down Expand Up @@ -749,6 +850,7 @@ function codegenReactiveScope(
);
}
computationBlock.body.push(...cacheStoreStatements);

memoStatement = t.ifStatement(
testCondition,
computationBlock,
Expand Down Expand Up @@ -820,6 +922,11 @@ function codegenReactiveScope(
);
const name: ValidIdentifierName = earlyReturnValue.value.name.value;
statements.push(
/**
* if (name !== Symbol.for("EARLY_RETURN_SENTINEL")) {
* return name;
* }
*/
t.ifStatement(
t.binaryExpression(
"!==",
Expand Down Expand Up @@ -1119,6 +1226,7 @@ function codegenInstructionNullable(
cx: Context,
instr: ReactiveInstruction
): t.Statement | null {
// TODO: know
if (
instr.value.kind === "StoreLocal" ||
instr.value.kind === "StoreContext" ||
Expand Down Expand Up @@ -1176,6 +1284,7 @@ function codegenInstructionNullable(
}
value = codegenPlaceToExpression(cx, instr.value.value);
}

switch (kind) {
case InstructionKind.Const: {
CompilerError.invariant(instr.lvalue === null, {
Expand Down Expand Up @@ -1294,6 +1403,7 @@ function codegenForInit(
instruction,
}))
).body;

const declarators: Array<t.VariableDeclarator> = [];
let kind: "let" | "const" = "const";
body.forEach((instr) => {
Expand Down

0 comments on commit 689ff4f

Please sign in to comment.