fix repl error recovery
This commit is contained in:
parent
6ca8d05c66
commit
83fad9a68f
26
bin/repl
26
bin/repl
|
|
@ -144,19 +144,33 @@ async function repl() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
codeHistory.push(trimmed)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const compiler = new Compiler(trimmed, Object.keys(globals))
|
const compiler = new Compiler(trimmed, Object.keys(globals))
|
||||||
|
|
||||||
vm.appendBytecode(compiler.bytecode)
|
// Save VM state before appending bytecode, in case execution fails
|
||||||
|
const savedInstructions = [...vm.instructions]
|
||||||
|
const savedConstants = [...vm.constants]
|
||||||
|
const savedPc = vm.pc
|
||||||
|
const savedScope = vm.scope
|
||||||
|
const savedStopped = vm.stopped
|
||||||
|
|
||||||
const result = await vm.continue()
|
try {
|
||||||
|
vm.appendBytecode(compiler.bytecode)
|
||||||
|
const result = await vm.continue()
|
||||||
|
|
||||||
console.log(`${colors.dim}=>${colors.reset} ${formatValue(result)}`)
|
codeHistory.push(trimmed)
|
||||||
|
console.log(`${colors.dim}=>${colors.reset} ${formatValue(result)}`)
|
||||||
|
} catch (error: any) {
|
||||||
|
vm.instructions = savedInstructions
|
||||||
|
vm.constants = savedConstants
|
||||||
|
vm.pc = savedPc
|
||||||
|
vm.scope = savedScope
|
||||||
|
vm.stopped = savedStopped
|
||||||
|
|
||||||
|
console.log(`\n${colors.red}Error:${colors.reset} ${error.message}`)
|
||||||
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.log(`\n${colors.red}Error:${colors.reset} ${error.message}`)
|
console.log(`\n${colors.red}Error:${colors.reset} ${error.message}`)
|
||||||
codeHistory.pop()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rl.prompt()
|
rl.prompt()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user