Ignore:
Timestamp:
Dec 16, 2021, 8:10:09 AM (3 years ago)
Author:
Devin Rousso
Message:

Implement Array.prototype.groupBy and Array.prototype.groupByToMap
https://bugs.webkit.org/show_bug.cgi?id=234327

Reviewed by Yusuke Suzuki.

JSTests:

  • stress/array-groupBy.js: Added.

(shouldBe):
(shouldBeObject):
(shouldBeObject.replacer):
(notReached):
(toObject):
(reverseInsertionOrder):

  • stress/array-groupByToMap.js: Added.

(shouldBe):
(shouldBeObject):
(shouldBeObject.replacer):
(shouldBeMap):
(notReached):
(toObject):
(reverseInsertionOrder):

Source/JavaScriptCore:

Implement new Array Grouping proposal <https://tc39.es/proposal-array-grouping/>, which just
reached Stage 3.

Array.prototype.groupBy/Array.prototype.groupByToMap will return a {}/Map where each
value in the array is put into a "bucket" keyed by the return value of the provoded callback.

`js
const array = [1, 2, 3, 4];

array.groupBy(n => n % 2 ? "odd" : "even") { odd: [1, 3], even: [2, 4] }
array.groupByToMap(n => n % 2 ? "odd" : "even")
new Map("odd", [1, 3, ["even", [2, 4]])
`

  • builtins/ArrayPrototype.js:

(groupBy): Added.
(groupByToMap): Added.

  • runtime/ArrayPrototype.cpp:

(JSC::ArrayPrototype::finishCreation):

  • bytecode/BytecodeIntrinsicRegistry.h:
  • bytecompiler/NodesCodegen.cpp:

(JSC::BytecodeIntrinsicNode::emit_intrinsic_toPropertyKey): Added.
Allow @toPropertyKey to be used in builtins to convert a value to a property key. This is
used to avoid converting the return value of the callback given to groupBy more than once.

  • builtins/BuiltinNames.h:
  • bytecode/LinkTimeConstant.h:
  • runtime/JSGlobalObject.cpp:

(JSC::JSGlobalObject::init):
Allow @Map to be used in builtins to create a primordial Map instance. This is used to
avoid side effects when creating and populating the Map returned by groupByToMap.

  • runtime/OptionsList.h:

Add useArrayGroupByMethod option.

Source/WebInspectorUI:

  • UserInterface/Models/NativeFunctionParameters.js:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/bytecompiler/NodesCodegen.cpp

    r286251 r287136  
    18401840
    18411841    return generator.move(dst, generator.emitToString(generator.tempDestination(dst), src.get()));
     1842}
     1843
     1844RegisterID* BytecodeIntrinsicNode::emit_intrinsic_toPropertyKey(BytecodeGenerator& generator, RegisterID* dst)
     1845{
     1846    ArgumentListNode* node = m_args->m_listNode;
     1847    RefPtr<RegisterID> src = generator.emitNode(node);
     1848    ASSERT(!node->m_next);
     1849
     1850    return generator.move(dst, generator.emitToPropertyKey(generator.tempDestination(dst), src.get()));
    18421851}
    18431852
Note: See TracChangeset for help on using the changeset viewer.