diff options
author | Idan Horowitz <idan.horowitz@gmail.com> | 2021-10-17 23:43:29 +0300 |
---|---|---|
committer | Idan Horowitz <idan.horowitz@gmail.com> | 2021-10-18 08:01:38 +0300 |
commit | cc94bba5c0461eebeb13a063c844330b0234e0bd (patch) | |
tree | ef837c94a8d60e7a04d65cf1e52ea54c3799afb4 /Userland | |
parent | f6a5ff7b003930a1994c0f4c4a1c4b1da7c7123d (diff) | |
download | serenity-cc94bba5c0461eebeb13a063c844330b0234e0bd.zip |
LibJS: Convert to_u32() to ThrowCompletionOr
Diffstat (limited to 'Userland')
13 files changed, 30 insertions, 65 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; diff --git a/Userland/Libraries/LibWeb/Bindings/ImageConstructor.cpp b/Userland/Libraries/LibWeb/Bindings/ImageConstructor.cpp index 46ec3ffaeb..3260bba7cc 100644 --- a/Userland/Libraries/LibWeb/Bindings/ImageConstructor.cpp +++ b/Userland/Libraries/LibWeb/Bindings/ImageConstructor.cpp @@ -47,16 +47,12 @@ JS::Value ImageConstructor::construct(FunctionObject&) auto image_element = DOM::create_element(document, HTML::TagNames::img, Namespace::HTML); if (vm().argument_count() > 0) { - u32 width = vm().argument(0).to_u32(global_object()); - if (vm().exception()) - return {}; + u32 width = TRY_OR_DISCARD(vm().argument(0).to_u32(global_object())); image_element->set_attribute(HTML::AttributeNames::width, String::formatted("{}", width)); } if (vm().argument_count() > 1) { - u32 height = vm().argument(1).to_u32(global_object()); - if (vm().exception()) - return {}; + u32 height = TRY_OR_DISCARD(vm().argument(1).to_u32(global_object())); image_element->set_attribute(HTML::AttributeNames::height, String::formatted("{}", height)); } diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp index 6546d01860..25bf3cbc5a 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryConstructor.cpp @@ -41,17 +41,12 @@ JS::Value WebAssemblyMemoryConstructor::construct(FunctionObject&) return {}; } - auto initial = initial_value.to_u32(global_object); - if (vm.exception()) - return {}; + auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object)); Optional<u32> maximum; - if (!maximum_value.is_empty()) { - maximum = maximum_value.to_u32(global_object); - if (vm.exception()) - return {}; - } + if (!maximum_value.is_empty()) + maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object)); auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } }); if (!address.has_value()) { diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp index e16abbf0b0..bc4020ce7a 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyMemoryPrototype.cpp @@ -19,9 +19,7 @@ void WebAssemblyMemoryPrototype::initialize(JS::GlobalObject& global_object) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow) { - auto page_count = vm.argument(0).to_u32(global_object); - if (vm.exception()) - return {}; + auto page_count = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); if (!is<WebAssemblyMemoryObject>(this_object)) { vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory"); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp index dc58bf4b42..6399af78f9 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTableConstructor.cpp @@ -56,17 +56,12 @@ JS::Value WebAssemblyTableConstructor::construct(FunctionObject&) auto initial_value = TRY_OR_DISCARD(descriptor->get("initial")); auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum")); - auto initial = initial_value.to_u32(global_object); - if (vm.exception()) - return {}; + auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object)); Optional<u32> maximum; - if (!maximum_value.is_undefined()) { - maximum = maximum_value.to_u32(global_object); - if (vm.exception()) - return {}; - } + if (!maximum_value.is_undefined()) + maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object)); if (maximum.has_value() && maximum.value() < initial) { vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial"); diff --git a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp index f8d2381007..7493579e2e 100644 --- a/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp +++ b/Userland/Libraries/LibWeb/WebAssembly/WebAssemblyTablePrototype.cpp @@ -21,9 +21,8 @@ void WebAssemblyTablePrototype::initialize(JS::GlobalObject& global_object) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow) { - auto delta = vm.argument(0).to_u32(global_object); - if (vm.exception()) - return {}; + auto delta = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); + auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); if (!is<WebAssemblyTableObject>(this_object)) { vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table"); @@ -59,9 +58,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get) { - auto index = vm.argument(0).to_u32(global_object); - if (vm.exception()) - return {}; + auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); if (!is<WebAssemblyTableObject>(this_object)) { @@ -89,9 +86,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get) JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set) { - auto index = vm.argument(0).to_u32(global_object); - if (vm.exception()) - return {}; + auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object)); auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object)); if (!is<WebAssemblyTableObject>(this_object)) { |