From a8dbbe63a7fc289a3ef7beb36c3f55cbbadb7c52 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Thu, 29 Jun 2023 09:00:27 +1000 Subject: [PATCH] Prefer InternalError over panicking --- valuescript_vm/src/bytecode_stack_frame.rs | 13 +++++++------ valuescript_vm/src/cat_stack_frame.rs | 6 ++++-- valuescript_vm/src/virtual_machine.rs | 2 +- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/valuescript_vm/src/bytecode_stack_frame.rs b/valuescript_vm/src/bytecode_stack_frame.rs index 39f0f8af..c9c10b8a 100644 --- a/valuescript_vm/src/bytecode_stack_frame.rs +++ b/valuescript_vm/src/bytecode_stack_frame.rs @@ -2,6 +2,7 @@ use std::mem::take; use valuescript_common::InstructionByte; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::type_error_builtin::ToTypeError; use crate::bytecode_decoder::BytecodeDecoder; use crate::bytecode_decoder::BytecodeType; @@ -272,7 +273,7 @@ impl StackFrameTrait for BytecodeStackFrame { return Ok(FrameStepOk::Push(new_frame)); } LoadFunctionResult::NativeFunction(_native_fn) => { - panic!("Not implemented"); + return Err("TODO: apply native functions".to_internal_error()); } } } @@ -287,7 +288,7 @@ impl StackFrameTrait for BytecodeStackFrame { if params_array.is_none() { // Not sure this needs to be an exception in future since compiled // code should never violate this - panic!("bind params should always be array") + return Err("bind params should always be array".to_internal_error()); } let bound_fn = fn_val.bind((*params_array.unwrap()).elements.clone()); @@ -295,7 +296,7 @@ impl StackFrameTrait for BytecodeStackFrame { if bound_fn.is_none() { // Not sure this needs to be an exception in future since compiled // code should never violate this - panic!("fn parameter of bind should always be bindable"); + return Err("fn parameter of bind should always be bindable".to_internal_error()); } if register_index.is_some() { @@ -485,7 +486,7 @@ impl StackFrameTrait for BytecodeStackFrame { } Import | ImportStar => { - panic!("TODO: Dynamic imports") + return Err("TODO: Dynamic imports".to_internal_error()); } SetCatch => { @@ -508,7 +509,7 @@ impl StackFrameTrait for BytecodeStackFrame { Next => { let iter_i = match self.decoder.decode_register_index() { Some(i) => i, - None => panic!("The ignore register is not iterable"), + None => return Err("The ignore register is not iterable".to_internal_error()), }; let res_i = self.decoder.decode_register_index(); @@ -540,7 +541,7 @@ impl StackFrameTrait for BytecodeStackFrame { UnpackIterRes => { let iter_res_i = match self.decoder.decode_register_index() { Some(i) => i, - None => panic!("Can't unpack the ignore register"), + None => return Err("Can't unpack the ignore register".to_internal_error()), }; let iter_res = take(&mut self.registers[iter_res_i]); diff --git a/valuescript_vm/src/cat_stack_frame.rs b/valuescript_vm/src/cat_stack_frame.rs index a2a9f62b..1ad25a8e 100644 --- a/valuescript_vm/src/cat_stack_frame.rs +++ b/valuescript_vm/src/cat_stack_frame.rs @@ -1,7 +1,7 @@ use std::{mem::take, rc::Rc}; use crate::{ - builtins::type_error_builtin::ToTypeError, + builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError}, native_function::ThisWrapper, operations::op_sub, stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrame, StackFrameTrait}, @@ -112,7 +112,9 @@ impl StackFrameTrait for CatStackFrame { match &mut self.state { CatFrameState::ReadNext => self.read_next(), - CatFrameState::MakingIterator => panic!("Unexpected step during MakingIterator"), + CatFrameState::MakingIterator => { + Err("Unexpected step during MakingIterator".to_internal_error()) + } CatFrameState::Iterating(iter) => match iter.sub(&"next".to_val())?.load_function() { LoadFunctionResult::NotAFunction => Err(".next was not a function".to_type_error()), LoadFunctionResult::NativeFunction(fn_) => { diff --git a/valuescript_vm/src/virtual_machine.rs b/valuescript_vm/src/virtual_machine.rs index 6bf20228..9d9b747f 100644 --- a/valuescript_vm/src/virtual_machine.rs +++ b/valuescript_vm/src/virtual_machine.rs @@ -26,7 +26,7 @@ impl VirtualMachine { let mut frame = match main_fn.load_function() { LoadFunctionResult::StackFrame(f) => f, - _ => panic!("bytecode does start with function"), + _ => return Err("bytecode does start with function".to_internal_error()), }; for p in params {