Skip to content

Commit

Permalink
Use flexstr for message storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Sep 16, 2022
1 parent f34a290 commit 8afcc6b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 105 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ dyn-clone = "1.0.9"
once_cell = "1.14.0"
tap = "1.0.1"
thiserror = "1.0.35"
flexstr = "0.9.2"
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
48 changes: 27 additions & 21 deletions boa_engine/src/error/mod.rs → boa_engine/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod vos;

use crate::{
builtins::{error::ErrorKind, Array},
object::JsObject,
Expand All @@ -9,9 +7,9 @@ use crate::{
Context, JsResult, JsValue,
};
use boa_gc::{Finalize, Trace};
use std::borrow::Cow;
use flexstr::LocalStr;
use std::{borrow::Cow, fmt::Display};
use thiserror::Error;
use vos::ViewStr;

/// The error type returned by all operations related
/// to the execution of Javascript code.
Expand Down Expand Up @@ -170,14 +168,13 @@ impl JsError {
let message = if obj.has_property("message", context)? {
obj.get("message", context)?
.as_string()
.map(ToString::to_string)
.map(LocalStr::from_ref)
.ok_or_else(|| {
JsNativeError::typ()
.with_message("invalid type for field `message`")
})?
.into()
} else {
"".into()
JsNativeError::DEFAULT_MSG
};

let cause = obj
Expand Down Expand Up @@ -293,7 +290,7 @@ impl From<JsNativeError> for JsError {
}
}

impl std::fmt::Display for JsError {
impl Display for JsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.inner {
Repr::Native(e) => e.fmt(f),
Expand Down Expand Up @@ -330,15 +327,16 @@ pub struct JsNativeError {
/// The kind of native error (e.g. `TypeError`, `SyntaxError`, etc.)
pub kind: JsNativeErrorKind,
#[unsafe_ignore_trace]
message: ViewStr,
message: LocalStr,
#[source]
cause: Option<Box<JsError>>,
}

impl JsNativeError {
const DEFAULT_MSG: LocalStr = LocalStr::from_static("");
/// Creates a new `JsNativeError` from its `kind`, `message` and (optionally)
/// its `cause`.
fn new(kind: JsNativeErrorKind, message: ViewStr, cause: Option<Box<JsError>>) -> Self {
fn new(kind: JsNativeErrorKind, message: LocalStr, cause: Option<Box<JsError>>) -> Self {
Self {
kind,
message,
Expand All @@ -365,7 +363,11 @@ impl JsNativeError {
/// ));
/// ```
pub fn aggregate(errors: Vec<JsError>) -> Self {
Self::new(JsNativeErrorKind::Aggregate(errors), "".into(), None)
Self::new(
JsNativeErrorKind::Aggregate(errors),
Self::DEFAULT_MSG,
None,
)
}

/// Creates a new `JsNativeError` of kind `Error`, with empty `message`
Expand All @@ -380,7 +382,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Error));
/// ```
pub fn error() -> Self {
Self::new(JsNativeErrorKind::Error, "".into(), None)
Self::new(JsNativeErrorKind::Error, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `EvalError`, with empty `message`
Expand All @@ -395,7 +397,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Eval));
/// ```
pub fn eval() -> Self {
Self::new(JsNativeErrorKind::Eval, "".into(), None)
Self::new(JsNativeErrorKind::Eval, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `RangeError`, with empty `message`
Expand All @@ -410,7 +412,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Range));
/// ```
pub fn range() -> Self {
Self::new(JsNativeErrorKind::Range, "".into(), None)
Self::new(JsNativeErrorKind::Range, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `ReferenceError`, with empty `message`
Expand All @@ -425,7 +427,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Reference));
/// ```
pub fn reference() -> Self {
Self::new(JsNativeErrorKind::Reference, "".into(), None)
Self::new(JsNativeErrorKind::Reference, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `SyntaxError`, with empty `message`
Expand All @@ -440,7 +442,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Syntax));
/// ```
pub fn syntax() -> Self {
Self::new(JsNativeErrorKind::Syntax, "".into(), None)
Self::new(JsNativeErrorKind::Syntax, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `TypeError`, with empty `message`
Expand All @@ -455,7 +457,7 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Type));
/// ```
pub fn typ() -> Self {
Self::new(JsNativeErrorKind::Type, "".into(), None)
Self::new(JsNativeErrorKind::Type, Self::DEFAULT_MSG, None)
}

/// Creates a new `JsNativeError` of kind `UriError`, with empty `message`
Expand All @@ -470,15 +472,15 @@ impl JsNativeError {
/// assert!(matches!(error.kind, JsNativeErrorKind::Uri));
/// ```
pub fn uri() -> Self {
Self::new(JsNativeErrorKind::Uri, "".into(), None)
Self::new(JsNativeErrorKind::Uri, Self::DEFAULT_MSG, None)
}

/// Appends a message to this error.
///
/// # Note
///
/// A static [`str`] will be stored as a simple pointer,
/// while a [`String`] will be converted to a [`Rc<str>`][std::rc::Rc<str>] in order
/// while a [`String`] will be converted to a <code>[Rc]<[str]></code> in order
/// to make clones more efficient. Prefer static `str`s if you want
/// efficiency, and [`String`] s if you prefer descriptive error messages.
///
Expand All @@ -495,7 +497,11 @@ impl JsNativeError {
where
S: Into<Cow<'static, str>>,
{
self.message = message.into().into();
let message = match message.into() {
Cow::Borrowed(str) => LocalStr::from_static(str),
Cow::Owned(string) => LocalStr::from_ref(string),
};
self.message = message;
self
}

Expand Down Expand Up @@ -727,7 +733,7 @@ pub enum JsNativeErrorKind {
Uri,
}

impl std::fmt::Display for JsNativeErrorKind {
impl Display for JsNativeErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
JsNativeErrorKind::Aggregate(_) => "AggregateError",
Expand Down
84 changes: 0 additions & 84 deletions boa_engine/src/error/vos.rs

This file was deleted.

0 comments on commit 8afcc6b

Please sign in to comment.