feat: 实现 new String 装箱构造 + String.prototype.toString,补齐 boxed primitives

This commit is contained in:
爱特每羊队
2026-06-23 22:33:37 +08:00
parent 05c8b1bb56
commit 9a36d76544
5 changed files with 134 additions and 0 deletions
@@ -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");
}