Skip to content

Commit

Permalink
refactor: fix construct_error functions (#1703)
Browse files Browse the repository at this point in the history
Before the interpreter would create the AST equivalent to `new Error(message)` and interpret it when constructing the builtin errors, this could fail if the gloabl in question had been overwritten. Now we use `Context::standard_objects` to get access to the
error constructors and invoke the functions directly.
  • Loading branch information
RageKnify committed Nov 14, 2021
1 parent db520eb commit 49ae844
Showing 1 changed file with 61 additions and 43 deletions.
104 changes: 61 additions & 43 deletions boa/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ use crate::{
realm::Realm,
syntax::{
ast::{
node::{
statement_list::RcStatementList, Call, FormalParameter, Identifier, New,
StatementList,
},
Const, Node,
node::{statement_list::RcStatementList, FormalParameter, StatementList},
Node,
},
Parser,
},
Expand Down Expand Up @@ -548,12 +545,11 @@ impl Context {
where
M: Into<Box<str>>,
{
// Runs a `new Error(message)`.
New::from(Call::new(
Identifier::from("Error"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::Error::constructor(
&self.standard_objects().error_object().constructor().into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -572,12 +568,15 @@ impl Context {
where
M: Into<Box<str>>,
{
// Runs a `new RangeError(message)`.
New::from(Call::new(
Identifier::from("RangeError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::RangeError::constructor(
&self
.standard_objects()
.range_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -596,12 +595,15 @@ impl Context {
where
M: Into<Box<str>>,
{
// Runs a `new TypeError(message)`.
New::from(Call::new(
Identifier::from("TypeError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::TypeError::constructor(
&self
.standard_objects()
.type_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -620,11 +622,15 @@ impl Context {
where
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("ReferenceError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::ReferenceError::constructor(
&self
.standard_objects()
.reference_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -643,11 +649,15 @@ impl Context {
where
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("SyntaxError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::SyntaxError::constructor(
&self
.standard_objects()
.syntax_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -665,11 +675,15 @@ impl Context {
where
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("EvalError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::EvalError::constructor(
&self
.standard_objects()
.eval_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand All @@ -678,11 +692,15 @@ impl Context {
where
M: Into<Box<str>>,
{
New::from(Call::new(
Identifier::from("URIError"),
vec![Const::from(message.into()).into()],
))
.run(self)
crate::builtins::error::UriError::constructor(
&self
.standard_objects()
.uri_error_object()
.constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message")
}

Expand Down

0 comments on commit 49ae844

Please sign in to comment.