feat: 支持 Date/Map/Set 成员方法

This commit is contained in:
爱特每羊队
2026-06-08 03:25:07 +08:00
parent 4ed8379ed9
commit 55b544da82
15 changed files with 2073 additions and 0 deletions
@@ -0,0 +1,73 @@
use oxide_types::value::JsValue;
use oxide_vm::builtins::map::{
map_clear, map_constructor as new_map, map_delete, map_get, map_has, map_set, map_size,
};
use oxide_vm::vm::Vm;
#[test]
fn tmap_constructor_returns_object() {
let mut vm = Vm::new();
let r = new_map(&mut vm, &[0]).unwrap();
assert!(r.is_object());
}
#[test]
fn tmap_set_get() {
let mut vm = Vm::new();
let m = new_map(&mut vm, &[0]).unwrap();
vm.set_reg(0, m);
vm.set_reg(1, JsValue::float(42.0));
vm.set_reg(2, JsValue::float(100.0));
map_set(&mut vm, &[0, 1, 2]).unwrap();
assert_eq!(map_get(&mut vm, &[0, 1]).unwrap().as_double(), 100.0);
}
#[test]
fn tmap_get_missing() {
let mut vm = Vm::new();
let m = new_map(&mut vm, &[0]).unwrap();
vm.set_reg(0, m);
vm.set_reg(1, JsValue::float(99.0));
assert!(map_get(&mut vm, &[0, 1]).unwrap().is_undefined());
}
#[test]
fn tmap_has_check() {
let mut vm = Vm::new();
let m = new_map(&mut vm, &[0]).unwrap();
vm.set_reg(0, m);
vm.set_reg(1, JsValue::float(1.0));
vm.set_reg(2, JsValue::float(2.0));
map_set(&mut vm, &[0, 1, 2]).unwrap();
assert!(map_has(&mut vm, &[0, 1]).unwrap().as_bool());
assert!(!map_has(&mut vm, &[0, 99]).unwrap().as_bool());
}
#[test]
fn tmap_delete_works() {
let mut vm = Vm::new();
let m = new_map(&mut vm, &[0]).unwrap();
vm.set_reg(0, m);
vm.set_reg(1, JsValue::float(7.0));
vm.set_reg(2, JsValue::float(8.0));
map_set(&mut vm, &[0, 1, 2]).unwrap();
assert!(map_delete(&mut vm, &[0, 1]).unwrap().as_bool());
assert!(!map_has(&mut vm, &[0, 1]).unwrap().as_bool());
}
#[test]
fn tmap_size_and_clear() {
let mut vm = Vm::new();
let m = new_map(&mut vm, &[0]).unwrap();
vm.set_reg(0, m);
assert_eq!(map_size(&mut vm, &[0]).unwrap().as_double(), 0.0);
vm.set_reg(1, JsValue::float(1.0));
vm.set_reg(2, JsValue::float(10.0));
map_set(&mut vm, &[0, 1, 2]).unwrap();
vm.set_reg(1, JsValue::float(2.0));
vm.set_reg(2, JsValue::float(20.0));
map_set(&mut vm, &[0, 1, 2]).unwrap();
assert_eq!(map_size(&mut vm, &[0]).unwrap().as_double(), 2.0);
map_clear(&mut vm, &[0]).unwrap();
assert_eq!(map_size(&mut vm, &[0]).unwrap().as_double(), 0.0);
}