From 67ba0d9a034752980a69dd14dd36464ec0dcfc99 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Fri, 26 May 2023 14:25:20 +1000 Subject: [PATCH] Reduce explicit Val::Static() --- valuescript_vm/src/bigint_methods.rs | 9 +-- valuescript_vm/src/builtins/debug_builtin.rs | 2 +- valuescript_vm/src/builtins/error_builtin.rs | 2 +- valuescript_vm/src/native_function.rs | 6 -- valuescript_vm/src/number_methods.rs | 15 +++-- valuescript_vm/src/operations.rs | 8 +-- valuescript_vm/src/string_methods.rs | 67 ++++++++++---------- valuescript_vm/src/vs_array.rs | 4 +- valuescript_vm/src/vs_value.rs | 9 +++ 9 files changed, 64 insertions(+), 58 deletions(-) diff --git a/valuescript_vm/src/bigint_methods.rs b/valuescript_vm/src/bigint_methods.rs index 4126d80..47aad4a 100644 --- a/valuescript_vm/src/bigint_methods.rs +++ b/valuescript_vm/src/bigint_methods.rs @@ -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 { diff --git a/valuescript_vm/src/builtins/debug_builtin.rs b/valuescript_vm/src/builtins/debug_builtin.rs index b1fa510..1f2eb5a 100644 --- a/valuescript_vm/src/builtins/debug_builtin.rs +++ b/valuescript_vm/src/builtins/debug_builtin.rs @@ -58,7 +58,7 @@ impl ValTrait for DebugBuiltin { fn sub(&self, key: Val) -> Result { Ok(match key.to_string().as_str() { - "log" => Val::Static(&LOG), + "log" => LOG.to_val(), _ => Val::Undefined, }) diff --git a/valuescript_vm/src/builtins/error_builtin.rs b/valuescript_vm/src/builtins/error_builtin.rs index 55bad4d..37a598c 100644 --- a/valuescript_vm/src/builtins/error_builtin.rs +++ b/valuescript_vm/src/builtins/error_builtin.rs @@ -54,7 +54,7 @@ impl ValTrait for ErrorBuiltin { } fn as_class_data(&self) -> Option> { Some(Rc::new(VsClass { - constructor: Val::Static(&SET_MESSAGE), + constructor: SET_MESSAGE.to_val(), instance_prototype: make_error_prototype(), })) } diff --git a/valuescript_vm/src/native_function.rs b/valuescript_vm/src/native_function.rs index 01522e1..eb6db02 100644 --- a/valuescript_vm/src/native_function.rs +++ b/valuescript_vm/src/native_function.rs @@ -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) - } -} diff --git a/valuescript_vm/src/number_methods.rs b/valuescript_vm/src/number_methods.rs index fb8ec41..7d17149 100644 --- a/valuescript_vm/src/number_methods.rs +++ b/valuescript_vm/src/number_methods.rs @@ -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 { diff --git a/valuescript_vm/src/operations.rs b/valuescript_vm/src/operations.rs index a2c1af6..43e8d3c 100644 --- a/valuescript_vm/src/operations.rs +++ b/valuescript_vm/src/operations.rs @@ -369,13 +369,13 @@ pub fn op_in(_left: Val, _right: Val) -> Result { } pub fn op_sub(left: Val, right: Val) -> Result { - 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::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> { diff --git a/valuescript_vm/src/string_methods.rs b/valuescript_vm/src/string_methods.rs index 287670d..46e1a4d 100644 --- a/valuescript_vm/src/string_methods.rs +++ b/valuescript_vm/src/string_methods.rs @@ -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 { diff --git a/valuescript_vm/src/vs_array.rs b/valuescript_vm/src/vs_array.rs index 1c8b69d..27fc57e 100644 --- a/valuescript_vm/src/vs_array.rs +++ b/valuescript_vm/src/vs_array.rs @@ -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()), }, }; } diff --git a/valuescript_vm/src/vs_value.rs b/valuescript_vm/src/vs_value.rs index 23fbba3..f57d38d 100644 --- a/valuescript_vm/src/vs_value.rs +++ b/valuescript_vm/src/vs_value.rs @@ -522,6 +522,15 @@ impl ToVal for Vec { } } +impl ToVal for &'static T +where + T: ValTrait, +{ + fn to_val(self) -> Val { + Val::Static(self) + } +} + pub struct PrettyVal<'a> { val: &'a Val, }