Emit type and range errors

This commit is contained in:
Andrew Morris
2023-03-21 16:44:41 +11:00
parent e657604cbe
commit feeb0594de
22 changed files with 382 additions and 79 deletions

View File

@@ -1,10 +1,10 @@
use std::rc::Rc;
use crate::format_err;
use crate::stack_frame::FrameStepResult;
use crate::stack_frame::{CallResult, FrameStepOk, StackFrameTrait};
use crate::vs_array::VsArray;
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
use crate::{builtins::type_error_builtin::to_type_error, type_error};
pub trait ArrayMappingState {
fn process(&mut self, i: usize, element: &Val, mapped: Val) -> Option<Val>;
@@ -58,7 +58,7 @@ impl StackFrameTrait for ArrayMappingFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return format_err!("TypeError: Array fn called on non-array"),
None => return type_error!("Array fn called on non-array"),
Some(ad) => ad,
};
@@ -79,7 +79,7 @@ impl StackFrameTrait for ArrayMappingFrame {
Val::Void => Ok(FrameStepOk::Continue),
_ => match self.mapper.load_function() {
LoadFunctionResult::NotAFunction => {
format_err!("TypeError: map fn is not a function")
type_error!("map fn is not a function")
}
LoadFunctionResult::NativeFunction(native_fn) => {
match self.state.process(
@@ -122,7 +122,7 @@ impl StackFrameTrait for ArrayMappingFrame {
let element = match &self.this {
None => {
self.early_exit = Some(format_err!("TypeError: Array fn called on non-array"));
self.early_exit = Some(type_error!("Array fn called on non-array"));
return;
}
Some(ad) => &ad.elements[array_i],

View File

@@ -1,10 +1,10 @@
use std::rc::Rc;
use crate::format_err;
use crate::native_frame_function::NativeFrameFunction;
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrameTrait};
use crate::vs_array::VsArray;
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
use crate::{builtins::type_error_builtin::to_type_error, type_error};
pub static REDUCE: NativeFrameFunction = NativeFrameFunction {
make_frame: || {
@@ -48,7 +48,7 @@ impl StackFrameTrait for ReduceFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return format_err!("TypeError: reduce called on non-array"),
None => return type_error!("reduce called on non-array"),
Some(ad) => ad,
};
@@ -64,9 +64,7 @@ impl StackFrameTrait for ReduceFrame {
FrameStepOk::Continue
}
Some(value) => match self.reducer.load_function() {
LoadFunctionResult::NotAFunction => {
return format_err!("TypeError: reduce fn is not a function")
}
LoadFunctionResult::NotAFunction => return type_error!("reduce fn is not a function"),
LoadFunctionResult::NativeFunction(native_fn) => {
self.value = Some(native_fn(
&mut Val::Undefined,
@@ -91,7 +89,7 @@ impl StackFrameTrait for ReduceFrame {
},
},
None => match &self.value {
None => return format_err!("TypeError: reduce of empty array with no initial value"),
None => return type_error!("reduce of empty array with no initial value"),
Some(value) => FrameStepOk::Pop(CallResult {
return_: value.clone(),
this: Val::Array(array_data.clone()),

View File

@@ -1,10 +1,10 @@
use std::rc::Rc;
use crate::format_err;
use crate::native_frame_function::NativeFrameFunction;
use crate::stack_frame::{CallResult, FrameStepOk, FrameStepResult, StackFrameTrait};
use crate::vs_array::VsArray;
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
use crate::{builtins::type_error_builtin::to_type_error, format_err, type_error};
pub static REDUCE_RIGHT: NativeFrameFunction = NativeFrameFunction {
make_frame: || {
@@ -53,14 +53,14 @@ impl StackFrameTrait for ReduceRightFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return format_err!("TypeError: reduceRight called on non-array"),
None => return type_error!("reduceRight called on non-array"),
Some(ad) => ad,
};
if self.array_i == 0 {
match &self.value {
None => {
return format_err!("TypeError: reduceRight of empty array with no initial value");
return type_error!("reduceRight of empty array with no initial value");
}
Some(value) => {
return Ok(FrameStepOk::Pop(CallResult {

View File

@@ -1,11 +1,11 @@
use std::rc::Rc;
use crate::format_err;
use crate::native_frame_function::NativeFrameFunction;
use crate::stack_frame::FrameStepResult;
use crate::stack_frame::{CallResult, FrameStepOk, StackFrameTrait};
use crate::vs_array::VsArray;
use crate::vs_value::{LoadFunctionResult, Val, ValTrait};
use crate::{builtins::type_error_builtin::to_type_error, type_error};
pub static SORT: NativeFrameFunction = NativeFrameFunction {
make_frame: || {
@@ -223,7 +223,7 @@ impl StackFrameTrait for SortFrame {
fn step(&mut self) -> FrameStepResult {
if !self.started {
let array_data = match &mut self.this {
None => return format_err!("TypeError: array fn called on non-array"),
None => return type_error!("array fn called on non-array"),
Some(ad) => ad,
};
@@ -268,7 +268,7 @@ impl StackFrameTrait for SortFrame {
},
Some((left, right)) => match self.comparator.load_function() {
LoadFunctionResult::NotAFunction => {
return format_err!("TypeError: comparator is not a function");
return type_error!("comparator is not a function");
}
LoadFunctionResult::NativeFunction(native_fn) => {
let res = native_fn(&mut Val::Undefined, vec![left, right])?.to_number();