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
Notes: sideshowbarker 2024-07-18 02:12:39 +09:00
14 changed files with 32 additions and 69 deletions

View file

@ -1117,13 +1117,11 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
}
} else if (parameter.type->name == "unsigned long") {
scoped_generator.append(R"~~~(
auto @cpp_name@ = @js_name@@js_suffix@.to_u32(global_object);
if (vm.exception())
@return_statement@
auto @cpp_name@ = TRY_OR_DISCARD(@js_name@@js_suffix@.to_u32(global_object));
)~~~");
} else if (parameter.type->name == "unsigned short") {
scoped_generator.append(R"~~~(
auto @cpp_name@ = (u16)@js_name@@js_suffix@.to_u32(global_object);
auto @cpp_name@ = @js_name@@js_suffix@.to_u16(global_object);
if (vm.exception())
@return_statement@
)~~~");

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;

View file

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

View file

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

View file

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

View file

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

View file

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