Skip to content

Commit

Permalink
Prefer InternalError over panicking
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Jun 28, 2023
1 parent 14f7ee2 commit a8dbbe6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
13 changes: 7 additions & 6 deletions valuescript_vm/src/bytecode_stack_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
Expand All @@ -287,15 +288,15 @@ 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());

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() {
Expand Down Expand Up @@ -485,7 +486,7 @@ impl StackFrameTrait for BytecodeStackFrame {
}

Import | ImportStar => {
panic!("TODO: Dynamic imports")
return Err("TODO: Dynamic imports".to_internal_error());
}

SetCatch => {
Expand All @@ -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();
Expand Down Expand Up @@ -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]);
Expand Down
6 changes: 4 additions & 2 deletions valuescript_vm/src/cat_stack_frame.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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_) => {
Expand Down
2 changes: 1 addition & 1 deletion valuescript_vm/src/virtual_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a8dbbe6

Please sign in to comment.