From f5c109c80eaedbead12b01352f620575d0289cd8 Mon Sep 17 00:00:00 2001 From: pjh456 <147148383@qq.com> Date: Tue, 22 Sep 2026 10:13:30 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=B1=BB=E5=9E=8B=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=B4=A2=E5=BC=95=E5=8A=A9=E6=89=8B=E5=AF=B9=20BigInt?= =?UTF-8?q?=20=E5=AE=9E=E5=8F=82=E6=8C=89=E6=88=AA=E6=96=AD=E8=AF=AD?= =?UTF-8?q?=E4=B9=89=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/oxide_builtins/src/typed_array.rs | 41 +++++++++++++++++------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/crates/oxide_builtins/src/typed_array.rs b/crates/oxide_builtins/src/typed_array.rs index eb1d3655..e5f330e4 100644 --- a/crates/oxide_builtins/src/typed_array.rs +++ b/crates/oxide_builtins/src/typed_array.rs @@ -35,6 +35,15 @@ fn range_error(vm: &mut H, msg: &str) -> JsValue { } fn to_index(vm: &mut H, value: JsValue, msg: &str) -> Result { + // ToIndex 的 BigInt 面:整数原语截断即自身,负值或超 2^53-1 抛 + // RangeError(与数值臂同消息),无 NaN 概念。 + if value.is_bigint() { + let b = vm.bigint_value(value); + if *b < BigInt::from(0) || *b > BigInt::from(9_007_199_254_740_991u64) { + return Err(range_error(vm, msg)); + } + return Ok(b.to_usize().unwrap_or(0)); + } let n = match vm.coerce_number_bounded(value) { Ok(n) => n, Err(e) => return Err(crate::iterator::engine_error(vm, &e)), @@ -51,19 +60,27 @@ fn to_index(vm: &mut H, value: JsValue, msg: &str) -> Result(vm: &mut H, value: JsValue, len: usize) -> Result { - let n = match vm.coerce_number_bounded(value) { - Ok(n) => n, - Err(e) => return Err(crate::iterator::engine_error(vm, &e)), + // BigInt 面:ToIntegerOrInfinity 对整数原语即截断自身(无 NaN/±Infinity 臂), + // 超 isize 域饱和到对应端点后走同一负/正夹取尾段。 + let int: isize = if value.is_bigint() { + let b = vm.bigint_value(value); + b.to_isize() + .unwrap_or(if *b < BigInt::from(0) { isize::MIN } else { isize::MAX }) + } else { + let n = match vm.coerce_number_bounded(value) { + Ok(n) => n, + Err(e) => return Err(crate::iterator::engine_error(vm, &e)), + }; + if n.is_nan() { + return Ok(0); + } + // ToIntegerOrInfinity:±Infinity 不截断,负无穷归 0、正无穷归 len, + // 避免饱和成 isize::MIN 后取负溢出。 + if n.is_infinite() { + return Ok(if n < 0.0 { 0 } else { len }); + } + n.trunc() as isize }; - if n.is_nan() { - return Ok(0); - } - // ToIntegerOrInfinity:±Infinity 不截断,负无穷归 0、正无穷归 len, - // 避免饱和成 isize::MIN 后取负溢出。 - if n.is_infinite() { - return Ok(if n < 0.0 { 0 } else { len }); - } - let int = n.trunc() as isize; if int < 0 { Ok(len.saturating_sub(int.unsigned_abs())) } else {