Implement to_val_string on Val and avoid conversion when already string

This commit is contained in:
Andrew Morris
2023-05-26 13:53:27 +10:00
parent bc89575eae
commit 4814329eca
16 changed files with 49 additions and 50 deletions

View File

@@ -10,7 +10,7 @@ use crate::{
vs_array::VsArray,
vs_class::VsClass,
vs_object::VsObject,
vs_value::{LoadFunctionResult, ToVal, ToValString, Val, VsType},
vs_value::{LoadFunctionResult, ToVal, Val, VsType},
ValTrait,
};
@@ -34,7 +34,7 @@ impl ValTrait for ArrayBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -7,7 +7,7 @@ use crate::{
vs_array::VsArray,
vs_class::VsClass,
vs_object::VsObject,
vs_value::{LoadFunctionResult, ToValString, Val, VsType},
vs_value::{LoadFunctionResult, ToVal, Val, VsType},
ValTrait,
};
@@ -31,7 +31,7 @@ impl ValTrait for BooleanBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -7,7 +7,7 @@ use crate::native_function::{NativeFunction, ThisWrapper};
use crate::vs_array::VsArray;
use crate::vs_class::VsClass;
use crate::vs_object::VsObject;
use crate::vs_value::{LoadFunctionResult, ToValString, Val, ValTrait, VsType};
use crate::vs_value::{LoadFunctionResult, ToVal, Val, ValTrait, VsType};
use super::type_error_builtin::ToTypeError;
@@ -29,7 +29,7 @@ impl ValTrait for DebugBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -4,7 +4,7 @@ use std::{collections::BTreeMap, rc::Rc};
use num_bigint::BigInt;
use crate::native_function::ThisWrapper;
use crate::vs_value::{ToVal, ToValString};
use crate::vs_value::ToVal;
use crate::{
native_function::NativeFunction,
operations::{op_sub, op_submov},
@@ -35,7 +35,7 @@ impl ValTrait for ErrorBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true
@@ -66,7 +66,7 @@ impl ValTrait for ErrorBuiltin {
LoadFunctionResult::NativeFunction(|_: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
Ok(
match params.get(0) {
Some(param) => param.to_val_string(),
Some(param) => param.clone().to_val_string(),
None => "".to_val(),
}
.to_error(),

View File

@@ -8,7 +8,7 @@ use crate::operations::to_u32;
use crate::vs_array::VsArray;
use crate::vs_class::VsClass;
use crate::vs_object::VsObject;
use crate::vs_value::{LoadFunctionResult, ToValString, Val, ValTrait, VsType};
use crate::vs_value::{LoadFunctionResult, ToVal, Val, ValTrait, VsType};
use super::type_error_builtin::ToTypeError;
@@ -30,7 +30,7 @@ impl ValTrait for MathBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -4,7 +4,7 @@ use std::rc::Rc;
use num_bigint::BigInt;
use crate::native_function::ThisWrapper;
use crate::vs_value::ToValString;
use crate::vs_value::ToVal;
use crate::{
native_function::NativeFunction,
vs_array::VsArray,
@@ -34,7 +34,7 @@ impl ValTrait for NumberBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -4,7 +4,7 @@ use std::{collections::BTreeMap, rc::Rc};
use num_bigint::BigInt;
use crate::native_function::ThisWrapper;
use crate::vs_value::{ToVal, ToValString};
use crate::vs_value::ToVal;
use crate::{
native_function::NativeFunction,
operations::{op_sub, op_submov},
@@ -35,7 +35,7 @@ impl ValTrait for RangeErrorBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true
@@ -99,7 +99,7 @@ pub fn to_range_error(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
string_map: BTreeMap::from([(
"message".to_string(),
match params.get(0) {
Some(param) => param.to_val_string(),
Some(param) => param.clone().to_val_string(),
None => "".to_val(),
},
)]),

View File

@@ -4,7 +4,7 @@ use std::rc::Rc;
use num_bigint::BigInt;
use crate::native_function::ThisWrapper;
use crate::vs_value::{ToVal, ToValString};
use crate::vs_value::ToVal;
use crate::{builtins::range_error_builtin::to_range_error, range_error};
use crate::{
native_function::NativeFunction,
@@ -35,7 +35,7 @@ impl ValTrait for StringBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true
@@ -119,7 +119,7 @@ static FROM_CODE_POINT: NativeFunction = NativeFunction {
fn to_string(_: ThisWrapper, params: Vec<Val>) -> Result<Val, Val> {
Ok(if let Some(value) = params.get(0) {
value.to_val_string()
value.clone().to_val_string()
} else {
"".to_val()
})

View File

@@ -7,7 +7,7 @@ use crate::{
vs_class::VsClass,
vs_object::VsObject,
vs_symbol::VsSymbol,
vs_value::{LoadFunctionResult, ToValString, Val, VsType},
vs_value::{LoadFunctionResult, ToVal, Val, VsType},
ValTrait,
};
@@ -31,7 +31,7 @@ impl ValTrait for SymbolBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true

View File

@@ -4,7 +4,7 @@ use std::{collections::BTreeMap, rc::Rc};
use num_bigint::BigInt;
use crate::native_function::ThisWrapper;
use crate::vs_value::{ToVal, ToValString};
use crate::vs_value::ToVal;
use crate::{
native_function::NativeFunction,
operations::{op_sub, op_submov},
@@ -33,7 +33,7 @@ impl ValTrait for TypeErrorBuiltin {
false
}
fn to_primitive(&self) -> Val {
self.to_val_string()
self.to_string().to_val()
}
fn is_truthy(&self) -> bool {
true
@@ -64,7 +64,7 @@ impl ValTrait for TypeErrorBuiltin {
LoadFunctionResult::NativeFunction(|_: ThisWrapper, params: Vec<Val>| -> Result<Val, Val> {
Ok(
match params.get(0) {
Some(param) => param.to_val_string(),
Some(param) => param.clone().to_val_string(),
None => "".to_val(),
}
.to_type_error(),