Skip to content

Commit

Permalink
Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
netalondon committed Mar 10, 2024
1 parent 99e14dc commit be82548
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
41 changes: 29 additions & 12 deletions components/src/stores/vm.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,26 @@ export interface VmFile {

function reduceVMTest(
vmTest: VMTest,
dispatch: MutableRefObject<VmStoreDispatch>
dispatch: MutableRefObject<VmStoreDispatch>,
setStatus: (status: string) => void
): VmSim {
const RAM = new ImmMemory(vmTest.vm.RAM, dispatch);
const Screen = new ImmMemory(vmTest.vm.Screen, dispatch);
const Keyboard = new MemoryKeyboard(new ImmMemory(vmTest.vm.RAM, dispatch));
const highlight = vmTest.vm.derivedLine();

let stack: VmFrame[] = [];
try {
stack = vmTest.vm.vmStack().reverse();
} catch (e) {
setStatus("Runtime error: Invalid stack");
}

return {
Keyboard,
RAM,
Screen,
Stack: vmTest.vm.vmStack().reverse(),
Stack: stack,
Prog: vmTest.vm.program,
Statics: [
...vmTest.vm.memory.map((_, v) => v, 16, 16 + vmTest.vm.getStaticCount()),
Expand Down Expand Up @@ -117,7 +125,7 @@ export function makeVmStore(
state.controls.valid = valid;
},
update(state: VmPageState) {
state.vm = reduceVMTest(test, dispatch);
state.vm = reduceVMTest(test, dispatch, setStatus);
state.test.useTest = useTest;
state.test.highlight = test.currentStep?.span;
},
Expand All @@ -137,7 +145,7 @@ export function makeVmStore(
},
};
const initialState: VmPageState = {
vm: reduceVMTest(test, dispatch),
vm: reduceVMTest(test, dispatch, setStatus),
controls: {
exitCode: undefined,
runningTest: false,
Expand Down Expand Up @@ -250,15 +258,22 @@ export function makeVmStore(
dispatch.current({ action: "setAnimate", payload: value });
},
testStep() {
const done = test.step();
dispatch.current({ action: "testStep" });
if (done) {
dispatch.current({ action: "testFinished" });
}
if (animate) {
dispatch.current({ action: "update" });
let done = false;
try {
done = test.step();
dispatch.current({ action: "testStep" });
if (done) {
dispatch.current({ action: "testFinished" });
}
if (animate) {
dispatch.current({ action: "update" });
}
return done;
} catch (e) {
setStatus(`Runtime error: ${(e as Error).message}`);
dispatch.current({ action: "setValid", payload: false });
return true;
}
return done;
},
step() {
try {
Expand All @@ -276,6 +291,7 @@ export function makeVmStore(
return done;
} catch (e) {
setStatus(`Runtime error: ${(e as Error).message}`);
dispatch.current({ action: "setValid", payload: false });
return true;
}
},
Expand All @@ -284,6 +300,7 @@ export function makeVmStore(
vm.reset();
dispatch.current({ action: "update" });
dispatch.current({ action: "setExitCode", payload: undefined });
dispatch.current({ action: "setValid", payload: true });
},
toggleUseTest() {
useTest = !useTest;
Expand Down
22 changes: 13 additions & 9 deletions web/src/pages/vm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,19 @@ const VM = () => {
/>
</Panel>
<Panel className="stack" header={<Trans>VM Structures</Trans>}>
<VMStackFrame
statics={state.vm.Statics}
temp={state.vm.Temp}
frame={state.vm.Stack[0]}
/>
<CallStack
stack={state.vm.Stack}
addedSysInit={state.vm.AddedSysInit}
/>
{state.controls.valid && state.vm.Stack.length > 0 && (
<>
<VMStackFrame
statics={state.vm.Statics}
temp={state.vm.Temp}
frame={state.vm.Stack[0]}
/>
<CallStack
stack={state.vm.Stack}
addedSysInit={state.vm.AddedSysInit}
/>
</>
)}
</Panel>
<Panel className="display" style={{ gridArea: "display" }}>
{runnersAssigned && vmRunner.current && (
Expand Down

0 comments on commit be82548

Please sign in to comment.