mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Replace val_to_string with to_string (via fmt::Display)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::rc::Rc;
|
||||
use std::{fmt, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
@@ -24,9 +24,6 @@ impl ValTrait for ArrayBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function Array() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -66,7 +63,7 @@ impl ValTrait for ArrayBuiltin {
|
||||
}
|
||||
|
||||
fn sub(&self, key: Val) -> Result<Val, Val> {
|
||||
Ok(Val::Static(match key.val_to_string().as_str() {
|
||||
Ok(Val::Static(match key.to_string().as_str() {
|
||||
"isArray" => &IS_ARRAY,
|
||||
"from" => &FROM,
|
||||
"of" => &OF,
|
||||
@@ -91,6 +88,12 @@ impl ValTrait for ArrayBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ArrayBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function Array() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
static IS_ARRAY: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
Ok(match params.get(0) {
|
||||
@@ -125,7 +128,7 @@ static FROM: NativeFunction = NativeFunction {
|
||||
}
|
||||
Val::Object(..) | Val::Function(..) | Val::Class(..) | Val::Static(..) | Val::Custom(..) => {
|
||||
let len = op_sub(first_param.clone(), "length".to_val())
|
||||
.map_err(|e| e.val_to_string())
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap() // TODO: Exception
|
||||
.to_number();
|
||||
|
||||
@@ -146,7 +149,7 @@ static FROM: NativeFunction = NativeFunction {
|
||||
for i in 0..len {
|
||||
arr.push(
|
||||
op_sub(first_param.clone(), Val::Number(i as f64))
|
||||
.map_err(|e| e.val_to_string())
|
||||
.map_err(|e| e.to_string())
|
||||
.unwrap(), // TODO: Exception
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::rc::Rc;
|
||||
use std::{fmt, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
@@ -21,9 +21,6 @@ impl ValTrait for BooleanBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function Boolean() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -83,6 +80,12 @@ impl ValTrait for BooleanBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for BooleanBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function Boolean() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
fn to_boolean(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
|
||||
Ok(if let Some(value) = params.get(0) {
|
||||
Val::Bool(value.is_truthy())
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -18,9 +19,6 @@ impl ValTrait for DebugBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"[object Debug]".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
f64::NAN
|
||||
}
|
||||
@@ -62,7 +60,7 @@ impl ValTrait for DebugBuiltin {
|
||||
}
|
||||
|
||||
fn sub(&self, key: Val) -> Result<Val, Val> {
|
||||
Ok(match key.val_to_string().as_str() {
|
||||
Ok(match key.to_string().as_str() {
|
||||
"log" => Val::Static(&LOG),
|
||||
|
||||
_ => Val::Undefined,
|
||||
@@ -86,6 +84,12 @@ impl ValTrait for DebugBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for DebugBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "[object Debug]")
|
||||
}
|
||||
}
|
||||
|
||||
static LOG: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
for p in params {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::{collections::BTreeMap, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -24,9 +25,6 @@ impl ValTrait for ErrorBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function Error() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -97,6 +95,12 @@ impl ValTrait for ErrorBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for ErrorBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function Error() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ToError {
|
||||
fn to_error(self) -> Val;
|
||||
}
|
||||
@@ -140,7 +144,7 @@ fn make_error_prototype() -> Val {
|
||||
static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
fn_: |mut this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = match params.get(0) {
|
||||
Some(param) => param.val_to_string(),
|
||||
Some(param) => param.to_string(),
|
||||
None => "".to_string(),
|
||||
};
|
||||
|
||||
@@ -153,6 +157,6 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
static ERROR_TO_STRING: NativeFunction = NativeFunction {
|
||||
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = op_sub(this.get().clone(), "message".to_val())?;
|
||||
Ok(format!("Error({})", message.val_to_string()).to_val()) // TODO: Fixes needed here (and other errors)
|
||||
Ok(format!("Error({})", message).to_val()) // TODO: Fixes needed here (and other errors)
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -19,9 +20,6 @@ impl ValTrait for MathBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"[object Math]".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
f64::NAN
|
||||
}
|
||||
@@ -63,7 +61,7 @@ impl ValTrait for MathBuiltin {
|
||||
}
|
||||
|
||||
fn sub(&self, key: Val) -> Result<Val, Val> {
|
||||
Ok(match key.val_to_string().as_str() {
|
||||
Ok(match key.to_string().as_str() {
|
||||
"E" => Val::Number(std::f64::consts::E),
|
||||
"LN10" => Val::Number(std::f64::consts::LN_10),
|
||||
"LN2" => Val::Number(std::f64::consts::LN_2),
|
||||
@@ -131,6 +129,12 @@ impl ValTrait for MathBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MathBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "[object Math]")
|
||||
}
|
||||
}
|
||||
|
||||
fn param_to_number(param: Option<&Val>) -> f64 {
|
||||
match param {
|
||||
None => f64::NAN,
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -23,9 +24,6 @@ impl ValTrait for NumberBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function Number() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -65,7 +63,7 @@ impl ValTrait for NumberBuiltin {
|
||||
}
|
||||
|
||||
fn sub(&self, key: Val) -> Result<Val, Val> {
|
||||
Ok(match key.val_to_string().as_str() {
|
||||
Ok(match key.to_string().as_str() {
|
||||
"EPSILON" => Val::Number(core::f64::EPSILON),
|
||||
"MAX_VALUE" => Val::Number(core::f64::MAX),
|
||||
"MAX_SAFE_INTEGER" => Val::Number(2f64.powi(53) - 1f64),
|
||||
@@ -101,6 +99,12 @@ impl ValTrait for NumberBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for NumberBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function Number() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
pub static IS_FINITE: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
Ok(if let Some(value) = params.get(0) {
|
||||
@@ -157,7 +161,7 @@ static IS_SAFE_INTEGER: NativeFunction = NativeFunction {
|
||||
pub static PARSE_FLOAT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
Ok(if let Some(value) = params.get(0) {
|
||||
let string_value = value.val_to_string().trim().to_string();
|
||||
let string_value = value.to_string().trim().to_string();
|
||||
|
||||
match string_value.parse::<f64>() {
|
||||
Ok(number) => Val::Number(number),
|
||||
@@ -172,7 +176,7 @@ pub static PARSE_FLOAT: NativeFunction = NativeFunction {
|
||||
pub static PARSE_INT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
Ok(if let Some(value) = params.get(0) {
|
||||
let string_value = value.val_to_string().trim_start().to_string();
|
||||
let string_value = value.to_string().trim_start().to_string();
|
||||
let radix = params.get(1).and_then(|v| v.to_index()).unwrap_or(10);
|
||||
|
||||
if radix < 2 || radix > 36 {
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::{collections::BTreeMap, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -24,9 +25,6 @@ impl ValTrait for RangeErrorBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function RangeError() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -89,6 +87,12 @@ impl ValTrait for RangeErrorBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for RangeErrorBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function RangeError() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_range_error(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
|
||||
Ok(
|
||||
VsObject {
|
||||
@@ -122,7 +126,7 @@ fn make_range_error_prototype() -> Val {
|
||||
static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
fn_: |mut this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = match params.get(0) {
|
||||
Some(param) => param.val_to_string(),
|
||||
Some(param) => param.to_string(),
|
||||
None => "".to_string(),
|
||||
};
|
||||
|
||||
@@ -135,7 +139,7 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
static RANGE_ERROR_TO_STRING: NativeFunction = NativeFunction {
|
||||
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = op_sub(this.get().clone(), "message".to_val())?;
|
||||
Ok(format!("RangeError({})", message.val_to_string()).to_val())
|
||||
Ok(format!("RangeError({})", message).to_val())
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -24,9 +25,6 @@ impl ValTrait for StringBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function String() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -69,7 +67,7 @@ impl ValTrait for StringBuiltin {
|
||||
// Not supported: fromCharCode.
|
||||
// See charAt etc in string_methods.rs.
|
||||
|
||||
Ok(match key.val_to_string().as_str() {
|
||||
Ok(match key.to_string().as_str() {
|
||||
"fromCodePoint" => Val::Static(&FROM_CODE_POINT),
|
||||
// "fromCharCode" => Val::Static(&FROM_CHAR_CODE),
|
||||
// "raw" => Val::Static(&RAW), // TODO
|
||||
@@ -94,6 +92,12 @@ impl ValTrait for StringBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for StringBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function String() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
static FROM_CODE_POINT: NativeFunction = NativeFunction {
|
||||
fn_: |_this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
let mut result = String::new();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::rc::Rc;
|
||||
use std::{fmt, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
|
||||
@@ -21,9 +21,6 @@ impl ValTrait for SymbolBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"[object Symbol]".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -63,7 +60,7 @@ impl ValTrait for SymbolBuiltin {
|
||||
}
|
||||
|
||||
fn sub(&self, key: Val) -> Result<Val, Val> {
|
||||
Ok(match key.val_to_string().as_str() {
|
||||
Ok(match key.to_string().as_str() {
|
||||
"iterator" => Val::Symbol(VsSymbol::ITERATOR),
|
||||
_ => Val::Undefined,
|
||||
})
|
||||
@@ -85,3 +82,9 @@ impl ValTrait for SymbolBuiltin {
|
||||
"Symbol".into()
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for SymbolBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "[object Symbol]")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use std::fmt;
|
||||
use std::{collections::BTreeMap, rc::Rc};
|
||||
|
||||
use num_bigint::BigInt;
|
||||
@@ -22,9 +23,6 @@ impl ValTrait for TypeErrorBuiltin {
|
||||
fn typeof_(&self) -> VsType {
|
||||
VsType::Object
|
||||
}
|
||||
fn val_to_string(&self) -> String {
|
||||
"function TypeError() { [native code] }".to_string()
|
||||
}
|
||||
fn to_number(&self) -> f64 {
|
||||
core::f64::NAN
|
||||
}
|
||||
@@ -95,6 +93,12 @@ impl ValTrait for TypeErrorBuiltin {
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TypeErrorBuiltin {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "function TypeError() {{ [native code] }}")
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Static? (Rc -> Arc?)
|
||||
fn make_type_error_prototype() -> Val {
|
||||
VsObject {
|
||||
@@ -111,7 +115,7 @@ fn make_type_error_prototype() -> Val {
|
||||
static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
fn_: |mut this: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = match params.get(0) {
|
||||
Some(param) => param.val_to_string(),
|
||||
Some(param) => param.to_string(),
|
||||
None => "".to_string(),
|
||||
};
|
||||
|
||||
@@ -124,7 +128,7 @@ static SET_MESSAGE: NativeFunction = NativeFunction {
|
||||
static TYPE_ERROR_TO_STRING: NativeFunction = NativeFunction {
|
||||
fn_: |this: ThisWrapper, _params: Vec<Val>| -> Result<Val, Val> {
|
||||
let message = op_sub(this.get().clone(), "message".to_val())?;
|
||||
Ok(format!("TypeError({})", message.val_to_string()).to_val())
|
||||
Ok(format!("TypeError({})", message).to_val())
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user