From be82548cbb5279019c582676b511961e71de9877 Mon Sep 17 00:00:00 2001 From: Neta London Date: Sun, 10 Mar 2024 15:39:43 +0200 Subject: [PATCH] Error handling --- components/src/stores/vm.store.ts | 41 ++++++++++++++++++++++--------- web/src/pages/vm.tsx | 22 ++++++++++------- 2 files changed, 42 insertions(+), 21 deletions(-) diff --git a/components/src/stores/vm.store.ts b/components/src/stores/vm.store.ts index 63809a7e1..2b01e210e 100644 --- a/components/src/stores/vm.store.ts +++ b/components/src/stores/vm.store.ts @@ -69,18 +69,26 @@ export interface VmFile { function reduceVMTest( vmTest: VMTest, - dispatch: MutableRefObject + dispatch: MutableRefObject, + 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()), @@ -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; }, @@ -137,7 +145,7 @@ export function makeVmStore( }, }; const initialState: VmPageState = { - vm: reduceVMTest(test, dispatch), + vm: reduceVMTest(test, dispatch, setStatus), controls: { exitCode: undefined, runningTest: false, @@ -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 { @@ -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; } }, @@ -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; diff --git a/web/src/pages/vm.tsx b/web/src/pages/vm.tsx index b29ed2c9f..13af381a8 100644 --- a/web/src/pages/vm.tsx +++ b/web/src/pages/vm.tsx @@ -225,15 +225,19 @@ const VM = () => { /> VM Structures}> - - + {state.controls.valid && state.vm.Stack.length > 0 && ( + <> + + + + )} {runnersAssigned && vmRunner.current && (