refactor: 哈希表存储属性以支持无上限属性个数

This commit is contained in:
爱特每羊队
2026-06-07 15:25:47 +08:00
parent 52fbd3c3d4
commit f6e635cdbe
19 changed files with 779 additions and 599 deletions
+36 -38
View File
@@ -100,7 +100,7 @@ pub fn array_constructor(vm: &mut Vm, args: &[u8]) -> NativeResult {
));
for i in 0..n_elems {
unsafe {
(*arr).set_prop(i as u8, vm.reg(args[1 + i]));
(*arr).set_prop_at(i as u8, vm.reg(args[1 + i]));
}
}
unsafe {
@@ -113,10 +113,9 @@ pub fn array_push(vm: &mut Vm, args: &[u8]) -> NativeResult {
let arr_ptr = get_this_array_ref(vm.reg(args[0]))?;
let arr = unsafe { &mut *arr_ptr };
let mut len = arr.prop_count() as usize;
let bump = vm.epoch().bump();
for &arg_reg in args.iter().skip(1) {
let val = vm.reg(arg_reg);
arr.set_prop_expand(len as u8, val, bump);
arr.set_prop_at(len as u8, val);
len += 1;
}
let new_len = (len as u8).min(31);
@@ -131,7 +130,7 @@ pub fn array_pop(vm: &mut Vm, args: &[u8]) -> NativeResult {
if len == 0 {
return Ok(JsValue::undefined());
}
let last = arr.get_prop(len - 1);
let last = arr.get_prop_at(len - 1);
arr.set_prop_count(len - 1);
Ok(last)
}
@@ -157,7 +156,7 @@ pub fn array_slice(vm: &mut Vm, args: &[u8]) -> NativeResult {
let new_arr = create_new_array(vm, count);
unsafe {
for i in 0..count {
(*new_arr).set_prop(i as u8, arr.get_prop((start + i) as u8));
(*new_arr).set_prop_at(i as u8, arr.get_prop_at((start + i) as u8));
}
(*new_arr).set_prop_count(count.min(31) as u8);
}
@@ -192,26 +191,25 @@ pub fn array_splice(vm: &mut Vm, args: &[u8]) -> NativeResult {
let mut removed: Vec<JsValue> = Vec::new();
for i in 0..delete_count {
removed.push(arr.get_prop((start + i) as u8));
removed.push(arr.get_prop_at((start + i) as u8));
}
let bump = vm.epoch().bump();
if insert_count > delete_count {
let shift = insert_count - delete_count;
for i in (start + delete_count..n).rev() {
let val = arr.get_prop(i as u8);
arr.set_prop_expand((i + shift) as u8, val, bump);
let val = arr.get_prop_at(i as u8);
arr.set_prop_at((i + shift) as u8, val);
}
} else if insert_count < delete_count {
let shift = delete_count - insert_count;
for i in start + delete_count..n {
let val = arr.get_prop(i as u8);
arr.set_prop((i - shift) as u8, val);
let val = arr.get_prop_at(i as u8);
arr.set_prop_at((i - shift) as u8, val);
}
}
for i in 0..insert_count {
arr.set_prop((start + i) as u8, vm.reg(args[3 + i]));
arr.set_prop_at((start + i) as u8, vm.reg(args[3 + i]));
}
let new_len = n + insert_count - delete_count;
@@ -220,7 +218,7 @@ pub fn array_splice(vm: &mut Vm, args: &[u8]) -> NativeResult {
let removed_arr = create_new_array(vm, removed.len());
unsafe {
for (i, val) in removed.iter().enumerate() {
(*removed_arr).set_prop(i as u8, *val);
(*removed_arr).set_prop_at(i as u8, *val);
}
(*removed_arr).set_prop_count(removed.len().min(31) as u8);
}
@@ -233,7 +231,7 @@ pub fn array_concat(vm: &mut Vm, args: &[u8]) -> NativeResult {
let n = arr.prop_count() as usize;
let mut all: Vec<JsValue> = Vec::new();
for i in 0..n {
all.push(arr.get_prop(i as u8));
all.push(arr.get_prop_at(i as u8));
}
for &arg_reg in args.iter().skip(1) {
let val = vm.reg(arg_reg);
@@ -244,7 +242,7 @@ pub fn array_concat(vm: &mut Vm, args: &[u8]) -> NativeResult {
if o.is_array() {
let on = o.prop_count() as usize;
for i in 0..on {
all.push(o.get_prop(i as u8));
all.push(o.get_prop_at(i as u8));
}
continue;
}
@@ -255,7 +253,7 @@ pub fn array_concat(vm: &mut Vm, args: &[u8]) -> NativeResult {
let new_arr = create_new_array(vm, all.len());
unsafe {
for (i, val) in all.iter().enumerate() {
(*new_arr).set_prop(i as u8, *val);
(*new_arr).set_prop_at(i as u8, *val);
}
(*new_arr).set_prop_count(all.len().min(31) as u8);
}
@@ -274,7 +272,7 @@ pub fn array_join(vm: &mut Vm, args: &[u8]) -> NativeResult {
let sf = vm.kernel().string_forge().as_ref();
let parts: Vec<String> = (0..n)
.map(|i| {
let v = arr.get_prop(i as u8);
let v = arr.get_prop_at(i as u8);
if v.is_undefined() || v.is_null() {
String::new()
} else {
@@ -296,7 +294,7 @@ pub fn array_index_of(vm: &mut Vm, args: &[u8]) -> NativeResult {
}
let target = vm.reg(args[1]);
for i in 0..n {
if coercion::strict_equality(arr.get_prop(i as u8), target) {
if coercion::strict_equality(arr.get_prop_at(i as u8), target) {
return Ok(JsValue::int(i as i32));
}
}
@@ -312,7 +310,7 @@ pub fn array_includes(vm: &mut Vm, args: &[u8]) -> NativeResult {
}
let target = vm.reg(args[1]);
for i in 0..n {
let elem = arr.get_prop(i as u8);
let elem = arr.get_prop_at(i as u8);
if elem.is_double() && target.is_double() {
let ea = elem.as_double();
let ta = target.as_double();
@@ -334,9 +332,9 @@ pub fn array_reverse(vm: &mut Vm, args: &[u8]) -> NativeResult {
let mut i = 0;
let mut j = n.saturating_sub(1);
while i < j {
let tmp = arr.get_prop(i as u8);
arr.set_prop(i as u8, arr.get_prop(j as u8));
arr.set_prop(j as u8, tmp);
let tmp = arr.get_prop_at(i as u8);
arr.set_prop_at(i as u8, arr.get_prop_at(j as u8));
arr.set_prop_at(j as u8, tmp);
i += 1;
j = j.saturating_sub(1);
}
@@ -363,7 +361,7 @@ pub fn array_flat(vm: &mut Vm, args: &[u8]) -> NativeResult {
let o = unsafe { &*ptr };
if o.is_array() {
let on = o.prop_count() as usize;
let sub: Vec<JsValue> = (0..on).map(|i| o.get_prop(i as u8)).collect();
let sub: Vec<JsValue> = (0..on).map(|i| o.get_prop_at(i as u8)).collect();
let flat = flatten(&sub, remaining_depth - 1);
out.extend(flat);
continue;
@@ -375,12 +373,12 @@ pub fn array_flat(vm: &mut Vm, args: &[u8]) -> NativeResult {
out
}
let all: Vec<JsValue> = (0..n).map(|i| arr.get_prop(i as u8)).collect();
let all: Vec<JsValue> = (0..n).map(|i| arr.get_prop_at(i as u8)).collect();
let flat = flatten(&all, depth);
let new_arr = create_new_array(vm, flat.len());
unsafe {
for (i, val) in flat.iter().enumerate() {
(*new_arr).set_prop(i as u8, *val);
(*new_arr).set_prop_at(i as u8, *val);
}
(*new_arr).set_prop_count(flat.len().min(31) as u8);
}
@@ -403,7 +401,7 @@ pub fn array_for_each(vm: &mut Vm, args: &[u8]) -> NativeResult {
JsValue::undefined()
};
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
let _ = invoke_native_callback(
vm,
callback_val,
@@ -431,7 +429,7 @@ pub fn array_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
};
let new_arr = create_new_array(vm, n);
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -439,7 +437,7 @@ pub fn array_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
&[elem, JsValue::int(i as i32), vm.reg(args[0])],
) {
Ok(mapped) => unsafe {
(*new_arr).set_prop(i as u8, mapped);
(*new_arr).set_prop_at(i as u8, mapped);
},
Err(_) => return Err(JsValue::undefined()),
}
@@ -467,7 +465,7 @@ pub fn array_filter(vm: &mut Vm, args: &[u8]) -> NativeResult {
};
let mut kept: Vec<JsValue> = Vec::new();
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -486,7 +484,7 @@ pub fn array_filter(vm: &mut Vm, args: &[u8]) -> NativeResult {
let new_arr = create_new_array(vm, kept.len());
unsafe {
for (i, val) in kept.iter().enumerate() {
(*new_arr).set_prop(i as u8, *val);
(*new_arr).set_prop_at(i as u8, *val);
}
(*new_arr).set_prop_count(kept.len().min(31) as u8);
}
@@ -513,12 +511,12 @@ pub fn array_reduce(vm: &mut Vm, args: &[u8]) -> NativeResult {
accumulator = vm.reg(args[2]);
start_idx = 0;
} else {
accumulator = unsafe { (*arr_ptr).get_prop(0) };
accumulator = unsafe { (*arr_ptr).get_prop_at(0) };
start_idx = 1;
}
let this_val = JsValue::undefined();
for i in start_idx..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -548,7 +546,7 @@ pub fn array_find(vm: &mut Vm, args: &[u8]) -> NativeResult {
JsValue::undefined()
};
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -583,7 +581,7 @@ pub fn array_some(vm: &mut Vm, args: &[u8]) -> NativeResult {
JsValue::undefined()
};
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -618,7 +616,7 @@ pub fn array_every(vm: &mut Vm, args: &[u8]) -> NativeResult {
JsValue::undefined()
};
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -654,7 +652,7 @@ pub fn array_flat_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
};
let mut flat: Vec<JsValue> = Vec::new();
for i in 0..n {
let elem = unsafe { (*arr_ptr).get_prop(i as u8) };
let elem = unsafe { (*arr_ptr).get_prop_at(i as u8) };
match invoke_native_callback(
vm,
callback_val,
@@ -669,7 +667,7 @@ pub fn array_flat_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
if r.is_array() {
let rn = r.prop_count() as usize;
for j in 0..rn {
flat.push(r.get_prop(j as u8));
flat.push(r.get_prop_at(j as u8));
}
continue;
}
@@ -683,7 +681,7 @@ pub fn array_flat_map(vm: &mut Vm, args: &[u8]) -> NativeResult {
let new_arr = create_new_array(vm, flat.len());
unsafe {
for (i, val) in flat.iter().enumerate() {
(*new_arr).set_prop(i as u8, *val);
(*new_arr).set_prop_at(i as u8, *val);
}
(*new_arr).set_prop_count(flat.len().min(31) as u8);
}
+2 -4
View File
@@ -28,8 +28,7 @@ fn create_error_object(
let msg_val = vm.intern(message);
unsafe {
(*obj).set_shape_id(sh_message);
(*obj).set_prop_count(1);
(*obj).set_inline_prop(0, msg_val);
(*obj).push_prop(msg_val);
}
let si_name = sf.intern("name").0;
@@ -37,8 +36,7 @@ fn create_error_object(
let name_val = vm.intern(name);
unsafe {
(*obj).set_shape_id(sh_name);
(*obj).set_prop_count(2);
(*obj).set_inline_prop(1, name_val);
(*obj).push_prop(name_val);
}
JsValue::from_js_object(obj)
+15 -7
View File
@@ -40,8 +40,14 @@ fn invoke_target(
fn bind_dispatcher(vm: &mut Vm, args: &[u8]) -> NativeResult {
let wrapper_val = vm.reg(254);
let wrapper = unsafe { &*wrapper_val.as_js_object_ptr() };
let bound_target = wrapper.get_prop(0);
let bound_this = wrapper.get_prop(1);
let bound_target = wrapper
.hash_props_vec()
.and_then(|v| v.first().map(|b| **b))
.unwrap_or(JsValue::undefined());
let bound_this = wrapper
.hash_props_vec()
.and_then(|v| v.get(1).map(|b| **b))
.unwrap_or(JsValue::undefined());
let n = args.len().saturating_sub(1);
let base = 230u8;
@@ -101,11 +107,14 @@ pub fn function_apply(vm: &mut Vm, args: &[u8]) -> NativeResult {
if !arr_ptr.is_null() {
let arr = unsafe { &*arr_ptr };
if arr.is_array() {
let n = arr.prop_count() as usize;
let n = arr.hash_props_vec().map_or(0, |v| v.len());
let base = 200u8;
arg_regs = (0..n).map(|i| base + i as u8).collect();
for i in 0..n {
vm.set_reg(base + i as u8, arr.get_prop(i as u8));
let vec = arr.hash_props_vec();
if let Some(v) = vec.and_then(|v| v.get(i)) {
vm.set_reg(base + i as u8, **v);
}
}
} else {
arg_regs = Vec::new();
@@ -143,9 +152,8 @@ pub fn function_bind(vm: &mut Vm, args: &[u8]) -> NativeResult {
(*wrapper).set_function(true);
(*wrapper).set_native_fn(Some(bind_dispatcher as *const ()));
(*wrapper).set_native_arg_count(0);
(*wrapper).set_prop_count(2);
(*wrapper).set_inline_prop(0, target_val);
(*wrapper).set_inline_prop(1, bound_this);
(*wrapper).ensure_hash_props().push(Box::new(target_val));
(*wrapper).ensure_hash_props().push(Box::new(bound_this));
}
Ok(JsValue::from_js_object(wrapper))
+36 -35
View File
@@ -9,18 +9,31 @@ use crate::vm::Vm;
fn walk_own_keys(vm: &Vm, obj: &JsObject) -> Vec<(u32, u8)> {
let mut keys: Vec<(u32, u8)> = Vec::new();
let mut shape_id = obj.shape_id();
while shape_id != EMPTY_SHAPE_ID {
if let Some(shape) = vm.kernel().shape_forge().get_shape(shape_id) {
if shape.property_name != 0 {
keys.push((shape.property_name, shape.property_offset));
let shape_id = obj.shape_id();
let mut pos: u8 = 0;
let mut shape_ids = Vec::new();
let mut cursor = Some(shape_id);
while let Some(id) = cursor {
if id == EMPTY_SHAPE_ID {
break;
}
if let Some(shape) = vm.kernel().shape_forge().get_shape(id) {
cursor = shape.parent;
if shape.property_name != u32::MAX {
shape_ids.push(id);
}
shape_id = shape.parent.unwrap_or(EMPTY_SHAPE_ID);
} else {
break;
}
}
keys.reverse();
for id in shape_ids.iter().rev() {
if let Some(shape) = vm.kernel().shape_forge().get_shape(*id) {
if shape.property_name != 0 {
keys.push((shape.property_name, pos));
}
}
pos += 1;
}
keys
}
@@ -62,15 +75,12 @@ pub fn object_keys(vm: &mut Vm, args: &[u8]) -> NativeResult {
n,
vm.epoch().bump(),
));
for (i, k) in key_names.iter().take(n).enumerate() {
for k in key_names.iter().take(n) {
let str_val = vm.intern(k);
unsafe {
(*arr).set_prop(i as u8, str_val);
(*arr).ensure_hash_props().push(Box::new(str_val));
}
}
unsafe {
(*arr).set_prop_count(n as u8);
}
Ok(JsValue::from_js_object(arr))
}
@@ -100,10 +110,8 @@ pub fn object_assign(vm: &mut Vm, args: &[u8]) -> NativeResult {
if target_ptr.is_null() {
return Ok(target_val);
}
let mut target_prop_count;
{
let target = unsafe { &mut *target_ptr };
target_prop_count = target.prop_count() as usize;
for &arg_reg in args.iter().skip(2) {
let source_val = vm.reg(arg_reg);
if !source_val.is_object() {
@@ -116,9 +124,8 @@ pub fn object_assign(vm: &mut Vm, args: &[u8]) -> NativeResult {
let source = unsafe { &*source_ptr };
let source_keys = walk_own_keys(vm, source);
for (_si, offset) in source_keys {
let prop_val = source.get_prop(offset);
target.set_prop_expand(target_prop_count as u8, prop_val, vm.epoch().bump());
target_prop_count += 1;
let prop_val = source.get_prop_at(offset);
target.push_prop(prop_val);
}
}
}
@@ -149,17 +156,16 @@ pub fn object_define_property(vm: &mut Vm, args: &[u8]) -> NativeResult {
let si = vm.kernel().string_forge().intern(&prop_name_str).0;
let shape_forge = vm.kernel().shape_forge().as_ref();
let new_shape = shape_forge.make_shape(unsafe { (&*obj_ptr).shape_id() }, si);
let new_offset;
let value = {
let desc = unsafe { &*desc_ptr };
desc.hash_props_vec()
.and_then(|v| v.first().map(|b| **b))
.unwrap_or(JsValue::undefined())
};
{
let obj = unsafe { &mut *obj_ptr };
new_offset = obj.prop_count();
obj.set_shape_id(new_shape);
obj.set_prop_count(new_offset + 1);
}
let desc = unsafe { &*desc_ptr };
let value = desc.get_prop(0);
unsafe {
(&mut *obj_ptr).set_prop_expand(new_offset, value, vm.epoch().bump());
obj.ensure_hash_props().push(Box::new(value));
}
Ok(obj_val)
}
@@ -186,7 +192,7 @@ pub fn object_get_own_property_descriptor(vm: &mut Vm, args: &[u8]) -> NativeRes
let mut found = false;
for (prop_si, offset) in keys {
if prop_si == si {
found_value = obj.get_prop(offset);
found_value = obj.get_prop_at(offset);
found = true;
break;
}
@@ -206,7 +212,6 @@ pub fn object_get_own_property_descriptor(vm: &mut Vm, args: &[u8]) -> NativeRes
JsValue::from_js_object(desc_proto),
));
let true_val = JsValue::bool(true);
let b = vm.epoch().bump();
let sf = unsafe { &*sf_ptr };
let sh = unsafe { &*sh_ptr };
@@ -214,26 +219,22 @@ pub fn object_get_own_property_descriptor(vm: &mut Vm, args: &[u8]) -> NativeRes
let si_val = sf.intern("value").0;
let sh_val = sh.make_shape(EMPTY_SHAPE_ID, si_val);
d.set_shape_id(sh_val);
d.set_prop_count(1);
d.set_prop_expand(0, found_value, b);
d.ensure_hash_props().push(Box::new(found_value));
let si_wr = sf.intern("writable").0;
let sh_wr = sh.make_shape(sh_val, si_wr);
d.set_shape_id(sh_wr);
d.set_prop_count(2);
d.set_prop_expand(1, true_val, b);
d.ensure_hash_props().push(Box::new(true_val));
let si_en = sf.intern("enumerable").0;
let sh_en = sh.make_shape(sh_wr, si_en);
d.set_shape_id(sh_en);
d.set_prop_count(3);
d.set_prop_expand(2, true_val, b);
d.ensure_hash_props().push(Box::new(true_val));
let si_cf = sf.intern("configurable").0;
let sh_cf = sh.make_shape(sh_en, si_cf);
d.set_shape_id(sh_cf);
d.set_prop_count(4);
d.set_prop_expand(3, true_val, b);
d.ensure_hash_props().push(Box::new(true_val));
Ok(JsValue::from_js_object(desc))
}
+1 -1
View File
@@ -24,7 +24,7 @@ fn make_string_array(vm: &mut Vm, parts: &[String]) -> JsValue {
unsafe {
for (i, s) in parts.iter().enumerate().take(n) {
let sv = vm.intern(s);
(*arr).set_prop(i as u8, sv);
(*arr).set_prop_at(i as u8, sv);
}
(*arr).set_prop_count(n as u8);
}
+1 -1
View File
@@ -284,7 +284,7 @@ pub fn to_object(val: JsValue, vm: &mut Vm) -> Result<JsValue, &'static str> {
.alloc(JsObject::new_empty(EMPTY_SHAPE_ID, proto_val));
let obj_val = JsValue::from_js_object(obj);
let obj_ref = unsafe { &mut *obj };
obj_ref.set_inline_prop(0, val);
obj_ref.ensure_hash_props().push(Box::new(val));
obj_ref.set_prop_count(1);
Ok(obj_val)
}
+407 -333
View File
File diff suppressed because it is too large Load Diff