Reduce explicit Val::Static()

This commit is contained in:
Andrew Morris
2023-05-26 14:25:20 +10:00
parent e0b66ca93f
commit 67ba0d9a03
9 changed files with 64 additions and 58 deletions

View File

@@ -9,11 +9,12 @@ use crate::{
pub fn op_sub_bigint(_bigint: &BigInt, subscript: &Val) -> Val {
match subscript.to_string().as_str() {
"toLocaleString" => Val::Static(&TODO),
"toString" => Val::Static(&TO_STRING),
"valueOf" => Val::Static(&VALUE_OF),
_ => Val::Undefined,
"toLocaleString" => &TODO,
"toString" => &TO_STRING,
"valueOf" => &VALUE_OF,
_ => return Val::Undefined,
}
.to_val()
}
static TO_STRING: NativeFunction = NativeFunction {

View File

@@ -58,7 +58,7 @@ impl ValTrait for DebugBuiltin {
fn sub(&self, key: Val) -> Result<Val, Val> {
Ok(match key.to_string().as_str() {
"log" => Val::Static(&LOG),
"log" => LOG.to_val(),
_ => Val::Undefined,
})

View File

@@ -54,7 +54,7 @@ impl ValTrait for ErrorBuiltin {
}
fn as_class_data(&self) -> Option<Rc<VsClass>> {
Some(Rc::new(VsClass {
constructor: Val::Static(&SET_MESSAGE),
constructor: SET_MESSAGE.to_val(),
instance_prototype: make_error_prototype(),
}))
}

View File

@@ -104,9 +104,3 @@ impl fmt::Display for NativeFunction {
write!(f, "function() {{ [native code] }}")
}
}
impl ToVal for &'static NativeFunction {
fn to_val(self) -> Val {
Val::Static(self)
}
}

View File

@@ -10,14 +10,15 @@ use crate::{
pub fn op_sub_number(_number: f64, subscript: &Val) -> Val {
match subscript.to_string().as_str() {
"toExponential" => Val::Static(&TO_EXPONENTIAL),
"toFixed" => Val::Static(&TO_FIXED),
"toLocaleString" => Val::Static(&TODO_LOCALE),
"toPrecision" => Val::Static(&TODO),
"toString" => Val::Static(&TO_STRING),
"valueOf" => Val::Static(&VALUE_OF),
_ => Val::Undefined,
"toExponential" => &TO_EXPONENTIAL,
"toFixed" => &TO_FIXED,
"toLocaleString" => &TODO_LOCALE,
"toPrecision" => &TODO,
"toString" => &TO_STRING,
"valueOf" => &VALUE_OF,
_ => return Val::Undefined,
}
.to_val()
}
static TO_FIXED: NativeFunction = NativeFunction {

View File

@@ -369,13 +369,13 @@ pub fn op_in(_left: Val, _right: Val) -> Result<Val, Val> {
}
pub fn op_sub(left: Val, right: Val) -> Result<Val, Val> {
return match left {
match left {
Val::Void => Err("Internal: Shouldn't happen".to_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() {
"toString" => Val::Static(&BOOL_TO_STRING),
"valueOf" => Val::Static(&BOOL_VALUE_OF),
"toString" => BOOL_TO_STRING.to_val(),
"valueOf" => BOOL_VALUE_OF.to_val(),
_ => Val::Undefined,
}),
Val::Number(number) => Ok(op_sub_number(number, &right)),
@@ -411,7 +411,7 @@ pub fn op_sub(left: Val, right: Val) -> Result<Val, Val> {
Val::Function(_) | Val::Class(_) => Ok(Val::Undefined),
Val::Static(s) => s.sub(right),
Val::Custom(custom_data) => custom_data.sub(right),
};
}
}
pub fn op_submov(target: &mut Val, subscript: Val, value: Val) -> Result<(), Val> {

View File

@@ -47,40 +47,41 @@ pub fn get_string_method(method: &str) -> Val {
// desirable.
match method {
"at" => Val::Static(&AT),
// "charAt" => Val::Static(&CHAR_AT),
// "charCodeAt" => Val::Static(&CHAR_CODE_AT),
"codePointAt" => Val::Static(&CODE_POINT_AT),
"concat" => Val::Static(&CONCAT),
"endsWith" => Val::Static(&ENDS_WITH),
"includes" => Val::Static(&INCLUDES),
"indexOf" => Val::Static(&INDEX_OF),
"lastIndexOf" => Val::Static(&LAST_INDEX_OF),
"localeCompare" => Val::Static(&TODO_LOCALE), // (TODO)
"match" => Val::Static(&TODO_REGEXES), // (TODO: regex)
"matchAll" => Val::Static(&TODO_REGEXES), // (TODO: regex)
"normalize" => Val::Static(&NORMALIZE), // (TODO)
"padEnd" => Val::Static(&PAD_END),
"padStart" => Val::Static(&PAD_START),
"repeat" => Val::Static(&REPEAT),
"replace" => Val::Static(&TODO_REGEXES), // (TODO: regex)
"replaceAll" => Val::Static(&TODO_REGEXES), // (TODO: regex)
"search" => Val::Static(&TODO_REGEXES), // (TODO: regex)
"slice" => Val::Static(&SLICE),
"split" => Val::Static(&SPLIT),
"startsWith" => Val::Static(&STARTS_WITH),
"substring" => Val::Static(&SUBSTRING),
"toLocaleLowerCase" => Val::Static(&TODO_LOCALE),
"toLocaleUpperCase" => Val::Static(&TODO_LOCALE),
"toLowerCase" => Val::Static(&TO_LOWER_CASE),
"toString" => Val::Static(&TO_STRING),
"toUpperCase" => Val::Static(&TO_UPPER_CASE),
"trim" => Val::Static(&TRIM),
"trimEnd" => Val::Static(&TRIM_END),
"trimStart" => Val::Static(&TRIM_START),
"valueOf" => Val::Static(&VALUE_OF),
_ => Val::Undefined,
"at" => &AT,
// "charAt" => &CHAR_AT,
// "charCodeAt" => &CHAR_CODE_AT,
"codePointAt" => &CODE_POINT_AT,
"concat" => &CONCAT,
"endsWith" => &ENDS_WITH,
"includes" => &INCLUDES,
"indexOf" => &INDEX_OF,
"lastIndexOf" => &LAST_INDEX_OF,
"localeCompare" => &TODO_LOCALE, // (TODO)
"match" => &TODO_REGEXES, // (TODO: regex)
"matchAll" => &TODO_REGEXES, // (TODO: regex)
"normalize" => &NORMALIZE, // (TODO)
"padEnd" => &PAD_END,
"padStart" => &PAD_START,
"repeat" => &REPEAT,
"replace" => &TODO_REGEXES, // (TODO: regex)
"replaceAll" => &TODO_REGEXES, // (TODO: regex)
"search" => &TODO_REGEXES, // (TODO: regex)
"slice" => &SLICE,
"split" => &SPLIT,
"startsWith" => &STARTS_WITH,
"substring" => &SUBSTRING,
"toLocaleLowerCase" => &TODO_LOCALE,
"toLocaleUpperCase" => &TODO_LOCALE,
"toLowerCase" => &TO_LOWER_CASE,
"toString" => &TO_STRING,
"toUpperCase" => &TO_UPPER_CASE,
"trim" => &TRIM,
"trimEnd" => &TRIM_END,
"trimStart" => &TRIM_START,
"valueOf" => &VALUE_OF,
_ => return Val::Undefined,
}
.to_val()
}
static AT: NativeFunction = NativeFunction {

View File

@@ -32,7 +32,7 @@ impl VsArray {
object: VsObject {
string_map: Default::default(),
symbol_map: Default::default(),
prototype: Some(Val::Static(&ARRAY_PROTOTYPE)),
prototype: Some(ARRAY_PROTOTYPE.to_val()),
},
};
}
@@ -43,7 +43,7 @@ impl VsArray {
object: VsObject {
string_map: Default::default(),
symbol_map: Default::default(),
prototype: Some(Val::Static(&ARRAY_PROTOTYPE)),
prototype: Some(ARRAY_PROTOTYPE.to_val()),
},
};
}

View File

@@ -522,6 +522,15 @@ impl ToVal for Vec<Val> {
}
}
impl<T> ToVal for &'static T
where
T: ValTrait,
{
fn to_val(self) -> Val {
Val::Static(self)
}
}
pub struct PrettyVal<'a> {
val: &'a Val,
}