Title: [272187] trunk
Revision
272187
Author
[email protected]
Date
2021-02-01 19:22:18 -0800 (Mon, 01 Feb 2021)

Log Message

[JSC] TypedArray#fill should be implemented in C++
https://bugs.webkit.org/show_bug.cgi?id=221182

Reviewed by Ross Kirsling.

JSTests:

* stress/bigint-typed-array-fill.js: Added.
(shouldBe):
* stress/typed-array-fill.js: Added.
(shouldBe):

Source/_javascript_Core:

Since TypedArray#fill does not invoke callbacks, implementing it in C++ is better.
This removes several utility functions exposed in JS for TypedArray#fill implementation,
and makes TypedArray#fill simple.

* builtins/BuiltinNames.h:
* builtins/TypedArrayPrototype.js:
(globalPrivate.typedArrayClampArgumentToStartOrEnd): Deleted.
(fill): Deleted.
* bytecode/LinkTimeConstant.h:
* runtime/JSArrayBufferView.cpp:
(JSC::validateTypedArray):
* runtime/JSGenericTypedArrayViewPrototypeFunctions.h:
(JSC::genericTypedArrayViewProtoFuncFill):
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::init):
* runtime/JSGlobalObjectFunctions.cpp:
* runtime/JSGlobalObjectFunctions.h:
* runtime/JSTypedArrayViewPrototype.cpp:
(JSC::JSC_DEFINE_HOST_FUNCTION):
(JSC::JSTypedArrayViewPrototype::finishCreation):
* runtime/JSTypedArrayViewPrototype.h:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (272186 => 272187)


--- trunk/JSTests/ChangeLog	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/JSTests/ChangeLog	2021-02-02 03:22:18 UTC (rev 272187)
@@ -1,3 +1,15 @@
+2021-02-01  Yusuke Suzuki  <[email protected]>
+
+        [JSC] TypedArray#fill should be implemented in C++
+        https://bugs.webkit.org/show_bug.cgi?id=221182
+
+        Reviewed by Ross Kirsling.
+
+        * stress/bigint-typed-array-fill.js: Added.
+        (shouldBe):
+        * stress/typed-array-fill.js: Added.
+        (shouldBe):
+
 2021-01-31  Yusuke Suzuki  <[email protected]>
 
         [JSC] Implement BigInt64Array and BigUint64Array

Added: trunk/JSTests/stress/bigint-typed-array-fill.js (0 => 272187)


--- trunk/JSTests/stress/bigint-typed-array-fill.js	                        (rev 0)
+++ trunk/JSTests/stress/bigint-typed-array-fill.js	2021-02-02 03:22:18 UTC (rev 272187)
@@ -0,0 +1,28 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+var array = new BigInt64Array(4);
+
+array.fill(2n);
+for (var index = 0; index < array.length; ++index)
+    shouldBe(array[index], 2n);
+
+array.fill(3n, 2);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 2n);
+for (var index = 2; index < array.length; ++index)
+    shouldBe(array[index], 3n);
+
+array.fill(4n, 2, 3);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 2n);
+shouldBe(array[2], 4n);
+shouldBe(array[3], 3n);
+
+array.fill(5n, 0, -2);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 5n);
+shouldBe(array[2], 4n);
+shouldBe(array[3], 3n);

Added: trunk/JSTests/stress/typed-array-fill.js (0 => 272187)


--- trunk/JSTests/stress/typed-array-fill.js	                        (rev 0)
+++ trunk/JSTests/stress/typed-array-fill.js	2021-02-02 03:22:18 UTC (rev 272187)
@@ -0,0 +1,28 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+var array = new Int32Array(4);
+
+array.fill(2);
+for (var index = 0; index < array.length; ++index)
+    shouldBe(array[index], 2);
+
+array.fill(3, 2);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 2);
+for (var index = 2; index < array.length; ++index)
+    shouldBe(array[index], 3);
+
+array.fill(4, 2, 3);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 2);
+shouldBe(array[2], 4);
+shouldBe(array[3], 3);
+
+array.fill(5, 0, -2);
+for (var index = 0; index < 2; ++index)
+    shouldBe(array[index], 5);
+shouldBe(array[2], 4);
+shouldBe(array[3], 3);

Modified: trunk/Source/_javascript_Core/ChangeLog (272186 => 272187)


