Skip to content

Commit

Permalink
Improve debug output of JsNativeError and Realm
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed May 3, 2023
1 parent 0636022 commit cd123f7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 11 additions & 1 deletion boa_engine/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ impl std::fmt::Display for JsError {
///
/// assert_eq!(native_error.message(), "cannot decode uri");
/// ```
#[derive(Debug, Clone, Trace, Finalize, Error)]
#[derive(Clone, Trace, Finalize, Error)]
#[error("{kind}: {message}")]
pub struct JsNativeError {
/// The kind of native error (e.g. `TypeError`, `SyntaxError`, etc.)
Expand All @@ -428,6 +428,16 @@ pub struct JsNativeError {
realm: Option<Realm>,
}

impl std::fmt::Debug for JsNativeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JsNativeError")
.field("kind", &self.kind)
.field("message", &self.message)
.field("cause", &self.cause)
.finish_non_exhaustive()
}
}

impl JsNativeError {
/// Creates a new `JsNativeError` from its `kind`, `message` and (optionally) its `cause`.
fn new(kind: JsNativeErrorKind, message: Box<str>, cause: Option<Box<JsError>>) -> Self {
Expand Down
17 changes: 15 additions & 2 deletions boa_engine/src/realm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//!
//! A realm is represented in this implementation as a Realm struct with the fields specified from the spec.

use std::fmt;

use crate::{
context::{intrinsics::Intrinsics, HostHooks},
environments::DeclarativeEnvironment,
Expand All @@ -17,18 +19,29 @@ use boa_profiler::Profiler;
/// Representation of a Realm.
///
/// In the specification these are called Realm Records.
#[derive(Clone, Debug, Trace, Finalize)]
#[derive(Clone, Trace, Finalize)]
pub struct Realm {
inner: Gc<Inner>,
}

impl fmt::Debug for Realm {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Realm")
.field("intrinsics", &self.inner.intrinsics)
.field("environment", &self.inner.environment)
.field("global_object", &self.inner.global_object)
.field("global_this", &self.inner.global_this)
.finish()
}
}

impl PartialEq for Realm {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(&*self.inner, &*other.inner)
}
}

#[derive(Debug, Trace, Finalize)]
#[derive(Trace, Finalize)]
struct Inner {
intrinsics: Intrinsics,
environment: Gc<DeclarativeEnvironment>,
Expand Down

0 comments on commit cd123f7

Please sign in to comment.