mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Use new construct AO overload where easily applicable
This commit is contained in:
parent
67e02f6ca6
commit
59ca435172
Notes:
sideshowbarker
2024-07-17 20:13:25 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/59ca435172a Pull-request: https://github.com/SerenityOS/serenity/pull/12136
10 changed files with 16 additions and 55 deletions
|
@ -63,10 +63,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayBufferPrototype::slice)
|
|||
auto new_length = max(final - first, 0.0);
|
||||
|
||||
auto* constructor = TRY(species_constructor(global_object, *array_buffer_object, *global_object.array_buffer_constructor()));
|
||||
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(Value(new_length));
|
||||
auto* new_array_buffer = TRY(construct(global_object, *constructor, move(arguments)));
|
||||
auto* new_array_buffer = TRY(construct(global_object, *constructor, Value(new_length)));
|
||||
|
||||
if (!is<ArrayBuffer>(new_array_buffer))
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::SpeciesConstructorDidNotCreate, "an ArrayBuffer");
|
||||
|
|
|
@ -148,13 +148,10 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
|
|||
auto length = TRY(length_of_array_like(global_object, *array_like));
|
||||
|
||||
Object* array;
|
||||
if (constructor.is_constructor()) {
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.empend(length);
|
||||
array = TRY(JS::construct(global_object, constructor.as_function(), move(arguments)));
|
||||
} else {
|
||||
if (constructor.is_constructor())
|
||||
array = TRY(JS::construct(global_object, constructor.as_function(), Value(length)));
|
||||
else
|
||||
array = TRY(Array::create(global_object, length));
|
||||
}
|
||||
|
||||
for (size_t k = 0; k < length; ++k) {
|
||||
auto k_value = TRY(array_like->get(k));
|
||||
|
@ -183,13 +180,10 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::of)
|
|||
{
|
||||
auto this_value = vm.this_value(global_object);
|
||||
Object* array;
|
||||
if (this_value.is_constructor()) {
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.empend(vm.argument_count());
|
||||
array = TRY(JS::construct(global_object, this_value.as_function(), move(arguments)));
|
||||
} else {
|
||||
if (this_value.is_constructor())
|
||||
array = TRY(JS::construct(global_object, this_value.as_function(), Value(vm.argument_count())));
|
||||
else
|
||||
array = TRY(Array::create(global_object, vm.argument_count()));
|
||||
}
|
||||
for (size_t k = 0; k < vm.argument_count(); ++k)
|
||||
TRY(array->create_data_property_or_throw(k, vm.argument(k)));
|
||||
TRY(array->set(vm.names.length, Value(vm.argument_count()), Object::ShouldThrowExceptions::Yes));
|
||||
|
|
|
@ -142,9 +142,7 @@ static ThrowCompletionOr<Object*> array_species_create(GlobalObject& global_obje
|
|||
if (!constructor.is_constructor())
|
||||
return vm.throw_completion<TypeError>(global_object, ErrorType::NotAConstructor, constructor.to_string_without_side_effects());
|
||||
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(Value(length));
|
||||
return TRY(construct(global_object, constructor.as_function(), move(arguments)));
|
||||
return TRY(construct(global_object, constructor.as_function(), Value(length)));
|
||||
}
|
||||
|
||||
// 23.1.3.8 Array.prototype.filter ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-array.prototype.filter
|
||||
|
|
|
@ -21,7 +21,6 @@
|
|||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||
#include <LibJS/Runtime/MarkedValueList.h>
|
||||
#include <LibJS/Runtime/Temporal/Instant.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibTimeZone/TimeZone.h>
|
||||
|
@ -890,11 +889,7 @@ JS_DEFINE_NATIVE_FUNCTION(DatePrototype::to_json)
|
|||
|
||||
static ThrowCompletionOr<Intl::DateTimeFormat*> construct_date_time_format(GlobalObject& global_object, Value locales, Value options)
|
||||
{
|
||||
MarkedValueList arguments { global_object.vm().heap() };
|
||||
arguments.append(locales);
|
||||
arguments.append(options);
|
||||
|
||||
auto* date_time_format = TRY(construct(global_object, *global_object.intl_date_time_format_constructor(), move(arguments)));
|
||||
auto* date_time_format = TRY(construct(global_object, *global_object.intl_date_time_format_constructor(), locales, options));
|
||||
return static_cast<Intl::DateTimeFormat*>(date_time_format);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,6 @@
|
|||
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||
#include <LibJS/Runtime/Intl/NumberFormat.h>
|
||||
#include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
|
||||
#include <LibJS/Runtime/MarkedValueList.h>
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
#include <LibJS/Runtime/Temporal/TimeZone.h>
|
||||
#include <LibJS/Runtime/Utf16String.h>
|
||||
|
@ -828,11 +827,7 @@ ThrowCompletionOr<Vector<PatternPartition>> format_date_time_pattern(GlobalObjec
|
|||
auto const& data_locale = date_time_format.data_locale();
|
||||
|
||||
auto construct_number_format = [&](auto* options) -> ThrowCompletionOr<NumberFormat*> {
|
||||
MarkedValueList arguments { vm.heap() };
|
||||
arguments.append(js_string(vm, locale));
|
||||
arguments.append(options);
|
||||
|
||||
auto* number_format = TRY(construct(global_object, *global_object.intl_number_format_constructor(), move(arguments)));
|
||||
auto* number_format = TRY(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale), options));
|
||||
return static_cast<NumberFormat*>(number_format);
|
||||
};
|
||||
|
||||
|
|
|
@ -110,11 +110,8 @@ ThrowCompletionOr<RelativeTimeFormat*> initialize_relative_time_format(GlobalObj
|
|||
// 18. Set relativeTimeFormat.[[Numeric]] to numeric.
|
||||
relative_time_format.set_numeric(numeric.as_string().string());
|
||||
|
||||
MarkedValueList arguments { vm.heap() };
|
||||
arguments.append(js_string(vm, locale));
|
||||
|
||||
// 19. Let relativeTimeFormat.[[NumberFormat]] be ! Construct(%NumberFormat%, « locale »).
|
||||
auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), move(arguments)));
|
||||
auto* number_format = MUST(construct(global_object, *global_object.intl_number_format_constructor(), js_string(vm, locale)));
|
||||
relative_time_format.set_number_format(static_cast<NumberFormat*>(number_format));
|
||||
|
||||
// 20. Let relativeTimeFormat.[[PluralRules]] be ! Construct(%PluralRules%, « locale »).
|
||||
|
|
|
@ -290,12 +290,8 @@ JS_DEFINE_NATIVE_FUNCTION(NumberPrototype::to_locale_string)
|
|||
// 1. Let x be ? thisNumberValue(this value).
|
||||
auto number_value = TRY(this_number_value(global_object, vm.this_value(global_object)));
|
||||
|
||||
MarkedValueList arguments { vm.heap() };
|
||||
arguments.append(locales);
|
||||
arguments.append(options);
|
||||
|
||||
// 2. Let numberFormat be ? Construct(%NumberFormat%, « locales, options »).
|
||||
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(global_object, *global_object.intl_number_format_constructor(), move(arguments))));
|
||||
auto* number_format = static_cast<Intl::NumberFormat*>(TRY(construct(global_object, *global_object.intl_number_format_constructor(), locales, options)));
|
||||
|
||||
// 3. Return ? FormatNumeric(numberFormat, x).
|
||||
// Note: Our implementation of FormatNumeric does not throw.
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include <LibJS/Runtime/AbstractOperations.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/MarkedValueList.h>
|
||||
#include <LibJS/Runtime/NativeFunction.h>
|
||||
#include <LibJS/Runtime/PromiseReaction.h>
|
||||
|
||||
|
@ -59,9 +58,7 @@ ThrowCompletionOr<PromiseCapability> new_promise_capability(GlobalObject& global
|
|||
executor->define_direct_property(vm.names.name, js_string(vm, String::empty()), Attribute::Configurable);
|
||||
|
||||
// 6. Let promise be ? Construct(C, « executor »).
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(executor);
|
||||
auto* promise = TRY(construct(global_object, constructor.as_function(), move(arguments)));
|
||||
auto* promise = TRY(construct(global_object, constructor.as_function(), executor));
|
||||
|
||||
// 7. If IsCallable(promiseCapability.[[Resolve]]) is false, throw a TypeError exception.
|
||||
if (!promise_capability_functions.resolve.is_function())
|
||||
|
|
|
@ -600,10 +600,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_match_all)
|
|||
bool full_unicode = flags.contains('u');
|
||||
|
||||
// 6. Let matcher be ? Construct(C, « R, flags »).
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(regexp_object);
|
||||
arguments.append(js_string(vm, move(flags)));
|
||||
auto* matcher = TRY(construct(global_object, *constructor, move(arguments)));
|
||||
auto* matcher = TRY(construct(global_object, *constructor, regexp_object, js_string(vm, move(flags))));
|
||||
|
||||
// 7. Let lastIndex be ? ToLength(? Get(R, "lastIndex")).
|
||||
auto last_index_value = TRY(regexp_object->get(vm.names.lastIndex));
|
||||
|
@ -894,10 +891,7 @@ JS_DEFINE_NATIVE_FUNCTION(RegExpPrototype::symbol_split)
|
|||
auto new_flags = flags.find('y').has_value() ? move(flags) : String::formatted("{}y", flags);
|
||||
|
||||
// 10. Let splitter be ? Construct(C, « rx, newFlags »).
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(regexp_object);
|
||||
arguments.append(js_string(vm, move(new_flags)));
|
||||
auto* splitter = TRY(construct(global_object, *constructor, move(arguments)));
|
||||
auto* splitter = TRY(construct(global_object, *constructor, regexp_object, js_string(vm, move(new_flags))));
|
||||
|
||||
// 11. Let A be ! ArrayCreate(0).
|
||||
auto* array = MUST(Array::create(global_object, 0));
|
||||
|
|
|
@ -70,9 +70,7 @@ ThrowCompletionOr<Calendar*> get_builtin_calendar(GlobalObject& global_object, S
|
|||
return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidCalendarIdentifier, identifier);
|
||||
|
||||
// 2. Return ? Construct(%Temporal.Calendar%, « id »).
|
||||
MarkedValueList arguments(vm.heap());
|
||||
arguments.append(js_string(vm, identifier));
|
||||
return static_cast<Calendar*>(TRY(construct(global_object, *global_object.temporal_calendar_constructor(), move(arguments))));
|
||||
return static_cast<Calendar*>(TRY(construct(global_object, *global_object.temporal_calendar_constructor(), js_string(vm, identifier))));
|
||||
}
|
||||
|
||||
// 12.1.4 GetISO8601Calendar ( ), https://tc39.es/proposal-temporal/#sec-temporal-getiso8601calendar
|
||||
|
|
Loading…
Add table
Reference in a new issue