From b26f8bcead45e757a477920d36a984d020a6c01f Mon Sep 17 00:00:00 2001 From: dlemel8 Date: Fri, 9 Oct 2020 13:00:36 +0300 Subject: [PATCH] calling "new" on a primitive value throw a type error (#825) * calling "new" on a primitive value throw a type error * fix format --- boa/src/exec/tests.rs | 13 +++++++++++++ boa/src/syntax/ast/node/new/mod.rs | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index f385cfeb42..21901b287a 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -754,6 +754,19 @@ mod in_operator { assert_eq!(forward(&mut engine, "bar.b"), "\"b\""); } + #[test] + fn should_type_error_when_new_is_not_constructor() { + let mut engine = Context::new(); + + let scenario = r#" + const a = ""; + new a(); + "#; + + let result = forward(&mut engine, scenario); + assert_eq!(result, "Uncaught \"TypeError\": \"a is not a constructor\""); + } + #[test] fn new_instance_should_point_to_prototype() { // A new instance should point to a prototype object created with the constructor function diff --git a/boa/src/syntax/ast/node/new/mod.rs b/boa/src/syntax/ast/node/new/mod.rs index eeb47dbed2..4a1d57a61f 100644 --- a/boa/src/syntax/ast/node/new/mod.rs +++ b/boa/src/syntax/ast/node/new/mod.rs @@ -55,7 +55,8 @@ impl Executable for New { match func_object { Value::Object(ref object) => object.construct(&v_args, interpreter), - _ => Ok(Value::undefined()), + _ => interpreter + .throw_type_error(format!("{} is not a constructor", self.expr().to_string(),)), } } }