diff --git a/js/src/jsnum.cpp b/js/src/jsnum.cpp index c64a1149f0..b778c8d739 100644 --- a/js/src/jsnum.cpp +++ b/js/src/jsnum.cpp @@ -1614,6 +1614,27 @@ js::ToInt32Slow(JSContext* cx, const HandleValue v, int32_t* out) return true; } +bool +js::ToInt32OrBigIntSlow(JSContext* cx, MutableHandleValue vp) +{ + MOZ_ASSERT(!vp.isInt32()); + if (vp.isDouble()) { + vp.setInt32(ToInt32(vp.toDouble())); + return true; + } + + if (!ToNumeric(cx, vp)) { + return false; + } + + if (vp.isBigInt()) { + return true; + } + + vp.setInt32(ToInt32(vp.toNumber())); + return true; +} + JS_PUBLIC_API(bool) js::ToUint32Slow(JSContext* cx, const HandleValue v, uint32_t* out) { diff --git a/js/src/jsnum.h b/js/src/jsnum.h index 295d237c22..9866a91eeb 100644 --- a/js/src/jsnum.h +++ b/js/src/jsnum.h @@ -376,6 +376,18 @@ ToNumeric(ExclusiveContext* cx, JS::MutableHandleValue vp) return ToNumericSlow(cx, vp); } +bool +ToInt32OrBigIntSlow(JSContext* cx, JS::MutableHandleValue vp); + +MOZ_ALWAYS_INLINE MOZ_MUST_USE bool +ToInt32OrBigInt(JSContext* cx, JS::MutableHandleValue vp) +{ + if (vp.isInt32()) { + return true; + } + return ToInt32OrBigIntSlow(cx, vp); +} + void FIX_FPU(); } /* namespace js */