Add makeReadOnly codegen into the compiler

This commit is contained in:
Mofei Zhang
2022-10-03 14:37:23 -04:00
parent 72775fba10
commit 4e0639c2dc
4 changed files with 25 additions and 0 deletions
+13
View File
@@ -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
+5
View File
@@ -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);
}
+6
View File
@@ -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,