Title: [268715] trunk/Source/_javascript_Core
Revision
268715
Author
ross.kirsl...@sony.com
Date
2020-10-19 23:40:43 -0700 (Mon, 19 Oct 2020)

Log Message

%TypedArray%#sort helper functions should be globalPrivate
https://bugs.webkit.org/show_bug.cgi?id=217928

Reviewed by Yusuke Suzuki and Alexey Shvayka.

Following r267827, this patch ensures that %TypedArray%.prototype.sort's helper functions:
  1. use parameters instead of capturing variables
  2. are converted from local functions to globalPrivate ones

To this end, also expose Math.min as a link-time constant.

* builtins/ArrayPrototype.js:
(globalPrivate.sortMerge):
(globalPrivate.sortMin): Deleted.
* builtins/BuiltinNames.h:
* builtins/TypedArrayPrototype.js:
(globalPrivate.typedArrayElementCompare): Added.
(globalPrivate.typedArrayMerge): Added.
(globalPrivate.typedArrayMergeSort): Added.
(sort):
(sort.min): Deleted.
(sort.merge): Deleted.
(sort.mergeSort): Deleted.
* bytecode/LinkTimeConstant.h:
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::init):
* runtime/MathObject.cpp:
* runtime/MathObject.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (268714 => 268715)


--- trunk/Source/_javascript_Core/ChangeLog	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/ChangeLog	2020-10-20 06:40:43 UTC (rev 268715)
@@ -1,3 +1,34 @@
+2020-10-19  Ross Kirsling  <ross.kirsl...@sony.com>
+
+        %TypedArray%#sort helper functions should be globalPrivate
+        https://bugs.webkit.org/show_bug.cgi?id=217928
+
+        Reviewed by Yusuke Suzuki and Alexey Shvayka.
+
+        Following r267827, this patch ensures that %TypedArray%.prototype.sort's helper functions:
+          1. use parameters instead of capturing variables
+          2. are converted from local functions to globalPrivate ones
+
+        To this end, also expose Math.min as a link-time constant.
+
+        * builtins/ArrayPrototype.js:
+        (globalPrivate.sortMerge):
+        (globalPrivate.sortMin): Deleted.
+        * builtins/BuiltinNames.h:
+        * builtins/TypedArrayPrototype.js:
+        (globalPrivate.typedArrayElementCompare): Added.
+        (globalPrivate.typedArrayMerge): Added.
+        (globalPrivate.typedArrayMergeSort): Added.
+        (sort):
+        (sort.min): Deleted.
+        (sort.merge): Deleted.
+        (sort.mergeSort): Deleted.
+        * bytecode/LinkTimeConstant.h:
+        * runtime/JSGlobalObject.cpp:
+        (JSC::JSGlobalObject::init):
+        * runtime/MathObject.cpp:
+        * runtime/MathObject.h:
+
 2020-10-19  Alexey Shvayka  <shvaikal...@gmail.com>
 
         [WebIDL] %Interface%.prototype.constructor should be defined on [[Set]] receiver

Modified: trunk/Source/_javascript_Core/builtins/ArrayPrototype.js (268714 => 268715)


--- trunk/Source/_javascript_Core/builtins/ArrayPrototype.js	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/builtins/ArrayPrototype.js	2020-10-20 06:40:43 UTC (rev 268715)
@@ -307,14 +307,6 @@
 }
 
 @globalPrivate
-function sortMin(a, b)
-{
-    "use strict";
-
-    return a < b ? a : b;
-}
-
-@globalPrivate
 function sortStringComparator(a, b)
 {
     "use strict";
@@ -379,7 +371,6 @@
         delete receiver[i];
 }
 
