fix: Date 普通调用忽略参数并移除 RFC2822 panic

This commit is contained in:
2026-08-16 10:58:12 +08:00
parent d237b4b70e
commit e97473c8ad
2 changed files with 66 additions and 13 deletions
+13 -13
View File
@@ -140,8 +140,9 @@ fn parse_iso_timestamp(s: &str) -> f64 {
f64::NAN f64::NAN
} }
/// JS `Date()` 构造逻辑:无参取当前时间;单参支持时间戳/字符串/Date 对象; /// JS `Date()` 构造逻辑:构造调用无参取当前时间、单参支持时间戳/字符串/Date 对象、
/// 多参按本地时间字段(年/月/日/时/分/秒/毫秒)组合。非构造调用返回日期字符串。 /// 多参按本地时间字段(年/月/日/时/分/秒/毫秒)组合;普通调用忽略全部参数,
/// 返回当前时刻的日期字符串(与 `new Date().toString()` 同格式)。
pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult { pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
let this_val = vm.reg(if args.is_empty() { 0 } else { args[0] }); let this_val = vm.reg(if args.is_empty() { 0 } else { args[0] });
let is_ctor_call = this_val.is_object() && { let is_ctor_call = this_val.is_object() && {
@@ -164,6 +165,16 @@ pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
} }
}; };
if !is_ctor_call {
// 普通调用:忽略全部参数(不做任何强制转换),返回当前时刻字符串。
let ms = Utc::now().timestamp_millis() as f64;
let s = match dt_from_ms(ms) {
Some(dt) => dt.format("%a %b %d %Y %H:%M:%S %Z %z").to_string(),
None => "Invalid Date".to_string(),
};
return NativeResult::Ok(vm.new_string_owned(s));
}
let timestamp = if args.len() < 2 { let timestamp = if args.len() < 2 {
Utc::now().timestamp_millis() as f64 Utc::now().timestamp_millis() as f64
} else if args.len() > 2 { } else if args.len() > 2 {
@@ -222,17 +233,6 @@ pub fn date_constructor<H: VmHost>(vm: &mut H, args: &[u8]) -> NativeResult {
} }
}; };
if !is_ctor_call {
let s = if timestamp.is_finite() {
dt_from_ms(timestamp)
.map(|dt| dt.to_rfc2822())
.unwrap_or_else(|| "Invalid Date".to_string())
} else {
"Invalid Date".to_string()
};
return NativeResult::Ok(vm.new_string_owned(s));
}
let mut obj = JsObject::new_empty( let mut obj = JsObject::new_empty(
EMPTY_SHAPE_ID, EMPTY_SHAPE_ID,
JsValue::from_js_object(vm.session().builtin_world().date_proto.as_ptr() as *mut JsObject), JsValue::from_js_object(vm.session().builtin_world().date_proto.as_ptr() as *mut JsObject),
@@ -49,6 +49,59 @@ fn date_parse_iso_with_offset() {
assert_eq!(r.as_double(), 1577836800000.0); assert_eq!(r.as_double(), 1577836800000.0);
} }
// ── Date() 普通调用(非构造,S15.9.2.1)──
#[test]
fn date_call_returns_current_time_string() {
let mut vm = Vm::new();
// 无参与多参均返回当前时刻字符串,参数被完全忽略。
let r = eval(&mut vm, "Date()").unwrap();
assert!(r.is_string());
let r = eval(&mut vm, "Date(0, 0, 0)").unwrap();
assert!(r.is_string());
// 参数不做 ToNumber:toString 抛错的对象也不触发异常。
let r = eval(&mut vm, "Date({ toString() { throw 1; } })").unwrap();
assert!(r.is_string());
}
#[test]
fn date_call_via_bind_no_panic() {
let mut vm = Vm::new();
// bind 转发后 this=null 落入普通调用路径;0,0,0 被忽略,不得按参数推导
// 时间戳(此前非 UTC+0 时区 year 0 回绕成负年触发 RFC2822 panic)。
let r = eval(&mut vm, "Date.bind(null)(0, 0, 0)").unwrap();
assert!(r.is_string());
}
#[test]
fn date_call_output_matches_to_string() {
let mut vm = Vm::new();
// 普通调用输出与 new Date().toString() 同格式同内容(S15.9.2.1_A2)。
// 两次取当前时刻可能跨毫秒边界,故比较固定宽度的日期前缀段 `Www Mmm DD YYYY`
// (仅跨日才变)并以长度一致锁定整体格式。
let r = eval(
&mut vm,
concat!(
"var a = Date(); var b = (new Date()).toString(); ",
"(a.slice(0, 15) === b.slice(0, 15) && a.length === b.length) ? 1 : 0"
),
)
.unwrap();
assert_eq!(r.as_int(), 1);
}
#[test]
fn date_constructor_path_unaffected() {
let mut vm = Vm::new();
// 构造路径分派不受影响:仍返回 Date 对象且时间戳语义不变。
let r = eval(&mut vm, "typeof new Date()").unwrap();
assert_eq!(str_val(&vm, r), "object");
let r = eval(&mut vm, "new Date(0).getTime()").unwrap();
assert_eq!(r.as_double(), 0.0);
let r = eval(&mut vm, "new Date(2020, 0, 15).getFullYear()").unwrap();
assert_eq!(r.as_double(), 2020.0);
}
// ── 经 JS 调用 new Date() 构造器 ── // ── 经 JS 调用 new Date() 构造器 ──
#[test] #[test]