Skip to content

Commit

Permalink
Remove as_defined in favor of map/map_or
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Dec 17, 2024
1 parent 81e92b6 commit 0e19caf
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 53 deletions.
45 changes: 23 additions & 22 deletions core/engine/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 )`
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()?;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 17 additions & 19 deletions core/engine/src/builtins/typed_array/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -2220,9 +2218,9 @@ impl BuiltinTypedArray {
context: &mut Context,
) -> JsResult<JsValue> {
// 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")
Expand Down Expand Up @@ -2272,9 +2270,9 @@ impl BuiltinTypedArray {
context: &mut Context,
) -> JsResult<JsValue> {
// 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")
Expand Down Expand Up @@ -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).
Expand All @@ -2414,8 +2412,8 @@ impl BuiltinTypedArray {
&[buffer.into(), begin_byte_offset.into(), new_len.into()],
context,
)?
.upcast()
.into())
.upcast()
.into())
}
}

Expand Down
13 changes: 1 addition & 12 deletions core/engine/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)?;
Expand Down

0 comments on commit 0e19caf

Please sign in to comment.