Skip to content

Commit

Permalink
Mutate original object instead of creating a new one
Browse files Browse the repository at this point in the history
  • Loading branch information
tedbauer committed Dec 6, 2020
1 parent 0f52a2f commit 5bc4d60
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions bril-ts/brili.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,8 +713,7 @@ function evalInstr(instr: bril.Instruction, state: State): Action {
throw error(`unhandled opcode ${(instr as any).op}`);
}

function evalFunc(func: bril.Function, state_ref: State): Value | null {
let state = state_ref;
function evalFunc(func: bril.Function, state: State): Value | null {
for (let i = 0; i < func.instrs.length; ++i) {
let line = func.instrs[i];
if ('op' in line) {
Expand All @@ -729,11 +728,8 @@ function evalFunc(func: bril.Function, state_ref: State): Value | null {
}
case 'speculate': {
// Begin speculation.
state = {
...state,
env: new Map(state.env), // Clone the environment.
specparent: state, // Save current state for aborts.
};
state.specparent = {...state};
state.env = new Map(state.env);
break;
}
case 'commit': {
Expand All @@ -749,9 +745,10 @@ function evalFunc(func: bril.Function, state_ref: State): Value | null {
if (!state.specparent) {
throw error(`abort in non-speculative state`);
}
let icount = state.icount;
state = state.specparent;
state.icount = icount;
state.env = state.specparent.env;
state.lastlabel = state.specparent.lastlabel;
state.curlabel = state.specparent.curlabel;
state.specparent = state.specparent.specparent;
break;
}
case 'next':
Expand All @@ -761,7 +758,6 @@ function evalFunc(func: bril.Function, state_ref: State): Value | null {
unreachable(action);
throw error(`unhandled action ${(action as any).action}`);
}

// Move to a label.
if ('label' in action) {
// Search for the label and transfer control.
Expand All @@ -783,12 +779,6 @@ function evalFunc(func: bril.Function, state_ref: State): Value | null {
}
}

state_ref.env = state.env;
state_ref.icount = state.icount;
state_ref.lastlabel = state.lastlabel;
state_ref.curlabel = state.curlabel;
state_ref.specparent = state.specparent;

// Reached the end of the function without hitting `ret`.
if (state.specparent) {
throw error(`implicit return in speculative state`);
Expand Down

0 comments on commit 5bc4d60

Please sign in to comment.