Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Rewrite some patterns with let-else and ok_or_else #2404

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ members = [
[workspace.package]
edition = "2021"
version = "0.16.0"
rust-version = "1.64"
rust-version = "1.65"
authors = ["boa-dev"]
repository = "https://github.com/boa-dev/boa"
license = "Unlicense/MIT"
Expand Down
6 changes: 1 addition & 5 deletions boa_ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@
nonstandard_style,
missing_docs
)]
#![allow(
clippy::module_name_repetitions,
clippy::too_many_lines,
rustdoc::missing_doc_code_examples
)]
#![allow(clippy::module_name_repetitions, clippy::too_many_lines)]

mod position;
mod punctuator;
Expand Down
11 changes: 4 additions & 7 deletions boa_engine/src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,10 @@ impl JsBigInt {

#[inline]
pub fn pow(x: &Self, y: &Self) -> JsResult<Self> {
let y = if let Some(y) = y.inner.to_biguint() {
y
} else {
return Err(JsNativeError::range()
.with_message("BigInt negative exponent")
.into());
};
let y = y
.inner
.to_biguint()
.ok_or_else(|| JsNativeError::range().with_message("BigInt negative exponent"))?;

let num_bits = (x.inner.bits() as f64
* y.to_f64().expect("Unable to convert from BigUInt to f64"))
Expand Down
8 changes: 2 additions & 6 deletions boa_engine/src/builtins/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,7 @@ impl Array {
/// by `Array.prototype.concat`.
fn is_concat_spreadable(o: &JsValue, context: &mut Context) -> JsResult<bool> {
// 1. If Type(O) is not Object, return false.
let o = if let Some(o) = o.as_object() {
o
} else {
let Some(o) = o.as_object() else {
return Ok(false);
};

Expand Down Expand Up @@ -461,9 +459,7 @@ impl Array {
let next = iterator_record.step(context)?;

// iv. If next is false, then
let next = if let Some(next) = next {
next
} else {
let Some(next) = next else {
// 1. Perform ? Set(A, "length", 𝔽(k), true).
a.set("length", k, true, context)?;

Expand Down
62 changes: 21 additions & 41 deletions boa_engine/src/builtins/array_buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,33 +148,25 @@ impl ArrayBuffer {
) -> JsResult<JsValue> {
// 1. Let O be the this value.
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
let obj = if let Some(obj) = this.as_object() {
obj
} else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer.byteLength called with non-object value")
.into());
};
let obj = this.as_object().ok_or_else(|| {
JsNativeError::typ().with_message("ArrayBuffer.byteLength called with non-object value")
})?;
let obj = obj.borrow();
let o = if let Some(o) = obj.as_array_buffer() {
o
} else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer.byteLength called with invalid object")
.into());
};
let buf = obj.as_array_buffer().ok_or_else(|| {
JsNativeError::typ().with_message("ArrayBuffer.byteLength called with invalid object")
})?;

// TODO: Shared Array Buffer
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.

// 4. If IsDetachedBuffer(O) is true, return +0𝔽.
if Self::is_detached_buffer(o) {
if Self::is_detached_buffer(buf) {
return Ok(0.into());
}

// 5. Let length be O.[[ArrayBufferByteLength]].
// 6. Return 𝔽(length).
Ok(o.array_buffer_byte_length.into())
Ok(buf.array_buffer_byte_length.into())
}

/// `25.1.5.3 ArrayBuffer.prototype.slice ( start, end )`
Expand All @@ -186,34 +178,26 @@ impl ArrayBuffer {
fn slice(this: &JsValue, args: &[JsValue], context: &mut Context) -> JsResult<JsValue> {
// 1. Let O be the this value.
// 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
let obj = if let Some(obj) = this.as_object() {
obj
} else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer.slice called with non-object value")
.into());
};
let obj = this.as_object().ok_or_else(|| {
JsNativeError::typ().with_message("ArrayBuffer.slice called with non-object value")
})?;
let obj_borrow = obj.borrow();
let o = if let Some(o) = obj_borrow.as_array_buffer() {
o
} else {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer.slice called with invalid object")
.into());
};
let buf = obj_borrow.as_array_buffer().ok_or_else(|| {
JsNativeError::typ().with_message("ArrayBuffer.slice called with invalid object")
})?;

// TODO: Shared Array Buffer
// 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.

// 4. If IsDetachedBuffer(O) is true, throw a TypeError exception.
if Self::is_detached_buffer(o) {
if Self::is_detached_buffer(buf) {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer.slice called with detached buffer")
.into());
}

// 5. Let len be O.[[ArrayBufferByteLength]].
let len = o.array_buffer_byte_length as i64;
let len = buf.array_buffer_byte_length as i64;

// 6. Let relativeStart be ? ToIntegerOrInfinity(start).
let relative_start = args.get_or_undefined(0).to_integer_or_infinity(context)?;
Expand Down Expand Up @@ -298,14 +282,14 @@ impl ArrayBuffer {

// 22. NOTE: Side-effects of the above steps may have detached O.
// 23. If IsDetachedBuffer(O) is true, throw a TypeError exception.
if Self::is_detached_buffer(o) {
if Self::is_detached_buffer(buf) {
return Err(JsNativeError::typ()
.with_message("ArrayBuffer detached while ArrayBuffer.slice was running")
.into());
}

// 24. Let fromBuf be O.[[ArrayBufferData]].
let from_buf = o
let from_buf = buf
.array_buffer_data
.as_ref()
.expect("ArrayBuffer cannot be detached here");
Expand Down Expand Up @@ -389,13 +373,9 @@ impl ArrayBuffer {

// 2. If IsDetachedBuffer(srcBuffer) is true, throw a TypeError exception.
// 3. Let srcBlock be srcBuffer.[[ArrayBufferData]].
let src_block = if let Some(b) = &self.array_buffer_data {
b
} else {
return Err(JsNativeError::syntax()
.with_message("Cannot clone detached array buffer")
.into());
};
let src_block = self.array_buffer_data.as_deref().ok_or_else(|| {
JsNativeError::syntax().with_message("Cannot clone detached array buffer")
})?;

{
// 4. Let targetBlock be targetBuffer.[[ArrayBufferData]].
Expand Down
Loading