From 44202ce6977f57485def07dbc2799d8c2a44deee Mon Sep 17 00:00:00 2001 From: Halid Odat Date: Sun, 15 Aug 2021 04:36:43 +0200 Subject: [PATCH] Feature throw `Error` object (#1465) * feature `Context::throw_error` * Fix doc --- boa/src/context.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/boa/src/context.rs b/boa/src/context.rs index b4d7bde43c..954a0171db 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 Error(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