From 3576607423ceaa4c410aa9e9726ff4927a0407ed Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Thu, 12 Aug 2021 02:16:44 +0200 Subject: [PATCH 1/2] feature `Context::throw_error` --- boa/src/context.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/boa/src/context.rs b/boa/src/context.rs index b4d7bde43c8..0c6ad984a46 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -358,6 +358,30 @@ impl Context { self.realm.global_object.clone() } + /// Constructs a `Error` with the specified message. + #[inline] + pub fn construct_error(&mut self, message: M) -> JsValue + where + M: Into>, + { + // Runs a `new RangeError(message)`. + New::from(Call::new( + Identifier::from("Error"), + vec![Const::from(message.into()).into()], + )) + .run(self) + .expect("Into used as message") + } + + /// Throws a `Error` with the specified message. + #[inline] + pub fn throw_error(&mut self, message: M) -> Result + where + M: Into>, + { + Err(self.construct_error(message)) + } + /// Constructs a `RangeError` with the specified message. #[inline] pub fn construct_range_error(&mut self, message: M) -> JsValue From 7a78396f42bf7a60b91f45e17f68f0e02a39326a Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Thu, 12 Aug 2021 02:55:00 +0200 Subject: [PATCH 2/2] Fix doc --- boa/src/context.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/context.rs b/boa/src/context.rs index 0c6ad984a46..954a0171db5 100644 --- a/boa/src/context.rs +++ b/boa/src/context.rs @@ -364,7 +364,7 @@ impl Context { where M: Into>, { - // Runs a `new RangeError(message)`. + // Runs a `new Error(message)`. New::from(Call::new( Identifier::from("Error"), vec![Const::from(message.into()).into()],