mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:07:35 +00:00
LibJS: Convert to_u32() to ThrowCompletionOr
This commit is contained in:
parent
f6a5ff7b00
commit
cc94bba5c0
14 changed files with 32 additions and 69 deletions
|
@ -1117,13 +1117,11 @@ static void generate_to_cpp(SourceGenerator& generator, ParameterType& parameter
|
||||||
}
|
}
|
||||||
} else if (parameter.type->name == "unsigned long") {
|
} else if (parameter.type->name == "unsigned long") {
|
||||||
scoped_generator.append(R"~~~(
|
scoped_generator.append(R"~~~(
|
||||||
auto @cpp_name@ = @js_name@@js_suffix@.to_u32(global_object);
|
auto @cpp_name@ = TRY_OR_DISCARD(@js_name@@js_suffix@.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
@return_statement@
|
|
||||||
)~~~");
|
)~~~");
|
||||||
} else if (parameter.type->name == "unsigned short") {
|
} else if (parameter.type->name == "unsigned short") {
|
||||||
scoped_generator.append(R"~~~(
|
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())
|
if (vm.exception())
|
||||||
@return_statement@
|
@return_statement@
|
||||||
)~~~");
|
)~~~");
|
||||||
|
|
|
@ -72,9 +72,7 @@ bool Array::set_length(PropertyDescriptor const& property_descriptor)
|
||||||
size_t new_length = indexed_properties().array_like_size();
|
size_t new_length = indexed_properties().array_like_size();
|
||||||
if (property_descriptor.value.has_value()) {
|
if (property_descriptor.value.has_value()) {
|
||||||
// 3. Let newLen be ? ToUint32(Desc.[[Value]]).
|
// 3. Let newLen be ? ToUint32(Desc.[[Value]]).
|
||||||
new_length = property_descriptor.value->to_u32(global_object);
|
new_length = TRY_OR_DISCARD(property_descriptor.value->to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
|
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
|
||||||
auto number_length = TRY_OR_DISCARD(property_descriptor.value->to_number(global_object));
|
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.
|
// 5. If newLen is not the same value as numberLen, throw a RangeError exception.
|
||||||
|
|
|
@ -176,7 +176,7 @@ static ByteBuffer numeric_to_raw_bytes(GlobalObject& global_object, Value value,
|
||||||
int_value = value.to_i8(global_object);
|
int_value = value.to_i8(global_object);
|
||||||
} else {
|
} else {
|
||||||
if constexpr (sizeof(UnderlyingBufferDataType) == 4)
|
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)
|
else if constexpr (sizeof(UnderlyingBufferDataType) == 2)
|
||||||
int_value = value.to_u16(global_object);
|
int_value = value.to_u16(global_object);
|
||||||
else if constexpr (!IsSame<T, ClampedU8>)
|
else if constexpr (!IsSame<T, ClampedU8>)
|
||||||
|
|
|
@ -68,7 +68,7 @@ Value ArrayConstructor::construct(FunctionObject& new_target)
|
||||||
MUST(array->create_data_property_or_throw(0, length));
|
MUST(array->create_data_property_or_throw(0, length));
|
||||||
int_length = 1;
|
int_length = 1;
|
||||||
} else {
|
} else {
|
||||||
int_length = length.to_u32(global_object());
|
int_length = MUST(length.to_u32(global_object()));
|
||||||
if (int_length != length.as_double()) {
|
if (int_length != length.as_double()) {
|
||||||
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
|
vm.throw_exception<RangeError>(global_object(), ErrorType::InvalidLength, "array");
|
||||||
return {};
|
return {};
|
||||||
|
|
|
@ -300,9 +300,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sign)
|
||||||
// 21.3.2.11 Math.clz32 ( x ), https://tc39.es/ecma262/#sec-math.clz32
|
// 21.3.2.11 Math.clz32 ( x ), https://tc39.es/ecma262/#sec-math.clz32
|
||||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
|
JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
|
||||||
{
|
{
|
||||||
auto number = vm.argument(0).to_u32(global_object);
|
auto number = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
if (number == 0)
|
if (number == 0)
|
||||||
return Value(32);
|
return Value(32);
|
||||||
return Value(__builtin_clz(number));
|
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
|
// 21.3.2.19 Math.imul ( x, y ), https://tc39.es/ecma262/#sec-math.imul
|
||||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
|
JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
|
||||||
{
|
{
|
||||||
auto a = vm.argument(0).to_u32(global_object);
|
auto a = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
auto b = TRY_OR_DISCARD(vm.argument(1).to_u32(global_object));
|
||||||
return {};
|
|
||||||
auto b = vm.argument(1).to_u32(global_object);
|
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
return Value(static_cast<i32>(a * b));
|
return Value(static_cast<i32>(a * b));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -639,11 +639,8 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
||||||
size_t array_length = 0;
|
size_t array_length = 0;
|
||||||
|
|
||||||
auto limit = NumericLimits<u32>::max();
|
auto limit = NumericLimits<u32>::max();
|
||||||
if (!vm.argument(1).is_undefined()) {
|
if (!vm.argument(1).is_undefined())
|
||||||
limit = vm.argument(1).to_u32(global_object);
|
limit = TRY_OR_DISCARD(vm.argument(1).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (limit == 0)
|
if (limit == 0)
|
||||||
return array;
|
return array;
|
||||||
|
|
|
@ -706,11 +706,8 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::split)
|
||||||
size_t array_length = 0;
|
size_t array_length = 0;
|
||||||
|
|
||||||
auto limit = NumericLimits<u32>::max();
|
auto limit = NumericLimits<u32>::max();
|
||||||
if (!limit_argument.is_undefined()) {
|
if (!limit_argument.is_undefined())
|
||||||
limit = limit_argument.to_u32(global_object);
|
limit = TRY_OR_DISCARD(limit_argument.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto separator = TRY_OR_DISCARD(separator_argument.to_utf16_string(global_object));
|
auto separator = TRY_OR_DISCARD(separator_argument.to_utf16_string(global_object));
|
||||||
|
|
||||||
|
|
|
@ -592,9 +592,9 @@ ThrowCompletionOr<i32> Value::to_i32(GlobalObject& global_object) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
// 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)
|
if (!isfinite(value) || value == 0)
|
||||||
return 0;
|
return 0;
|
||||||
auto int_val = floor(fabs(value));
|
auto int_val = floor(fabs(value));
|
||||||
|
@ -925,7 +925,7 @@ Value left_shift(GlobalObject& global_object, Value lhs, Value rhs)
|
||||||
return lhs_numeric;
|
return lhs_numeric;
|
||||||
// Ok, so this performs toNumber() again but that "can't" throw
|
// Ok, so this performs toNumber() again but that "can't" throw
|
||||||
auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
|
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);
|
return Value(lhs_i32 << rhs_u32);
|
||||||
}
|
}
|
||||||
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
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())
|
if (!rhs_numeric.is_finite_number())
|
||||||
return lhs_numeric;
|
return lhs_numeric;
|
||||||
auto lhs_i32 = MUST(lhs_numeric.to_i32(global_object));
|
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);
|
return Value(lhs_i32 >> rhs_u32);
|
||||||
}
|
}
|
||||||
if (both_bigint(lhs_numeric, rhs_numeric)) {
|
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())
|
if (!rhs_numeric.is_finite_number())
|
||||||
return lhs_numeric;
|
return lhs_numeric;
|
||||||
// Ok, so this performs toNumber() again but that "can't" throw
|
// Ok, so this performs toNumber() again but that "can't" throw
|
||||||
auto lhs_u32 = lhs_numeric.to_u32(global_object);
|
auto lhs_u32 = MUST(lhs_numeric.to_u32(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_u32 >> rhs_u32);
|
return Value(lhs_u32 >> rhs_u32);
|
||||||
}
|
}
|
||||||
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
|
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::BigIntBadOperator, "unsigned right-shift");
|
||||||
|
|
|
@ -316,7 +316,7 @@ public:
|
||||||
ThrowCompletionOr<double> to_double(GlobalObject&) const;
|
ThrowCompletionOr<double> to_double(GlobalObject&) const;
|
||||||
ThrowCompletionOr<StringOrSymbol> to_property_key(GlobalObject&) const;
|
ThrowCompletionOr<StringOrSymbol> to_property_key(GlobalObject&) const;
|
||||||
ThrowCompletionOr<i32> to_i32(GlobalObject& global_object) 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;
|
i16 to_i16(GlobalObject&) const;
|
||||||
u16 to_u16(GlobalObject&) const;
|
u16 to_u16(GlobalObject&) const;
|
||||||
i8 to_i8(GlobalObject&) const;
|
i8 to_i8(GlobalObject&) const;
|
||||||
|
|
|
@ -47,16 +47,12 @@ JS::Value ImageConstructor::construct(FunctionObject&)
|
||||||
auto image_element = DOM::create_element(document, HTML::TagNames::img, Namespace::HTML);
|
auto image_element = DOM::create_element(document, HTML::TagNames::img, Namespace::HTML);
|
||||||
|
|
||||||
if (vm().argument_count() > 0) {
|
if (vm().argument_count() > 0) {
|
||||||
u32 width = vm().argument(0).to_u32(global_object());
|
u32 width = TRY_OR_DISCARD(vm().argument(0).to_u32(global_object()));
|
||||||
if (vm().exception())
|
|
||||||
return {};
|
|
||||||
image_element->set_attribute(HTML::AttributeNames::width, String::formatted("{}", width));
|
image_element->set_attribute(HTML::AttributeNames::width, String::formatted("{}", width));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (vm().argument_count() > 1) {
|
if (vm().argument_count() > 1) {
|
||||||
u32 height = vm().argument(1).to_u32(global_object());
|
u32 height = TRY_OR_DISCARD(vm().argument(1).to_u32(global_object()));
|
||||||
if (vm().exception())
|
|
||||||
return {};
|
|
||||||
image_element->set_attribute(HTML::AttributeNames::height, String::formatted("{}", height));
|
image_element->set_attribute(HTML::AttributeNames::height, String::formatted("{}", height));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,17 +41,12 @@ JS::Value WebAssemblyMemoryConstructor::construct(FunctionObject&)
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
auto initial = initial_value.to_u32(global_object);
|
auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
Optional<u32> maximum;
|
Optional<u32> maximum;
|
||||||
|
|
||||||
if (!maximum_value.is_empty()) {
|
if (!maximum_value.is_empty())
|
||||||
maximum = maximum_value.to_u32(global_object);
|
maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } });
|
auto address = WebAssemblyObject::s_abstract_machine.store().allocate(Wasm::MemoryType { Wasm::Limits { initial, maximum } });
|
||||||
if (!address.has_value()) {
|
if (!address.has_value()) {
|
||||||
|
|
|
@ -19,9 +19,7 @@ void WebAssemblyMemoryPrototype::initialize(JS::GlobalObject& global_object)
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
|
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyMemoryPrototype::grow)
|
||||||
{
|
{
|
||||||
auto page_count = vm.argument(0).to_u32(global_object);
|
auto page_count = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
||||||
if (!is<WebAssemblyMemoryObject>(this_object)) {
|
if (!is<WebAssemblyMemoryObject>(this_object)) {
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Memory");
|
||||||
|
|
|
@ -56,17 +56,12 @@ JS::Value WebAssemblyTableConstructor::construct(FunctionObject&)
|
||||||
auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
|
auto initial_value = TRY_OR_DISCARD(descriptor->get("initial"));
|
||||||
auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));
|
auto maximum_value = TRY_OR_DISCARD(descriptor->get("maximum"));
|
||||||
|
|
||||||
auto initial = initial_value.to_u32(global_object);
|
auto initial = TRY_OR_DISCARD(initial_value.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
Optional<u32> maximum;
|
Optional<u32> maximum;
|
||||||
|
|
||||||
if (!maximum_value.is_undefined()) {
|
if (!maximum_value.is_undefined())
|
||||||
maximum = maximum_value.to_u32(global_object);
|
maximum = TRY_OR_DISCARD(maximum_value.to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (maximum.has_value() && maximum.value() < initial) {
|
if (maximum.has_value() && maximum.value() < initial) {
|
||||||
vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
|
vm.throw_exception<JS::RangeError>(global_object, "maximum should be larger than or equal to initial");
|
||||||
|
|
|
@ -21,9 +21,8 @@ void WebAssemblyTablePrototype::initialize(JS::GlobalObject& global_object)
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
|
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::grow)
|
||||||
{
|
{
|
||||||
auto delta = vm.argument(0).to_u32(global_object);
|
auto delta = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
||||||
if (!is<WebAssemblyTableObject>(this_object)) {
|
if (!is<WebAssemblyTableObject>(this_object)) {
|
||||||
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAnObjectOfType, "WebAssembly.Table");
|
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)
|
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
|
||||||
{
|
{
|
||||||
auto index = vm.argument(0).to_u32(global_object);
|
auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
||||||
if (!is<WebAssemblyTableObject>(this_object)) {
|
if (!is<WebAssemblyTableObject>(this_object)) {
|
||||||
|
@ -89,9 +86,7 @@ JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::get)
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
JS_DEFINE_NATIVE_FUNCTION(WebAssemblyTablePrototype::set)
|
||||||
{
|
{
|
||||||
auto index = vm.argument(0).to_u32(global_object);
|
auto index = TRY_OR_DISCARD(vm.argument(0).to_u32(global_object));
|
||||||
if (vm.exception())
|
|
||||||
return {};
|
|
||||||
|
|
||||||
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
|
||||||
if (!is<WebAssemblyTableObject>(this_object)) {
|
if (!is<WebAssemblyTableObject>(this_object)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue