style: 自动格式化

This commit is contained in:
2026-06-30 15:09:19 +08:00
parent 32092bddd5
commit 9f3e353f56
5 changed files with 75 additions and 27 deletions
+37 -12
View File
@@ -85,8 +85,7 @@ fn get_this_arraylike<H: VmHost>(vm: &mut H, val: JsValue) -> Result<(*mut JsObj
// 读取 length 属性
let length_key = vm.new_string("length");
let length_si = vm.property_key_si(length_key);
let len_val = vm.ordinary_get(unsafe { &*ptr }, length_si, val)
.unwrap_or(JsValue::int(0));
let len_val = vm.ordinary_get(unsafe { &*ptr }, length_si, val).unwrap_or(JsValue::int(0));
let len_num = vm.coerce_number_bounded(len_val).unwrap_or(0.0);
let len = if !len_num.is_finite() || len_num <= 0.0 {
0
@@ -262,11 +261,19 @@ pub fn array_slice<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let n_isize = n as isize;
let rel_start = if args.len() > 1 {
oxide_runtime_api::to_integer_or_infinity(vm.reg(args[1])) as isize
} else { 0 };
} else {
0
};
let rel_end = if args.len() > 2 {
oxide_runtime_api::to_integer_or_infinity(vm.reg(args[2])) as isize
} else { n_isize };
let start = if rel_start < 0 { (n_isize + rel_start).max(0) } else { rel_start.min(n_isize) } as usize;
} else {
n_isize
};
let start = if rel_start < 0 {
(n_isize + rel_start).max(0)
} else {
rel_start.min(n_isize)
} as usize;
let end = if rel_end < 0 { (n_isize + rel_end).max(0) } else { rel_end.min(n_isize) } as usize;
let count = end.saturating_sub(start);
let new_arr = create_new_array(vm, count);
@@ -433,7 +440,11 @@ pub fn array_index_of<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
(f as usize).min(n)
} else {
let from = n as f64 + f;
if from < 0.0 { 0 } else { from as usize }
if from < 0.0 {
0
} else {
from as usize
}
}
} else {
0
@@ -466,7 +477,11 @@ pub fn array_includes<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
(f as usize).min(n)
} else {
let from = n as f64 + f;
if from < 0.0 { 0 } else { from as usize }
if from < 0.0 {
0
} else {
from as usize
}
}
} else {
0
@@ -489,7 +504,9 @@ fn same_value_zero(a: JsValue, b: JsValue) -> bool {
if a_is_num && b_is_num {
let av = if a.is_int() { a.as_int() as f64 } else { a.as_double() };
let bv = if b.is_int() { b.as_int() as f64 } else { b.as_double() };
if av.is_nan() && bv.is_nan() { return true; }
if av.is_nan() && bv.is_nan() {
return true;
}
return av == bv; // +0 == -0 in Rust f64
}
oxide_runtime_api::strict_equality(a, b)
@@ -606,12 +623,16 @@ pub fn array_map<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
for i in 0..n {
let elem = arraylike_get(vm, arr_ptr, is_array, i);
match invoke_native_callback(vm, callback_val, this_val, &[elem, JsValue::int(i as i32), vm.reg(args[0])]) {
NativeResult::Ok(mapped) => unsafe { (*new_arr).set_prop_at(i, mapped); },
NativeResult::Ok(mapped) => unsafe {
(*new_arr).set_prop_at(i, mapped);
},
NativeResult::Err(err) => return NativeResult::Err(err),
NativeResult::TailCall { .. } => return unexpected_tail_call_error(vm),
}
}
unsafe { (*new_arr).set_prop_count(n); }
unsafe {
(*new_arr).set_prop_count(n);
}
NativeResult::Ok(JsValue::from_js_object(new_arr))
}
@@ -635,7 +656,9 @@ pub fn array_filter<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let elem = arraylike_get(vm, arr_ptr, is_array, i);
match invoke_native_callback(vm, callback_val, this_val, &[elem, JsValue::int(i as i32), vm.reg(args[0])]) {
NativeResult::Ok(result_val) => {
if oxide_runtime_api::to_boolean(result_val) { kept.push(elem); }
if oxide_runtime_api::to_boolean(result_val) {
kept.push(elem);
}
}
NativeResult::Err(err) => return NativeResult::Err(err),
NativeResult::TailCall { .. } => return unexpected_tail_call_error(vm),
@@ -997,7 +1020,9 @@ pub fn array_last_index_of<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let from_index_isize: isize = if args.len() >= 3 {
let v = vm.reg(args[2]);
let f = vm.coerce_number_bounded(v).unwrap_or(0.0);
if f.is_nan() { return NativeResult::Ok(JsValue::int(-1)); }
if f.is_nan() {
return NativeResult::Ok(JsValue::int(-1));
}
let f = f.trunc();
if f >= 0.0 {
(f as isize).min(n as isize - 1)
+12 -3
View File
@@ -82,11 +82,20 @@ pub fn function_apply<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
pub fn function_bind<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
if args.is_empty() {
return NativeResult::Err(crate::error::create_type_error(vm, "Function.prototype.bind called on null or undefined"));
return NativeResult::Err(crate::error::create_type_error(
vm,
"Function.prototype.bind called on null or undefined",
));
}
let target_val = vm.reg(args[0]);
if !target_val.is_object() || target_val.as_js_object_ptr().is_null() || !unsafe { &*target_val.as_js_object_ptr() }.is_function() {
return NativeResult::Err(crate::error::create_type_error(vm, "Function.prototype.bind called on non-function"));
if !target_val.is_object()
|| target_val.as_js_object_ptr().is_null()
|| !unsafe { &*target_val.as_js_object_ptr() }.is_function()
{
return NativeResult::Err(crate::error::create_type_error(
vm,
"Function.prototype.bind called on non-function",
));
}
let bound_this = if args.len() > 1 { vm.reg(args[1]) } else { JsValue::undefined() };
+20 -5
View File
@@ -529,7 +529,11 @@ pub fn string_split<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
// ToUint32(limit), default 2^32-1
let limit = if args.len() > 2 {
let l = oxide_runtime_api::to_integer_or_infinity(vm.reg(args[2]));
if l.is_infinite() { u32::MAX as usize } else { (l.max(0.0).trunc() as u64).min(u32::MAX as u64) as usize }
if l.is_infinite() {
u32::MAX as usize
} else {
(l.max(0.0).trunc() as u64).min(u32::MAX as u64) as usize
}
} else {
u32::MAX as usize
};
@@ -542,12 +546,18 @@ pub fn string_split<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let mut parts: Vec<String> = Vec::new();
let mut last_end = 0;
for caps in regex.captures_iter(&s) {
if parts.len() >= limit { break; }
if parts.len() >= limit {
break;
}
let full = caps.get(0).unwrap();
parts.push(s[last_end..full.start()].to_string());
if parts.len() >= limit { break; }
if parts.len() >= limit {
break;
}
for i in 1..caps.len() {
if parts.len() >= limit { break; }
if parts.len() >= limit {
break;
}
parts.push(caps.get(i).map(|m| m.as_str()).unwrap_or("").to_string());
}
last_end = full.end();
@@ -603,7 +613,12 @@ pub fn string_replace<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let output = format!("{}{}{}", &s[..full.start()], &result_str, &s[full.end()..]);
return NativeResult::Ok(vm.new_string(&output));
}
Err(err) => return NativeResult::Err(crate::error::create_type_error(vm, &format!("replace replacer: {}", err))),
Err(err) => {
return NativeResult::Err(crate::error::create_type_error(
vm,
&format!("replace replacer: {}", err),
))
}
}
}
return NativeResult::Ok(vm.new_string(&s));
+2 -1
View File
@@ -251,7 +251,8 @@ fn make_pair(
proto.ensure_hash_props().push(JsValue::undefined()); // placeholder
ctor.ensure_hash_props().push(JsValue::undefined()); // placeholder for "prototype"
// Set the actual name value immediately (ctor vec[1])
ctor.ensure_hash_props().push(JsValue::perm_string(string_forge.string_ptr(name_si)));
ctor.ensure_hash_props()
.push(JsValue::perm_string(string_forge.string_ptr(name_si)));
// Set attribute metadata: .prototype (ctor[0]) = writable:false, enumerable:false, configurable:false
ctor.set_data_meta(0u32, oxide_types::object::PropAttributes::new(false, false, false));
// .name (ctor[1]) = writable:false, enumerable:false, configurable:true
+3 -5
View File
@@ -537,7 +537,8 @@ fn run_test_inner(
|| e.contains("Expected SameValue")
|| e.contains("cannot assign to read-only property")
|| e.contains("cannot delete non-configurable property")
|| e.contains("private field") // class private fields not implemented
|| e.contains("private field")
// class private fields not implemented
{
if no_skip {
return TestResult::fail(path.to_path_buf(), dur, format!("vm error: {e}"));
@@ -973,10 +974,7 @@ fn process_path(
if !no_skip {
let path_str = path.to_string_lossy().replace('\\', "/");
if path_str.contains("/eval/")
|| path_str.contains("/function-ctor/")
|| path_str.contains("/realm/")
{
if path_str.contains("/eval/") || path_str.contains("/function-ctor/") || path_str.contains("/realm/") {
return TestResult::skip(path.to_path_buf(), "unsupported class/eval feature excluded".into());
}
if let Some(reason) = is_skipped(&meta) {