From 14f7ee2cb62e5787e811d425e900343aeb2f4ded Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Thu, 29 Jun 2023 08:50:48 +1000 Subject: [PATCH] Add internal error and use it for todos --- valuescript_common/src/builtins.rs | 2 + valuescript_vm/src/bigint_methods.rs | 8 +- valuescript_vm/src/builtins/bigint_builtin.rs | 4 +- .../src/builtins/internal_error_builtin.rs | 109 ++++++++++++++++++ valuescript_vm/src/builtins/mod.rs | 5 +- valuescript_vm/src/generator.rs | 4 +- .../src/iteration/array_entries_iterator.rs | 4 +- .../src/iteration/array_iterator.rs | 4 +- .../src/iteration/string_iterator.rs | 4 +- valuescript_vm/src/native_frame_function.rs | 4 +- valuescript_vm/src/native_function.rs | 4 +- valuescript_vm/src/number_methods.rs | 14 +-- valuescript_vm/src/operations.rs | 16 +-- valuescript_vm/src/string_methods.rs | 58 +++++----- valuescript_vm/src/todo_fn.rs | 4 +- valuescript_vm/src/virtual_machine.rs | 12 +- valuescript_vm/src/vs_array.rs | 42 +++---- 17 files changed, 208 insertions(+), 90 deletions(-) create mode 100644 valuescript_vm/src/builtins/internal_error_builtin.rs diff --git a/valuescript_common/src/builtins.rs b/valuescript_common/src/builtins.rs index 793e622..c886e9a 100644 --- a/valuescript_common/src/builtins.rs +++ b/valuescript_common/src/builtins.rs @@ -25,6 +25,7 @@ pub enum BuiltinName { Error, TypeError, RangeError, + InternalError, Symbol, SymbolIterator, @@ -46,6 +47,7 @@ pub const BUILTIN_NAMES: [&str; BuiltinName::COUNT] = [ "Error", "TypeError", "RangeError", + "InternalError", "Symbol", "SymbolIterator", "BigInt", diff --git a/valuescript_vm/src/bigint_methods.rs b/valuescript_vm/src/bigint_methods.rs index 0bd126c..3af051e 100644 --- a/valuescript_vm/src/bigint_methods.rs +++ b/valuescript_vm/src/bigint_methods.rs @@ -1,7 +1,7 @@ use num_bigint::BigInt; use crate::{ - builtins::error_builtin::ToError, + builtins::internal_error_builtin::ToInternalError, native_function::{native_fn, NativeFunction}, todo_fn::TODO, vs_value::{ToVal, Val}, @@ -21,18 +21,18 @@ static TO_STRING: NativeFunction = native_fn(|this, params| { Ok(match this.get() { Val::BigInt(bigint) => match params.get(0) { Some(_) => { - return Err("TODO: toString with radix".to_error()); + return Err("TODO: toString with radix".to_internal_error()); } None => bigint.clone().to_val().to_string().to_val(), }, - _ => return Err("TODO: bigint indirection".to_error()), + _ => return Err("TODO: bigint indirection".to_internal_error()), }) }); static VALUE_OF: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::BigInt(bigint) => Val::BigInt(bigint.clone()), - _ => return Err("TODO: bigint indirection".to_error()), + _ => return Err("TODO: bigint indirection".to_internal_error()), }) }); diff --git a/valuescript_vm/src/builtins/bigint_builtin.rs b/valuescript_vm/src/builtins/bigint_builtin.rs index 6226c53..2a489e2 100644 --- a/valuescript_vm/src/builtins/bigint_builtin.rs +++ b/valuescript_vm/src/builtins/bigint_builtin.rs @@ -10,7 +10,7 @@ use crate::{ }; use super::builtin_object::BuiltinObject; -use super::error_builtin::ToError; +use super::internal_error_builtin::ToInternalError; use super::range_error_builtin::ToRangeError; use super::type_error_builtin::ToTypeError; @@ -42,7 +42,7 @@ impl BuiltinObject for BigIntBuiltin { } } Some(Val::Undefined) | None => Err("Can't convert undefined to BigInt".to_type_error()), - _ => Err("TODO: Other BigInt conversions".to_error()), + _ => Err("TODO: Other BigInt conversions".to_internal_error()), } }) } diff --git a/valuescript_vm/src/builtins/internal_error_builtin.rs b/valuescript_vm/src/builtins/internal_error_builtin.rs new file mode 100644 index 0000000..c1df770 --- /dev/null +++ b/valuescript_vm/src/builtins/internal_error_builtin.rs @@ -0,0 +1,109 @@ +use std::fmt; +use std::{collections::BTreeMap, rc::Rc}; + +use crate::native_function::{native_fn, ThisWrapper}; +use crate::vs_value::ToVal; +use crate::ValTrait; +use crate::{ + native_function::NativeFunction, + operations::op_submov, + vs_class::VsClass, + vs_object::VsObject, + vs_value::{LoadFunctionResult, Val}, +}; + +use super::builtin_object::BuiltinObject; + +pub struct InternalErrorBuiltin {} + +impl BuiltinObject for InternalErrorBuiltin { + fn bo_name() -> &'static str { + "InternalError" + } + + fn bo_sub(_key: &str) -> Val { + Val::Undefined + } + + fn bo_load_function() -> LoadFunctionResult { + LoadFunctionResult::NativeFunction(|_: ThisWrapper, params: Vec| -> Result { + Ok( + match params.get(0) { + Some(param) => param.clone().to_val_string(), + None => "".to_val(), + } + .to_internal_error(), + ) + }) + } + + fn bo_as_class_data() -> Option> { + Some(Rc::new(VsClass { + constructor: Val::Static(&SET_MESSAGE), + prototype: make_internal_error_prototype(), + static_: VsObject::default().to_val(), + })) + } +} + +impl fmt::Display for InternalErrorBuiltin { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "function InternalError() {{ [native code] }}") + } +} + +// TODO: Static? (Rc -> Arc?) +fn make_internal_error_prototype() -> Val { + VsObject { + string_map: BTreeMap::from([ + ("name".to_string(), "InternalError".to_val()), + ("toString".to_string(), INTERNAL_ERROR_TO_STRING.to_val()), + ]), + symbol_map: Default::default(), + prototype: None, + } + .to_val() +} + +static SET_MESSAGE: NativeFunction = native_fn(|mut this, params| { + let message = match params.get(0) { + Some(param) => param.to_string(), + None => "".to_string(), + }; + + op_submov(this.get_mut()?, &"message".to_val(), message.to_val())?; + + Ok(Val::Undefined) +}); + +static INTERNAL_ERROR_TO_STRING: NativeFunction = native_fn(|this, _params| { + let message = this.get().sub(&"message".to_val())?; + Ok(format!("InternalError({})", message).to_val()) +}); + +pub trait ToInternalError { + fn to_internal_error(self) -> Val; +} + +impl ToInternalError for &str { + fn to_internal_error(self) -> Val { + self.to_string().to_internal_error() + } +} + +impl ToInternalError for String { + fn to_internal_error(self) -> Val { + self.to_val().to_internal_error() + } +} + +impl ToInternalError for Val { + fn to_internal_error(self) -> Val { + VsObject { + string_map: BTreeMap::from([("message".to_string(), self)]), + symbol_map: Default::default(), + prototype: Some(make_internal_error_prototype()), + } + .to_val() + } +} diff --git a/valuescript_vm/src/builtins/mod.rs b/valuescript_vm/src/builtins/mod.rs index 34beb4a..0d3626d 100644 --- a/valuescript_vm/src/builtins/mod.rs +++ b/valuescript_vm/src/builtins/mod.rs @@ -4,6 +4,7 @@ mod boolean_builtin; mod builtin_object; mod debug_builtin; pub mod error_builtin; +pub mod internal_error_builtin; mod math_builtin; mod number_builtin; pub mod range_error_builtin; @@ -20,7 +21,8 @@ use crate::{ use self::{ array_builtin::ArrayBuiltin, bigint_builtin::BigIntBuiltin, boolean_builtin::BooleanBuiltin, - debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin, math_builtin::MathBuiltin, + debug_builtin::DebugBuiltin, error_builtin::ErrorBuiltin, + internal_error_builtin::InternalErrorBuiltin, math_builtin::MathBuiltin, number_builtin::NumberBuiltin, range_error_builtin::RangeErrorBuiltin, string_builtin::StringBuiltin, symbol_builtin::SymbolBuiltin, type_error_builtin::TypeErrorBuiltin, @@ -40,6 +42,7 @@ pub static BUILTIN_VALS: [fn() -> Val; BUILTIN_COUNT] = [ || ErrorBuiltin {}.to_val(), || TypeErrorBuiltin {}.to_val(), || RangeErrorBuiltin {}.to_val(), + || InternalErrorBuiltin {}.to_val(), || SymbolBuiltin {}.to_val(), || VsSymbol::ITERATOR.to_val(), || BigIntBuiltin {}.to_val(), diff --git a/valuescript_vm/src/generator.rs b/valuescript_vm/src/generator.rs index 1c41a4a..7521388 100644 --- a/valuescript_vm/src/generator.rs +++ b/valuescript_vm/src/generator.rs @@ -7,7 +7,7 @@ use std::{ use num_bigint::BigInt; use crate::{ - builtins::{error_builtin::ToError, type_error_builtin::ToTypeError}, + builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError}, iteration::{iteration_result::IterationResult, return_this::RETURN_THIS}, native_frame_function::NativeFrameFunction, native_function::ThisWrapper, @@ -132,7 +132,7 @@ impl StackFrameTrait for GeneratorFrame { fn write_this(&mut self, const_: bool, this: Val) -> Result<(), Val> { let mut dynamic = match this { Val::Dynamic(dynamic) => dynamic, - _ => return Err("TODO: indirection".to_error()), + _ => return Err("TODO: indirection".to_internal_error()), }; if const_ { diff --git a/valuescript_vm/src/iteration/array_entries_iterator.rs b/valuescript_vm/src/iteration/array_entries_iterator.rs index 2a4ccd8..afa4951 100644 --- a/valuescript_vm/src/iteration/array_entries_iterator.rs +++ b/valuescript_vm/src/iteration/array_entries_iterator.rs @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc}; use num_bigint::BigInt; use crate::{ - builtins::{error_builtin::ToError, type_error_builtin::ToTypeError}, + builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError}, native_function::{native_fn, NativeFunction}, vs_array::VsArray, vs_class::VsClass, @@ -113,7 +113,7 @@ impl fmt::Display for ArrayEntriesIterator { static NEXT: NativeFunction = native_fn(|mut this, _| { let dynamic = match this.get_mut()? { Val::Dynamic(dynamic) => dynamic, - _ => return Err("TODO: indirection".to_error()), + _ => return Err("TODO: indirection".to_internal_error()), }; let iter = dynamic_make_mut(dynamic) diff --git a/valuescript_vm/src/iteration/array_iterator.rs b/valuescript_vm/src/iteration/array_iterator.rs index 25a5d87..edac182 100644 --- a/valuescript_vm/src/iteration/array_iterator.rs +++ b/valuescript_vm/src/iteration/array_iterator.rs @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc}; use num_bigint::BigInt; use crate::{ - builtins::{error_builtin::ToError, type_error_builtin::ToTypeError}, + builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError}, native_function::{native_fn, NativeFunction}, vs_array::VsArray, vs_class::VsClass, @@ -113,7 +113,7 @@ impl fmt::Display for ArrayIterator { static NEXT: NativeFunction = native_fn(|mut this, _| { let dynamic = match this.get_mut()? { Val::Dynamic(dynamic) => dynamic, - _ => return Err("TODO: indirection".to_error()), + _ => return Err("TODO: indirection".to_internal_error()), }; let iter = dynamic_make_mut(dynamic) diff --git a/valuescript_vm/src/iteration/string_iterator.rs b/valuescript_vm/src/iteration/string_iterator.rs index 564375b..7843767 100644 --- a/valuescript_vm/src/iteration/string_iterator.rs +++ b/valuescript_vm/src/iteration/string_iterator.rs @@ -3,7 +3,7 @@ use std::{fmt, rc::Rc}; use num_bigint::BigInt; use crate::{ - builtins::{error_builtin::ToError, type_error_builtin::ToTypeError}, + builtins::{internal_error_builtin::ToInternalError, type_error_builtin::ToTypeError}, native_function::{native_fn, NativeFunction}, vs_array::VsArray, vs_class::VsClass, @@ -149,7 +149,7 @@ impl fmt::Display for StringIterator { static NEXT: NativeFunction = native_fn(|mut this, _| { let dynamic = match this.get_mut()? { Val::Dynamic(dynamic) => dynamic, - _ => return Err("TODO: indirection".to_error()), + _ => return Err("TODO: indirection".to_internal_error()), }; let iter = dynamic_make_mut(dynamic) diff --git a/valuescript_vm/src/native_frame_function.rs b/valuescript_vm/src/native_frame_function.rs index e8f1434..9e82df5 100644 --- a/valuescript_vm/src/native_frame_function.rs +++ b/valuescript_vm/src/native_frame_function.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use num_bigint::BigInt; -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::type_error_builtin::ToTypeError; use crate::stack_frame::StackFrame; use crate::vs_array::VsArray; @@ -60,7 +60,7 @@ impl ValTrait for NativeFrameFunction { } fn sub(&self, _key: &Val) -> Result { - Err("TODO: Subscript native function".to_error()) + Err("TODO: Subscript native function".to_internal_error()) } fn submov(&mut self, _key: &Val, _value: Val) -> Result<(), Val> { diff --git a/valuescript_vm/src/native_function.rs b/valuescript_vm/src/native_function.rs index 4f4f796..96b3808 100644 --- a/valuescript_vm/src/native_function.rs +++ b/valuescript_vm/src/native_function.rs @@ -3,7 +3,7 @@ use std::rc::Rc; use num_bigint::BigInt; -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::type_error_builtin::ToTypeError; use crate::vs_array::VsArray; use crate::vs_class::VsClass; @@ -81,7 +81,7 @@ impl ValTrait for NativeFunction { } fn sub(&self, _key: &Val) -> Result { - Err("TODO: Subscript native function".to_error()) + Err("TODO: Subscript native function".to_internal_error()) } fn submov(&mut self, _key: &Val, _value: Val) -> Result<(), Val> { diff --git a/valuescript_vm/src/number_methods.rs b/valuescript_vm/src/number_methods.rs index 5a375fa..5de4da1 100644 --- a/valuescript_vm/src/number_methods.rs +++ b/valuescript_vm/src/number_methods.rs @@ -1,4 +1,4 @@ -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::range_error_builtin::ToRangeError; use crate::native_function::native_fn; use crate::vs_value::ToVal; @@ -67,32 +67,32 @@ static TO_EXPONENTIAL: NativeFunction = native_fn(|this, params| { } None => format_exponential(*number, None), }, - _ => return Err("number indirection".to_error()), + _ => return Err("number indirection".to_internal_error()), }) }); static TODO_LOCALE: NativeFunction = native_fn(|this, _params| match this.get() { - Val::Number(_number) => return Err("TODO: locale".to_error()), - _ => return Err("number indirection".to_error()), + Val::Number(_number) => return Err("TODO: locale".to_internal_error()), + _ => return Err("number indirection".to_internal_error()), }); static TO_STRING: NativeFunction = native_fn(|this, params| { Ok(match this.get() { Val::Number(number) => match params.get(0) { Some(_) => { - return Err("TODO: toString with radix".to_error()); + return Err("TODO: toString with radix".to_internal_error()); } None => number.to_val().to_string().to_val(), }, - _ => return Err("number indirection".to_error()), + _ => return Err("number indirection".to_internal_error()), }) }); static VALUE_OF: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::Number(number) => Val::Number(*number), - _ => return Err("number indirection".to_error()), + _ => return Err("number indirection".to_internal_error()), }) }); diff --git a/valuescript_vm/src/operations.rs b/valuescript_vm/src/operations.rs index f6661bc..8b63f3b 100644 --- a/valuescript_vm/src/operations.rs +++ b/valuescript_vm/src/operations.rs @@ -8,7 +8,7 @@ use num_traits::FromPrimitive; use num_traits::ToPrimitive; use crate::bigint_methods::op_sub_bigint; -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::range_error_builtin::ToRangeError; use crate::builtins::type_error_builtin::ToTypeError; use crate::native_function::native_fn; @@ -40,7 +40,7 @@ pub fn op_plus(left: &Val, right: &Val) -> Result { (Some(left_bigint), Some(right_bigint)) => { return Ok(Val::BigInt(left_bigint + right_bigint)); } - _ => return Err("TODO: plus with bigint and non-bigint".to_error()), + _ => return Err("TODO: plus with bigint and non-bigint".to_internal_error()), } } @@ -131,7 +131,7 @@ pub fn op_eq_impl(left: &Val, right: &Val) -> Result { left.codify(), right.codify() ) - .to_error(), + .to_internal_error(), ) } }) @@ -157,7 +157,7 @@ pub fn op_triple_eq_impl(left: &Val, right: &Val) -> Result { if left.typeof_() != right.typeof_() { false } else { - return Err("TODO: op=== with other types".to_error()); + return Err("TODO: op=== with other types".to_internal_error()); } } }) @@ -386,16 +386,16 @@ pub fn op_typeof(input: &Val) -> Val { } pub fn op_instance_of(_left: &Val, _right: &Val) -> Result { - Err("TODO: op_instance_of".to_error()) + Err("TODO: op_instance_of".to_internal_error()) } pub fn op_in(_left: &Val, _right: &Val) -> Result { - Err("TODO: op_in".to_error()) + Err("TODO: op_in".to_internal_error()) } pub fn op_sub(left: &mut Val, right: &Val) -> Result { match left { - Val::Void => Err("Internal: Shouldn't happen".to_error()), // TODO: Internal errors + Val::Void => Err("Internal: Shouldn't happen".to_internal_error()), // TODO: Internal errors Val::Undefined => Err("Cannot subscript undefined".to_type_error()), Val::Null => Err("Cannot subscript null".to_type_error()), Val::Bool(_) => Ok(match right.to_string().as_str() { @@ -450,7 +450,7 @@ pub fn op_sub(left: &mut Val, right: &Val) -> Result { pub fn op_submov(target: &mut Val, subscript: &Val, value: Val) -> Result<(), Val> { match target { - Val::Void => Err("Internal: Shouldn't happen".to_error()), // TODO: Internal errors + Val::Void => Err("Internal: Shouldn't happen".to_internal_error()), // TODO: Internal errors Val::Undefined => Err("Cannot assign to subscript of undefined".to_type_error()), Val::Null => Err("Cannot assign to subscript of null".to_type_error()), Val::Bool(_) => Err("Cannot assign to subscript of bool".to_type_error()), diff --git a/valuescript_vm/src/string_methods.rs b/valuescript_vm/src/string_methods.rs index fe7af26..feb5dc5 100644 --- a/valuescript_vm/src/string_methods.rs +++ b/valuescript_vm/src/string_methods.rs @@ -1,7 +1,7 @@ use std::{rc::Rc, str::Chars}; use crate::{ - builtins::error_builtin::ToError, + builtins::internal_error_builtin::ToInternalError, helpers::{to_wrapping_index, to_wrapping_index_clamped}, iteration::string_iterator::StringIterator, native_function::{native_fn, NativeFunction}, @@ -104,7 +104,7 @@ static AT: NativeFunction = native_fn(|this, params| { None => "".to_val(), } } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -126,7 +126,7 @@ static CODE_POINT_AT: NativeFunction = native_fn(|this, params| { None => Val::Undefined, } } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -141,7 +141,7 @@ static CONCAT: NativeFunction = native_fn(|this, params| { result.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -182,7 +182,7 @@ static ENDS_WITH: NativeFunction = native_fn(|this, params| { Val::Bool(true) } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -212,7 +212,7 @@ static INCLUDES: NativeFunction = native_fn(|this, params| { None => Val::Bool(false), } } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -242,7 +242,7 @@ static INDEX_OF: NativeFunction = native_fn(|this, params| { None => Val::Number(-1.0), } } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -272,7 +272,7 @@ static LAST_INDEX_OF: NativeFunction = native_fn(|this, params| { None => Val::Number(-1.0), } } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -280,9 +280,9 @@ static TODO_LOCALE: NativeFunction = native_fn(|this, _params| { // TODO: Ok(...) match this.get() { Val::String(_string_data) => { - return Err("TODO: locale".to_error()); + return Err("TODO: locale".to_internal_error()); } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), } }); @@ -290,9 +290,9 @@ static TODO_REGEXES: NativeFunction = native_fn(|this, _params| { // TODO: Ok(...) match this.get() { Val::String(_string_data) => { - return Err("TODO: regexes".to_error()); + return Err("TODO: regexes".to_internal_error()); } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), } }); @@ -301,9 +301,9 @@ static NORMALIZE: NativeFunction = native_fn(|this, _params| { match this.get() { Val::String(_string_data) => { // Consider https://docs.rs/unicode-normalization/latest/unicode_normalization/ - return Err("TODO: normalize".to_error()); + return Err("TODO: normalize".to_internal_error()); } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), } }); @@ -355,7 +355,7 @@ static PAD_END: NativeFunction = native_fn(|this, params| { string.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -409,7 +409,7 @@ static PAD_START: NativeFunction = native_fn(|this, params| { prefix.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -432,7 +432,7 @@ static REPEAT: NativeFunction = native_fn(|this, params| { result.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -464,7 +464,7 @@ static SLICE: NativeFunction = native_fn(|this, params| { new_string.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -528,7 +528,7 @@ static SPLIT: NativeFunction = native_fn(|this, params| { result.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -567,7 +567,7 @@ static STARTS_WITH: NativeFunction = native_fn(|this, params| { Val::Bool(true) } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); @@ -608,62 +608,62 @@ static SUBSTRING: NativeFunction = native_fn(|this, params| { new_string.to_val() } - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TO_LOWER_CASE: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => string_data.to_lowercase().to_val(), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TO_STRING: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => Val::String(string_data.clone()), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TO_UPPER_CASE: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => string_data.to_uppercase().to_val(), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TRIM: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => string_data.trim().to_val(), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TRIM_END: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => string_data.trim_end().to_val(), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static TRIM_START: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => string_data.trim_start().to_val(), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static VALUE_OF: NativeFunction = native_fn(|this, _params| { Ok(match this.get() { Val::String(string_data) => Val::String(string_data.clone()), - _ => return Err("string indirection".to_error()), + _ => return Err("string indirection".to_internal_error()), }) }); static VALUES: NativeFunction = native_fn(|this, _params| match this.get() { Val::String(string_data) => Ok(StringIterator::new(string_data.clone()).to_dynamic_val()), - _ => Err("string indirection".to_error()), + _ => Err("string indirection".to_internal_error()), }); /** diff --git a/valuescript_vm/src/todo_fn.rs b/valuescript_vm/src/todo_fn.rs index d863590..8d4fb45 100644 --- a/valuescript_vm/src/todo_fn.rs +++ b/valuescript_vm/src/todo_fn.rs @@ -1,6 +1,6 @@ use crate::{ - builtins::error_builtin::ToError, + builtins::internal_error_builtin::ToInternalError, native_function::{native_fn, NativeFunction}, }; -pub static TODO: NativeFunction = native_fn(|_, _| Err("TODO".to_error())); +pub static TODO: NativeFunction = native_fn(|_, _| Err("TODO".to_internal_error())); diff --git a/valuescript_vm/src/virtual_machine.rs b/valuescript_vm/src/virtual_machine.rs index 1d87d2b..6bf2022 100644 --- a/valuescript_vm/src/virtual_machine.rs +++ b/valuescript_vm/src/virtual_machine.rs @@ -1,6 +1,6 @@ use std::rc::Rc; -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::bytecode::Bytecode; use crate::bytecode::DecoderMaker; use crate::first_stack_frame::FirstStackFrame; @@ -48,7 +48,7 @@ impl VirtualMachine { } } - Err("step limit reached".to_error()) + Err("step limit reached".to_internal_error()) } None => { while self.stack.len() > 0 { @@ -87,8 +87,12 @@ impl VirtualMachine { self.push(new_frame); } // TODO: Internal errors - FrameStepOk::Yield(_) => return self.handle_exception("Unexpected yield".to_error()), - FrameStepOk::YieldStar(_) => return self.handle_exception("Unexpected yield*".to_error()), + FrameStepOk::Yield(_) => { + return self.handle_exception("Unexpected yield".to_internal_error()) + } + FrameStepOk::YieldStar(_) => { + return self.handle_exception("Unexpected yield*".to_internal_error()) + } } Ok(()) diff --git a/valuescript_vm/src/vs_array.rs b/valuescript_vm/src/vs_array.rs index 0f98c4f..cf6fcb4 100644 --- a/valuescript_vm/src/vs_array.rs +++ b/valuescript_vm/src/vs_array.rs @@ -9,7 +9,7 @@ use crate::array_higher_functions::{ array_flat_map::FLAT_MAP, array_map::MAP, array_reduce::REDUCE, array_reduce_right::REDUCE_RIGHT, array_some::SOME, array_sort::SORT, }; -use crate::builtins::error_builtin::ToError; +use crate::builtins::internal_error_builtin::ToInternalError; use crate::builtins::type_error_builtin::ToTypeError; use crate::helpers::{to_wrapping_index, to_wrapping_index_clamped}; use crate::iteration::array_entries_iterator::ArrayEntriesIterator; @@ -174,7 +174,7 @@ static AT: NativeFunction = native_fn(|this, params| { None => Val::Undefined, Some(i) => array_data.elements[i].clone(), }, - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -198,7 +198,7 @@ static CONCAT: NativeFunction = native_fn(|this, params| { new_array.to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -211,7 +211,7 @@ static COPY_WITHIN: NativeFunction = native_fn(|mut this, params| { let ulen = array_data_mut.elements.len(); if ulen > isize::MAX as usize { - return Err("TODO: array len exceeds isize".to_error()); + return Err("TODO: array len exceeds isize".to_internal_error()); } let mut target = match params.get(0) { @@ -269,13 +269,13 @@ static COPY_WITHIN: NativeFunction = native_fn(|mut this, params| { this.clone() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); static ENTRIES: NativeFunction = native_fn(|this, _params| match this.get() { Val::Array(array_data) => Ok(ArrayEntriesIterator::new(array_data.clone()).to_dynamic_val()), - _ => Err("array indirection".to_error()), + _ => Err("array indirection".to_internal_error()), }); static FILL: NativeFunction = native_fn(|mut this, params| { @@ -304,7 +304,7 @@ static FILL: NativeFunction = native_fn(|mut this, params| { this.clone() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -312,7 +312,7 @@ static FLAT: NativeFunction = native_fn(|this, params| { Ok(match this.get() { Val::Array(array_data) => { if params.len() > 0 { - return Err("TODO: .flat depth parameter".to_error()); + return Err("TODO: .flat depth parameter".to_internal_error()); } let mut new_elems = Vec::::new(); @@ -332,7 +332,7 @@ static FLAT: NativeFunction = native_fn(|this, params| { new_elems.to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -353,7 +353,7 @@ static INCLUDES: NativeFunction = native_fn(|this, params| { Val::Bool(false) } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -374,7 +374,7 @@ static INDEX_OF: NativeFunction = native_fn(|this, params| { Val::Number(-1.0) } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -410,7 +410,7 @@ static JOIN: NativeFunction = native_fn(|this, params| { res.to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -431,7 +431,7 @@ static LAST_INDEX_OF: NativeFunction = native_fn(|this, params| { Val::Number(-1_f64) } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -455,7 +455,7 @@ static POP: NativeFunction = native_fn(|mut this, _params| { _ => removed_el, } } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -468,7 +468,7 @@ static PUSH: NativeFunction = native_fn(|mut this, mut params| { array_data_mut.elements.append(&mut params); (array_data_mut.elements.len() as f64).to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -495,7 +495,7 @@ static REVERSE: NativeFunction = native_fn(|mut this, _params| { this.clone() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -512,7 +512,7 @@ static SHIFT: NativeFunction = native_fn(|mut this, _params| { array_data_mut.elements.remove(0) } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -537,7 +537,7 @@ static SLICE: NativeFunction = native_fn(|this, params| { new_elems.to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -614,7 +614,7 @@ static SPLICE: NativeFunction = native_fn(|mut this, params| { deleted_elements.to_val() } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); @@ -637,11 +637,11 @@ static UNSHIFT: NativeFunction = native_fn(|mut this, params| { Val::Number(array_data_mut.elements.len() as f64) } - _ => return Err("array indirection".to_error()), + _ => return Err("array indirection".to_internal_error()), }) }); static VALUES: NativeFunction = native_fn(|this, _params| match this.get() { Val::Array(array_data) => Ok(ArrayIterator::new(array_data.clone()).to_dynamic_val()), - _ => Err("array indirection".to_error()), + _ => Err("array indirection".to_internal_error()), });