to_val refactor, remove error macros

This commit is contained in:
Andrew Morris
2023-05-26 11:54:43 +10:00
parent 80cd4deac8
commit ea42e94d5d
36 changed files with 632 additions and 585 deletions

View File

@@ -1,9 +1,8 @@
use std::rc::Rc;
use crate::vs_value::ToVal;
use super::super::vs_value::{Val, ValTrait};
use super::super::vs_array::VsArray;
use super::super::native_frame_function::NativeFrameFunction;
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
use super::super::vs_value::{Val, ValTrait};
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
pub static FILTER: NativeFrameFunction = NativeFrameFunction {
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FilterState::default()))),
@@ -26,6 +25,6 @@ impl ArrayMappingState for FilterState {
fn finish(&mut self) -> Val {
let mut filter_results = Vec::new();
std::mem::swap(&mut self.filter_results, &mut filter_results);
return Val::Array(Rc::new(VsArray::from(filter_results)));
filter_results.to_val()
}
}

View File

@@ -1,9 +1,8 @@
use std::rc::Rc;
use crate::vs_value::ToVal;
use super::super::vs_value::{Val, ValTrait};
use super::super::vs_array::VsArray;
use super::super::native_frame_function::NativeFrameFunction;
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
use super::super::vs_value::{Val, ValTrait};
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
pub static FLAT_MAP: NativeFrameFunction = NativeFrameFunction {
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(FlatMapState::default()))),
@@ -31,6 +30,6 @@ impl ArrayMappingState for FlatMapState {
fn finish(&mut self) -> Val {
let mut flat_map_results = Vec::new();
std::mem::swap(&mut self.flat_map_results, &mut flat_map_results);
return Val::Array(Rc::new(VsArray::from(flat_map_results)));
flat_map_results.to_val()
}
}

View File

