fix: 还原 instanceof 的 null handler 归一化条件并翻转单测断言

This commit is contained in:
2026-10-14 07:14:36 +00:00
parent f3c3620b70
commit b1e2367556
4 changed files with 31 additions and 29 deletions
+3 -3
View File
@@ -416,9 +416,9 @@ impl Vm {
Ok(_) => return Ok(()),
Err(e) => return self.raise_call_error(&e).map(|_| ()),
};
// 仅 undefined 视为无 handler;null 非 undefined,须调用,Call(null) 抛
// TypeError。handler 在场须可调用;调用异常原值上抛(外围 try/catch 可捕获)。
if !has_instance_val.is_undefined() {
// GetMethod 语义:null 与 undefined 均归一化为无 handler,走 prototype
// 分支。handler 在场须可调用;调用异常原值上抛(外围 try/catch 可捕获)。
if !has_instance_val.is_undefined() && !has_instance_val.is_null() {
if !is_callable(has_instance_val) {
return self.raise_type_error("instanceof: @@hasInstance handler is not callable");
}
@@ -195,15 +195,17 @@ fn has_instance_non_callable_handler_throws() {
}
#[test]
fn has_instance_null_handler_throws_type_error() {
fn has_instance_null_handler_returns_false() {
let mut vm = Vm::new();
// null 非 undefined,规范仍须调用 handler,Call(null) 抛 TypeError。
// GetMethod 将 null handler 归一化为无 handler,走 prototype 分支:
// D.prototype 不在 ({}) 原型链上,返 false 不抛。
let source = r#"
class D {}
D[Symbol.hasInstance] = null;
var threw = false;
try { ({} ) instanceof D; } catch (e) { threw = e instanceof TypeError; }
threw
var result = false;
try { result = ({} ) instanceof D; } catch (e) { threw = true; }
threw === false && result === false
"#;
let result = eval(&mut vm, source).unwrap();
assert!(result.as_bool());