feat: 实现 new String 装箱构造 + String.prototype.toString,补齐 boxed primitives
This commit is contained in:
@@ -136,3 +136,16 @@ fn boolean_boxes_participate_in_numeric_coercion() {
|
||||
let r = eval(&mut vm, "new Boolean(false) | 0").unwrap();
|
||||
assert_eq!(r.as_int(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boxed_boolean_valueof_tostring_and_object_e2e() {
|
||||
let mut vm = Vm::new();
|
||||
assert!(!eval(&mut vm, "new Boolean(false).valueOf()").unwrap().as_bool());
|
||||
assert!(eval(&mut vm, "new Boolean(1).valueOf()").unwrap().as_bool());
|
||||
|
||||
let t = eval(&mut vm, "new Boolean(false).toString()").unwrap();
|
||||
assert_eq!(str_val(&vm, t), "false");
|
||||
|
||||
let ty = eval(&mut vm, "typeof new Boolean(true)").unwrap();
|
||||
assert_eq!(str_val(&vm, ty), "object");
|
||||
}
|
||||
|
||||
@@ -135,3 +135,28 @@ fn number_static_constants_remain_bound() {
|
||||
assert_eq!(eval(&mut vm, "Number.MAX_SAFE_INTEGER").unwrap().as_double(), 9007199254740991f64);
|
||||
assert!(eval(&mut vm, "Number.POSITIVE_INFINITY > 0").unwrap().as_bool());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boxed_number_valueof_preserves_int_and_float() {
|
||||
let mut vm = Vm::new();
|
||||
let i = eval(&mut vm, "new Number(5).valueOf()").unwrap();
|
||||
assert!(i.is_int());
|
||||
assert_eq!(i.as_int(), 5);
|
||||
|
||||
let f = eval(&mut vm, "new Number(1.5).valueOf()").unwrap();
|
||||
assert!(f.is_double());
|
||||
assert_eq!(f.as_double(), 1.5);
|
||||
|
||||
let s = eval(&mut vm, "new Number('7').valueOf()").unwrap();
|
||||
assert_eq!(s.as_int(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boxed_number_is_object_and_call_stays_primitive() {
|
||||
let mut vm = Vm::new();
|
||||
let ty = eval(&mut vm, "typeof new Number(5)").unwrap();
|
||||
assert_eq!(vm.kernel_core().string_forge().lookup(ty.as_string_index()).unwrap(), "object");
|
||||
|
||||
let n = eval(&mut vm, "Number('7')").unwrap();
|
||||
assert_eq!(n.as_int(), 7);
|
||||
}
|
||||
|
||||
@@ -240,3 +240,33 @@ fn string_replace_all() {
|
||||
let result = eval(&mut vm, "'aba'.replaceAll('a', 'c')").unwrap();
|
||||
assert_eq!(to_str(&vm, result), "cbc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boxed_string_valueof_and_tostring() {
|
||||
let mut vm = Vm::new();
|
||||
let v = eval(&mut vm, "new String('abc').valueOf()").unwrap();
|
||||
assert!(v.is_string());
|
||||
assert_eq!(to_str(&vm, v), "abc");
|
||||
|
||||
let t = eval(&mut vm, "new String('abc').toString()").unwrap();
|
||||
assert!(t.is_string());
|
||||
assert_eq!(to_str(&vm, t), "abc");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn boxed_string_is_object_and_empty_default() {
|
||||
let mut vm = Vm::new();
|
||||
let ty = eval(&mut vm, "typeof new String('x')").unwrap();
|
||||
assert_eq!(to_str(&vm, ty), "object");
|
||||
|
||||
let empty = eval(&mut vm, "new String().valueOf()").unwrap();
|
||||
assert_eq!(to_str(&vm, empty), "");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn string_call_conversion_stays_primitive() {
|
||||
let mut vm = Vm::new();
|
||||
let v = eval(&mut vm, "String(123)").unwrap();
|
||||
assert!(v.is_string());
|
||||
assert_eq!(to_str(&vm, v), "123");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user