Commit a438bc41 authored by legendecas's avatar legendecas Committed by V8 LUCI CQ

[builtins] Rename array grouping to group and groupToMap

Rename array grouping methods according to the consensus on TC39 June
meeting.

Bug: v8:12499
Change-Id: I89cb3a49b0de45b74131877bb70d38b49233f275
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3692436
Commit-Queue: Chengzhong Wu <legendecas@gmail.com>
Reviewed-by: 's avatarShu-yu Guo <syg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81009}
parent 7d34f881
......@@ -1586,10 +1586,10 @@ inline bool CheckArrayMapNotModified(Handle<JSArray> array,
return Protectors::IsNoElementsIntact(array->GetIsolate());
}
enum class GroupByMode { kToObject, kToMap };
enum class ArrayGroupMode { kToObject, kToMap };
template <GroupByMode mode>
inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy(
template <ArrayGroupMode mode>
inline MaybeHandle<OrderedHashMap> GenericArrayGroup(
Isolate* isolate, Handle<JSReceiver> O, Handle<Object> callbackfn,
Handle<OrderedHashMap> groups, double initialK, double len) {
// 6. Repeat, while k < len
......@@ -1605,7 +1605,7 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy(
Object::GetPropertyOrElement(isolate, O, Pk),
OrderedHashMap);
// Common steps for ArrayPrototypeGroupBy and ArrayPrototypeGroupByToMap
// Common steps for ArrayPrototypeGroup and ArrayPrototypeGroupToMap
// 6c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
Handle<Object> propertyKey;
Handle<Object> argv[] = {kValue, isolate->factory()->NewNumber(k), O};
......@@ -1613,7 +1613,7 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy(
Execution::Call(isolate, callbackfn, O, 3, argv),
OrderedHashMap);
if (mode == GroupByMode::kToMap) {
if (mode == ArrayGroupMode::kToMap) {
// 6d. If key is -0𝔽, set key to +0𝔽.
if (propertyKey->IsMinusZero()) {
propertyKey = Handle<Smi>(Smi::FromInt(0), isolate);
......@@ -1638,8 +1638,8 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroupBy(
return groups;
}
template <GroupByMode mode>
inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
template <ArrayGroupMode mode>
inline MaybeHandle<OrderedHashMap> FastArrayGroup(
Isolate* isolate, Handle<JSArray> array, Handle<Object> callbackfn,
Handle<OrderedHashMap> groups, double len,
ElementsKind* result_elements_kind) {
......@@ -1654,8 +1654,8 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
for (InternalIndex k : InternalIndex::Range(uint_len)) {
if (!CheckArrayMapNotModified(array, original_map) ||
k.as_uint32() >= static_cast<uint32_t>(array->length().Number())) {
return GenericArrayGroupBy<mode>(isolate, array, callbackfn, groups,
k.as_uint32(), len);
return GenericArrayGroup<mode>(isolate, array, callbackfn, groups,
k.as_uint32(), len);
}
// 6a. Let Pk be ! ToString(𝔽(k)).
// 6b. Let kValue be ? Get(O, Pk).
......@@ -1664,7 +1664,7 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
kValue = isolate->factory()->undefined_value();
}
// Common steps for ArrayPrototypeGroupBy and ArrayPrototypeGroupByToMap
// Common steps for ArrayPrototypeGroup and ArrayPrototypeGroupToMap
// 6c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
Handle<Object> propertyKey;
Handle<Object> argv[] = {
......@@ -1673,7 +1673,7 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
isolate, propertyKey,
Execution::Call(isolate, callbackfn, array, 3, argv), OrderedHashMap);
if (mode == GroupByMode::kToMap) {
if (mode == ArrayGroupMode::kToMap) {
// 6d. If key is -0𝔽, set key to +0𝔽.
if (propertyKey->IsMinusZero()) {
propertyKey = Handle<Smi>(Smi::FromInt(0), isolate);
......@@ -1712,8 +1712,8 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroupBy(
} // namespace
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupby
BUILTIN(ArrayPrototypeGroupBy) {
const char* const kMethodName = "Array.prototype.groupBy";
BUILTIN(ArrayPrototypeGroup) {
const char* const kMethodName = "Array.prototype.group";
HandleScope scope(isolate);
Handle<JSReceiver> O;
......@@ -1740,14 +1740,14 @@ BUILTIN(ArrayPrototypeGroupBy) {
Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
FastArrayGroupBy<GroupByMode::kToObject>(
FastArrayGroup<ArrayGroupMode::kToObject>(
isolate, array, callbackfn, groups, len, &result_elements_kind));
} else {
// 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
GenericArrayGroupBy<GroupByMode::kToObject>(isolate, O, callbackfn,
groups, 0, len));
GenericArrayGroup<ArrayGroupMode::kToObject>(isolate, O, callbackfn,
groups, 0, len));
}
// 7. Let obj be ! OrdinaryObjectCreate(null).
......@@ -1774,8 +1774,8 @@ BUILTIN(ArrayPrototypeGroupBy) {
}
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
BUILTIN(ArrayPrototypeGroupByToMap) {
const char* const kMethodName = "Array.prototype.groupByToMap";
BUILTIN(ArrayPrototypeGroupToMap) {
const char* const kMethodName = "Array.prototype.groupToMap";
HandleScope scope(isolate);
Handle<JSReceiver> O;
......@@ -1802,14 +1802,14 @@ BUILTIN(ArrayPrototypeGroupByToMap) {
Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
FastArrayGroupBy<GroupByMode::kToMap>(
FastArrayGroup<ArrayGroupMode::kToMap>(
isolate, array, callbackfn, groups, len, &result_elements_kind));
} else {
// 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
GenericArrayGroupBy<GroupByMode::kToMap>(isolate, O, callbackfn, groups,
0, len));
GenericArrayGroup<ArrayGroupMode::kToMap>(isolate, O, callbackfn,
groups, 0, len));
}
// 7. Let map be ! Construct(%Map%).
......
......@@ -381,9 +381,9 @@ namespace internal {
/* ES6 #sec-array.prototype.pop */ \
CPP(ArrayPop) \
TFJ(ArrayPrototypePop, kDontAdaptArgumentsSentinel) \
/* ES6 #sec-array.prototype.groupby */ \
CPP(ArrayPrototypeGroupBy) \
CPP(ArrayPrototypeGroupByToMap) \
/* ES6 #sec-array.prototype.group */ \
CPP(ArrayPrototypeGroup) \
CPP(ArrayPrototypeGroupToMap) \
/* ES6 #sec-array.prototype.push */ \
CPP(ArrayPush) \
TFJ(ArrayPrototypePush, kDontAdaptArgumentsSentinel) \
......
......@@ -4693,18 +4693,18 @@ void Genesis::InitializeGlobal_harmony_array_grouping() {
Handle<JSObject> array_prototype(
JSObject::cast(array_function->instance_prototype()), isolate());
SimpleInstallFunction(isolate_, array_prototype, "groupBy",
Builtin::kArrayPrototypeGroupBy, 1, false);
SimpleInstallFunction(isolate_, array_prototype, "groupByToMap",
Builtin::kArrayPrototypeGroupByToMap, 1, false);
SimpleInstallFunction(isolate_, array_prototype, "group",
Builtin::kArrayPrototypeGroup, 1, false);
SimpleInstallFunction(isolate_, array_prototype, "groupToMap",
Builtin::kArrayPrototypeGroupToMap, 1, false);
Handle<JSObject> unscopables = Handle<JSObject>::cast(
JSObject::GetProperty(isolate(), array_prototype,
isolate()->factory()->unscopables_symbol())
.ToHandleChecked());
InstallTrueValuedProperty(isolate_, unscopables, "groupBy");
InstallTrueValuedProperty(isolate_, unscopables, "groupByToMap");
InstallTrueValuedProperty(isolate_, unscopables, "group");
InstallTrueValuedProperty(isolate_, unscopables, "groupToMap");
}
void Genesis::InitializeGlobal_harmony_object_has_own() {
......
......@@ -8,7 +8,7 @@
let arr1 = [];
for (let i = 0; i < 32; i++) arr1.push(i);
let popped = false;
let grouped1 = arr1.groupBy(() => {
let grouped1 = arr1.group(() => {
// Pop all of the elements to trigger right-trimming of the elements
// FixedArray.
for (let i = 0, len = arr1.length; i < len; i++) {
......@@ -27,7 +27,7 @@ assertArrayEquals(expectedGrouped1, grouped1['undefined']);
//
// Initial Smi array, but due to length truncation result is not a Smi array.
let arr2 = [0,1,2,3,4,5,6,7,8,9];
let grouped2 = arr2.groupBy(() => { arr2.length = 2; });
let grouped2 = arr2.group(() => { arr2.length = 2; });
// 'undefined' is the only group.
assertArrayEquals(['undefined'], Object.getOwnPropertyNames(grouped2));
// 0,1 are the only values in the group because the source array gets truncated
......
......@@ -4,17 +4,17 @@
// Flags: --harmony-array-grouping
assertEquals(Array.prototype[Symbol.unscopables].groupBy, true);
assertEquals(Array.prototype[Symbol.unscopables].group, true);
var array = [-0, 1, 0, 2];
var groupBy = () => {
let result = array.groupBy(v => v > 0);
var group = () => {
let result = array.group(v => v > 0);
result = Array.from(Object.entries(result));
return result;
}
// entry order matters
assertEquals(groupBy(), [
assertEquals(group(), [
['false', [-0, 0]],
['true', [1, 2]],
]);
......@@ -25,7 +25,7 @@ Object.defineProperty(array, 4, {
writable: true,
value: 3,
});
assertEquals(groupBy(), [
assertEquals(group(), [
['false', [-0, 0]],
['true', [1, 2, 3]],
]);
......@@ -35,7 +35,7 @@ Object.defineProperty(array, 5, {
configurable: true,
get: () => 4,
});
var result = groupBy();
var result = group();
assertEquals(result, [
['false', [-0, 0]],
['true', [1, 2, 3, 4]],
......@@ -49,12 +49,12 @@ var array = new Array(length);
for (var idx = 0; idx < length; idx++) {
array[idx] = idx;
}
var groupBy = () => {
let result = array.groupBy(v => v % 2);
var group = () => {
let result = array.group(v => v % 2);
result = Array.from(Object.entries(result));
return result;
}
var result = groupBy();
var result = group();
assertEquals(result, [
['0', array.filter(v => v % 2 === 0)],
['1', array.filter(v => v % 2 === 1)],
......@@ -62,8 +62,8 @@ assertEquals(result, [
// check array changed by callbackfn
var array = [-0, 0, 1, 2];
groupBy = () => {
let result = array.groupBy((v, idx) => {
group = () => {
let result = array.group((v, idx) => {
if (idx === 1) {
array[2] = {a: 'b'};
}
......@@ -73,7 +73,7 @@ groupBy = () => {
return result;
}
assertEquals(groupBy(), [
assertEquals(group(), [
['false', [-0, 0, {a: 'b'}]],
['true', [2]],
]);
......@@ -81,8 +81,8 @@ assertEquals(groupBy(), [
// check array with holes
var array = [1, , 2, , 3, , 4];
var groupBy = () => {
let result = array.groupBy(v => v % 2 === 0 ? 'even' : 'not_even');
var group = () => {
let result = array.group(v => v % 2 === 0 ? 'even' : 'not_even');
result = Array.from(Object.entries(result));
return result;
};
......@@ -91,7 +91,7 @@ function checkNoHoles(arr) {
assertTrue(Object.getOwnPropertyDescriptor(arr, idx) !== undefined);
}
}
var result = groupBy();
var result = group();
assertEquals(result, [
['not_even', [1, undefined, undefined, 3, undefined]],
['even', [2, 4]],
......@@ -100,7 +100,7 @@ checkNoHoles(result[0][1]);
checkNoHoles(result[1][1]);
var array = [1, undefined, 2, undefined, 3, undefined, 4];
result = groupBy();
result = group();
assertEquals(result, [
['not_even', [1, undefined, undefined, 3, undefined]],
['even', [2, 4]],
......@@ -120,13 +120,13 @@ var arrayLikeObjects = [
Int8Array.from([-1, 1, 2]),
Float32Array.from([-1, 1, 2]),
];
var groupBy = () => {
let result = Array.prototype.groupBy.call(array, v => v > 0);
var group = () => {
let result = Array.prototype.group.call(array, v => v > 0);
result = Array.from(Object.entries(result));
return result;
};
for (var array of arrayLikeObjects) {
assertEquals(groupBy(), [
assertEquals(group(), [
['false', [-1]],
['true', [1, 2]],
]);
......@@ -135,18 +135,18 @@ for (var array of arrayLikeObjects) {
// check proto elements
var array = [,];
var groupBy = () => {
let result = array.groupBy(v => v);
var group = () => {
let result = array.group(v => v);
result = Array.from(Object.entries(result));
return result;
}
assertEquals(groupBy(), [
assertEquals(group(), [
['undefined', [,]],
]);
array.__proto__.push(6);
assertEquals(groupBy(), [
assertEquals(group(), [
['6', [6]],
]);
......@@ -154,7 +154,7 @@ assertEquals(groupBy(), [
// callbackfn throws
var array = [-0, 1, 0, 2];
assertThrows(
() => array.groupBy(() => { throw new Error('foobar'); }),
() => array.group(() => { throw new Error('foobar'); }),
Error,
'foobar'
);
......@@ -163,7 +163,7 @@ assertThrows(
// ToPropertyKey throws
var array = [-0, 1, 0, 2];
assertThrows(
() => array.groupBy(() => {
() => array.group(() => {
return {
toString() {
throw new Error('foobar');
......@@ -178,6 +178,6 @@ assertThrows(
// callbackfn is not callable
var array = [-0, 1, 0, 2];
assertThrows(
() => array.groupBy('foobar'),
() => array.group('foobar'),
TypeError,
);
......@@ -4,17 +4,17 @@
// Flags: --harmony-array-grouping
assertEquals(Array.prototype[Symbol.unscopables].groupByToMap, true);
assertEquals(Array.prototype[Symbol.unscopables].groupToMap, true);
var array = [-0, 1, 0, 2];
var groupByToMap = () => {
let result = array.groupByToMap(v => v > 0);
var groupToMap = () => {
let result = array.groupToMap(v => v > 0);
result = Array.from(result.entries());
return result;
}
// entry order matters
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[false, [-0, 0]],
[true, [1, 2]],
]);
......@@ -25,7 +25,7 @@ Object.defineProperty(array, 4, {
writable: true,
value: 3,
});
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[false, [-0, 0]],
[true, [1, 2, 3]],
]);
......@@ -35,7 +35,7 @@ Object.defineProperty(array, 5, {
configurable: true,
get: () => 4,
});
var result = groupByToMap();
var result = groupToMap();
assertEquals(result, [
[false, [-0, 0]],
[true, [1, 2, 3, 4]],
......@@ -49,28 +49,28 @@ var array = new Array(length);
for (var idx = 0; idx < length; idx++) {
array[idx] = idx;
}
var groupByToMap = () => {
let result = array.groupByToMap(v => v % 2);
var groupToMap = () => {
let result = array.groupToMap(v => v % 2);
result = Array.from(result.entries());
return result;
}
var result = groupByToMap();
var result = groupToMap();
assertEquals(result, [
[0, array.filter(v => v % 2 === 0)],
[1, array.filter(v => v % 2 === 1)],
]);
// check section groupByToMap 6.d
// check #sec-array.prototype.groupbymap 6.d
var array = [-0, 0];
var result = array.groupByToMap(v => v);
var result = array.groupToMap(v => v);
assertEquals(result.get(0), [-0, 0]);
// check array changed by callbackfn
var array = [-0, 0, 1, 2];
var groupByToMap = () => {
let result = array.groupByToMap((v, idx) => {
var groupToMap = () => {
let result = array.groupToMap((v, idx) => {
if (idx === 1) {
array[2] = {a: 'b'};
}
......@@ -80,15 +80,15 @@ var groupByToMap = () => {
return result;
}
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[false, [-0, 0, {a: 'b'}]],
[true, [2]],
]);
// check array with holes
var array = [1, , 2, , 3, , 4];
var groupByToMap = () => {
let result = array.groupByToMap(v => v % 2 === 0 ? 'even' : 'not_even');
var groupToMap = () => {
let result = array.groupToMap(v => v % 2 === 0 ? 'even' : 'not_even');
result = Array.from(result.entries());
return result;
};
......@@ -97,7 +97,7 @@ function checkNoHoles(arr) {
assertTrue(Object.getOwnPropertyDescriptor(arr, idx) !== undefined);
}
}
var result = groupByToMap();
var result = groupToMap();
assertEquals(result, [
['not_even', [1, undefined, undefined, 3, undefined]],
['even', [2, 4]],
......@@ -106,7 +106,7 @@ checkNoHoles(result[0][1]);
checkNoHoles(result[1][1]);
var array = [1, undefined, 2, undefined, 3, undefined, 4];
result = groupByToMap();
result = groupToMap();
assertEquals(result, [
['not_even', [1, undefined, undefined, 3, undefined]],
['even', [2, 4]],
......@@ -126,13 +126,13 @@ var arrayLikeObjects = [
Int8Array.from([-1, 1, 2]),
Float32Array.from([-1, 1, 2]),
];
var groupByToMap = () => {
let result = Array.prototype.groupByToMap.call(array, v => v > 0);
var groupToMap = () => {
let result = Array.prototype.groupToMap.call(array, v => v > 0);
result = Array.from(result.entries());
return result;
};
for (var array of arrayLikeObjects) {
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[false, [-1]],
[true, [1, 2]],
]);
......@@ -140,17 +140,17 @@ for (var array of arrayLikeObjects) {
// check proto elements
var array = [,];
var groupByToMap = () => {
let result = array.groupByToMap(v => v);
var groupToMap = () => {
let result = array.groupToMap(v => v);
result = Array.from(result.entries());
return result;
}
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[undefined, [,]],
]);
array.__proto__.push(6);
assertEquals(groupByToMap(), [
assertEquals(groupToMap(), [
[6, [6]],
]);
......@@ -158,7 +158,7 @@ assertEquals(groupByToMap(), [
// callbackfn throws
var array = [-0, 1, 0, 2];
assertThrows(
() => array.groupByToMap(() => { throw new Error('foobar'); }),
() => array.groupToMap(() => { throw new Error('foobar'); }),
Error,
'foobar'
);
......@@ -167,6 +167,6 @@ assertThrows(
// callbackfn is not callable
var array = [-0, 1, 0, 2];
assertThrows(
() => array.groupByToMap('foobar'),
() => array.groupToMap('foobar'),
TypeError,
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment