From 0e19cafc0ad4946afa3af2e6b99b626f58709107 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Mon, 16 Dec 2024 19:28:59 -0800 Subject: [PATCH] Remove as_defined in favor of map/map_or --- core/engine/src/builtins/string/mod.rs | 45 ++++++++++--------- .../src/builtins/typed_array/builtin.rs | 36 +++++++-------- core/engine/src/value/mod.rs | 13 +----- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/core/engine/src/builtins/string/mod.rs b/core/engine/src/builtins/string/mod.rs index f6879ac67d8..52709169319 100644 --- a/core/engine/src/builtins/string/mod.rs +++ b/core/engine/src/builtins/string/mod.rs @@ -283,7 +283,7 @@ impl String { .configurable(false), context, ) - .expect("length definition for a new string must not fail"); + .expect("length definition for a new string must not fail"); // 9. Return S. s @@ -714,21 +714,21 @@ impl String { // 3. Let n be ? ToIntegerOrInfinity(count). match args.get_or_undefined(0).to_integer_or_infinity(context)? { IntegerOrInfinity::Integer(n) - if n > 0 && (n as usize) * len <= Self::MAX_STRING_LENGTH => - { - if string.is_empty() { - return Ok(js_string!().into()); - } - let n = n as usize; - let mut result = Vec::with_capacity(n); + if n > 0 && (n as usize) * len <= Self::MAX_STRING_LENGTH => + { + if string.is_empty() { + return Ok(js_string!().into()); + } + let n = n as usize; + let mut result = Vec::with_capacity(n); - std::iter::repeat(string.as_str()) - .take(n) - .for_each(|s| result.push(s)); + std::iter::repeat(string.as_str()) + .take(n) + .for_each(|s| result.push(s)); - // 6. Return the String value that is made from n copies of S appended together. - Ok(JsString::concat_array(&result).into()) - } + // 6. Return the String value that is made from n copies of S appended together. + Ok(JsString::concat_array(&result).into()) + } // 5. If n is 0, return the empty String. IntegerOrInfinity::Integer(0) => Ok(js_string!().into()), // 4. If n < 0 or n is +∞, throw a RangeError exception. @@ -1099,7 +1099,7 @@ impl String { &replacement, &JsString::from(string.get_expect(position + search_length..)) ) - .into()) + .into()) } /// `22.1.3.18 String.prototype.replaceAll ( searchValue, replaceValue )` @@ -1236,7 +1236,7 @@ impl String { replace_str, context, ) - .expect("GetSubstitution should never fail here."), + .expect("GetSubstitution should never fail here."), }; // d. Set result to the string-concatenation of result, preserved, and replacement. @@ -1795,7 +1795,7 @@ impl String { [requested_locale], context.intl_provider(), ) - .unwrap_or(Locale::UND); + .unwrap_or(Locale::UND); let casemapper = context.intl_provider().case_mapper()?; @@ -1892,10 +1892,11 @@ impl String { let int_start = args.get_or_undefined(0).to_integer_or_infinity(context)?; // 5. If end is undefined, let intEnd be len; else let intEnd be ? ToIntegerOrInfinity(end). - let int_end = match args.get_or_undefined(1).as_defined() { - None => IntegerOrInfinity::Integer(len), - Some(end) => end.to_integer_or_infinity(context)?, - }; + let int_end = args + .get_or_undefined(1) + .map_or(Ok(IntegerOrInfinity::Integer(len)), |end| { + end.to_integer_or_infinity(context) + })?; // 6. Let finalStart be the result of clamping intStart between 0 and len. let final_start = int_start.clamp_finite(0, len) as usize; @@ -2014,7 +2015,7 @@ impl String { substrings.into_iter().map(JsValue::from), context, ) - .into()); + .into()); } // d. Set i to j + separatorLength. i = index + separator_length; diff --git a/core/engine/src/builtins/typed_array/builtin.rs b/core/engine/src/builtins/typed_array/builtin.rs index 6972a2dd927..632a799af88 100644 --- a/core/engine/src/builtins/typed_array/builtin.rs +++ b/core/engine/src/builtins/typed_array/builtin.rs @@ -205,20 +205,18 @@ impl BuiltinTypedArray { } }; - let mapping = match args.get_or_undefined(1).as_defined() { + let mapping = match args.first().map(JsValue::variant) { // 3. If mapfn is undefined, let mapping be false. None => None, // 4. Else, - Some(v) => match v.as_object() { - // b. Let mapping be true. - Some(obj) if obj.is_callable() => Some(obj), - // a. If IsCallable(mapfn) is false, throw a TypeError exception. - _ => { - return Err(JsNativeError::typ() - .with_message("TypedArray.from called with non-callable mapfn") - .into()) - } - }, + // b. Let mapping be true. + Some(JsVariant::Object(obj)) if obj.is_callable() => Some(obj), + // a. If IsCallable(mapfn) is false, throw a TypeError exception. + _ => { + return Err(JsNativeError::typ() + .with_message("TypedArray.from called with non-callable mapfn") + .into()) + } }; // 5. Let usingIterator be ? GetMethod(source, @@iterator). @@ -2220,9 +2218,9 @@ impl BuiltinTypedArray { context: &mut Context, ) -> JsResult { // 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception. - let compare_fn = match args.first().and_then(JsValue::as_defined) { + let compare_fn = match args.first().map(JsValue::variant) { None => None, - Some(obj) if obj.is_callable() => obj.as_callable(), + Some(JsVariant::Object(obj)) if obj.is_callable() => Some(obj), _ => { return Err(JsNativeError::typ() .with_message("TypedArray.sort called with non-callable comparefn") @@ -2272,9 +2270,9 @@ impl BuiltinTypedArray { context: &mut Context, ) -> JsResult { // 1. If comparefn is not undefined and IsCallable(comparefn) is false, throw a TypeError exception. - let compare_fn = match args.first().and_then(JsValue::as_defined) { + let compare_fn = match args.first().map(JsValue::variant) { None => None, - Some(obj) if obj.is_callable() => obj.as_callable(), + Some(JsVariant::Object(obj)) if obj.is_callable() => Some(obj), _ => { return Err(JsNativeError::typ() .with_message("TypedArray.sort called with non-callable comparefn") @@ -2394,8 +2392,8 @@ impl BuiltinTypedArray { &[buffer.into(), begin_byte_offset.into()], context, )? - .upcast() - .into()) + .upcast() + .into()) } else { // 16. Else, // a. If end is undefined, let relativeEnd be srcLength; else let relativeEnd be ? ToIntegerOrInfinity(end). @@ -2414,8 +2412,8 @@ impl BuiltinTypedArray { &[buffer.into(), begin_byte_offset.into(), new_len.into()], context, )? - .upcast() - .into()) + .upcast() + .into()) } } diff --git a/core/engine/src/value/mod.rs b/core/engine/src/value/mod.rs index d257357f99b..0c3efcfae1d 100644 --- a/core/engine/src/value/mod.rs +++ b/core/engine/src/value/mod.rs @@ -327,17 +327,6 @@ impl JsValue { matches!(&self.inner, InnerValue::Undefined) } - /// Returns `Some(self)` if the value is defined, otherwise `None`. - #[inline] - #[must_use] - pub const fn as_defined(&self) -> Option<&Self> { - if self.is_undefined() { - None - } else { - Some(self) - } - } - /// Returns true if the value is null. #[inline] #[must_use] @@ -487,7 +476,7 @@ impl JsValue { PreferredType::String => js_string!("string"), PreferredType::Number => js_string!("number"), } - .into(); + .into(); // iv. Let result be ? Call(exoticToPrim, input, « hint »). let result = exotic_to_prim.call(self, &[hint], context)?;