Commit a7093ce6 authored by Shu-yu Guo's avatar Shu-yu Guo Committed by V8 LUCI CQ

Fix optional thisArg for Array grouping

Array.prototype.group and Array.prototype.groupToMap have an optional
thisArg, which is currently not passed.

Bug: v8:13301, v8:12499
Change-Id: Ib927638401d39ed0cbcc0595ab41fde328ef43cc
Fixed: v8:13301
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3905934
Auto-Submit: Shu-yu Guo <syg@chromium.org>
Reviewed-by: 's avatarMarja Hölttä <marja@chromium.org>
Commit-Queue: Marja Hölttä <marja@chromium.org>
Cr-Commit-Position: refs/heads/main@{#83323}
parent 3285ad9e
......@@ -1601,7 +1601,8 @@ enum class ArrayGroupMode { kToObject, kToMap };
template <ArrayGroupMode mode>
inline MaybeHandle<OrderedHashMap> GenericArrayGroup(
Isolate* isolate, Handle<JSReceiver> O, Handle<Object> callbackfn,
Handle<OrderedHashMap> groups, double initialK, double len) {
Handle<Object> thisArg, Handle<OrderedHashMap> groups, double initialK,
double len) {
// 6. Repeat, while k < len
for (double k = initialK; k < len; ++k) {
// 6a. Let Pk be ! ToString(𝔽(k)).
......@@ -1619,9 +1620,9 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroup(
// 6c. Let key be ? Call(callbackfn, thisArg, « kValue, 𝔽(k), O »).
Handle<Object> propertyKey;
Handle<Object> argv[] = {kValue, isolate->factory()->NewNumber(k), O};
ASSIGN_RETURN_ON_EXCEPTION(isolate, propertyKey,
Execution::Call(isolate, callbackfn, O, 3, argv),
OrderedHashMap);
ASSIGN_RETURN_ON_EXCEPTION(
isolate, propertyKey,
Execution::Call(isolate, callbackfn, thisArg, 3, argv), OrderedHashMap);
if (mode == ArrayGroupMode::kToMap) {
// 6d. If key is -0𝔽, set key to +0𝔽.
......@@ -1651,7 +1652,7 @@ inline MaybeHandle<OrderedHashMap> GenericArrayGroup(
template <ArrayGroupMode mode>
inline MaybeHandle<OrderedHashMap> FastArrayGroup(
Isolate* isolate, Handle<JSArray> array, Handle<Object> callbackfn,
Handle<OrderedHashMap> groups, double len,
Handle<Object> thisArg, Handle<OrderedHashMap> groups, double len,
ElementsKind* result_elements_kind) {
DCHECK_NOT_NULL(result_elements_kind);
......@@ -1664,8 +1665,8 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroup(
for (InternalIndex k : InternalIndex::Range(uint_len)) {
if (!CheckArrayMapNotModified(array, original_map) ||
k.as_uint32() >= static_cast<uint32_t>(array->length().Number())) {
return GenericArrayGroup<mode>(isolate, array, callbackfn, groups,
k.as_uint32(), len);
return GenericArrayGroup<mode>(isolate, array, callbackfn, thisArg,
groups, k.as_uint32(), len);
}
// 6a. Let Pk be ! ToString(𝔽(k)).
// 6b. Let kValue be ? Get(O, Pk).
......@@ -1681,7 +1682,7 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroup(
kValue, isolate->factory()->NewNumber(k.as_uint32()), array};
ASSIGN_RETURN_ON_EXCEPTION(
isolate, propertyKey,
Execution::Call(isolate, callbackfn, array, 3, argv), OrderedHashMap);
Execution::Call(isolate, callbackfn, thisArg, 3, argv), OrderedHashMap);
if (mode == ArrayGroupMode::kToMap) {
// 6d. If key is -0𝔽, set key to +0𝔽.
......@@ -1721,7 +1722,7 @@ inline MaybeHandle<OrderedHashMap> FastArrayGroup(
} // namespace
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupby
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.group
BUILTIN(ArrayPrototypeGroup) {
const char* const kMethodName = "Array.prototype.group";
HandleScope scope(isolate);
......@@ -1743,6 +1744,8 @@ BUILTIN(ArrayPrototypeGroup) {
isolate, NewTypeError(MessageTemplate::kCalledNonCallable, callbackfn));
}
Handle<Object> thisArg = args.atOrUndefined(isolate, 2);
// 5. Let groups be a new empty List.
Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap();
ElementsKind result_elements_kind = ElementsKind::PACKED_ELEMENTS;
......@@ -1750,14 +1753,15 @@ BUILTIN(ArrayPrototypeGroup) {
Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
FastArrayGroup<ArrayGroupMode::kToObject>(
isolate, array, callbackfn, groups, len, &result_elements_kind));
FastArrayGroup<ArrayGroupMode::kToObject>(isolate, array, callbackfn,
thisArg, groups, len,
&result_elements_kind));
} else {
// 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
GenericArrayGroup<ArrayGroupMode::kToObject>(isolate, O, callbackfn,
groups, 0, len));
thisArg, groups, 0, len));
}
// 7. Let obj be ! OrdinaryObjectCreate(null).
......@@ -1783,7 +1787,7 @@ BUILTIN(ArrayPrototypeGroup) {
return *obj;
}
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.groupbymap
// https://tc39.es/proposal-array-grouping/#sec-array.prototype.grouptomap
BUILTIN(ArrayPrototypeGroupToMap) {
const char* const kMethodName = "Array.prototype.groupToMap";
HandleScope scope(isolate);
......@@ -1805,21 +1809,23 @@ BUILTIN(ArrayPrototypeGroupToMap) {
isolate, NewTypeError(MessageTemplate::kCalledNonCallable, callbackfn));
}
Handle<Object> thisArg = args.atOrUndefined(isolate, 2);
// 5. Let groups be a new empty List.
Handle<OrderedHashMap> groups = isolate->factory()->NewOrderedHashMap();
ElementsKind result_elements_kind = ElementsKind::PACKED_ELEMENTS;
if (IsFastArray(O)) {
Handle<JSArray> array = Handle<JSArray>::cast(O);
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
FastArrayGroup<ArrayGroupMode::kToMap>(
isolate, array, callbackfn, groups, len, &result_elements_kind));
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, groups,
FastArrayGroup<ArrayGroupMode::kToMap>(
isolate, array, callbackfn, thisArg,
groups, len, &result_elements_kind));
} else {
// 4. Let k be 0.
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, groups,
GenericArrayGroup<ArrayGroupMode::kToMap>(isolate, O, callbackfn,
groups, 0, len));
thisArg, groups, 0, len));
}
// 7. Let map be ! Construct(%Map%).
......
......@@ -746,12 +746,6 @@
'intl402/DurationFormat/prototype/formatToParts/invalid-arguments-throws': [FAIL],
'intl402/DurationFormat/prototype/formatToParts/not-a-constructor': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=13301
'built-ins/Array/prototype/group/this-arg': [FAIL],
'built-ins/Array/prototype/group/this-arg-strict': [FAIL],
'built-ins/Array/prototype/groupToMap/this-arg': [FAIL],
'built-ins/Array/prototype/groupToMap/this-arg-strict': [FAIL],
# https://bugs.chromium.org/p/v8/issues/detail?id=13302
'staging/ArrayBuffer/resizable/subarray-parameter-conversion-grows': [FAIL],
'staging/ArrayBuffer/resizable/subarray-parameter-conversion-shrinks': [FAIL],
......
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