mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:57:45 +00:00
LibJS: Convert to_number() to ThrowCompletionOr
This commit is contained in:
parent
a36ee213b9
commit
20d990563c
22 changed files with 149 additions and 343 deletions
|
@ -50,7 +50,7 @@ JS::Value NumericCell::js_value(Cell& cell, const CellTypeMetadata&) const
|
|||
cell.set_exception(exc);
|
||||
}
|
||||
} };
|
||||
return cell.js_data().to_number(cell.sheet().global_object());
|
||||
return TRY_OR_DISCARD(cell.js_data().to_number(cell.sheet().global_object()));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -352,9 +352,7 @@ JS_DEFINE_NATIVE_FUNCTION(SheetGlobalObject::column_arithmetic)
|
|||
|
||||
auto& column_name_str = column_name.as_string().string();
|
||||
|
||||
auto offset = vm.argument(1).to_number(global_object);
|
||||
if (!offset.is_number())
|
||||
return {};
|
||||
auto offset = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
|
||||
auto offset_number = offset.as_i32();
|
||||
|
||||
|
|
|
@ -870,7 +870,7 @@ Value canonical_numeric_index_string(GlobalObject& global_object, PropertyName c
|
|||
return Value(-0.0);
|
||||
|
||||
// 3. Let n be ! ToNumber(argument).
|
||||
auto n = argument.to_number(global_object);
|
||||
auto n = MUST(argument.to_number(global_object));
|
||||
|
||||
// 4. If SameValue(! ToString(n), argument) is false, return undefined.
|
||||
if (!same_value(MUST(n.to_primitive_string(global_object)), argument))
|
||||
|
|
|
@ -76,9 +76,7 @@ bool Array::set_length(PropertyDescriptor const& property_descriptor)
|
|||
if (vm.exception())
|
||||
return {};
|
||||
// 4. Let numberLen be ? ToNumber(Desc.[[Value]]).
|
||||
auto number_length = property_descriptor.value->to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
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.
|
||||
if (new_length != number_length.as_double()) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidLength, "array");
|
||||
|
|
|
@ -960,9 +960,10 @@ static void array_merge_sort(VM& vm, GlobalObject& global_object, FunctionObject
|
|||
return;
|
||||
auto call_result = call_result_or_error.release_value();
|
||||
|
||||
auto number = call_result.to_number(global_object);
|
||||
if (vm.exception())
|
||||
auto number_or_error = call_result.to_number(global_object);
|
||||
if (number_or_error.is_error())
|
||||
return;
|
||||
auto number = number_or_error.release_value();
|
||||
|
||||
if (number.is_nan())
|
||||
comparison_result = 0;
|
||||
|
|
|
@ -110,9 +110,7 @@ static Value set_view_value(GlobalObject& global_object, Value request_index, Va
|
|||
if constexpr (IsIntegral<T> && sizeof(T) == 8)
|
||||
number_value = TRY_OR_DISCARD(value.to_bigint(global_object));
|
||||
else
|
||||
number_value = value.to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
number_value = TRY_OR_DISCARD(value.to_number(global_object));
|
||||
|
||||
auto little_endian = is_little_endian.to_boolean();
|
||||
|
||||
|
|
|
@ -209,10 +209,7 @@ Value DateConstructor::construct(FunctionObject& new_target)
|
|||
if (value.is_string())
|
||||
value = parse_date_string(value.as_string().string());
|
||||
else
|
||||
value = value.to_number(global_object);
|
||||
|
||||
if (vm.exception())
|
||||
return {};
|
||||
value = TRY_OR_DISCARD(value.to_number(global_object));
|
||||
|
||||
if (!value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
|
@ -228,61 +225,47 @@ Value DateConstructor::construct(FunctionObject& new_target)
|
|||
}
|
||||
|
||||
// A date/time in components, in local time.
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) {
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto year_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto year_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!year_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto year = year_value.as_i32();
|
||||
|
||||
auto month_index_value = vm.argument(1).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto month_index_value = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
if (!month_index_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto month_index = month_index_value.as_i32();
|
||||
|
||||
auto day_value = arg_or(2, 1);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto day_value = TRY_OR_DISCARD(arg_or(2, 1));
|
||||
if (!day_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto day = day_value.as_i32();
|
||||
|
||||
auto hours_value = arg_or(3, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto hours_value = TRY_OR_DISCARD(arg_or(3, 0));
|
||||
if (!hours_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto hours = hours_value.as_i32();
|
||||
|
||||
auto minutes_value = arg_or(4, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto minutes_value = TRY_OR_DISCARD(arg_or(4, 0));
|
||||
if (!minutes_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto minutes = minutes_value.as_i32();
|
||||
|
||||
auto seconds_value = arg_or(5, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto seconds_value = TRY_OR_DISCARD(arg_or(5, 0));
|
||||
if (!seconds_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
auto seconds = seconds_value.as_i32();
|
||||
|
||||
auto milliseconds_value = arg_or(6, 0);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto milliseconds_value = TRY_OR_DISCARD(arg_or(6, 0));
|
||||
if (!milliseconds_value.is_finite_number()) {
|
||||
return create_invalid_date();
|
||||
}
|
||||
|
|
|
@ -114,9 +114,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_date)
|
|||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_date_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_date_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_date_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -168,29 +166,25 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_full_year)
|
|||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto new_year_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_year_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_year_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_year = new_year_value.as_i32();
|
||||
|
||||
auto new_month_value = arg_or(1, datetime.month() - 1);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_month_value = TRY_OR_DISCARD(arg_or(1, datetime.month() - 1));
|
||||
if (!new_month_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_month = new_month_value.as_i32() + 1; // JS Months: 0 - 11, DateTime months: 1-12
|
||||
|
||||
auto new_day_value = arg_or(2, datetime.day());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_day_value = TRY_OR_DISCARD(arg_or(2, datetime.day()));
|
||||
if (!new_day_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -230,9 +224,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_year)
|
|||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_year_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_year_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_year_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -272,40 +264,34 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_hours)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_hours_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_hours_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_hours_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_hours = new_hours_value.as_i32();
|
||||
|
||||
auto new_minutes_value = arg_or(1, datetime.minute());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_minutes_value = TRY_OR_DISCARD(arg_or(1, datetime.minute()));
|
||||
if (!new_minutes_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_minutes = new_minutes_value.as_i32();
|
||||
|
||||
auto new_seconds_value = arg_or(2, datetime.second());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_seconds_value = TRY_OR_DISCARD(arg_or(2, datetime.second()));
|
||||
if (!new_seconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_seconds = new_seconds_value.as_i32();
|
||||
|
||||
auto new_milliseconds_value = arg_or(3, this_object->milliseconds());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_milliseconds_value = TRY_OR_DISCARD(arg_or(3, this_object->milliseconds()));
|
||||
if (!new_milliseconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -345,9 +331,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_milliseconds)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto new_milliseconds_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_milliseconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
|
||||
if (!new_milliseconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
|
@ -394,31 +378,27 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_minutes)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_minutes_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_minutes_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_minutes_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_minutes = new_minutes_value.as_i32();
|
||||
|
||||
auto new_seconds_value = arg_or(1, datetime.second());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_seconds_value = TRY_OR_DISCARD(arg_or(1, datetime.second()));
|
||||
if (!new_seconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_seconds = new_seconds_value.as_i32();
|
||||
|
||||
auto new_milliseconds_value = arg_or(2, this_object->milliseconds());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_milliseconds_value = TRY_OR_DISCARD(arg_or(2, this_object->milliseconds()));
|
||||
if (!new_milliseconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -458,22 +438,20 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_month)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_month_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_month_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_month_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_month = new_month_value.as_i32() + 1; // JS Months: 0 - 11, DateTime months: 1-12
|
||||
|
||||
auto new_date_value = arg_or(1, this_object->date());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_date_value = TRY_OR_DISCARD(arg_or(1, this_object->date()));
|
||||
if (!new_date_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -510,22 +488,20 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_seconds)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) { return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback); };
|
||||
auto arg_or = [&vm, &global_object](size_t i, i32 fallback) -> ThrowCompletionOr<Value> {
|
||||
return vm.argument_count() > i ? vm.argument(i).to_number(global_object) : Value(fallback);
|
||||
};
|
||||
|
||||
auto& datetime = this_object->datetime();
|
||||
|
||||
auto new_seconds_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_seconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_seconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
}
|
||||
auto new_seconds = new_seconds_value.as_i32();
|
||||
|
||||
auto new_milliseconds_value = arg_or(1, this_object->milliseconds());
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_milliseconds_value = TRY_OR_DISCARD(arg_or(1, this_object->milliseconds()));
|
||||
if (!new_milliseconds_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
@ -565,9 +541,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::set_time)
|
|||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto new_time_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto new_time_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (!new_time_value.is_finite_number()) {
|
||||
this_object->set_is_invalid(true);
|
||||
return js_nan();
|
||||
|
|
|
@ -341,19 +341,13 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::gc)
|
|||
// 19.2.3 isNaN ( number ), https://tc39.es/ecma262/#sec-isnan-number
|
||||
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return Value(number.is_nan());
|
||||
return Value(TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).is_nan());
|
||||
}
|
||||
|
||||
// 19.2.2 isFinite ( number ), https://tc39.es/ecma262/#sec-isfinite-number
|
||||
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return Value(number.is_finite_number());
|
||||
return Value(TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).is_finite_number());
|
||||
}
|
||||
|
||||
// 19.2.4 parseFloat ( string ), https://tc39.es/ecma262/#sec-parsefloat-string
|
||||
|
@ -364,8 +358,7 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
|
|||
auto input_string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object));
|
||||
auto trimmed_string = input_string.trim_whitespace(TrimMode::Left);
|
||||
for (size_t length = trimmed_string.length(); length > 0; --length) {
|
||||
// This can't throw, so no exception check is fine.
|
||||
auto number = Value(js_string(vm, trimmed_string.substring(0, length))).to_number(global_object);
|
||||
auto number = MUST(Value(js_string(vm, trimmed_string.substring(0, length))).to_number(global_object));
|
||||
if (!number.is_nan())
|
||||
return number;
|
||||
}
|
||||
|
|
|
@ -651,9 +651,7 @@ ThrowCompletionOr<Optional<int>> default_number_option(GlobalObject& global_obje
|
|||
return fallback;
|
||||
|
||||
// 2. Let value be ? ToNumber(value).
|
||||
value = value.to_number(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_number(global_object));
|
||||
|
||||
// 3. If value is NaN or less than minimum or greater than maximum, throw a RangeError exception.
|
||||
if (value.is_nan() || (value.as_double() < minimum) || (value.as_double() > maximum))
|
||||
|
|
|
@ -82,14 +82,11 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
|
|||
|
||||
if (space.is_object()) {
|
||||
auto& space_object = space.as_object();
|
||||
if (is<NumberObject>(space_object)) {
|
||||
space = space.to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
} else if (is<StringObject>(space_object)) {
|
||||
if (is<NumberObject>(space_object))
|
||||
space = TRY_OR_DISCARD(space.to_number(global_object));
|
||||
else if (is<StringObject>(space_object))
|
||||
space = TRY_OR_DISCARD(space.to_primitive_string(global_object));
|
||||
}
|
||||
}
|
||||
|
||||
if (space.is_number()) {
|
||||
auto space_mv = space.to_integer_or_infinity(global_object);
|
||||
|
@ -148,18 +145,15 @@ String JSONObject::serialize_json_property(GlobalObject& global_object, Stringif
|
|||
|
||||
if (value.is_object()) {
|
||||
auto& value_object = value.as_object();
|
||||
if (is<NumberObject>(value_object)) {
|
||||
value = value.to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
} else if (is<StringObject>(value_object)) {
|
||||
if (is<NumberObject>(value_object))
|
||||
value = TRY_OR_DISCARD(value.to_number(global_object));
|
||||
else if (is<StringObject>(value_object))
|
||||
value = TRY_OR_DISCARD(value.to_primitive_string(global_object));
|
||||
} else if (is<BooleanObject>(value_object)) {
|
||||
else if (is<BooleanObject>(value_object))
|
||||
value = static_cast<BooleanObject&>(value_object).value_of();
|
||||
} else if (is<BigIntObject>(value_object)) {
|
||||
else if (is<BigIntObject>(value_object))
|
||||
value = static_cast<BigIntObject&>(value_object).value_of();
|
||||
}
|
||||
}
|
||||
|
||||
if (value.is_null())
|
||||
return "null";
|
||||
|
|
|
@ -81,9 +81,7 @@ MathObject::~MathObject()
|
|||
// 21.3.2.1 Math.abs ( x ), https://tc39.es/ecma262/#sec-math.abs
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::abs)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
if (number.is_negative_zero())
|
||||
|
@ -103,9 +101,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::random)
|
|||
// 21.3.2.32 Math.sqrt ( x ), https://tc39.es/ecma262/#sec-math.sqrt
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::sqrt(number.as_double()));
|
||||
|
@ -114,9 +110,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sqrt)
|
|||
// 21.3.2.16 Math.floor ( x ), https://tc39.es/ecma262/#sec-math.floor
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::floor)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::floor(number.as_double()));
|
||||
|
@ -125,9 +119,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::floor)
|
|||
// 21.3.2.10 Math.ceil ( x ), https://tc39.es/ecma262/#sec-math.ceil
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::ceil)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
auto number_double = number.as_double();
|
||||
|
@ -139,10 +131,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::ceil)
|
|||
// 21.3.2.28 Math.round ( x ), https://tc39.es/ecma262/#sec-math.round
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::round)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto value = number.as_double();
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
double integer = ::ceil(value);
|
||||
if (integer - 0.5 > value)
|
||||
integer--;
|
||||
|
@ -153,12 +142,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::round)
|
|||
JS_DEFINE_NATIVE_FUNCTION(MathObject::max)
|
||||
{
|
||||
Vector<Value> coerced;
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
||||
auto number = vm.argument(i).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
coerced.append(number);
|
||||
}
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i)
|
||||
coerced.append(TRY_OR_DISCARD(vm.argument(i).to_number(global_object)));
|
||||
|
||||
auto highest = js_negative_infinity();
|
||||
for (auto& number : coerced) {
|
||||
|
@ -174,12 +159,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::max)
|
|||
JS_DEFINE_NATIVE_FUNCTION(MathObject::min)
|
||||
{
|
||||
Vector<Value> coerced;
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
||||
auto number = vm.argument(i).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
coerced.append(number);
|
||||
}
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i)
|
||||
coerced.append(TRY_OR_DISCARD(vm.argument(i).to_number(global_object)));
|
||||
|
||||
auto lowest = js_infinity();
|
||||
for (auto& number : coerced) {
|
||||
|
@ -194,9 +175,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::min)
|
|||
// 21.3.2.35 Math.trunc ( x ), https://tc39.es/ecma262/#sec-math.trunc
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::trunc)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
if (number.as_double() < 0)
|
||||
|
@ -207,9 +186,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::trunc)
|
|||
// 21.3.2.30 Math.sin ( x ), https://tc39.es/ecma262/#sec-math.sin
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::sin)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::sin(number.as_double()));
|
||||
|
@ -218,9 +195,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sin)
|
|||
// 21.3.2.12 Math.cos ( x ), https://tc39.es/ecma262/#sec-math.cos
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::cos)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::cos(number.as_double()));
|
||||
|
@ -229,9 +204,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::cos)
|
|||
// 21.3.2.33 Math.tan ( x ), https://tc39.es/ecma262/#sec-math.tan
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::tan)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::tan(number.as_double()));
|
||||
|
@ -240,12 +213,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::tan)
|
|||
// 21.3.2.26 Math.pow ( base, exponent ), https://tc39.es/ecma262/#sec-math.pow
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::pow)
|
||||
{
|
||||
auto base = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto exponent = vm.argument(1).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto base = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
auto exponent = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
if (exponent.is_nan())
|
||||
return js_nan();
|
||||
if (exponent.is_positive_zero() || exponent.is_negative_zero())
|
||||
|
@ -298,9 +267,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::pow)
|
|||
// 21.3.2.14 Math.exp ( x ), https://tc39.es/ecma262/#sec-math.exp
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::exp)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::exp(number.as_double()));
|
||||
|
@ -309,9 +276,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::exp)
|
|||
// 21.3.2.15 Math.expm1 ( x ), https://tc39.es/ecma262/#sec-math.expm1
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::expm1(number.as_double()));
|
||||
|
@ -320,9 +285,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::expm1)
|
|||
// 21.3.2.29 Math.sign ( x ), https://tc39.es/ecma262/#sec-math.sign
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::sign)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_positive_zero())
|
||||
return Value(0);
|
||||
if (number.is_negative_zero())
|
||||
|
@ -348,9 +311,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::clz32)
|
|||
// 21.3.2.2 Math.acos ( x ), https://tc39.es/ecma262/#sec-math.acos
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::acos)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan() || number.as_double() > 1 || number.as_double() < -1)
|
||||
return js_nan();
|
||||
if (number.as_double() == 1)
|
||||
|
@ -361,20 +322,16 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::acos)
|
|||
// 21.3.2.3 Math.acosh ( x ), https://tc39.es/ecma262/#sec-math.acosh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::acosh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() < 1)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value < 1)
|
||||
return js_nan();
|
||||
return Value(::acosh(number.as_double()));
|
||||
return Value(::acosh(value));
|
||||
}
|
||||
|
||||
// 21.3.2.4 Math.asin ( x ), https://tc39.es/ecma262/#sec-math.asin
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::asin)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
|
||||
return number;
|
||||
return Value(::asin(number.as_double()));
|
||||
|
@ -383,18 +340,13 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::asin)
|
|||
// 21.3.2.5 Math.asinh ( x ), https://tc39.es/ecma262/#sec-math.asinh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::asinh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return Value(::asinh(number.as_double()));
|
||||
return Value(::asinh(TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double()));
|
||||
}
|
||||
|
||||
// 21.3.2.6 Math.atan ( x ), https://tc39.es/ecma262/#sec-math.atan
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::atan)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan() || number.is_positive_zero() || number.is_negative_zero())
|
||||
return number;
|
||||
if (number.is_positive_infinity())
|
||||
|
@ -407,32 +359,25 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::atan)
|
|||
// 21.3.2.7 Math.atanh ( x ), https://tc39.es/ecma262/#sec-math.atanh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::atanh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() > 1 || number.as_double() < -1)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value > 1 || value < -1)
|
||||
return js_nan();
|
||||
return Value(::atanh(number.as_double()));
|
||||
return Value(::atanh(value));
|
||||
}
|
||||
|
||||
// 21.3.2.21 Math.log1p ( x ), https://tc39.es/ecma262/#sec-math.log1p
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::log1p)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() < -1)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value < -1)
|
||||
return js_nan();
|
||||
return Value(::log1p(number.as_double()));
|
||||
return Value(::log1p(value));
|
||||
}
|
||||
|
||||
// 21.3.2.9 Math.cbrt ( x ), https://tc39.es/ecma262/#sec-math.cbrt
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::cbrt)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
return Value(::cbrt(number.as_double()));
|
||||
return Value(::cbrt(TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double()));
|
||||
}
|
||||
|
||||
// 21.3.2.8 Math.atan2 ( y, x ), https://tc39.es/ecma262/#sec-math.atan2
|
||||
|
@ -440,12 +385,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::atan2)
|
|||
{
|
||||
auto constexpr three_quarters_pi = M_PI_4 + M_PI_2;
|
||||
|
||||
auto y = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto x = vm.argument(1).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto y = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
auto x = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
|
||||
if (y.is_nan() || x.is_nan())
|
||||
return js_nan();
|
||||
|
@ -501,9 +442,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::atan2)
|
|||
// 21.3.2.17 Math.fround ( x ), https://tc39.es/ecma262/#sec-math.fround
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::fround)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value((float)number.as_double());
|
||||
|
@ -513,12 +452,8 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::fround)
|
|||
JS_DEFINE_NATIVE_FUNCTION(MathObject::hypot)
|
||||
{
|
||||
Vector<Value> coerced;
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
||||
auto number = vm.argument(i).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
coerced.append(number);
|
||||
}
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i)
|
||||
coerced.append(TRY_OR_DISCARD(vm.argument(i).to_number(global_object)));
|
||||
|
||||
for (auto& number : coerced) {
|
||||
if (number.is_positive_infinity() || number.is_negative_infinity())
|
||||
|
@ -556,42 +491,34 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::imul)
|
|||
// 21.3.2.20 Math.log ( x ), https://tc39.es/ecma262/#sec-math.log
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::log)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() < 0)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value < 0)
|
||||
return js_nan();
|
||||
return Value(::log(number.as_double()));
|
||||
return Value(::log(value));
|
||||
}
|
||||
|
||||
// 21.3.2.23 Math.log2 ( x ), https://tc39.es/ecma262/#sec-math.log2
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::log2)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() < 0)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value < 0)
|
||||
return js_nan();
|
||||
return Value(::log2(number.as_double()));
|
||||
return Value(::log2(value));
|
||||
}
|
||||
|
||||
// 21.3.2.22 Math.log10 ( x ), https://tc39.es/ecma262/#sec-math.log10
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::log10)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (number.as_double() < 0)
|
||||
auto value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double();
|
||||
if (value < 0)
|
||||
return js_nan();
|
||||
return Value(::log10(number.as_double()));
|
||||
return Value(::log10(value));
|
||||
}
|
||||
|
||||
// 21.3.2.31 Math.sinh ( x ), https://tc39.es/ecma262/#sec-math.sinh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::sinh(number.as_double()));
|
||||
|
@ -600,9 +527,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::sinh)
|
|||
// 21.3.2.13 Math.cosh ( x ), https://tc39.es/ecma262/#sec-math.cosh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
return Value(::cosh(number.as_double()));
|
||||
|
@ -611,9 +536,7 @@ JS_DEFINE_NATIVE_FUNCTION(MathObject::cosh)
|
|||
// 21.3.2.34 Math.tanh ( x ), https://tc39.es/ecma262/#sec-math.tanh
|
||||
JS_DEFINE_NATIVE_FUNCTION(MathObject::tanh)
|
||||
{
|
||||
auto number = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return js_nan();
|
||||
if (number.is_positive_infinity())
|
||||
|
|
|
@ -122,9 +122,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
|
|||
string.ensure_capacity(vm.argument_count()); // This will be an under-estimate if any code point is > 0xffff.
|
||||
|
||||
for (size_t i = 0; i < vm.argument_count(); ++i) {
|
||||
auto next_code_point = vm.argument(i).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto next_code_point = TRY_OR_DISCARD(vm.argument(i).to_number(global_object));
|
||||
if (!next_code_point.is_integral_number()) {
|
||||
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
|
||||
return {};
|
||||
|
|
|
@ -766,9 +766,7 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of)
|
|||
auto string_length = string.length_in_code_units();
|
||||
auto search_length = search_string.length_in_code_units();
|
||||
|
||||
auto position = vm.argument(1).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto position = TRY_OR_DISCARD(vm.argument(1).to_number(global_object));
|
||||
double pos = position.is_nan() ? static_cast<double>(INFINITY) : position.to_integer_or_infinity(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
|
|
|
@ -139,9 +139,7 @@ ThrowCompletionOr<Value> get_option(GlobalObject& global_object, Object const& o
|
|||
// 8. Else if type is Number, then
|
||||
else if (type == OptionType::Number) {
|
||||
// a. Set value to ? ToNumber(value).
|
||||
value = value.to_number(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_number(global_object));
|
||||
|
||||
// b. If value is NaN, throw a RangeError exception.
|
||||
if (value.is_nan())
|
||||
|
|
|
@ -98,9 +98,7 @@ ThrowCompletionOr<TemporalDuration> to_temporal_duration_record(GlobalObject& gl
|
|||
any = true;
|
||||
|
||||
// ii. Let val be ? ToNumber(val).
|
||||
value = value.to_number(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_number(global_object));
|
||||
|
||||
// iii. If ! IsIntegralNumber(val) is false, then
|
||||
if (!value.is_integral_number()) {
|
||||
|
@ -196,9 +194,7 @@ ThrowCompletionOr<PartialDuration> to_partial_duration(GlobalObject& global_obje
|
|||
any = true;
|
||||
|
||||
// ii. Set value to ? ToNumber(value).
|
||||
value = value.to_number(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
value = TRY(value.to_number(global_object));
|
||||
|
||||
// iii. If ! IsIntegralNumber(value) is false, then
|
||||
if (!value.is_integral_number()) {
|
||||
|
|
|
@ -87,9 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from)
|
|||
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
|
||||
{
|
||||
// 1. Set epochSeconds to ? ToNumber(epochSeconds).
|
||||
auto epoch_seconds_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto epoch_seconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
|
||||
// 2. Set epochSeconds to ? NumberToBigInt(epochSeconds).
|
||||
auto* epoch_seconds = number_to_bigint(global_object, epoch_seconds_value);
|
||||
|
@ -113,9 +111,7 @@ JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_seconds)
|
|||
JS_DEFINE_NATIVE_FUNCTION(InstantConstructor::from_epoch_milliseconds)
|
||||
{
|
||||
// 1. Set epochMilliseconds to ? ToNumber(epochMilliseconds).
|
||||
auto epoch_milliseconds_value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto epoch_milliseconds_value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
|
||||
// 2. Set epochMilliseconds to ? NumberToBigInt(epochMilliseconds).
|
||||
auto* epoch_milliseconds = number_to_bigint(global_object, epoch_milliseconds_value);
|
||||
|
|
|
@ -133,7 +133,6 @@ template<typename T>
|
|||
inline void integer_indexed_element_set(TypedArrayBase& typed_array, Value property_index, Value value)
|
||||
{
|
||||
VERIFY(!value.is_empty());
|
||||
auto& vm = typed_array.vm();
|
||||
auto& global_object = typed_array.global_object();
|
||||
|
||||
// 1. Assert: O is an Integer-Indexed exotic object.
|
||||
|
@ -149,9 +148,10 @@ inline void integer_indexed_element_set(TypedArrayBase& typed_array, Value prope
|
|||
}
|
||||
// 3. Otherwise, let numValue be ? ToNumber(value).
|
||||
else {
|
||||
num_value = value.to_number(global_object);
|
||||
if (vm.exception())
|
||||
auto num_value_or_error = value.to_number(global_object);
|
||||
if (num_value_or_error.is_error())
|
||||
return;
|
||||
num_value = num_value_or_error.release_value();
|
||||
}
|
||||
|
||||
// 4. If ! IsValidIntegerIndex(O, index) is true, then
|
||||
|
|
|
@ -248,13 +248,10 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::fill)
|
|||
auto length = typed_array->array_length();
|
||||
|
||||
Value value;
|
||||
if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt) {
|
||||
if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt)
|
||||
value = TRY_OR_DISCARD(vm.argument(0).to_bigint(global_object));
|
||||
} else {
|
||||
value = vm.argument(0).to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
}
|
||||
else
|
||||
value = TRY_OR_DISCARD(vm.argument(0).to_number(global_object));
|
||||
|
||||
auto relative_start = vm.argument(1).to_integer_or_infinity(global_object);
|
||||
if (vm.exception())
|
||||
|
@ -827,9 +824,7 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::set)
|
|||
if (typed_array->content_type() == TypedArrayBase::ContentType::BigInt)
|
||||
value = TRY_OR_DISCARD(value.to_bigint(global_object));
|
||||
else
|
||||
value = value.to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
value = TRY_OR_DISCARD(value.to_number(global_object));
|
||||
|
||||
if (target_buffer->is_detached()) {
|
||||
vm.throw_exception<JS::TypeError>(global_object, ErrorType::DetachedArrayBuffer);
|
||||
|
@ -978,9 +973,10 @@ static void typed_array_merge_sort(GlobalObject& global_object, FunctionObject*
|
|||
return;
|
||||
auto result = result_or_error.release_value();
|
||||
|
||||
auto value = result.to_number(global_object);
|
||||
if (vm.exception())
|
||||
auto value_or_error = result.to_number(global_object);
|
||||
if (value_or_error.is_error())
|
||||
return;
|
||||
auto value = value_or_error.release_value();
|
||||
|
||||
if (buffer.is_detached()) {
|
||||
vm.throw_exception<TypeError>(global_object, ErrorType::DetachedArrayBuffer);
|
||||
|
|
|
@ -461,18 +461,14 @@ ThrowCompletionOr<Object*> Value::to_object(GlobalObject& global_object) const
|
|||
// 7.1.3 ToNumeric ( value ), https://tc39.es/ecma262/#sec-tonumeric
|
||||
FLATTEN ThrowCompletionOr<Value> Value::to_numeric(GlobalObject& global_object) const
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
auto primitive = TRY(to_primitive(global_object, Value::PreferredType::Number));
|
||||
if (primitive.is_bigint())
|
||||
return primitive;
|
||||
auto number = primitive.to_number(global_object);
|
||||
if (auto* exception = vm.exception())
|
||||
return throw_completion(exception->value());
|
||||
return number;
|
||||
return primitive.to_number(global_object);
|
||||
}
|
||||
|
||||
// 7.1.4 ToNumber ( argument ), https://tc39.es/ecma262/#sec-tonumber
|
||||
Value Value::to_number(GlobalObject& global_object) const
|
||||
ThrowCompletionOr<Value> Value::to_number(GlobalObject& global_object) const
|
||||
{
|
||||
switch (m_type) {
|
||||
case Type::Undefined:
|
||||
|
@ -499,13 +495,11 @@ Value Value::to_number(GlobalObject& global_object) const
|
|||
return Value(parsed_double);
|
||||
}
|
||||
case Type::Symbol:
|
||||
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
|
||||
return {};
|
||||
return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "symbol", "number");
|
||||
case Type::BigInt:
|
||||
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
|
||||
return {};
|
||||
return global_object.vm().throw_completion<TypeError>(global_object, ErrorType::Convert, "BigInt", "number");
|
||||
case Type::Object: {
|
||||
auto primitive = TRY_OR_DISCARD(to_primitive(global_object, PreferredType::Number));
|
||||
auto primitive = TRY(to_primitive(global_object, PreferredType::Number));
|
||||
return primitive.to_number(global_object);
|
||||
}
|
||||
default:
|
||||
|
@ -561,10 +555,7 @@ ThrowCompletionOr<u64> Value::to_bigint_uint64(GlobalObject& global_object) cons
|
|||
|
||||
ThrowCompletionOr<double> Value::to_double(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (auto* exception = global_object.vm().exception())
|
||||
return throw_completion(exception->value());
|
||||
return number.as_double();
|
||||
return TRY(to_number(global_object)).as_double();
|
||||
}
|
||||
|
||||
// 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
|
||||
|
@ -579,10 +570,7 @@ ThrowCompletionOr<StringOrSymbol> Value::to_property_key(GlobalObject& global_ob
|
|||
i32 Value::to_i32_slow_case(GlobalObject& global_object) const
|
||||
{
|
||||
VERIFY(type() != Type::Int32);
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto abs = fabs(value);
|
||||
|
@ -599,10 +587,7 @@ i32 Value::to_i32_slow_case(GlobalObject& global_object) const
|
|||
// 7.1.7 ToUint32 ( argument ), https://tc39.es/ecma262/#sec-touint32
|
||||
u32 Value::to_u32(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto int_val = floor(fabs(value));
|
||||
|
@ -617,10 +602,7 @@ u32 Value::to_u32(GlobalObject& global_object) const
|
|||
// 7.1.8 ToInt16 ( argument ), https://tc39.es/ecma262/#sec-toint16
|
||||
i16 Value::to_i16(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto abs = fabs(value);
|
||||
|
@ -637,10 +619,7 @@ i16 Value::to_i16(GlobalObject& global_object) const
|
|||
// 7.1.9 ToUint16 ( argument ), https://tc39.es/ecma262/#sec-touint16
|
||||
u16 Value::to_u16(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto int_val = floor(fabs(value));
|
||||
|
@ -655,10 +634,7 @@ u16 Value::to_u16(GlobalObject& global_object) const
|
|||
// 7.1.10 ToInt8 ( argument ), https://tc39.es/ecma262/#sec-toint8
|
||||
i8 Value::to_i8(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto abs = fabs(value);
|
||||
|
@ -675,10 +651,7 @@ i8 Value::to_i8(GlobalObject& global_object) const
|
|||
// 7.1.11 ToUint8 ( argument ), https://tc39.es/ecma262/#sec-touint8
|
||||
u8 Value::to_u8(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
double value = number.as_double();
|
||||
double value = TRY_OR_DISCARD(to_number(global_object)).as_double();
|
||||
if (!isfinite(value) || value == 0)
|
||||
return 0;
|
||||
auto int_val = floor(fabs(value));
|
||||
|
@ -693,9 +666,7 @@ u8 Value::to_u8(GlobalObject& global_object) const
|
|||
// 7.1.12 ToUint8Clamp ( argument ), https://tc39.es/ecma262/#sec-touint8clamp
|
||||
u8 Value::to_u8_clamp(GlobalObject& global_object) const
|
||||
{
|
||||
auto number = to_number(global_object);
|
||||
if (global_object.vm().exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(to_number(global_object));
|
||||
if (number.is_nan())
|
||||
return 0;
|
||||
double value = number.as_double();
|
||||
|
@ -754,11 +725,7 @@ size_t Value::to_index(GlobalObject& global_object) const
|
|||
// 7.1.5 ToIntegerOrInfinity ( argument ), https://tc39.es/ecma262/#sec-tointegerorinfinity
|
||||
double Value::to_integer_or_infinity(GlobalObject& global_object) const
|
||||
{
|
||||
auto& vm = global_object.vm();
|
||||
|
||||
auto number = to_number(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto number = TRY_OR_DISCARD(to_number(global_object));
|
||||
if (number.is_nan() || number.as_double() == 0)
|
||||
return 0;
|
||||
if (number.is_infinity())
|
||||
|
@ -918,7 +885,7 @@ Value bitwise_not(GlobalObject& global_object, Value lhs)
|
|||
// 13.5.4 Unary + Operator, https://tc39.es/ecma262/#sec-unary-plus-operator
|
||||
Value unary_plus(GlobalObject& global_object, Value lhs)
|
||||
{
|
||||
return lhs.to_number(global_object);
|
||||
return TRY_OR_DISCARD(lhs.to_number(global_object));
|
||||
}
|
||||
|
||||
// 13.5.5 Unary - Operator, https://tc39.es/ecma262/#sec-unary-minus-operator
|
||||
|
@ -1354,11 +1321,11 @@ bool is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
|
|||
|
||||
// 5. If Type(x) is Number and Type(y) is String, return IsLooselyEqual(x, ! ToNumber(y)).
|
||||
if (lhs.is_number() && rhs.is_string())
|
||||
return is_loosely_equal(global_object, lhs, rhs.to_number(global_object));
|
||||
return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
|
||||
|
||||
// 6. If Type(x) is String and Type(y) is Number, return IsLooselyEqual(! ToNumber(x), y).
|
||||
if (lhs.is_string() && rhs.is_number())
|
||||
return is_loosely_equal(global_object, lhs.to_number(global_object), rhs);
|
||||
return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
|
||||
|
||||
// 7. If Type(x) is BigInt and Type(y) is String, then
|
||||
if (lhs.is_bigint() && rhs.is_string()) {
|
||||
|
@ -1377,11 +1344,11 @@ bool is_loosely_equal(GlobalObject& global_object, Value lhs, Value rhs)
|
|||
|
||||
// 9. If Type(x) is Boolean, return IsLooselyEqual(! ToNumber(x), y).
|
||||
if (lhs.is_boolean())
|
||||
return is_loosely_equal(global_object, lhs.to_number(global_object), rhs);
|
||||
return is_loosely_equal(global_object, MUST(lhs.to_number(global_object)), rhs);
|
||||
|
||||
// 10. If Type(y) is Boolean, return IsLooselyEqual(x, ! ToNumber(y)).
|
||||
if (rhs.is_boolean())
|
||||
return is_loosely_equal(global_object, lhs, rhs.to_number(global_object));
|
||||
return is_loosely_equal(global_object, lhs, MUST(rhs.to_number(global_object)));
|
||||
|
||||
// 11. If Type(x) is either String, Number, BigInt, or Symbol and Type(y) is Object, return IsLooselyEqual(x, ? ToPrimitive(y)).
|
||||
if ((lhs.is_string() || lhs.is_number() || lhs.is_bigint() || lhs.is_symbol()) && rhs.is_object()) {
|
||||
|
|
|
@ -309,7 +309,7 @@ public:
|
|||
ThrowCompletionOr<Value> to_primitive(GlobalObject&, PreferredType preferred_type = PreferredType::Default) const;
|
||||
ThrowCompletionOr<Object*> to_object(GlobalObject&) const;
|
||||
ThrowCompletionOr<Value> to_numeric(GlobalObject&) const;
|
||||
Value to_number(GlobalObject&) const;
|
||||
ThrowCompletionOr<Value> to_number(GlobalObject&) const;
|
||||
ThrowCompletionOr<BigInt*> to_bigint(GlobalObject&) const;
|
||||
ThrowCompletionOr<i64> to_bigint_int64(GlobalObject&) const;
|
||||
ThrowCompletionOr<u64> to_bigint_uint64(GlobalObject&) const;
|
||||
|
|
|
@ -964,10 +964,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReplObject::exit_interpreter)
|
|||
{
|
||||
if (!vm.argument_count())
|
||||
exit(0);
|
||||
auto exit_code = vm.argument(0).to_number(global_object);
|
||||
if (::vm->exception())
|
||||
return {};
|
||||
exit(exit_code.as_double());
|
||||
exit(TRY_OR_DISCARD(vm.argument(0).to_number(global_object)).as_double());
|
||||
}
|
||||
|
||||
JS_DEFINE_NATIVE_FUNCTION(ReplObject::repl_help)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue