1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 14:17:36 +00:00

LibJS: Convert to_u32() to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-17 23:43:29 +03:00
parent f6a5ff7b00
commit cc94bba5c0
14 changed files with 32 additions and 69 deletions

View file

@ -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.

View file

@ -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>)

View file

@ -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 {};

View file

@ -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));
}

View file

@ -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;

View file

@ -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));

View file

@ -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");

View file

@ -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;