fix: 修复同步调用参数限制与内建语义错误

This commit is contained in:
爱特每羊队
2026-06-23 14:38:58 +08:00
parent 933612c89d
commit 51ebcbdb9b
13 changed files with 675 additions and 189 deletions
@@ -318,6 +318,33 @@ fn array_sort_propagates_comparator_exception() {
assert_eq!(to_str(&vm, result), "cmp");
}
#[test]
fn array_callback_type_errors_are_not_sentinels() {
let (vm, result) = eval("try { [1].map(null) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
let (vm, result) = eval("try { [1].filter(undefined) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
let (vm, result) = eval("try { [1].findIndex(0) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
}
#[test]
fn array_reduce_and_sort_invalid_usage_throw_type_error() {
let (vm, result) = eval("try { [].reduce(function(a, b) { return a + b; }) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
let (vm, result) = eval("try { [].reduceRight(function(a, b) { return a + b; }) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
let (vm, result) = eval("try { [1].sort(null) } catch (e) { e.name }").unwrap();
assert_eq!(to_str(&vm, result), "TypeError");
let (vm, result) = eval("[2,1].sort(undefined).join(',')").unwrap();
assert_eq!(to_str(&vm, result), "1,2");
}
#[test]
fn array_copy_within_copies() {
let (_vm, result) = eval("[1,2,3,4,5].copyWithin(0,3)").unwrap();
@@ -126,3 +126,13 @@ fn boolean_primitive_value_of_autoboxes() {
let r = eval(&mut vm, "(true).constructor === Boolean").unwrap();
assert_eq!(r, JsValue::bool(true));
}
#[test]
fn boolean_boxes_participate_in_numeric_coercion() {
let mut vm = Vm::new();
let r = eval(&mut vm, "+new Boolean(true)").unwrap();
assert_eq!(r.as_double(), 1.0);
let r = eval(&mut vm, "new Boolean(false) | 0").unwrap();
assert_eq!(r.as_int(), 0);
}
@@ -129,3 +129,10 @@ fn date_to_utc_string_not_empty() {
let r = eval(&mut vm, "new Date(0).toUTCString()").unwrap();
assert!(r.is_string());
}
#[test]
fn date_objects_use_bounded_numeric_coercion() {
let mut vm = Vm::new();
let r = eval(&mut vm, "+new Date(0)").unwrap();
assert_eq!(r.as_double(), 0.0);
}
+17
View File
@@ -213,3 +213,20 @@ fn test_typed_array_survives_promote_reset() {
fn test_flat_infinity_bounded() {
assert_eq!(eval("[1,[2,[3,[4]]]].flat(Infinity).length"), "4");
}
#[test]
fn regression_bounded_object_numeric_coercion() {
assert_eq!(eval("+new Number(1)"), "1");
assert_eq!(eval("+new Boolean(true)"), "1");
assert_eq!(eval("new Boolean(false) | 0"), "0");
assert_eq!(eval("+({ valueOf: function() { return 7; } })"), "7");
assert_eq!(eval("+({ toString: function() { return '9'; } })"), "9");
assert_eq!(
eval("+({ valueOf: function() { return {}; }, toString: function() { return {}; } })"),
"vm error: TypeError: Cannot convert object to primitive value"
);
assert_eq!(
eval("+({ valueOf: function() { throw new RangeError('coerce'); } })"),
"vm error: uncaught RangeError: coerce"
);
}