mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Replace range_error macro
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
use std::{fmt, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
builtins::range_error_builtin::to_range_error,
|
||||
native_function::{native_fn, NativeFunction, ThisWrapper},
|
||||
operations::op_sub,
|
||||
range_error,
|
||||
vs_array::VsArray,
|
||||
vs_class::VsClass,
|
||||
vs_value::{LoadFunctionResult, ToVal, Val},
|
||||
ValTrait,
|
||||
};
|
||||
|
||||
use super::{builtin_object::BuiltinObject, type_error_builtin::ToTypeError};
|
||||
use super::{
|
||||
builtin_object::BuiltinObject, range_error_builtin::ToRangeError, type_error_builtin::ToTypeError,
|
||||
};
|
||||
|
||||
pub struct ArrayBuiltin {}
|
||||
|
||||
@@ -82,7 +82,7 @@ static FROM: NativeFunction = native_fn(|_this, params| {
|
||||
}
|
||||
|
||||
if len.is_infinite() {
|
||||
return range_error!("Invalid array length");
|
||||
return Err("Invalid array length".to_range_error());
|
||||
}
|
||||
|
||||
let len = len as usize;
|
||||
@@ -114,7 +114,7 @@ fn to_array(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
|
||||
Ok(match params[0] {
|
||||
Val::Number(number) => {
|
||||
if number.is_sign_negative() || number != number.floor() {
|
||||
return range_error!("Invalid array length");
|
||||
return Err("Invalid array length".to_range_error());
|
||||
}
|
||||
|
||||
let len = number as usize;
|
||||
|
||||
@@ -90,13 +90,29 @@ static RANGE_ERROR_TO_STRING: NativeFunction = native_fn(|this, _params| {
|
||||
Ok(format!("RangeError({})", message).to_val())
|
||||
});
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! range_error {
|
||||
($fmt:expr $(, $($arg:expr),*)?) => {{
|
||||
let formatted_string = format!($fmt $(, $($arg),*)?);
|
||||
Err(to_range_error(
|
||||
ThisWrapper::new(true, &mut Val::Undefined),
|
||||
vec![formatted_string.to_val()],
|
||||
).unwrap())
|
||||
}};
|
||||
pub trait ToRangeError {
|
||||
fn to_range_error(self) -> Val;
|
||||
}
|
||||
|
||||
impl ToRangeError for &str {
|
||||
fn to_range_error(self) -> Val {
|
||||
self.to_string().to_range_error()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRangeError for String {
|
||||
fn to_range_error(self) -> Val {
|
||||
self.to_val().to_range_error()
|
||||
}
|
||||
}
|
||||
|
||||
impl ToRangeError for Val {
|
||||
fn to_range_error(self) -> Val {
|
||||
VsObject {
|
||||
string_map: BTreeMap::from([("message".to_string(), self)]),
|
||||
symbol_map: Default::default(),
|
||||
prototype: Some(make_range_error_prototype()),
|
||||
}
|
||||
.to_val()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ use std::rc::Rc;
|
||||
|
||||
use crate::native_function::{native_fn, ThisWrapper};
|
||||
use crate::vs_value::ToVal;
|
||||
use crate::{builtins::range_error_builtin::to_range_error, range_error};
|
||||
use crate::{
|
||||
native_function::NativeFunction,
|
||||
vs_class::VsClass,
|
||||
@@ -12,6 +11,7 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::builtin_object::BuiltinObject;
|
||||
use super::range_error_builtin::ToRangeError;
|
||||
|
||||
pub struct StringBuiltin {}
|
||||
|
||||
@@ -57,7 +57,7 @@ static FROM_CODE_POINT: NativeFunction = native_fn(|_this, params| {
|
||||
|
||||
let char = match std::char::from_u32(code_point) {
|
||||
Some(c) => c,
|
||||
None => return range_error!("Invalid code point"),
|
||||
None => return Err("Invalid code point".to_range_error()),
|
||||
};
|
||||
|
||||
result.push(char);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::builtins::error_builtin::ToError;
|
||||
use crate::native_function::{native_fn, ThisWrapper};
|
||||
use crate::builtins::range_error_builtin::ToRangeError;
|
||||
use crate::native_function::native_fn;
|
||||
use crate::vs_value::ToVal;
|
||||
use crate::{builtins::range_error_builtin::to_range_error, range_error};
|
||||
use crate::{
|
||||
native_function::NativeFunction,
|
||||
todo_fn::TODO,
|
||||
@@ -43,7 +43,7 @@ static TO_FIXED: NativeFunction = native_fn(|this, params| {
|
||||
precision = f64::floor(precision);
|
||||
|
||||
if precision < 1.0 || precision > 100.0 {
|
||||
return range_error!("precision must be between 1 and 100");
|
||||
return Err("precision must be between 1 and 100".to_range_error());
|
||||
}
|
||||
|
||||
format!("{:.*}", precision as usize, number).to_val()
|
||||
@@ -60,7 +60,7 @@ static TO_EXPONENTIAL: NativeFunction = native_fn(|this, params| {
|
||||
precision = f64::floor(precision);
|
||||
|
||||
if precision < 0.0 || precision > 100.0 {
|
||||
return range_error!("precision must be between 0 and 100");
|
||||
return Err("precision must be between 0 and 100".to_range_error());
|
||||
}
|
||||
|
||||
format_exponential(*number, Some(precision as usize))
|
||||
|
||||
@@ -5,17 +5,16 @@ use num_traits::ToPrimitive;
|
||||
|
||||
use crate::bigint_methods::op_sub_bigint;
|
||||
use crate::builtins::error_builtin::ToError;
|
||||
use crate::builtins::range_error_builtin::ToRangeError;
|
||||
use crate::builtins::type_error_builtin::ToTypeError;
|
||||
use crate::native_function::native_fn;
|
||||
use crate::native_function::NativeFunction;
|
||||
use crate::native_function::ThisWrapper;
|
||||
use crate::number_methods::op_sub_number;
|
||||
use crate::string_methods::op_sub_string;
|
||||
use crate::vs_value::ToVal;
|
||||
use crate::vs_value::Val;
|
||||
use crate::vs_value::ValTrait;
|
||||
use crate::vs_value::VsType;
|
||||
use crate::{builtins::range_error_builtin::to_range_error, range_error};
|
||||
|
||||
pub fn op_plus(left: Val, right: Val) -> Result<Val, Val> {
|
||||
let left_prim = left.to_primitive();
|
||||
@@ -96,12 +95,12 @@ pub fn op_exp(left: Val, right: Val) -> Result<Val, Val> {
|
||||
match (left.as_bigint_data(), right.as_bigint_data()) {
|
||||
(Some(left_bigint), Some(right_bigint)) => {
|
||||
if right_bigint.sign() == Sign::Minus {
|
||||
return range_error!("Exponent must be non-negative");
|
||||
return Err("Exponent must be non-negative".to_range_error());
|
||||
}
|
||||
|
||||
let exp = match right_bigint.to_u32() {
|
||||
Some(exp) => exp,
|
||||
None => return range_error!("Exponent must be less than 2^32"),
|
||||
None => return Err("Exponent must be less than 2^32".to_range_error()),
|
||||
};
|
||||
|
||||
Ok(Val::BigInt(left_bigint.pow(exp)))
|
||||
|
||||
Reference in New Issue
Block a user