summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-10-17 23:43:29 +0300
committerIdan Horowitz <idan.horowitz@gmail.com>2021-10-18 08:01:38 +0300
commitcc94bba5c0461eebeb13a063c844330b0234e0bd (patch)
treeef837c94a8d60e7a04d65cf1e52ea54c3799afb4 /Userland/Libraries/LibJS/Runtime
parentf6a5ff7b003930a1994c0f4c4a1c4b1da7c7123d (diff)
downloadserenity-cc94bba5c0461eebeb13a063c844330b0234e0bd.zip
LibJS: Convert to_u32() to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Array.cpp4
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayBuffer.h2
-rw-r--r--Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp2
-rw-r--r--Userland/Libraries/LibJS/Runtime/MathObject.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp7
-rw-r--r--Userland/Libraries/LibJS/Runtime/StringPrototype.cpp7
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.h2
8 files changed, 17 insertions, 31 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Array.cpp b/Userland/Libraries/LibJS/Runtime/Array.cpp
index 3d34bcbb23..f609274aa7 100644
--- a/Userland/Libraries/LibJS/Runtime/Array.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Array.cpp
@@ -72,9 +72,7 @@ bool Array::set_length(PropertyDescriptor const& property_descriptor)
size_t new_length = indexed_properties().array_like_size();
if (property_descriptor.value.has_value()) {
// 3. Let newLen be ? ToUint32(Desc.[[Value]]).
- new_length = property_descriptor.value->to_u32(global_object);
- if (vm.exception())
- return {};
+ new_length = TRY_OR_DISCARD(property_descriptor.value->to_u32(global_object));
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
auto number_length = TRY_OR_DISCARD(property_descriptor.value->to_number(global_object));
// 5. If newLen is not the same value as numberLen, throw a RangeError exception.
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
index 7bceadd133..b980f294ca 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
+++ b/Userland/Libraries/LibJS/Runtime/ArrayBuffer.h
@@ -176,7 +176,7 @@ static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value,
int_value = value.to_i8(global_object);
} else {
if constexpr (sizeof(UnderlyingBufferDataType) == 4)
- int_value = value.to_u32(global_object);
+ int_value = MUST(value.to_u32(global_object));
else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
int_value = value.to_u16(global_object);
else if constexpr (!IsSame<T, ClampedU8>)
diff --git a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp
index 6d40742b56..4185fc4c95 100644
--- a/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ArrayConstructor.cpp
@@ -68,7 +68,7 @@ Value ArrayConstructor::construct(FunctionObject& new_target)
MUST(array->create_data_property_or_throw(0, length));
int_length = 1;
} else {
- int_length = length.to_u32(global_object());
+ int_length = MUST(length.to_u32(global_object()));
if (int_length != length.as_double()) {
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
return {};
diff --git a/Userland/Libraries/LibJS/Runtime/MathObject.cpp b/Userland/Libraries/LibJS/Runtime/MathObject.cpp
index c543f64be2..5bfe6e12b5 100644
--- a/Userland/Libraries/LibJS/Runtime/MathObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/MathObject.cpp
@@ -300,9 +300,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sign)
// 21.3.2.11 Math.clz32 ( x ), https://tc39.es/ecma262/#sec-math.clz32
JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
{
- auto number = vm.argument(0).to_u32(global_object);
- if (vm.exception())
- return {};
+ auto number = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
if (number == 0)
return Value(32);
return Value(__builtin_clz(number));
@@ -479,12 +477,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::hypot)
// 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
{
- auto a = vm.argument(0).to_u32(global_object);
- if (vm.exception())
- return {};
- auto b = vm.argument(1).to_u32(global_object);
- if (vm.exception())
- return {};
+ auto a = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
+ auto b = TRY_OR_DISCARD(vm.argument(1).to_u32(global_object));
return Value(static_cast<i32>(a * b));
}
diff --git a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
index 21f0533b01..20a9d5170d 100644
--- a/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/RegExpPrototype.cpp
@@ -639,11 +639,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
size_t array_length = 0;
auto limit = NumericLimits<u32>::max();
- if (!vm.argument(1).is_undefined()) {
- limit = vm.argument(1).to_u32(global_object);
- if (vm.exception())
- return {};
- }
+ if (!vm.argument(1).is_undefined())
+ limit = TRY_OR_DISCARD(vm.argument(1).to_u32(global_object));
if (limit == 0)
return array;
diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
index 4cef3fdd81..36ee5a65e3 100644
--- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
@@ -706,11 +706,8 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
size_t array_length = 0;
auto limit = NumericLimits<u32>::max();
- if (!limit_argument.is_undefined()) {
- limit = limit_argument.to_u32(global_object);
- if (vm.exception())
- return {};
- }
+ if (!limit_argument.is_undefined())
+ limit = TRY_OR_DISCARD(limit_argument.to_u32(global_object));
auto separator = TRY_OR_DISCARD(separator_argument.to_utf16_string(global_object));
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 453e8f8fb0..148f6647e8 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -592,9 +592,9 @@ ThrowCompletionOr<i32> Value::to_i32(GlobalObject& global_object) const
}
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
-u32 Value::to_u32(GlobalObject& global_object) const
+ThrowCompletionOr<u32> Value::to_u32(GlobalObject& global_object) const
{
- double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
+ double value = TRY(to_number(global_object)).as_double();
if (!isfinite(value) || value == 0)
return 0;
auto int_val = floor(fabs(value));
@@ -925,7 +925,7 @@ Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
return lhs_numeric;
// Ok, so this performs toNumber() again but that "can't" throw
auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
- auto rhs_u32 = rhs_numeric.to_u32(global_object) % 32;
+ auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
return Value(lhs_i32 << rhs_u32);
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
@@ -951,7 +951,7 @@ Value right_shift(GlobalObject& global_object, Value lhs, Value rhs)
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
- auto rhs_u32 = rhs_numeric.to_u32(global_object) % 32;
+ auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
return Value(lhs_i32 >> rhs_u32);
}
if (both_bigint(lhs_numeric, rhs_numeric)) {
@@ -974,8 +974,8 @@ Value unsigned_right_shift(GlobalObject& global_object, Value lhs, Value rhs)
if (!rhs_numeric.is_finite_number())
return lhs_numeric;
// Ok, so this performs toNumber() again but that "can't" throw
- auto lhs_u32 = lhs_numeric.to_u32(global_object);
- auto rhs_u32 = rhs_numeric.to_u32(global_object) % 32;
+ auto lhs_u32 = MUST(lhs_numeric.to_u32(global_object));
+ auto rhs_u32 = MUST(rhs_numeric.to_u32(global_object)) % 32;
return Value(lhs_u32 >> rhs_u32);
}
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h
index ae3e6a7188..296bb0eab0 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.h
+++ b/Userland/Libraries/LibJS/Runtime/Value.h
@@ -316,7 +316,7 @@ public:
ThrowCompletionOr<double> to_double(GlobalObject&) const;
ThrowCompletionOr<StringOrSymbol> to_property_key(GlobalObject&) const;
ThrowCompletionOr<i32> to_i32(GlobalObject& global_object) const;
- u32 to_u32(GlobalObject&) const;
+ ThrowCompletionOr<u32> to_u32(GlobalObject&) const;
i16 to_i16(GlobalObject&) const;
u16 to_u16(GlobalObject&) const;
i8 to_i8(GlobalObject&) const;