--- trunk/Source/_javascript_Core/ChangeLog	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/ChangeLog	2021-02-02 03:22:18 UTC (rev 272187)
@@ -1,3 +1,32 @@
+2021-02-01  Yusuke Suzuki  <[email protected]>
+
+        [JSC] TypedArray#fill should be implemented in C++
+        https://bugs.webkit.org/show_bug.cgi?id=221182
+
+        Reviewed by Ross Kirsling.
+
+        Since TypedArray#fill does not invoke callbacks, implementing it in C++ is better.
+        This removes several utility functions exposed in JS for TypedArray#fill implementation,
+        and makes TypedArray#fill simple.
+
+        * builtins/BuiltinNames.h:
+        * builtins/TypedArrayPrototype.js:
+        (globalPrivate.typedArrayClampArgumentToStartOrEnd): Deleted.
+        (fill): Deleted.
+        * bytecode/LinkTimeConstant.h:
+        * runtime/JSArrayBufferView.cpp:
+        (JSC::validateTypedArray):
+        * runtime/JSGenericTypedArrayViewPrototypeFunctions.h:
+        (JSC::genericTypedArrayViewProtoFuncFill):
+        * runtime/JSGlobalObject.cpp:
+        (JSC::JSGlobalObject::init):
+        * runtime/JSGlobalObjectFunctions.cpp:
+        * runtime/JSGlobalObjectFunctions.h:
+        * runtime/JSTypedArrayViewPrototype.cpp:
+        (JSC::JSC_DEFINE_HOST_FUNCTION):
+        (JSC::JSTypedArrayViewPrototype::finishCreation):
+        * runtime/JSTypedArrayViewPrototype.h:
+
 2021-02-01  Saam Barati  <[email protected]>
 
         Lazily create m_windowCloseWatchpoints so we don't mistakenly think we have a frame when re-associating a document to a given cached frame

Modified: trunk/Source/_javascript_Core/builtins/BuiltinNames.h (272186 => 272187)


--- trunk/Source/_javascript_Core/builtins/BuiltinNames.h	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/builtins/BuiltinNames.h	2021-02-02 03:22:18 UTC (rev 272187)
@@ -177,9 +177,8 @@
     macro(instanceFieldInitializer) \
     macro(hasOwnPropertyFunction) \
     macro(createPrivateSymbol) \
-    macro(toBigInt) \
-    macro(isBigIntTypedArrayView)
 
+
 namespace Symbols {
 #define DECLARE_BUILTIN_STATIC_SYMBOLS(name) extern JS_EXPORT_PRIVATE SymbolImpl::StaticSymbolImpl name##Symbol;
 JSC_COMMON_PRIVATE_IDENTIFIERS_EACH_WELL_KNOWN_SYMBOL(DECLARE_BUILTIN_STATIC_SYMBOLS)

Modified: trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js (272186 => 272187)


--- trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/builtins/TypedArrayPrototype.js	2021-02-02 03:22:18 UTC (rev 272187)
@@ -52,22 +52,6 @@
     return constructor;
 }
 
-@globalPrivate
-function typedArrayClampArgumentToStartOrEnd(value, length, undefinedValue)
-{
-    "use strict";
-
-    if (value === @undefined)
-        return undefinedValue;
-
-    var int = @toInteger(value);
-    if (int < 0) {
-        int += length;
-        return int < 0 ? 0 : int;
-    }
-    return int > length ? length : int;
-}
-
 function every(callback /*, thisArg */)
 {
     "use strict";
@@ -85,28 +69,6 @@
     return true;
 }
 
-function fill(value /* [, start [, end]] */)
-{
-    "use strict";
-
-    var length = @typedArrayLength(this);
-
-    if (@isBigIntTypedArrayView(this))
-        var number = @toBigInt(value);
-    else
-        var number = @toNumber(value);
-
-    var start = @typedArrayClampArgumentToStartOrEnd(@argument(1), length, 0);
-    var end = @typedArrayClampArgumentToStartOrEnd(@argument(2), length, length);
-
-    if (@isDetached(this))
-        @throwTypeError("Underlying ArrayBuffer has been detached from the view");
-
-    for (var i = start; i < end; i++)
-        this[i] = number;
-    return this;
-}
-
 function find(callback /* [, thisArg] */)
 {
     "use strict";

Modified: trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h (272186 => 272187)


--- trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/bytecode/LinkTimeConstant.h	2021-02-02 03:22:18 UTC (rev 272187)
@@ -111,8 +111,6 @@
     v(callFunction, nullptr) \
     v(hasOwnPropertyFunction, nullptr) \
     v(createPrivateSymbol, nullptr) \
-    v(toBigInt, nullptr) \
-    v(isBigIntTypedArrayView, nullptr) \
 
 
 #define DECLARE_LINK_TIME_CONSTANT(name, code) name,

Modified: trunk/Source/_javascript_Core/runtime/JSArrayBufferView.cpp (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSArrayBufferView.cpp	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSArrayBufferView.cpp	2021-02-02 03:22:18 UTC (rev 272187)
@@ -358,7 +358,7 @@
 
     JSArrayBufferView* typedArray = jsCast<JSArrayBufferView*>(typedArrayCell);
     if (typedArray->isDetached()) {
-        throwTypeError(globalObject, scope, "Argument typed array is detached."_s);
+        throwTypeError(globalObject, scope, typedArrayBufferHasBeenDetachedErrorMessage);
         return nullptr;
     }
     return typedArray;

Modified: trunk/Source/_javascript_Core/runtime/JSGenericTypedArrayViewPrototypeFunctions.h (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSGenericTypedArrayViewPrototypeFunctions.h	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSGenericTypedArrayViewPrototypeFunctions.h	2021-02-02 03:22:18 UTC (rev 272187)
@@ -308,6 +308,34 @@
 }
 
 template<typename ViewClass>
+ALWAYS_INLINE EncodedJSValue genericTypedArrayViewProtoFuncFill(VM& vm, JSGlobalObject* globalObject, CallFrame* callFrame)
+{
+    // https://tc39.es/ecma262/#sec-%typedarray%.prototype.fill
+    auto scope = DECLARE_THROW_SCOPE(vm);
+
+    ViewClass* thisObject = jsCast<ViewClass*>(callFrame->thisValue());
+    if (thisObject->isDetached())
+        return throwVMTypeError(globalObject, scope, typedArrayBufferHasBeenDetachedErrorMessage);
+
+    unsigned length = thisObject->length();
+    auto nativeValue = ViewClass::toAdaptorNativeFromValue(globalObject, callFrame->argument(0));
+    RETURN_IF_EXCEPTION(scope, { });
+
+    unsigned start = argumentClampedIndexFromStartOrEnd(globalObject, callFrame->argument(1), length, 0);
+    RETURN_IF_EXCEPTION(scope, { });
+    unsigned end = argumentClampedIndexFromStartOrEnd(globalObject, callFrame->argument(2), length, length);
+    RETURN_IF_EXCEPTION(scope, { });
+
+    if (thisObject->isDetached())
+        return throwVMTypeError(globalObject, scope, typedArrayBufferHasBeenDetachedErrorMessage);
+
+    for (unsigned index = start; index < end; ++index)
+        thisObject->setIndexQuicklyToNativeValue(index, nativeValue);
+
+    return JSValue::encode(thisObject);
+}
+
+template<typename ViewClass>
 ALWAYS_INLINE EncodedJSValue genericTypedArrayViewProtoFuncLastIndexOf(VM& vm, JSGlobalObject* globalObject, CallFrame* callFrame)
 {
     auto scope = DECLARE_THROW_SCOPE(vm);

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2021-02-02 03:22:18 UTC (rev 272187)
@@ -1218,9 +1218,6 @@
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::isTypedArrayView)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), typedArrayViewPrivateFuncIsTypedArrayView, IsTypedArrayViewIntrinsic));
         });
-    m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::isBigIntTypedArrayView)].initLater([] (const Initializer<JSCell>& init) {
-            init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), typedArrayViewPrivateFuncIsBigIntTypedArrayView));
-        });
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::isSharedTypedArrayView)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), typedArrayViewPrivateFuncIsSharedTypedArrayView));
         });
@@ -1242,9 +1239,6 @@
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::instanceOf)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), objectPrivateFuncInstanceOf));
         });
-    m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::toBigInt)].initLater([] (const Initializer<JSCell>& init) {
-            init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 0, String(), globalFuncToBigInt));
-        });
     m_linkTimeConstants[static_cast<unsigned>(LinkTimeConstant::BuiltinLog)].initLater([] (const Initializer<JSCell>& init) {
             init.set(JSFunction::create(init.vm, jsCast<JSGlobalObject*>(init.owner), 1, String(), globalFuncBuiltinLog));
         });

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.cpp	2021-02-02 03:22:18 UTC (rev 272187)
@@ -967,9 +967,4 @@
     RELEASE_AND_RETURN(scope, JSValue::encode(dateTimeFormat->format(globalObject, value)));
 }
 