@@ -1,9 +1,8 @@
use std::rc::Rc;
use crate::vs_value::ToVal;
use super::super::vs_value::{Val};
use super::super::vs_array::VsArray;
use super::super::native_frame_function::NativeFrameFunction;
use super::array_mapping_frame::{ArrayMappingState, ArrayMappingFrame};
use super::super::vs_value::Val;
use super::array_mapping_frame::{ArrayMappingFrame, ArrayMappingState};
pub static MAP: NativeFrameFunction = NativeFrameFunction {
make_frame: || Box::new(ArrayMappingFrame::new(Box::new(MapState::default()))),
@@ -23,6 +22,6 @@ impl ArrayMappingState for MapState {
fn finish(&mut self) -> Val {
let mut map_results = Vec::new();
std::mem::swap(&mut self.map_results, &mut map_results);
return Val::Array(Rc::new(VsArray::from(map_results)));
map_results.to_val()
}
}

View File

@@ -1,11 +1,11 @@
use std::rc::Rc;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::native_function::ThisWrapper;
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>;
@@ -60,7 +60,7 @@ impl StackFrameTrait for ArrayMappingFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return type_error!("Array fn called on non-array"),
None => return Err("Array fn called on non-array".to_type_error()),
Some(ad) => ad,
};
@@ -80,9 +80,7 @@ impl StackFrameTrait for ArrayMappingFrame {
Some(el) => match el {
Val::Void => Ok(FrameStepOk::Continue),
_ => match self.mapper.load_function() {
LoadFunctionResult::NotAFunction => {
type_error!("map fn is not a function")
}
LoadFunctionResult::NotAFunction => Err("map fn is not a function".to_type_error()),
LoadFunctionResult::NativeFunction(native_fn) => {
match self.state.process(
array_i,
@@ -124,7 +122,7 @@ impl StackFrameTrait for ArrayMappingFrame {
let element = match &self.this {
None => {
self.early_exit = Some(type_error!("Array fn called on non-array"));
self.early_exit = Some(Err("Array fn called on non-array".to_type_error()));
return;
}
Some(ad) => &ad.elements[array_i],

View File

@@ -1,11 +1,11 @@
use std::rc::Rc;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::native_frame_function::NativeFrameFunction;
use crate::native_function::ThisWrapper;
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: || {
@@ -50,7 +50,7 @@ impl StackFrameTrait for ReduceFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return type_error!("reduce called on non-array"),
None => return Err("reduce called on non-array".to_type_error()),
Some(ad) => ad,
};
@@ -66,7 +66,9 @@ impl StackFrameTrait for ReduceFrame {
FrameStepOk::Continue
}
Some(value) => match self.reducer.load_function() {
LoadFunctionResult::NotAFunction => return type_error!("reduce fn is not a function"),
LoadFunctionResult::NotAFunction => {
return Err("reduce fn is not a function".to_type_error())
}
LoadFunctionResult::NativeFunction(native_fn) => {
self.value = Some(native_fn(
ThisWrapper::new(true, &mut Val::Undefined),
@@ -91,7 +93,7 @@ impl StackFrameTrait for ReduceFrame {
},
},
None => match &self.value {
None => return type_error!("reduce of empty array with no initial value"),
None => return Err("reduce of empty array with no initial value".to_type_error()),
Some(value) => FrameStepOk::Pop(CallResult {
return_: value.clone(),
this: Val::Array(array_data.clone()),

View File

@@ -1,11 +1,11 @@
use std::rc::Rc;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::native_frame_function::NativeFrameFunction;
use crate::native_function::ThisWrapper;
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: || {
@@ -56,14 +56,14 @@ impl StackFrameTrait for ReduceRightFrame {
fn step(&mut self) -> FrameStepResult {
let array_data = match &self.this {
None => return type_error!("reduceRight called on non-array"),
None => return Err("reduceRight called on non-array".to_type_error()),
Some(ad) => ad,
};
if self.array_i == 0 {
match &self.value {
None => {
return type_error!("reduceRight of empty array with no initial value");
return Err("reduceRight of empty array with no initial value".to_type_error());
}
Some(value) => {
return Ok(FrameStepOk::Pop(CallResult {
@@ -88,7 +88,7 @@ impl StackFrameTrait for ReduceRightFrame {
}
Some(value) => match self.reducer.load_function() {
LoadFunctionResult::NotAFunction => {
return format_err!("TpeError: reduceRight fn is not a function")
return Err("reduceRight fn is not a function".to_type_error())
}
LoadFunctionResult::NativeFunction(native_fn) => {
self.value = Some(native_fn(

View File

@@ -1,12 +1,12 @@
use std::rc::Rc;
use crate::builtins::type_error_builtin::ToTypeError;
use crate::native_frame_function::NativeFrameFunction;
use crate::native_function::ThisWrapper;
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};
use crate::vs_value::{LoadFunctionResult, ToVal, Val, ValTrait};
pub static SORT: NativeFrameFunction = NativeFrameFunction {
make_frame: || {
@@ -208,7 +208,7 @@ enum SortTreeNodeData {
impl StackFrameTrait for SortFrame {
fn write_this(&mut self, const_: bool, this: Val) -> Result<(), Val> {
if const_ {
return type_error!("Cannot sort const array");
return Err("Cannot sort const array".to_type_error());
}
self.this = this.as_array_data();
@@ -229,7 +229,7 @@ impl StackFrameTrait for SortFrame {
fn step(&mut self) -> FrameStepResult {
if !self.started {
let array_data = match &mut self.this {
None => return type_error!("array fn called on non-array"),
None => return Err("array fn called on non-array".to_type_error()),
Some(ad) => ad,
};
@@ -263,7 +263,7 @@ impl StackFrameTrait for SortFrame {
SortTreeNodeData::Sorted(vals) => {
let mut owned_vals = vec![];
std::mem::swap(&mut owned_vals, vals);
let res = Val::Array(Rc::new(VsArray::from(owned_vals)));
let res = owned_vals.to_val();
FrameStepOk::Pop(CallResult {
return_: res.clone(),
@@ -274,7 +274,7 @@ impl StackFrameTrait for SortFrame {
},
Some((left, right)) => match self.comparator.load_function() {
LoadFunctionResult::NotAFunction => {
return type_error!("comparator is not a function");
return Err("comparator is not a function".to_type_error());
}
LoadFunctionResult::NativeFunction(native_fn) => {
let res = native_fn(