mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibJS: Avoid cast to out-of-range value in MathObject::abs() fast path
This commit is contained in:
parent
2d6bc21a33
commit
166c8b116f
Notes:
github-actions[bot]
2025-11-30 10:56:35 +00:00
Author: https://github.com/tcl3
Commit: 166c8b116f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6974
2 changed files with 9 additions and 2 deletions
|
|
@ -89,8 +89,11 @@ void MathObject::initialize(Realm& realm)
|
|||
ThrowCompletionOr<Value> MathObject::abs_impl(VM& vm, Value x)
|
||||
{
|
||||
// OPTIMIZATION: Fast path for Int32 values.
|
||||
if (x.is_int32())
|
||||
return Value(AK::abs(x.as_i32()));
|
||||
if (x.is_int32()) {
|
||||
if (auto x_int32 = x.as_i32(); x_int32 != NumericLimits<i32>::min()) [[likely]]
|
||||
return Value(AK::abs(x_int32));
|
||||
return Value(static_cast<u32>(NumericLimits<i32>::max()) + 1);
|
||||
}
|
||||
|
||||
// Let n be ? ToNumber(x).
|
||||
auto number = TRY(x.to_number(vm));
|
||||
|
|
|
|||
|
|
@ -12,3 +12,7 @@ test("basic functionality", () => {
|
|||
expect(Math.abs("string")).toBeNaN();
|
||||
expect(Math.abs()).toBeNaN();
|
||||
});
|
||||
|
||||
test("i32 min value", () => {
|
||||
expect(Math.abs(-2_147_483_648)).toBe(2_147_483_648);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue