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