summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-09-15 18:41:33 +0100
committerLinus Groh <mail@linusgroh.de>2021-09-15 18:41:33 +0100
commit1a7828a9f3e0540eb4f2ef07d9f4f9ea4eded783 (patch)
tree48847d328de58b14fab9cd302b8d922fba87eac9
parent657d17ace3c3fd4e99083e4fedbc2b430c256e8d (diff)
downloadserenity-1a7828a9f3e0540eb4f2ef07d9f4f9ea4eded783.zip
LibJS: Return default-constructed values instead of the INVALID constant
This is much more common across the whole codebase and even these two files. The same is used to return an empty JS::Value in an exception check, for example.
-rw-r--r--Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp5
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp33
2 files changed, 16 insertions, 22 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
index 7e4642c350..bb3afd0430 100644
--- a/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
+++ b/Userland/Libraries/LibJS/Runtime/AbstractOperations.cpp
@@ -31,9 +31,6 @@
namespace JS {
-// Used in various abstract operations to make it obvious when a non-optional return value must be discarded.
-static constexpr double INVALID { 0 };
-
// 7.2.1 RequireObjectCoercible ( argument ), https://tc39.es/ecma262/#sec-requireobjectcoercible
Value require_object_coercible(GlobalObject& global_object, Value value)
{
@@ -51,7 +48,7 @@ size_t length_of_array_like(GlobalObject& global_object, Object const& object)
auto& vm = global_object.vm();
auto result = object.get(vm.names.length);
if (vm.exception())
- return INVALID;
+ return {};
return result.to_length(global_object);
}
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 506113bbb4..46837c96da 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -34,9 +34,6 @@
namespace JS {
-// Used in various abstract operations to make it obvious when a non-optional return value must be discarded.
-static constexpr double INVALID { 0 };
-
static inline bool same_type_for_equality(const Value& lhs, const Value& rhs)
{
if (lhs.type() == rhs.type())
@@ -569,7 +566,7 @@ i64 Value::to_bigint_int64(GlobalObject& global_object) const
{
auto* bigint = to_bigint(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
return static_cast<i64>(bigint->big_integer().to_u64());
}
@@ -578,7 +575,7 @@ u64 Value::to_bigint_uint64(GlobalObject& global_object) const
{
auto* bigint = to_bigint(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
return bigint->big_integer().to_u64();
}
@@ -598,7 +595,7 @@ double Value::to_double(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
return number.as_double();
}
@@ -618,7 +615,7 @@ i32 Value::to_i32_slow_case(GlobalObject& global_object) const
VERIFY(type() != Type::Int32);
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -638,7 +635,7 @@ u32 Value::to_u32(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -656,7 +653,7 @@ i16 Value::to_i16(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -676,7 +673,7 @@ u16 Value::to_u16(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -694,7 +691,7 @@ i8 Value::to_i8(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -714,7 +711,7 @@ u8 Value::to_u8(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
double value = number.as_double();
if (!isfinite(value) || value == 0)
return 0;
@@ -732,7 +729,7 @@ u8 Value::to_u8_clamp(GlobalObject& global_object) const
{
auto number = to_number(global_object);
if (global_object.vm().exception())
- return INVALID;
+ return {};
if (number.is_nan())
return 0;
double value = number.as_double();
@@ -757,7 +754,7 @@ size_t Value::to_length(GlobalObject& global_object) const
auto len = to_integer_or_infinity(global_object);
if (vm.exception())
- return INVALID;
+ return {};
if (len <= 0)
return 0;
// FIXME: The spec says that this function's output range is 0 - 2^53-1. But we don't want to overflow the size_t.
@@ -774,16 +771,16 @@ size_t Value::to_index(GlobalObject& global_object) const
return 0;
auto integer_index = to_integer_or_infinity(global_object);
if (vm.exception())
- return INVALID;
+ return {};
if (integer_index < 0) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
- return INVALID;
+ return {};
}
auto index = Value(integer_index).to_length(global_object);
VERIFY(!vm.exception());
if (integer_index != index) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidIndex);
- return INVALID;
+ return {};
}
return index;
}
@@ -795,7 +792,7 @@ double Value::to_integer_or_infinity(GlobalObject& global_object) const
auto number = to_number(global_object);
if (vm.exception())
- return INVALID;
+ return {};
if (number.is_nan() || number.as_double() == 0)
return 0;
if (number.is_infinity())