-
 @globalPrivate
 function sortMerge(dst, src, srcIndex, srcEnd, width, comparator)
 {
@@ -386,9 +377,9 @@
     "use strict";
 
     var left = srcIndex;
-    var leftEnd = @sortMin(left + width, srcEnd);
+    var leftEnd = @min(left + width, srcEnd);
     var right = leftEnd;
-    var rightEnd = @sortMin(right + width, srcEnd);
+    var rightEnd = @min(right + width, srcEnd);
 
     for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) {
         if (right < rightEnd) {

Modified: trunk/Source/_javascript_Core/builtins/BuiltinNames.h (268714 => 268715)


--- trunk/Source/_javascript_Core/builtins/BuiltinNames.h	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/builtins/BuiltinNames.h	2020-10-20 06:40:43 UTC (rev 268715)
@@ -61,6 +61,7 @@
     macro(Array) \
     macro(ArrayBuffer) \
     macro(RegExp) \
+    macro(min) \
     macro(trunc) \
     macro(create) \
     macro(defineProperty) \

Modified: trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js (268714 => 268715)


--- trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js	2020-10-20 06:40:43 UTC (rev 268715)
@@ -168,67 +168,69 @@
     return false;
 }
 
-function sort(comparator)
+@globalPrivate
+function typedArrayElementCompare(array, a, b, comparator)
 {
-    // 22.2.3.25
     "use strict";
 
-    function min(a, b)
-    {
-        return a < b ? a : b;
-    }
+    var result = @toNumber(comparator(a, b));
 
-    var compare = (a, b) => {
-        var result = @toNumber(comparator(a, b));
+    if (@isNeutered(array))
+        @throwTypeError("Underlying ArrayBuffer has been detached from the view");
 
-        if (@isNeutered(this))
-            @throwTypeError("Underlying ArrayBuffer has been detached from the view");
+    return result;
+}
 
-        return result;
-    };
+@globalPrivate
+function typedArrayMerge(array, dst, src, srcIndex, srcEnd, width, comparator)
+{
+    "use strict";
 
-    function merge(dst, src, srcIndex, srcEnd, width)
-    {
-        var left = srcIndex;
-        var leftEnd = min(left + width, srcEnd);
-        var right = leftEnd;
-        var rightEnd = min(right + width, srcEnd);
+    var left = srcIndex;
+    var leftEnd = @min(left + width, srcEnd);
+    var right = leftEnd;
+    var rightEnd = @min(right + width, srcEnd);
 
-        for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) {
-            if (right < rightEnd) {
-                if (left >= leftEnd || compare(src[right], src[left]) < 0) {
-                    dst[dstIndex] = src[right++];
-                    continue;
-                }
+    for (var dstIndex = left; dstIndex < rightEnd; ++dstIndex) {
+        if (right < rightEnd) {
+            if (left >= leftEnd || @typedArrayElementCompare(array, src[right], src[left], comparator) < 0) {
+                dst[dstIndex] = src[right++];
+                continue;
             }
+        }
 
-            dst[dstIndex] = src[left++];
-        }
+        dst[dstIndex] = src[left++];
     }
+}
 
-    function mergeSort(array, valueCount)
-    {
-        var buffer = [ ];
-        buffer.length = valueCount;
+@globalPrivate
+function typedArrayMergeSort(array, valueCount, comparator)
+{
+    "use strict";
 
-        var dst = buffer;
-        var src = ""
+    var buffer = @newArrayWithSize(valueCount);
+    var dst = buffer;
+    var src = ""
 
-        for (var width = 1; width < valueCount; width *= 2) {
-            for (var srcIndex = 0; srcIndex < valueCount; srcIndex += 2 * width)
-                merge(dst, src, srcIndex, valueCount, width);
+    for (var width = 1; width < valueCount; width *= 2) {
+        for (var srcIndex = 0; srcIndex < valueCount; srcIndex += 2 * width)
+            @typedArrayMerge(array, dst, src, srcIndex, valueCount, width, comparator);
 
-            var tmp = src;
-            src = ""
-            dst = tmp;
-        }
+        var tmp = src;
+        src = ""
+        dst = tmp;
+    }
 
-        if (src != array) {
-            for(var i = 0; i < valueCount; i++)
-                array[i] = src[i];
-        }
+    if (src != array) {
+        for (var i = 0; i < valueCount; ++i)
+            array[i] = src[i];
     }
+}
 
+function sort(comparator)
+{
+    "use strict";
+
     if (comparator !== @undefined && !@isCallable(comparator))
         @throwTypeError("TypedArray.prototype.sort requires the comparator argument to be a function or undefined");
 
@@ -237,7 +239,7 @@
         return;
 
     if (comparator !== @undefined)
-        mergeSort(this, length);
+        @typedArrayMergeSort(this, length, comparator);
     else
         @typedArraySort(this);
 

Modified: trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h (268714 => 268715)


--- trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h	2020-10-20 06:40:43 UTC (rev 268715)
@@ -61,6 +61,7 @@
     v(BuiltinLog, nullptr) \
     v(BuiltinDescribe, nullptr) \
     v(RegExp, nullptr) \
+    v(min, nullptr) \
     v(trunc, nullptr) \
     v(Promise, nullptr) \
     v(InternalPromise, nullptr) \

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp (268714 => 268715)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2020-10-20 06:40:43 UTC (rev 268715)
@@ -1227,6 +1227,9 @@
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::BuiltinDescribe)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 1, String(), globalFuncBuiltinDescribe));
         });
+    m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::min)].initLater([] (const Initializer<JSCell>& init) {
+            init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), mathProtoFuncMin, MinIntrinsic));
+        });
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::trunc)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), mathProtoFuncTrunc, TruncIntrinsic));
         });

Modified: trunk/Source/_javascript_Core/runtime/MathObject.cpp (268714 => 268715)


--- trunk/Source/_javascript_Core/runtime/MathObject.cpp	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/runtime/MathObject.cpp	2020-10-20 06:40:43 UTC (rev 268715)
@@ -52,7 +52,6 @@
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncLog10);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncLog2);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncMax);
-JSC_DECLARE_HOST_FUNCTION(mathProtoFuncMin);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncPow);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncRandom);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncRound);

Modified: trunk/Source/_javascript_Core/runtime/MathObject.h (268714 => 268715)


--- trunk/Source/_javascript_Core/runtime/MathObject.h	2020-10-20 06:20:03 UTC (rev 268714)
+++ trunk/Source/_javascript_Core/runtime/MathObject.h	2020-10-20 06:40:43 UTC (rev 268715)
@@ -56,6 +56,7 @@
 
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncAbs);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncFloor);
+JSC_DECLARE_HOST_FUNCTION(mathProtoFuncMin);
 JSC_DECLARE_HOST_FUNCTION(mathProtoFuncTrunc);
 
 } // namespace JSC
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to