mirror of
https://github.com/pjh456/OxideJS.git
synced 2026-10-23 18:21:13 +08:00
fix: Boolean 构造器对象臂按 B.3.4 特判 DDA 宿主对象为 falsy
This commit is contained in:
@@ -18,7 +18,9 @@ pub fn boolean_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
|
||||
// SAFETY: arg 已确认是字符串值。
|
||||
!unsafe { (*arg.as_string_ptr()).is_empty() }
|
||||
} else if arg.is_object() {
|
||||
true
|
||||
// [[IsHTMLDDA]] 宿主对象按 undefined 处理(B.3.4),falsy。
|
||||
// SAFETY: arg 已确认是对象值,借用即时消费。
|
||||
!unsafe { (*arg.as_js_object_ptr()).is_html_dda_obj() }
|
||||
} else if arg.is_int() {
|
||||
arg.as_int() != 0
|
||||
} else if arg.is_double() {
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use oxide_compiler::compiler::Compiler;
|
||||
use oxide_types::value::JsValue;
|
||||
use oxide_vm::vm::Vm;
|
||||
|
||||
fn eval(source: &str) -> Result<JsValue, String> {
|
||||
let allocator = oxide_parser::Allocator::default();
|
||||
let program = oxide_parser::parse(&allocator, source).map_err(|e| format!("Parse error: {:?}", e))?;
|
||||
let module = Compiler::new().compile(&program).map_err(|e| format!("Compile error: {}", e))?;
|
||||
let mut vm = Vm::new();
|
||||
vm.run(&Arc::new(module))
|
||||
}
|
||||
|
||||
/// 求值一个应得布尔结果的表达式并断言其类型。
|
||||
fn eval_bool(source: &str) -> bool {
|
||||
let result = eval(source).expect("eval should succeed");
|
||||
assert!(result.is_bool(), "expected boolean result, got {result:?}");
|
||||
result.as_bool()
|
||||
}
|
||||
|
||||
/// [[IsHTMLDDA]] 宿主对象按 undefined 处理(B.3.4),Boolean() 应返 false。
|
||||
#[test]
|
||||
fn boolean_html_dda_is_falsy() {
|
||||
assert!(!eval_bool("Boolean($262.IsHTMLDDA)"));
|
||||
}
|
||||
|
||||
/// 普通对象在 ToBoolean 下保持 truthy。
|
||||
#[test]
|
||||
fn boolean_plain_object_is_truthy() {
|
||||
assert!(eval_bool("Boolean({})"));
|
||||
}
|
||||
Reference in New Issue
Block a user