Browse Source

calling "new" on a primitive value throw a type error (#825)

* calling "new" on a primitive value throw a type error

* fix format
pull/840/head
dlemel8 4 years ago committed by GitHub
parent
commit
b26f8bcead
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      boa/src/exec/tests.rs
  2. 3
      boa/src/syntax/ast/node/new/mod.rs

13
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

3
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(),)),
}
}
}

Loading…
Cancel
Save