mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-29 20:29:18 +00:00
LibJS: Inline the fast path of Value::to_i32() and simplify to_u32()
The fast path of to_i32() can be neatly inlined everywhere, and we still have to_i32_slow_case() for non-trivial conversions. For to_u32(), it really can just be implemented as a static cast to i32!
This commit is contained in:
parent
f1fba24538
commit
938b1e91fe
Notes:
github-actions[bot]
2025-04-09 20:07:48 +00:00
Author: https://github.com/awesomekling
Commit: 938b1e91fe
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4301
9 changed files with 22 additions and 36 deletions
|
@ -953,42 +953,6 @@ ThrowCompletionOr<i32> Value::to_i32_slow_case(VM& vm) const
|
||||||
return static_cast<i32>(int32bit);
|
return static_cast<i32>(int32bit);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
|
|
||||||
ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
|
|
||||||
{
|
|
||||||
if (is_int32())
|
|
||||||
return as_i32();
|
|
||||||
return to_i32_slow_case(vm);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
|
||||||
ThrowCompletionOr<u32> Value::to_u32(VM& vm) const
|
|
||||||
{
|
|
||||||
// OPTIMIZATION: If this value is encoded as a positive i32, return it directly.
|
|
||||||
if (is_int32() && as_i32() >= 0)
|
|
||||||
return as_i32();
|
|
||||||
|
|
||||||
// 1. Let number be ? ToNumber(argument).
|
|
||||||
double number = TRY(to_number(vm)).as_double();
|
|
||||||
|
|
||||||
// 2. If number is not finite or number is either +0𝔽 or -0𝔽, return +0𝔽.
|
|
||||||
if (!isfinite(number) || number == 0)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// 3. Let int be the mathematical value whose sign is the sign of number and whose magnitude is floor(abs(ℝ(number))).
|
|
||||||
auto int_val = floor(fabs(number));
|
|
||||||
if (signbit(number))
|
|
||||||
int_val = -int_val;
|
|
||||||
|
|
||||||
// 4. Let int32bit be int modulo 2^32.
|
|
||||||
auto int32bit = modulo(int_val, NumericLimits<u32>::max() + 1.0);
|
|
||||||
|
|
||||||
// 5. Return 𝔽(int32bit).
|
|
||||||
// Cast to i64 here to ensure that the double --> u32 cast doesn't invoke undefined behavior
|
|
||||||
// Otherwise, negative numbers cause a UBSAN warning.
|
|
||||||
return static_cast<u32>(static_cast<i64>(int32bit));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
|
// 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
|
||||||
ThrowCompletionOr<i16> Value::to_i16(VM& vm) const
|
ThrowCompletionOr<i16> Value::to_i16(VM& vm) const
|
||||||
{
|
{
|
||||||
|
|
|
@ -48,4 +48,19 @@ inline ThrowCompletionOr<Value> Value::to_primitive(VM& vm, PreferredType prefer
|
||||||
return to_primitive_slow_case(vm, preferred_type);
|
return to_primitive_slow_case(vm, preferred_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 7.1.6 ToInt32 ( argument ), https://tc39.es/ecma262/#sec-toint32
|
||||||
|
inline ThrowCompletionOr<i32> Value::to_i32(VM& vm) const
|
||||||
|
{
|
||||||
|
if (is_int32())
|
||||||
|
return as_i32();
|
||||||
|
|
||||||
|
return to_i32_slow_case(vm);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
||||||
|
inline ThrowCompletionOr<u32> Value::to_u32(VM& vm) const
|
||||||
|
{
|
||||||
|
return static_cast<u32>(TRY(to_i32(vm)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||||
#include <LibWeb/Bindings/HTMLImageElementPrototype.h>
|
#include <LibWeb/Bindings/HTMLImageElementPrototype.h>
|
||||||
#include <LibWeb/Bindings/ImageConstructor.h>
|
#include <LibWeb/Bindings/ImageConstructor.h>
|
||||||
|
|
|
@ -30,6 +30,7 @@
|
||||||
#include <LibJS/Runtime/ArrayBuffer.h>
|
#include <LibJS/Runtime/ArrayBuffer.h>
|
||||||
#include <LibJS/Runtime/DataView.h>
|
#include <LibJS/Runtime/DataView.h>
|
||||||
#include <LibJS/Runtime/TypedArray.h>
|
#include <LibJS/Runtime/TypedArray.h>
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||||
#include <LibWeb/Crypto/CryptoAlgorithms.h>
|
#include <LibWeb/Crypto/CryptoAlgorithms.h>
|
||||||
#include <LibWeb/Crypto/KeyAlgorithms.h>
|
#include <LibWeb/Crypto/KeyAlgorithms.h>
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <LibCrypto/Hash/HashManager.h>
|
#include <LibCrypto/Hash/HashManager.h>
|
||||||
#include <LibJS/Runtime/ArrayBuffer.h>
|
#include <LibJS/Runtime/ArrayBuffer.h>
|
||||||
#include <LibJS/Runtime/JSONObject.h>
|
#include <LibJS/Runtime/JSONObject.h>
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
#include <LibWeb/Bindings/ExceptionOrUtils.h>
|
||||||
#include <LibWeb/Bindings/Intrinsics.h>
|
#include <LibWeb/Bindings/Intrinsics.h>
|
||||||
#include <LibWeb/Bindings/SubtleCryptoPrototype.h>
|
#include <LibWeb/Bindings/SubtleCryptoPrototype.h>
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWeb/Bindings/NodeIteratorPrototype.h>
|
#include <LibWeb/Bindings/NodeIteratorPrototype.h>
|
||||||
#include <LibWeb/DOM/Document.h>
|
#include <LibWeb/DOM/Document.h>
|
||||||
#include <LibWeb/DOM/Node.h>
|
#include <LibWeb/DOM/Node.h>
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWeb/Bindings/Intrinsics.h>
|
#include <LibWeb/Bindings/Intrinsics.h>
|
||||||
#include <LibWeb/Bindings/TreeWalkerPrototype.h>
|
#include <LibWeb/Bindings/TreeWalkerPrototype.h>
|
||||||
#include <LibWeb/DOM/Node.h>
|
#include <LibWeb/DOM/Node.h>
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#include <LibJS/Runtime/NativeFunction.h>
|
#include <LibJS/Runtime/NativeFunction.h>
|
||||||
#include <LibJS/Runtime/Object.h>
|
#include <LibJS/Runtime/Object.h>
|
||||||
#include <LibJS/Runtime/VM.h>
|
#include <LibJS/Runtime/VM.h>
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibWasm/AbstractMachine/Validator.h>
|
#include <LibWasm/AbstractMachine/Validator.h>
|
||||||
#include <LibWeb/Bindings/ResponsePrototype.h>
|
#include <LibWeb/Bindings/ResponsePrototype.h>
|
||||||
#include <LibWeb/Fetch/Response.h>
|
#include <LibWeb/Fetch/Response.h>
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/MemoryStream.h>
|
#include <AK/MemoryStream.h>
|
||||||
|
#include <LibJS/Runtime/ValueInlines.h>
|
||||||
#include <LibTest/JavaScriptTestRunner.h>
|
#include <LibTest/JavaScriptTestRunner.h>
|
||||||
#include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
|
#include <LibWasm/AbstractMachine/BytecodeInterpreter.h>
|
||||||
#include <LibWasm/Types.h>
|
#include <LibWasm/Types.h>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue