diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index b8ac1d8b05..11ec55613e 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -425,7 +425,9 @@ impl Interpreter { #[allow(clippy::wrong_self_convention)] pub(crate) fn to_object(&mut self, value: &Value) -> ResultValue { match value { - Value::Undefined | Value::Null => Err(Value::undefined()), + Value::Undefined | Value::Null => { + self.throw_type_error("cannot convert 'null' or 'undefined' to object") + } Value::Boolean(boolean) => { let proto = self .realm diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index d9a5c67e71..79d68a82af 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -927,3 +927,15 @@ fn calling_function_with_unspecified_arguments() { assert_eq!(forward(&mut engine, scenario), "undefined"); } + +#[test] +fn to_object() { + let realm = Realm::create(); + let mut engine = Interpreter::new(realm); + + assert!(engine + .to_object(&Value::undefined()) + .unwrap_err() + .is_object()); + assert!(engine.to_object(&Value::null()).unwrap_err().is_object()); +}