fix: hasInstance handler 为 null 时按规范抛 TypeError

This commit is contained in:
2026-10-14 06:31:20 +00:00
parent ad5502353a
commit f945616d5f
4 changed files with 40 additions and 25 deletions
+3 -3
View File
@@ -416,9 +416,9 @@ impl Vm {
Ok(_) => return Ok(()),
Err(e) => return self.raise_call_error(&e).map(|_| ()),
};
// GetMethod 语义:null/undefined 均视为无 handler。handler 在场须可调用,
// 否则 Call(null) 抛 TypeError;调用异常原值上抛(外围 try/catch 可捕获)。
if !has_instance_val.is_undefined() && !has_instance_val.is_null() {
// 仅 undefined 视为无 handler;null 非 undefined,须调用,Call(null) 抛
// TypeError。handler 在场须可调用;调用异常原值上抛(外围 try/catch 可捕获)。
if !has_instance_val.is_undefined() {
if !is_callable(has_instance_val) {
return self.raise_type_error("instanceof: @@hasInstance handler is not callable");
}
@@ -194,6 +194,21 @@ fn has_instance_non_callable_handler_throws() {
assert!(result.as_bool());
}
#[test]
fn has_instance_null_handler_throws_type_error() {
let mut vm = Vm::new();
// null 非 undefined,规范仍须调用 handler,Call(null) 抛 TypeError。
let source = r#"
class D {}
D[Symbol.hasInstance] = null;
var threw = false;
try { ({} ) instanceof D; } catch (e) { threw = e instanceof TypeError; }
threw
"#;
let result = eval(&mut vm, source).unwrap();
assert!(result.as_bool());
}
#[test]
fn has_instance_coerces_return_to_boolean() {
let mut vm = Vm::new();