feat: 搭建 Iterator 与 for-of 语法基础设施

This commit is contained in:
爱特每羊队
2026-06-08 13:48:54 +08:00
parent 4d60491e0e
commit 3880882fda
12 changed files with 246 additions and 4 deletions
@@ -160,3 +160,66 @@ fn array_empty_literal() {
assert!(obj.is_array());
assert_eq!(obj.prop_count(), 0);
}
#[test]
fn array_shift_removes_first() {
let result = eval("[1,2,3].shift()").unwrap();
assert_eq!(result.as_int(), 1);
}
#[test]
fn array_unshift_adds_front() {
let result = eval("[3].unshift(1,2)").unwrap();
assert_eq!(result.as_int(), 3);
}
#[test]
fn array_fill_replaces() {
let result = eval("[1,2,3,4].fill(0,1,3)").unwrap();
let obj = unsafe { &*result.as_js_object_ptr() };
assert_eq!(obj.prop_count(), 4);
}
#[test]
fn array_at_positive() {
let result = eval("[10,20,30].at(1)").unwrap();
assert_eq!(result.as_int(), 20);
}
#[test]
fn array_at_negative() {
let result = eval("[10,20,30].at(-1)").unwrap();
assert_eq!(result.as_int(), 30);
}
#[test]
fn array_last_index_of_found() {
let result = eval("[1,2,3,2].lastIndexOf(2)").unwrap();
assert_eq!(result.as_int(), 3);
}
#[test]
fn array_last_index_of_not_found() {
let result = eval("[1,2,3].lastIndexOf(99)").unwrap();
assert_eq!(result.as_int(), -1);
}
#[test]
fn array_sort_default() {
let result = eval("[3,1,2].sort()").unwrap();
let obj = unsafe { &*result.as_js_object_ptr() };
assert_eq!(obj.prop_count(), 3);
}
#[test]
fn array_copy_within_copies() {
let result = eval("[1,2,3,4,5].copyWithin(0,3)").unwrap();
assert!(result.is_object());
}
#[test]
fn array_fill_single() {
let result = eval("[1,2,3].fill(0)").unwrap();
let obj = unsafe { &*result.as_js_object_ptr() };
assert_eq!(obj.get_prop_at(0).as_int(), 0);
}
@@ -51,3 +51,27 @@ fn object_define_property_sets_value() {
let result = eval("Object.defineProperty({},'x',{value:42})").unwrap();
assert!(result.is_object());
}
#[test]
fn object_get_prototype_of() {
let result = eval("Object.getPrototypeOf({})").unwrap();
assert!(result.is_object() || result.is_null());
}
#[test]
fn object_has_own_true() {
let result = eval("Object.hasOwn({a:1}, 'a')").unwrap();
assert!(result.is_bool());
}
#[test]
fn object_entries_returns_array() {
let result = eval("Object.entries({a:1,b:2})").unwrap();
assert!(result.is_object());
}
#[test]
fn object_values_returns_array() {
let result = eval("Object.values({a:1,b:2})").unwrap();
assert!(result.is_object());
}
@@ -165,3 +165,45 @@ fn string_search_not_found() {
let result = eval(&mut vm, "'hello'.search('x')").unwrap();
assert_eq!(result.as_int(), -1);
}
#[test]
fn string_trim_start() {
let mut vm = Vm::new();
let result = eval(&mut vm, "' hi '.trimStart()").unwrap();
assert_eq!(to_str(&vm, result), "hi ");
}
#[test]
fn string_trim_end() {
let mut vm = Vm::new();
let result = eval(&mut vm, "' hi '.trimEnd()").unwrap();
assert_eq!(to_str(&vm, result), " hi");
}
#[test]
fn string_code_point_at_ascii() {
let mut vm = Vm::new();
let result = eval(&mut vm, "'ABC'.codePointAt(1)").unwrap();
assert_eq!(result.as_int(), 66);
}
#[test]
fn string_normalize_nfc() {
let mut vm = Vm::new();
let result = eval(&mut vm, "'hello'.normalize('NFC')").unwrap();
assert_eq!(to_str(&vm, result), "hello");
}
#[test]
fn string_match_all() {
let mut vm = Vm::new();
let result = eval(&mut vm, "'aba'.matchAll(/a/g)").unwrap();
assert!(result.is_object());
}
#[test]
fn string_replace_all() {
let mut vm = Vm::new();
let result = eval(&mut vm, "'aba'.replaceAll('a', 'c')").unwrap();
assert_eq!(to_str(&vm, result), "cbc");
}
@@ -119,3 +119,10 @@ fn symbol_not_equals_string() {
let result = eval(&mut vm, "Symbol() === 'symbol'").unwrap();
assert!(!result.as_bool());
}
#[test]
fn symbol_iterator_exists() {
let mut vm = Vm::new();
let result = eval(&mut vm, "typeof Symbol.iterator").unwrap();
assert_eq!(to_str(&vm, result), "object");
}