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] - Create new lazy Error type #2283

Closed
wants to merge 6 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 2 additions & 7 deletions boa_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ impl Opt {
#[derive(Debug, Clone, ValueEnum)]
enum DumpFormat {
/// The different types of format available for dumping.
///
// NOTE: This can easily support other formats just by
// adding a field to this enum and adding the necessary
// implementation. Example: Toml, Html, etc.
Expand Down Expand Up @@ -204,7 +203,7 @@ pub fn main() -> Result<(), io::Error> {
} else {
match context.eval(&buffer) {
Ok(v) => println!("{}", v.display()),
Err(v) => eprintln!("Uncaught {}", v.display()),
Err(v) => eprintln!("Uncaught {v}"),
}
}
}
Expand Down Expand Up @@ -251,11 +250,7 @@ pub fn main() -> Result<(), io::Error> {
match context.eval(line.trim_end()) {
Ok(v) => println!("{}", v.display()),
Err(v) => {
eprintln!(
"{}: {}",
"Uncaught".red(),
v.display().to_string().red()
);
eprintln!("{}: {}", "Uncaught".red(), v.to_string().red());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ once_cell = "1.15.0"
tap = "1.0.1"
sptr = "0.3.2"
static_assertions = "1.1.0"
thiserror = "1.0.37"
icu_locale_canonicalizer = { version = "0.6.0", features = ["serde"], optional = true }
icu_locid = { version = "0.6.0", features = ["serde"], optional = true }
icu_datetime = { version = "0.6.0", features = ["serde"], optional = true }
Expand Down
24 changes: 16 additions & 8 deletions boa_engine/src/bigint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module implements the JavaScript bigint primitive rust type.

use crate::{builtins::Number, Context, JsValue};
use crate::{builtins::Number, error::JsNativeError, JsResult};
use num_integer::Integer;
use num_traits::{pow::Pow, FromPrimitive, One, ToPrimitive, Zero};
use std::{
Expand Down Expand Up @@ -148,11 +148,13 @@ impl JsBigInt {
}

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

let num_bits = (x.inner.bits() as f64
Expand All @@ -161,14 +163,16 @@ impl JsBigInt {
+ 1f64;

if num_bits > 1_000_000_000f64 {
return context.throw_range_error("Maximum BigInt size exceeded");
return Err(JsNativeError::range()
.with_message("Maximum BigInt size exceeded")
.into());
}

Ok(Self::new(x.inner.as_ref().clone().pow(y)))
}

#[inline]
pub fn shift_right(x: &Self, y: &Self, context: &mut Context) -> Result<Self, JsValue> {
pub fn shift_right(x: &Self, y: &Self) -> JsResult<Self> {
if let Some(n) = y.inner.to_i32() {
let inner = if n > 0 {
x.inner.as_ref().clone().shr(n as usize)
Expand All @@ -178,12 +182,14 @@ impl JsBigInt {

Ok(Self::new(inner))
} else {
context.throw_range_error("Maximum BigInt size exceeded")
Err(JsNativeError::range()
.with_message("Maximum BigInt size exceeded")
.into())
}
}

#[inline]
pub fn shift_left(x: &Self, y: &Self, context: &mut Context) -> Result<Self, JsValue> {
pub fn shift_left(x: &Self, y: &Self) -> JsResult<Self> {
if let Some(n) = y.inner.to_i32() {
let inner = if n > 0 {
x.inner.as_ref().clone().shl(n as usize)
Expand All @@ -193,7 +199,9 @@ impl JsBigInt {

Ok(Self::new(inner))
} else {
context.throw_range_error("Maximum BigInt size exceeded")
Err(JsNativeError::range()
.with_message("Maximum BigInt size exceeded")
.into())
}
}

Expand Down
11 changes: 7 additions & 4 deletions boa_engine/src/builtins/array/array_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::{
builtins::{function::make_builtin_fn, iterable::create_iter_result_object, Array, JsValue},
error::JsNativeError,
object::{JsObject, ObjectData},
property::{PropertyDescriptor, PropertyNameKind},
symbol::WellKnownSymbols,
Expand Down Expand Up @@ -72,7 +73,7 @@ impl ArrayIterator {
let array_iterator = array_iterator
.as_mut()
.and_then(|obj| obj.as_array_iterator_mut())
.ok_or_else(|| context.construct_type_error("`this` is not an ArrayIterator"))?;
.ok_or_else(|| JsNativeError::typ().with_message("`this` is not an ArrayIterator"))?;
let index = array_iterator.next_index;
if array_iterator.done {
return Ok(create_iter_result_object(
Expand All @@ -84,9 +85,11 @@ impl ArrayIterator {

let len = if let Some(f) = array_iterator.array.borrow().as_typed_array() {
if f.is_detached() {
return context.throw_type_error(
"Cannot get value from typed array that has a detached array buffer",
);
return Err(JsNativeError::typ()
.with_message(
"Cannot get value from typed array that has a detached array buffer",
)
.into());
}

f.array_length()
Expand Down