mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
LibJS: Re-implement the Array Grouping proposal as static methods
Closes #19495.
This commit is contained in:
parent
f418605ec7
commit
419e710c1c
10 changed files with 220 additions and 276 deletions
|
@ -14,6 +14,7 @@
|
||||||
#include <LibJS/Runtime/CanonicalIndex.h>
|
#include <LibJS/Runtime/CanonicalIndex.h>
|
||||||
#include <LibJS/Runtime/FunctionObject.h>
|
#include <LibJS/Runtime/FunctionObject.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
|
#include <LibJS/Runtime/IteratorOperations.h>
|
||||||
#include <LibJS/Runtime/PrivateEnvironment.h>
|
#include <LibJS/Runtime/PrivateEnvironment.h>
|
||||||
#include <LibJS/Runtime/Value.h>
|
#include <LibJS/Runtime/Value.h>
|
||||||
|
|
||||||
|
@ -180,6 +181,117 @@ Vector<T> merge_lists(Vector<T> const& a, Vector<T> const& b)
|
||||||
return merged;
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4.2 AddValueToKeyedGroup ( groups, key, value ), https://tc39.es/proposal-array-grouping/#sec-add-value-to-keyed-group
|
||||||
|
template<typename GroupsType, typename KeyType>
|
||||||
|
void add_value_to_keyed_group(VM& vm, GroupsType& groups, KeyType key, Value value)
|
||||||
|
{
|
||||||
|
// 1. For each Record { [[Key]], [[Elements]] } g of groups, do
|
||||||
|
// a. If SameValue(g.[[Key]], key) is true, then
|
||||||
|
// NOTE: This is performed in KeyedGroupTraits::equals for groupToMap and Traits<JS::PropertyKey>::equals for group.
|
||||||
|
auto existing_elements_iterator = groups.find(key);
|
||||||
|
if (existing_elements_iterator != groups.end()) {
|
||||||
|
// i. Assert: exactly one element of groups meets this criteria.
|
||||||
|
// NOTE: This is done on insertion into the hash map, as only `set` tells us if we overrode an entry.
|
||||||
|
|
||||||
|
// ii. Append value as the last element of g.[[Elements]].
|
||||||
|
existing_elements_iterator->value.append(value);
|
||||||
|
|
||||||
|
// iii. Return unused.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Let group be the Record { [[Key]]: key, [[Elements]]: « value » }.
|
||||||
|
MarkedVector<Value> new_elements { vm.heap() };
|
||||||
|
new_elements.append(value);
|
||||||
|
|
||||||
|
// 3. Append group as the last element of groups.
|
||||||
|
auto result = groups.set(key, move(new_elements));
|
||||||
|
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
||||||
|
|
||||||
|
// 4. Return unused.
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4.1 GroupBy ( items, callbackfn, keyCoercion ), https://tc39.es/proposal-array-grouping/#sec-group-by
|
||||||
|
template<typename GroupsType, typename KeyType>
|
||||||
|
ThrowCompletionOr<GroupsType> group_by(VM& vm, Value items, Value callback_function)
|
||||||
|
{
|
||||||
|
// 1. Perform ? RequireObjectCoercible(items).
|
||||||
|
TRY(require_object_coercible(vm, items));
|
||||||
|
|
||||||
|
// 2. If IsCallable(callbackfn) is false, throw a TypeError exception.
|
||||||
|
if (!callback_function.is_function())
|
||||||
|
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, callback_function.to_string_without_side_effects()));
|
||||||
|
|
||||||
|
// 3. Let groups be a new empty List.
|
||||||
|
GroupsType groups;
|
||||||
|
|
||||||
|
// 4. Let iteratorRecord be ? GetIterator(items).
|
||||||
|
auto iterator_record = TRY(get_iterator(vm, items));
|
||||||
|
|
||||||
|
// 5. Let k be 0.
|
||||||
|
u64 k = 0;
|
||||||
|
|
||||||
|
// 6. Repeat,
|
||||||
|
while (true) {
|
||||||
|
// a. If k ≥ 2^53 - 1, then
|
||||||
|
if (k >= MAX_ARRAY_LIKE_INDEX) {
|
||||||
|
// i. Let error be ThrowCompletion(a newly created TypeError object).
|
||||||
|
auto error = vm.throw_completion<TypeError>(ErrorType::ArrayMaxSize);
|
||||||
|
|
||||||
|
// ii. Return ? IteratorClose(iteratorRecord, error).
|
||||||
|
return iterator_close(vm, iterator_record, move(error));
|
||||||
|
}
|
||||||
|
|
||||||
|
// b. Let next be ? IteratorStep(iteratorRecord).
|
||||||
|
auto next = TRY(iterator_step(vm, iterator_record));
|
||||||
|
|
||||||
|
// c. If next is false, then
|
||||||
|
if (!next) {
|
||||||
|
// i. Return groups.
|
||||||
|
return ThrowCompletionOr<GroupsType> { move(groups) };
|
||||||
|
}
|
||||||
|
|
||||||
|
// d. Let value be ? IteratorValue(next).
|
||||||
|
auto value = TRY(iterator_value(vm, *next));
|
||||||
|
|
||||||
|
// e. Let key be Completion(Call(callbackfn, undefined, « value, 𝔽(k) »)).
|
||||||
|
auto key = call(vm, callback_function, js_undefined(), value, Value(k));
|
||||||
|
|
||||||
|
// f. IfAbruptCloseIterator(key, iteratorRecord).
|
||||||
|
if (key.is_error())
|
||||||
|
return Completion { *TRY(iterator_close(vm, iterator_record, key.release_error())) };
|
||||||
|
|
||||||
|
// g. If keyCoercion is property, then
|
||||||
|
if constexpr (IsSame<KeyType, PropertyKey>) {
|
||||||
|
// i. Set key to Completion(ToPropertyKey(key)).
|
||||||
|
auto property_key = key.value().to_property_key(vm);
|
||||||
|
|
||||||
|
// ii. IfAbruptCloseIterator(key, iteratorRecord).
|
||||||
|
if (property_key.is_error())
|
||||||
|
return Completion { *TRY(iterator_close(vm, iterator_record, key.release_error())) };
|
||||||
|
|
||||||
|
add_value_to_keyed_group(vm, groups, property_key.release_value(), value);
|
||||||
|
}
|
||||||
|
// h. Else,
|
||||||
|
else {
|
||||||
|
// i. Assert: keyCoercion is zero.
|
||||||
|
static_assert(IsSame<KeyType, void>);
|
||||||
|
|
||||||
|
// ii. If key is -0𝔽, set key to +0𝔽.
|
||||||
|
if (key.value().is_negative_zero())
|
||||||
|
key = Value(0);
|
||||||
|
|
||||||
|
add_value_to_keyed_group(vm, groups, make_handle(key.release_value()), value);
|
||||||
|
}
|
||||||
|
|
||||||
|
// i. Perform AddValueToKeyedGroup(groups, key, value).
|
||||||
|
// NOTE: This is dependent on the `key_coercion` template parameter and thus done separately in the branches above.
|
||||||
|
|
||||||
|
// j. Set k to k + 1.
|
||||||
|
++k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// x modulo y, https://tc39.es/ecma262/#eqn-modulo
|
// x modulo y, https://tc39.es/ecma262/#eqn-modulo
|
||||||
template<Arithmetic T, Arithmetic U>
|
template<Arithmetic T, Arithmetic U>
|
||||||
auto modulo(T x, U y)
|
auto modulo(T x, U y)
|
||||||
|
|
|
@ -53,8 +53,6 @@ ThrowCompletionOr<void> ArrayPrototype::initialize(Realm& realm)
|
||||||
define_native_function(realm, vm.names.flat, flat, 0, attr);
|
define_native_function(realm, vm.names.flat, flat, 0, attr);
|
||||||
define_native_function(realm, vm.names.flatMap, flat_map, 1, attr);
|
define_native_function(realm, vm.names.flatMap, flat_map, 1, attr);
|
||||||
define_native_function(realm, vm.names.forEach, for_each, 1, attr);
|
define_native_function(realm, vm.names.forEach, for_each, 1, attr);
|
||||||
define_native_function(realm, vm.names.group, group, 1, attr);
|
|
||||||
define_native_function(realm, vm.names.groupToMap, group_to_map, 1, attr);
|
|
||||||
define_native_function(realm, vm.names.includes, includes, 1, attr);
|
define_native_function(realm, vm.names.includes, includes, 1, attr);
|
||||||
define_native_function(realm, vm.names.indexOf, index_of, 1, attr);
|
define_native_function(realm, vm.names.indexOf, index_of, 1, attr);
|
||||||
define_native_function(realm, vm.names.join, join, 1, attr);
|
define_native_function(realm, vm.names.join, join, 1, attr);
|
||||||
|
@ -87,7 +85,6 @@ ThrowCompletionOr<void> ArrayPrototype::initialize(Realm& realm)
|
||||||
define_direct_property(vm.well_known_symbol_iterator(), get_without_side_effects(vm.names.values), attr);
|
define_direct_property(vm.well_known_symbol_iterator(), get_without_side_effects(vm.names.values), attr);
|
||||||
|
|
||||||
// 23.1.3.41 Array.prototype [ @@unscopables ], https://tc39.es/ecma262/#sec-array.prototype-@@unscopables
|
// 23.1.3.41 Array.prototype [ @@unscopables ], https://tc39.es/ecma262/#sec-array.prototype-@@unscopables
|
||||||
// With array grouping proposal, https://tc39.es/proposal-array-grouping/#sec-array.prototype-@@unscopables
|
|
||||||
auto unscopable_list = Object::create(realm, nullptr);
|
auto unscopable_list = Object::create(realm, nullptr);
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.at, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.at, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.copyWithin, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.copyWithin, Value(true)));
|
||||||
|
@ -99,8 +96,6 @@ ThrowCompletionOr<void> ArrayPrototype::initialize(Realm& realm)
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.findLastIndex, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.findLastIndex, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.flat, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.flat, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.flatMap, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.flatMap, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.group, Value(true)));
|
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.groupToMap, Value(true)));
|
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.includes, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.includes, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.keys, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.keys, Value(true)));
|
||||||
MUST(unscopable_list->create_data_property_or_throw(vm.names.toReversed, Value(true)));
|
MUST(unscopable_list->create_data_property_or_throw(vm.names.toReversed, Value(true)));
|
||||||
|
@ -723,163 +718,6 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::for_each)
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2.3 AddValueToKeyedGroup ( groups, key, value ), https://tc39.es/proposal-array-grouping/#sec-add-value-to-keyed-group
|
|
||||||
template<typename GroupsType, typename KeyType>
|
|
||||||
static void add_value_to_keyed_group(VM& vm, GroupsType& groups, KeyType key, Value value)
|
|
||||||
{
|
|
||||||
// 1. For each Record { [[Key]], [[Elements]] } g of groups, do
|
|
||||||
// a. If SameValue(g.[[Key]], key) is true, then
|
|
||||||
// NOTE: This is performed in KeyedGroupTraits::equals for groupToMap and Traits<JS::PropertyKey>::equals for group.
|
|
||||||
auto existing_elements_iterator = groups.find(key);
|
|
||||||
if (existing_elements_iterator != groups.end()) {
|
|
||||||
// i. Assert: exactly one element of groups meets this criteria.
|
|
||||||
// NOTE: This is done on insertion into the hash map, as only `set` tells us if we overrode an entry.
|
|
||||||
|
|
||||||
// ii. Append value as the last element of g.[[Elements]].
|
|
||||||
existing_elements_iterator->value.append(value);
|
|
||||||
|
|
||||||
// iii. Return unused.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Let group be the Record { [[Key]]: key, [[Elements]]: « value » }.
|
|
||||||
MarkedVector<Value> new_elements { vm.heap() };
|
|
||||||
new_elements.append(value);
|
|
||||||
|
|
||||||
// 3. Append group as the last element of groups.
|
|
||||||
auto result = groups.set(key, move(new_elements));
|
|
||||||
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2.1 Array.prototype.group ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.group
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group)
|
|
||||||
{
|
|
||||||
auto& realm = *vm.current_realm();
|
|
||||||
|
|
||||||
auto callback_function = vm.argument(0);
|
|
||||||
auto this_arg = vm.argument(1);
|
|
||||||
|
|
||||||
// 1. Let O be ? ToObject(this value).
|
|
||||||
auto this_object = TRY(vm.this_value().to_object(vm));
|
|
||||||
|
|
||||||
// 2. Let len be ? LengthOfArrayLike(O).
|
|
||||||
auto length = TRY(length_of_array_like(vm, this_object));
|
|
||||||
|
|
||||||
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
|
|
||||||
if (!callback_function.is_function())
|
|
||||||
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, callback_function.to_string_without_side_effects()));
|
|
||||||
|
|
||||||
// 5. Let groups be a new empty List.
|
|
||||||
OrderedHashMap<PropertyKey, MarkedVector<Value>> groups;
|
|
||||||
|
|
||||||
// 4. Let k be 0.
|
|
||||||
// 6. Repeat, while k < len
|
|
||||||
for (size_t index = 0; index < length; ++index) {
|
|
||||||
// a. Let Pk be ! ToString(𝔽(k)).
|
|
||||||
auto index_property = PropertyKey { index };
|
|
||||||
|
|
||||||
// b. Let kValue be ? Get(O, Pk).
|
|
||||||
auto k_value = TRY(this_object->get(index_property));
|
|
||||||
|
|
||||||
// c. Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »)).
|
|
||||||
auto property_key_value = TRY(call(vm, callback_function.as_function(), this_arg, k_value, Value(index), this_object));
|
|
||||||
auto property_key = TRY(property_key_value.to_property_key(vm));
|
|
||||||
|
|
||||||
// d. Perform AddValueToKeyedGroup(groups, propertyKey, kValue).
|
|
||||||
add_value_to_keyed_group(vm, groups, property_key, k_value);
|
|
||||||
|
|
||||||
// e. Set k to k + 1.
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7. Let obj be OrdinaryObjectCreate(null).
|
|
||||||
auto object = Object::create(realm, nullptr);
|
|
||||||
|
|
||||||
// 8. For each Record { [[Key]], [[Elements]] } g of groups, do
|
|
||||||
for (auto& group : groups) {
|
|
||||||
// a. Let elements be CreateArrayFromList(g.[[Elements]]).
|
|
||||||
auto elements = Array::create_from(realm, group.value);
|
|
||||||
|
|
||||||
// b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
|
|
||||||
MUST(object->create_data_property_or_throw(group.key, elements));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 9. Return obj.
|
|
||||||
return object;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2.2 Array.prototype.groupToMap ( callbackfn [ , thisArg ] ), https://tc39.es/proposal-array-grouping/#sec-array.prototype.grouptomap
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::group_to_map)
|
|
||||||
{
|
|
||||||
auto& realm = *vm.current_realm();
|
|
||||||
|
|
||||||
auto callback_function = vm.argument(0);
|
|
||||||
auto this_arg = vm.argument(1);
|
|
||||||
|
|
||||||
// 1. Let O be ? ToObject(this value).
|
|
||||||
auto this_object = TRY(vm.this_value().to_object(vm));
|
|
||||||
|
|
||||||
// 2. Let len be ? LengthOfArrayLike(O).
|
|
||||||
auto length = TRY(length_of_array_like(vm, this_object));
|
|
||||||
|
|
||||||
// 3. If IsCallable(callbackfn) is false, throw a TypeError exception.
|
|
||||||
if (!callback_function.is_function())
|
|
||||||
return vm.throw_completion<TypeError>(ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, callback_function.to_string_without_side_effects()));
|
|
||||||
|
|
||||||
struct KeyedGroupTraits : public Traits<Handle<Value>> {
|
|
||||||
static unsigned hash(Handle<Value> const& value_handle)
|
|
||||||
{
|
|
||||||
return ValueTraits::hash(value_handle.value());
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool equals(Handle<Value> const& a, Handle<Value> const& b)
|
|
||||||
{
|
|
||||||
// AddValueToKeyedGroup uses SameValue on the keys on Step 1.a.
|
|
||||||
return same_value(a.value(), b.value());
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 5. Let groups be a new empty List.
|
|
||||||
OrderedHashMap<Handle<Value>, MarkedVector<Value>, KeyedGroupTraits> groups;
|
|
||||||
|
|
||||||
// 4. Let k be 0.
|
|
||||||
// 6. Repeat, while k < len
|
|
||||||
for (size_t index = 0; index < length; ++index) {
|
|
||||||
// a. Let Pk be ! ToString(𝔽(k)).
|
|
||||||
auto index_property = PropertyKey { index };
|
|
||||||
|
|
||||||
// b. Let kValue be ? Get(O, Pk).
|
|
||||||
auto k_value = TRY(this_object->get(index_property));
|
|
||||||
|
|
||||||
// c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
|
|
||||||
auto key = TRY(call(vm, callback_function.as_function(), this_arg, k_value, Value(index), this_object));
|
|
||||||
|
|
||||||
// d. If key is -0𝔽, set key to +0𝔽.
|
|
||||||
if (key.is_negative_zero())
|
|
||||||
key = Value(0);
|
|
||||||
|
|
||||||
// e. Perform AddValueToKeyedGroup(groups, key, kValue).
|
|
||||||
add_value_to_keyed_group(vm, groups, make_handle(key), k_value);
|
|
||||||
|
|
||||||
// f. Set k to k + 1.
|
|
||||||
}
|
|
||||||
|
|
||||||
// 7. Let map be ! Construct(%Map%).
|
|
||||||
auto map = Map::create(realm);
|
|
||||||
|
|
||||||
// 8. For each Record { [[Key]], [[Elements]] } g of groups, do
|
|
||||||
for (auto& group : groups) {
|
|
||||||
// a. Let elements be CreateArrayFromList(g.[[Elements]]).
|
|
||||||
auto elements = Array::create_from(realm, group.value);
|
|
||||||
|
|
||||||
// b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
|
|
||||||
// c. Append entry as the last element of map.[[MapData]].
|
|
||||||
map->map_set(group.key.value(), elements);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 9. Return map.
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 23.1.3.16 Array.prototype.includes ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-array.prototype.includes
|
// 23.1.3.16 Array.prototype.includes ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-array.prototype.includes
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::includes)
|
JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::includes)
|
||||||
{
|
{
|
||||||
|
|
|
@ -265,10 +265,10 @@ namespace JS {
|
||||||
P(globalThis) \
|
P(globalThis) \
|
||||||
P(granularity) \
|
P(granularity) \
|
||||||
P(group) \
|
P(group) \
|
||||||
|
P(groupBy) \
|
||||||
P(groupCollapsed) \
|
P(groupCollapsed) \
|
||||||
P(groupEnd) \
|
P(groupEnd) \
|
||||||
P(groups) \
|
P(groups) \
|
||||||
P(groupToMap) \
|
|
||||||
P(has) \
|
P(has) \
|
||||||
P(hasIndices) \
|
P(hasIndices) \
|
||||||
P(hasOwn) \
|
P(hasOwn) \
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
* Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
|
||||||
|
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibJS/Runtime/AbstractOperations.h>
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
|
#include <LibJS/Runtime/Array.h>
|
||||||
#include <LibJS/Runtime/Error.h>
|
#include <LibJS/Runtime/Error.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
|
||||||
#include <LibJS/Runtime/IteratorOperations.h>
|
#include <LibJS/Runtime/IteratorOperations.h>
|
||||||
#include <LibJS/Runtime/Map.h>
|
#include <LibJS/Runtime/Map.h>
|
||||||
#include <LibJS/Runtime/MapConstructor.h>
|
#include <LibJS/Runtime/MapConstructor.h>
|
||||||
|
@ -26,6 +27,9 @@ ThrowCompletionOr<void> MapConstructor::initialize(Realm& realm)
|
||||||
// 24.1.2.1 Map.prototype, https://tc39.es/ecma262/#sec-map.prototype
|
// 24.1.2.1 Map.prototype, https://tc39.es/ecma262/#sec-map.prototype
|
||||||
define_direct_property(vm.names.prototype, realm.intrinsics().map_prototype(), 0);
|
define_direct_property(vm.names.prototype, realm.intrinsics().map_prototype(), 0);
|
||||||
|
|
||||||
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
|
define_native_function(realm, vm.names.groupBy, group_by, 2, attr);
|
||||||
|
|
||||||
define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
define_native_accessor(realm, vm.well_known_symbol_species(), symbol_species_getter, {}, Attribute::Configurable);
|
||||||
|
|
||||||
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||||
|
@ -68,6 +72,47 @@ ThrowCompletionOr<NonnullGCPtr<Object>> MapConstructor::construct(FunctionObject
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 3.1 Map.groupBy ( items, callbackfn ), https://tc39.es/proposal-array-grouping/#sec-map.groupby
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(MapConstructor::group_by)
|
||||||
|
{
|
||||||
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
auto items = vm.argument(0);
|
||||||
|
auto callback_function = vm.argument(1);
|
||||||
|
|
||||||
|
struct KeyedGroupTraits : public Traits<Handle<Value>> {
|
||||||
|
static unsigned hash(Handle<Value> const& value_handle)
|
||||||
|
{
|
||||||
|
return ValueTraits::hash(value_handle.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool equals(Handle<Value> const& a, Handle<Value> const& b)
|
||||||
|
{
|
||||||
|
// AddValueToKeyedGroup uses SameValue on the keys on Step 1.a.
|
||||||
|
return same_value(a.value(), b.value());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 1. Let groups be ? GroupBy(items, callbackfn, zero).
|
||||||
|
auto groups = TRY((JS::group_by<OrderedHashMap<Handle<Value>, MarkedVector<Value>, KeyedGroupTraits>, void>(vm, items, callback_function)));
|
||||||
|
|
||||||
|
// 2. Let map be ! Construct(%Map%).
|
||||||
|
auto map = Map::create(realm);
|
||||||
|
|
||||||
|
// 3. For each Record { [[Key]], [[Elements]] } g of groups, do
|
||||||
|
for (auto& group : groups) {
|
||||||
|
// a. Let elements be CreateArrayFromList(g.[[Elements]]).
|
||||||
|
auto elements = Array::create_from(realm, group.value);
|
||||||
|
|
||||||
|
// b. Let entry be the Record { [[Key]]: g.[[Key]], [[Value]]: elements }.
|
||||||
|
// c. Append entry to map.[[MapData]].
|
||||||
|
map->map_set(group.key.value(), elements);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Return map.
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
|
||||||
// 24.1.2.2 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
|
// 24.1.2.2 get Map [ @@species ], https://tc39.es/ecma262/#sec-get-map-@@species
|
||||||
JS_DEFINE_NATIVE_FUNCTION(MapConstructor::symbol_species_getter)
|
JS_DEFINE_NATIVE_FUNCTION(MapConstructor::symbol_species_getter)
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,6 +25,8 @@ private:
|
||||||
|
|
||||||
virtual bool has_constructor() const override { return true; }
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(group_by);
|
||||||
|
|
||||||
JS_DECLARE_NATIVE_FUNCTION(symbol_species_getter);
|
JS_DECLARE_NATIVE_FUNCTION(symbol_species_getter);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,7 @@ ThrowCompletionOr<void> ObjectConstructor::initialize(Realm& realm)
|
||||||
define_native_function(realm, vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
|
define_native_function(realm, vm.names.getOwnPropertyNames, get_own_property_names, 1, attr);
|
||||||
define_native_function(realm, vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
|
define_native_function(realm, vm.names.getOwnPropertySymbols, get_own_property_symbols, 1, attr);
|
||||||
define_native_function(realm, vm.names.getPrototypeOf, get_prototype_of, 1, attr);
|
define_native_function(realm, vm.names.getPrototypeOf, get_prototype_of, 1, attr);
|
||||||
|
define_native_function(realm, vm.names.groupBy, group_by, 2, attr);
|
||||||
define_native_function(realm, vm.names.setPrototypeOf, set_prototype_of, 2, attr);
|
define_native_function(realm, vm.names.setPrototypeOf, set_prototype_of, 2, attr);
|
||||||
define_native_function(realm, vm.names.isExtensible, is_extensible, 1, attr);
|
define_native_function(realm, vm.names.isExtensible, is_extensible, 1, attr);
|
||||||
define_native_function(realm, vm.names.isFrozen, is_frozen, 1, attr);
|
define_native_function(realm, vm.names.isFrozen, is_frozen, 1, attr);
|
||||||
|
@ -372,6 +373,33 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_prototype_of)
|
||||||
return TRY(object->internal_get_prototype_of());
|
return TRY(object->internal_get_prototype_of());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2.1 Object.groupBy ( items, callbackfn ), https://tc39.es/proposal-array-grouping/#sec-object.groupby
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::group_by)
|
||||||
|
{
|
||||||
|
auto& realm = *vm.current_realm();
|
||||||
|
|
||||||
|
auto items = vm.argument(0);
|
||||||
|
auto callback_function = vm.argument(1);
|
||||||
|
|
||||||
|
// 1. Let groups be ? GroupBy(items, callbackfn, property).
|
||||||
|
auto groups = TRY((JS::group_by<OrderedHashMap<PropertyKey, MarkedVector<Value>>, PropertyKey>(vm, items, callback_function)));
|
||||||
|
|
||||||
|
// 2. Let obj be OrdinaryObjectCreate(null).
|
||||||
|
auto object = Object::create(realm, nullptr);
|
||||||
|
|
||||||
|
// 3. For each Record { [[Key]], [[Elements]] } g of groups, do
|
||||||
|
for (auto& group : groups) {
|
||||||
|
// a. Let elements be CreateArrayFromList(g.[[Elements]]).
|
||||||
|
auto elements = Array::create_from(realm, group.value);
|
||||||
|
|
||||||
|
// b. Perform ! CreateDataPropertyOrThrow(obj, g.[[Key]], elements).
|
||||||
|
MUST(object->create_data_property_or_throw(group.key, elements));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Return obj.
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
|
||||||
// 20.1.2.13 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
|
// 20.1.2.13 Object.hasOwn ( O, P ), https://tc39.es/ecma262/#sec-object.hasown
|
||||||
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
|
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
|
||||||
{
|
{
|
||||||
|
|
|
@ -34,6 +34,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_own_property_names);
|
JS_DECLARE_NATIVE_FUNCTION(get_own_property_names);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_own_property_symbols);
|
JS_DECLARE_NATIVE_FUNCTION(get_own_property_symbols);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(get_prototype_of);
|
JS_DECLARE_NATIVE_FUNCTION(get_prototype_of);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(group_by);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(set_prototype_of);
|
JS_DECLARE_NATIVE_FUNCTION(set_prototype_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(is_extensible);
|
JS_DECLARE_NATIVE_FUNCTION(is_extensible);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(is_frozen);
|
JS_DECLARE_NATIVE_FUNCTION(is_frozen);
|
||||||
|
|
|
@ -306,43 +306,6 @@ describe("ability to work with generic non-array objects", () => {
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("group", () => {
|
|
||||||
const visited = [];
|
|
||||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
|
||||||
const result = Array.prototype.group.call(o, (value, _, object) => {
|
|
||||||
expect(object).toBe(o);
|
|
||||||
visited.push(value);
|
|
||||||
return value !== undefined ? value.startsWith("b") : false;
|
|
||||||
});
|
|
||||||
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
|
|
||||||
expect(result.false).toEqual(["foo", undefined, undefined]);
|
|
||||||
expect(result.true).toEqual(["bar", "baz"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("groupToMap", () => {
|
|
||||||
const visited = [];
|
|
||||||
const o = { length: 5, 0: "foo", 1: "bar", 3: "baz" };
|
|
||||||
const falseObject = { false: false };
|
|
||||||
const trueObject = { true: true };
|
|
||||||
const result = Array.prototype.groupToMap.call(o, (value, _, object) => {
|
|
||||||
expect(object).toBe(o);
|
|
||||||
visited.push(value);
|
|
||||||
return value !== undefined
|
|
||||||
? value.startsWith("b")
|
|
||||||
? trueObject
|
|
||||||
: falseObject
|
|
||||||
: falseObject;
|
|
||||||
});
|
|
||||||
expect(visited).toEqual(["foo", "bar", undefined, "baz", undefined]);
|
|
||||||
expect(result).toBeInstanceOf(Map);
|
|
||||||
|
|
||||||
const falseResult = result.get(falseObject);
|
|
||||||
expect(falseResult).toEqual(["foo", undefined, undefined]);
|
|
||||||
|
|
||||||
const trueResult = result.get(trueObject);
|
|
||||||
expect(trueResult).toEqual(["bar", "baz"]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("toReversed", () => {
|
test("toReversed", () => {
|
||||||
const result = Array.prototype.toReversed.call(o);
|
const result = Array.prototype.toReversed.call(o);
|
||||||
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
|
expect(result).toEqual([undefined, "baz", undefined, "bar", "foo"]);
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
test("length is 1", () => {
|
test("length is 2", () => {
|
||||||
expect(Array.prototype.groupToMap).toHaveLength(1);
|
expect(Map.groupBy).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
test("callback must be a function", () => {
|
test("callback must be a function", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
[].groupToMap(undefined);
|
Map.groupBy([], undefined);
|
||||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("null or undefined this value", () => {
|
test("null or undefined items value", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.groupToMap.call();
|
Map.groupBy();
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.groupToMap.call(undefined);
|
Map.groupBy(undefined);
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.groupToMap.call(null);
|
Map.groupBy(null);
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "null cannot be converted to an object");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ describe("normal behavior", () => {
|
||||||
const trueObject = { true: true };
|
const trueObject = { true: true };
|
||||||
const falseObject = { false: false };
|
const falseObject = { false: false };
|
||||||
|
|
||||||
const firstResult = array.groupToMap(value => {
|
const firstResult = Map.groupBy(array, value => {
|
||||||
visited.push(value);
|
visited.push(value);
|
||||||
return value % 2 === 0 ? trueObject : falseObject;
|
return value % 2 === 0 ? trueObject : falseObject;
|
||||||
});
|
});
|
||||||
|
@ -42,7 +42,7 @@ describe("normal behavior", () => {
|
||||||
expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
|
expect(firstResult.get(trueObject)).toEqual([2, 4, 6]);
|
||||||
expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
|
expect(firstResult.get(falseObject)).toEqual([1, 3, 5]);
|
||||||
|
|
||||||
const secondResult = array.groupToMap((_, index) => {
|
const secondResult = Map.groupBy(array, (_, index) => {
|
||||||
return index < array.length / 2 ? trueObject : falseObject;
|
return index < array.length / 2 ? trueObject : falseObject;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -50,33 +50,11 @@ describe("normal behavior", () => {
|
||||||
expect(secondResult.size).toBe(2);
|
expect(secondResult.size).toBe(2);
|
||||||
expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
|
expect(secondResult.get(trueObject)).toEqual([1, 2, 3]);
|
||||||
expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
|
expect(secondResult.get(falseObject)).toEqual([4, 5, 6]);
|
||||||
|
|
||||||
const thisArg = [7, 8, 9, 10, 11, 12];
|
|
||||||
const thirdResult = array.groupToMap(function (_, __, arrayVisited) {
|
|
||||||
expect(arrayVisited).toBe(array);
|
|
||||||
expect(this).toBe(thisArg);
|
|
||||||
}, thisArg);
|
|
||||||
|
|
||||||
expect(thirdResult).toBeInstanceOf(Map);
|
|
||||||
expect(thirdResult.size).toBe(1);
|
|
||||||
expect(thirdResult.get(undefined)).not.toBe(array);
|
|
||||||
expect(thirdResult.get(undefined)).not.toBe(thisArg);
|
|
||||||
expect(thirdResult.get(undefined)).toEqual(array);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("is unscopable", () => {
|
|
||||||
expect(Array.prototype[Symbol.unscopables].groupToMap).toBeTrue();
|
|
||||||
const array = [];
|
|
||||||
with (array) {
|
|
||||||
expect(() => {
|
|
||||||
groupToMap;
|
|
||||||
}).toThrowWithMessage(ReferenceError, "'groupToMap' is not defined");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("never calls callback with empty array", () => {
|
test("never calls callback with empty array", () => {
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
const result = [].groupToMap(() => {
|
const result = Map.groupBy([], () => {
|
||||||
callbackCalled++;
|
callbackCalled++;
|
||||||
});
|
});
|
||||||
expect(result).toBeInstanceOf(Map);
|
expect(result).toBeInstanceOf(Map);
|
||||||
|
@ -86,7 +64,7 @@ describe("normal behavior", () => {
|
||||||
|
|
||||||
test("calls callback once for every item", () => {
|
test("calls callback once for every item", () => {
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
const result = [1, 2, 3].groupToMap(() => {
|
const result = Map.groupBy([1, 2, 3], () => {
|
||||||
callbackCalled++;
|
callbackCalled++;
|
||||||
});
|
});
|
||||||
expect(result).toBeInstanceOf(Map);
|
expect(result).toBeInstanceOf(Map);
|
||||||
|
@ -96,8 +74,9 @@ describe("normal behavior", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("still returns a Map even if the global Map constructor was changed", () => {
|
test("still returns a Map even if the global Map constructor was changed", () => {
|
||||||
|
const mapGroupBy = Map.groupBy;
|
||||||
globalThis.Map = null;
|
globalThis.Map = null;
|
||||||
const result = [1, 2].groupToMap(value => {
|
const result = mapGroupBy([1, 2], value => {
|
||||||
return value % 2 === 0;
|
return value % 2 === 0;
|
||||||
});
|
});
|
||||||
expect(result.size).toBe(2);
|
expect(result.size).toBe(2);
|
|
@ -1,26 +1,26 @@
|
||||||
test("length is 1", () => {
|
test("length is 2", () => {
|
||||||
expect(Array.prototype.group).toHaveLength(1);
|
expect(Object.groupBy).toHaveLength(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
test("callback must be a function", () => {
|
test("callback must be a function", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
[].group(undefined);
|
Object.groupBy([], undefined);
|
||||||
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
}).toThrowWithMessage(TypeError, "undefined is not a function");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("null or undefined this value", () => {
|
test("null or undefined items value", () => {
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.group.call();
|
Object.groupBy();
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.group.call(undefined);
|
Object.groupBy(undefined);
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "undefined cannot be converted to an object");
|
||||||
|
|
||||||
expect(() => {
|
expect(() => {
|
||||||
Array.prototype.group.call(null);
|
Object.groupBy(null);
|
||||||
}).toThrowWithMessage(TypeError, "ToObject on null or undefined");
|
}).toThrowWithMessage(TypeError, "null cannot be converted to an object");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -29,7 +29,7 @@ describe("normal behavior", () => {
|
||||||
const array = [1, 2, 3, 4, 5, 6];
|
const array = [1, 2, 3, 4, 5, 6];
|
||||||
const visited = [];
|
const visited = [];
|
||||||
|
|
||||||
const firstResult = array.group(value => {
|
const firstResult = Object.groupBy(array, value => {
|
||||||
visited.push(value);
|
visited.push(value);
|
||||||
return value % 2 === 0;
|
return value % 2 === 0;
|
||||||
});
|
});
|
||||||
|
@ -43,7 +43,7 @@ describe("normal behavior", () => {
|
||||||
expect(firstKeys[0]).toBe("false");
|
expect(firstKeys[0]).toBe("false");
|
||||||
expect(firstKeys[1]).toBe("true");
|
expect(firstKeys[1]).toBe("true");
|
||||||
|
|
||||||
const secondResult = array.group((_, index) => {
|
const secondResult = Object.groupBy(array, (_, index) => {
|
||||||
return index < array.length / 2;
|
return index < array.length / 2;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -54,36 +54,12 @@ describe("normal behavior", () => {
|
||||||
expect(secondKeys).toHaveLength(2);
|
expect(secondKeys).toHaveLength(2);
|
||||||
expect(secondKeys[0]).toBe("true");
|
expect(secondKeys[0]).toBe("true");
|
||||||
expect(secondKeys[1]).toBe("false");
|
expect(secondKeys[1]).toBe("false");
|
||||||
|
|
||||||
const thisArg = [7, 8, 9, 10, 11, 12];
|
|
||||||
const thirdResult = array.group(function (_, __, arrayVisited) {
|
|
||||||
expect(arrayVisited).toBe(array);
|
|
||||||
expect(this).toBe(thisArg);
|
|
||||||
}, thisArg);
|
|
||||||
|
|
||||||
expect(thirdResult.undefined).not.toBe(array);
|
|
||||||
expect(thirdResult.undefined).not.toBe(thisArg);
|
|
||||||
expect(thirdResult.undefined).toEqual(array);
|
|
||||||
|
|
||||||
const thirdKeys = Object.keys(thirdResult);
|
|
||||||
expect(thirdKeys).toHaveLength(1);
|
|
||||||
expect(thirdKeys[0]).toBe("undefined");
|
|
||||||
});
|
|
||||||
|
|
||||||
test("is unscopable", () => {
|
|
||||||
expect(Array.prototype[Symbol.unscopables].group).toBeTrue();
|
|
||||||
const array = [];
|
|
||||||
with (array) {
|
|
||||||
expect(() => {
|
|
||||||
group;
|
|
||||||
}).toThrowWithMessage(ReferenceError, "'group' is not defined");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("never calls callback with empty array", () => {
|
test("never calls callback with empty array", () => {
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
expect(
|
expect(
|
||||||
[].group(() => {
|
Object.groupBy([], () => {
|
||||||
callbackCalled++;
|
callbackCalled++;
|
||||||
})
|
})
|
||||||
).toEqual({});
|
).toEqual({});
|
||||||
|
@ -92,7 +68,7 @@ describe("normal behavior", () => {
|
||||||
|
|
||||||
test("calls callback once for every item", () => {
|
test("calls callback once for every item", () => {
|
||||||
var callbackCalled = 0;
|
var callbackCalled = 0;
|
||||||
const result = [1, 2, 3].group(() => {
|
const result = Object.groupBy([1, 2, 3], () => {
|
||||||
callbackCalled++;
|
callbackCalled++;
|
||||||
});
|
});
|
||||||
expect(result.undefined).toEqual([1, 2, 3]);
|
expect(result.undefined).toEqual([1, 2, 3]);
|
Loading…
Add table
Reference in a new issue