mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Add makeReadOnly codegen into the compiler
This commit is contained in:
@@ -188,6 +188,19 @@ export class Func {
|
||||
]);
|
||||
}
|
||||
|
||||
// Generate calls to utility function that freezes immut objects
|
||||
emitMakeReadOnly(val: IR.BindingVal) {
|
||||
if (this.context.opts.flags.addFreeze === true) {
|
||||
this.code.push(
|
||||
t.expressionStatement(
|
||||
t.callExpression(t.identifier("useMemoCache.makeReadOnly"), [
|
||||
val.binding.identifier,
|
||||
])
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate code materializing reactions
|
||||
* @returns
|
||||
|
||||
@@ -85,6 +85,8 @@ export function runFunc(
|
||||
*/
|
||||
function genPrologue(): void {
|
||||
for (const param of irFunc.params) {
|
||||
// params are immut
|
||||
jsFunc.emitMakeReadOnly(param);
|
||||
if (IR.isReactiveVal(param)) {
|
||||
jsFunc.emitReactiveVal(param);
|
||||
}
|
||||
@@ -102,6 +104,9 @@ export function runFunc(
|
||||
jsFunc.emit(instr.ast.node);
|
||||
|
||||
for (const decl of instr.ir.decls) {
|
||||
if (decl.immutable) {
|
||||
jsFunc.emitMakeReadOnly(decl);
|
||||
}
|
||||
if (IR.isReactiveVal(decl)) {
|
||||
jsFunc.emitReactiveVal(decl);
|
||||
}
|
||||
|
||||
@@ -81,6 +81,10 @@ export type CompilerFlags = {
|
||||
* can remove this flag.
|
||||
*/
|
||||
guardThrows: boolean;
|
||||
/**
|
||||
* Experimental runtime logging to collect data
|
||||
*/
|
||||
addFreeze: boolean;
|
||||
};
|
||||
|
||||
export function createCompilerFlags(): CompilerFlags {
|
||||
@@ -95,6 +99,7 @@ export function createCompilerFlags(): CompilerFlags {
|
||||
guardReads: false,
|
||||
guardHooks: false,
|
||||
guardThrows: false,
|
||||
addFreeze: false,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -115,6 +120,7 @@ export function parseCompilerFlags(
|
||||
case "guardReads":
|
||||
case "guardHooks":
|
||||
case "guardThrows":
|
||||
case "addFreeze":
|
||||
if (typeof value !== "boolean") {
|
||||
throw `Expected boolean for flag '${key}': ${value}`;
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ describe("CompilerOptions", () => {
|
||||
const fullInput = {
|
||||
outputKinds: [OutputKind.JS, OutputKind.LIR],
|
||||
flags: {
|
||||
addFreeze: true,
|
||||
condCache: true,
|
||||
guardReads: true,
|
||||
guardHooks: true,
|
||||
|
||||
Reference in New Issue
Block a user