Browse Source

refactor: fix construct_error functions (#1703)

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.
pull/1712/head
João Borges 3 years ago
parent
commit
49ae8441c4
  1. 104
      boa/src/context.rs

104
boa/src/context.rs

@ -16,11 +16,8 @@ use crate::{
realm::Realm, realm::Realm,
syntax::{ syntax::{
ast::{ ast::{
node::{ node::{statement_list::RcStatementList, FormalParameter, StatementList},
statement_list::RcStatementList, Call, FormalParameter, Identifier, New, Node,
StatementList,
},
Const, Node,
}, },
Parser, Parser,
}, },
@ -548,12 +545,11 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
// Runs a `new Error(message)`. crate::builtins::error::Error::constructor(
New::from(Call::new( &self.standard_objects().error_object().constructor().into(),
Identifier::from("Error"), &[message.into().into()],
vec![Const::from(message.into()).into()], self,
)) )
.run(self)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -572,12 +568,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
// Runs a `new RangeError(message)`. crate::builtins::error::RangeError::constructor(
New::from(Call::new( &self
Identifier::from("RangeError"), .standard_objects()
vec![Const::from(message.into()).into()], .range_error_object()
)) .constructor()
.run(self) .into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -596,12 +595,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
// Runs a `new TypeError(message)`. crate::builtins::error::TypeError::constructor(
New::from(Call::new( &self
Identifier::from("TypeError"), .standard_objects()
vec![Const::from(message.into()).into()], .type_error_object()
)) .constructor()
.run(self) .into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -620,11 +622,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
New::from(Call::new( crate::builtins::error::ReferenceError::constructor(
Identifier::from("ReferenceError"), &self
vec![Const::from(message.into()).into()], .standard_objects()
)) .reference_error_object()
.run(self) .constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -643,11 +649,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
New::from(Call::new( crate::builtins::error::SyntaxError::constructor(
Identifier::from("SyntaxError"), &self
vec![Const::from(message.into()).into()], .standard_objects()
)) .syntax_error_object()
.run(self) .constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -665,11 +675,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
New::from(Call::new( crate::builtins::error::EvalError::constructor(
Identifier::from("EvalError"), &self
vec![Const::from(message.into()).into()], .standard_objects()
)) .eval_error_object()
.run(self) .constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }
@ -678,11 +692,15 @@ impl Context {
where where
M: Into<Box<str>>, M: Into<Box<str>>,
{ {
New::from(Call::new( crate::builtins::error::UriError::constructor(
Identifier::from("URIError"), &self
vec![Const::from(message.into()).into()], .standard_objects()
)) .uri_error_object()
.run(self) .constructor()
.into(),
&[message.into().into()],
self,
)
.expect("Into<String> used as message") .expect("Into<String> used as message")
} }

Loading…
Cancel
Save