-JSC_DEFINE_HOST_FUNCTION(globalFuncToBigInt, (JSGlobalObject* globalObject, CallFrame* callFrame))
-{
-    return JSValue::encode(callFrame->argument(0).toBigInt(globalObject));
-}
-
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.h (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.h	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObjectFunctions.h	2021-02-02 03:22:18 UTC (rev 272187)
@@ -59,7 +59,6 @@
 JSC_DECLARE_HOST_FUNCTION(globalFuncPropertyIsEnumerable);
 JSC_DECLARE_HOST_FUNCTION(globalFuncCopyDataProperties);
 JSC_DECLARE_HOST_FUNCTION(globalFuncDateTimeFormat);
-JSC_DECLARE_HOST_FUNCTION(globalFuncToBigInt);
 
 JS_EXPORT_PRIVATE double jsToNumber(StringView);
 

Modified: trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.cpp (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.cpp	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.cpp	2021-02-02 03:22:18 UTC (rev 272187)
@@ -44,6 +44,7 @@
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoFuncLastIndexOf);
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoFuncIndexOf);
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoFuncJoin);
+static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoFuncFill);
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoGetterFuncBuffer);
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoGetterFuncLength);
 static JSC_DECLARE_HOST_FUNCTION(typedArrayViewProtoGetterFuncByteLength);
@@ -90,12 +91,6 @@
     return JSValue::encode(jsBoolean(value.isCell() && isTypedView(value.asCell()->classInfo(globalObject->vm())->typedArrayStorageType)));
 }
 
-JSC_DEFINE_HOST_FUNCTION(typedArrayViewPrivateFuncIsBigIntTypedArrayView, (JSGlobalObject* globalObject, CallFrame* callFrame))
-{
-    JSValue value = callFrame->uncheckedArgument(0);
-    return JSValue::encode(jsBoolean(value.isCell() && isBigIntTypedView(value.asCell()->classInfo(globalObject->vm())->typedArrayStorageType)));
-}
-
 JSC_DEFINE_HOST_FUNCTION(typedArrayViewPrivateFuncIsSharedTypedArrayView, (JSGlobalObject* globalObject, CallFrame* callFrame))
 {
     JSValue value = callFrame->uncheckedArgument(0);
@@ -305,6 +300,17 @@
     CALL_GENERIC_TYPEDARRAY_PROTOTYPE_FUNCTION(genericTypedArrayViewProtoFuncJoin);
 }
 
+JSC_DEFINE_HOST_FUNCTION(typedArrayViewProtoFuncFill, (JSGlobalObject* globalObject, CallFrame* callFrame))
+{
+    VM& vm = globalObject->vm();
+    auto scope = DECLARE_THROW_SCOPE(vm);
+    JSValue thisValue = callFrame->thisValue();
+    if (!thisValue.isObject())
+        return throwVMTypeError(globalObject, scope, "Receiver should be a typed array view but was not an object"_s);
+    scope.release();
+    CALL_GENERIC_TYPEDARRAY_PROTOTYPE_FUNCTION(genericTypedArrayViewProtoFuncFill);
+}
+
 JSC_DEFINE_HOST_FUNCTION(typedArrayViewProtoGetterFuncBuffer, (JSGlobalObject* globalObject, CallFrame* callFrame))
 {
     VM& vm = globalObject->vm();
@@ -444,7 +450,7 @@
     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("sort", typedArrayPrototypeSortCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
     JSC_NATIVE_INTRINSIC_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().entriesPublicName(), typedArrayProtoViewFuncEntries, static_cast<unsigned>(PropertyAttribute::DontEnum), 0, TypedArrayEntriesIntrinsic);
     JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION("includes", typedArrayViewProtoFuncIncludes, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
-    JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("fill", typedArrayPrototypeFillCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->builtinNames().fillPublicName(), typedArrayViewProtoFuncFill, static_cast<unsigned>(PropertyAttribute::DontEnum), 1);
     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("find", typedArrayPrototypeFindCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION("findIndex", typedArrayPrototypeFindIndexCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));
     JSC_BUILTIN_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->forEach, typedArrayPrototypeForEachCodeGenerator, static_cast<unsigned>(PropertyAttribute::DontEnum));

Modified: trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.h (272186 => 272187)


--- trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.h	2021-02-02 03:11:39 UTC (rev 272186)
+++ trunk/Source/_javascript_Core/runtime/JSTypedArrayViewPrototype.h	2021-02-02 03:22:18 UTC (rev 272187)
@@ -52,7 +52,6 @@
 };
 
 JSC_DECLARE_HOST_FUNCTION(typedArrayViewPrivateFuncIsTypedArrayView);
-JSC_DECLARE_HOST_FUNCTION(typedArrayViewPrivateFuncIsBigIntTypedArrayView);
 JSC_DECLARE_HOST_FUNCTION(typedArrayViewPrivateFuncIsSharedTypedArrayView);
 JSC_DECLARE_HOST_FUNCTION(typedArrayViewPrivateFuncIsDetached);
 JSC_DECLARE_HOST_FUNCTION(typedArrayViewPrivateFuncDefaultComparator);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to