Title: [197536] trunk
Revision
197536
Author
[email protected]
Date
2016-03-03 17:56:39 -0800 (Thu, 03 Mar 2016)

Log Message

Array prototype JS builtins should support Symbol.species
https://bugs.webkit.org/show_bug.cgi?id=154710

Reviewed by Geoffrey Garen.

Source/_javascript_Core:

Add support for Symbol.species in the Array.prototype JS
builtin functions.

* builtins/ArrayPrototype.js:
(filter):
(map):
* runtime/ArrayConstructor.cpp:
(JSC::ArrayConstructor::finishCreation):
(JSC::arrayConstructorPrivateFuncIsArrayConstructor):
* runtime/ArrayConstructor.h:
(JSC::ArrayConstructor::create):
* runtime/CommonIdentifiers.h:
* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::init):
* tests/stress/array-species-functions.js:
(id):

LayoutTests:

Add test that checks the Array object created by a species
constructor in Array.prototype functions cannot come from a
different global object without subclassing.

* js/array-species-different-globalobjects-expected.txt: Added.
* js/array-species-different-globalobjects.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (197535 => 197536)


--- trunk/LayoutTests/ChangeLog	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/LayoutTests/ChangeLog	2016-03-04 01:56:39 UTC (rev 197536)
@@ -1,3 +1,17 @@
+2016-03-03  Keith Miller  <[email protected]>
+
+        Array prototype JS builtins should support Symbol.species
+        https://bugs.webkit.org/show_bug.cgi?id=154710
+
+        Reviewed by Geoffrey Garen.
+
+        Add test that checks the Array object created by a species
+        constructor in Array.prototype functions cannot come from a
+        different global object without subclassing.
+
+        * js/array-species-different-globalobjects-expected.txt: Added.
+        * js/array-species-different-globalobjects.html: Added.
+
 2016-03-03  Michael Saboff  <[email protected]>
 
         [ES6] Make Unicode RegExp pattern parsing conform to the spec

Modified: trunk/LayoutTests/fast/profiler/built-in-function-calls-anonymous-expected.txt (197535 => 197536)


--- trunk/LayoutTests/fast/profiler/built-in-function-calls-anonymous-expected.txt	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/LayoutTests/fast/profiler/built-in-function-calls-anonymous-expected.txt	2016-03-04 01:56:39 UTC (rev 197536)
@@ -12,6 +12,10 @@
             toInteger (no file) (line 0:0)
                Number (no file) (line 0:0)
                (anonymous function) (no file) (line 0:0)
+         isArray (no file) (line 0:0)
+         (anonymous function) (no file) (line 0:0)
+         speciesGetter (no file) (line 0:0)
+         Array (no file) (line 0:0)
          myFunction built-in-function-calls-anonymous.html (line 14:30)
             arrayOperatorFunction profiler-test-JS-resources.js (line 25:31)
       endTest profiler-test-JS-resources.js (line 1:17)

Modified: trunk/LayoutTests/fast/profiler/built-in-function-calls-user-defined-function-expected.txt (197535 => 197536)


--- trunk/LayoutTests/fast/profiler/built-in-function-calls-user-defined-function-expected.txt	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/LayoutTests/fast/profiler/built-in-function-calls-user-defined-function-expected.txt	2016-03-04 01:56:39 UTC (rev 197536)
@@ -12,6 +12,10 @@
             toInteger (no file) (line 0:0)
                Number (no file) (line 0:0)
                (anonymous function) (no file) (line 0:0)
+         isArray (no file) (line 0:0)
+         (anonymous function) (no file) (line 0:0)
+         speciesGetter (no file) (line 0:0)
+         Array (no file) (line 0:0)
          arrayOperatorFunction profiler-test-JS-resources.js (line 25:31)
       endTest profiler-test-JS-resources.js (line 1:17)
 

Added: trunk/LayoutTests/js/array-species-different-globalobjects-expected.txt (0 => 197536)


--- trunk/LayoutTests/js/array-species-different-globalobjects-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/js/array-species-different-globalobjects-expected.txt	2016-03-04 01:56:39 UTC (rev 197536)
@@ -0,0 +1,10 @@
+PASS result instanceof Array is true
+PASS result instanceof Array is true
+PASS result instanceof Array is true
+PASS result instanceof Array is true
+PASS result instanceof Array is true
+PASS result instanceof Array is true
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/js/array-species-different-globalobjects.html (0 => 197536)


--- trunk/LayoutTests/js/array-species-different-globalobjects.html	                        (rev 0)
+++ trunk/LayoutTests/js/array-species-different-globalobjects.html	2016-03-04 01:56:39 UTC (rev 197536)
@@ -0,0 +1,40 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<iframe srcdoc="<script></script>">
+</iframe>
+<script>
+
+function id(x) { return x; }
+
+testFunctions = [
+    [Array.prototype.concat, []],
+    [Array.prototype.slice, [1,2]],
+    [Array.prototype.splice, []],
+    [Array.prototype.splice, [0,1]],
+    [Array.prototype.map, [id]],
+    [Array.prototype.filter, [id]]
+];
+
+let otherArray = frames[0].Array(10);
+
+var result;
+
+function testFunction(testArgs) {
+    let [func, args] = testArgs;
+
+    result = func.call(otherArray, ...args);
+
+    shouldBeTrue("result instanceof Array");
+}
+
+testFunctions.forEach(testFunction);
+
+
+</script>
+<script src=""
+</body>
+</html>

Modified: trunk/Source/_javascript_Core/ChangeLog (197535 => 197536)


--- trunk/Source/_javascript_Core/ChangeLog	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-03-04 01:56:39 UTC (rev 197536)
@@ -1,3 +1,27 @@
+2016-03-03  Keith Miller  <[email protected]>
+
+        Array prototype JS builtins should support Symbol.species
+        https://bugs.webkit.org/show_bug.cgi?id=154710
+
+        Reviewed by Geoffrey Garen.
+
+        Add support for Symbol.species in the Array.prototype JS
+        builtin functions.
+
+        * builtins/ArrayPrototype.js:
+        (filter):
+        (map):
+        * runtime/ArrayConstructor.cpp:
+        (JSC::ArrayConstructor::finishCreation):
+        (JSC::arrayConstructorPrivateFuncIsArrayConstructor):
+        * runtime/ArrayConstructor.h:
+        (JSC::ArrayConstructor::create):
+        * runtime/CommonIdentifiers.h:
+        * runtime/JSGlobalObject.cpp:
+        (JSC::JSGlobalObject::init):
+        * tests/stress/array-species-functions.js:
+        (id):
+
 2016-03-03  Michael Saboff  <[email protected]>
 
         [ES6] Make Unicode RegExp pattern parsing conform to the spec

Modified: trunk/Source/_javascript_Core/builtins/ArrayPrototype.js (197535 => 197536)


--- trunk/Source/_javascript_Core/builtins/ArrayPrototype.js	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/builtins/ArrayPrototype.js	2016-03-04 01:56:39 UTC (rev 197536)
@@ -169,7 +169,28 @@
         throw new @TypeError("Array.prototype.filter callback must be a function");
     
     var thisArg = arguments.length > 1 ? arguments[1] : @undefined;
-    var result = [];
+
+    // Do 9.4.2.3 ArraySpeciesCreate
+    var result;
+    var constructor;
+    if (@isArray(array)) {
+        constructor = array.constructor;
+        // We have this check so that if some array from a different global object
+        // calls this map they don't get an array with the Array.prototype of the
+        // other global object.
+        if (@isArrayConstructor(constructor) && @Array !== constructor)
+            constructor = @undefined;
+        if (@isObject(constructor)) {
+            constructor = constructor[@symbolSpecies];
+            if (constructor === null)
+                constructor = @undefined;
+        }
+    }
+    if (constructor === @undefined) {
+        result = [];
+    } else
+        result = new constructor(0);
+
     var nextIndex = 0;
     for (var i = 0; i < length; i++) {
         if (!(i in array))
@@ -200,8 +221,28 @@
         throw new @TypeError("Array.prototype.map callback must be a function");
     
     var thisArg = arguments.length > 1 ? arguments[1] : @undefined;
-    var result = [];
-    result.length = length;
+
+    // Do 9.4.2.3 ArraySpeciesCreate
+    var result;
+    var constructor;
+    if (@isArray(array)) {
+        constructor = array.constructor;
+        // We have this check so that if some array from a different global object
+        // calls this map they don't get an array with the Array.prototype of the
+        // other global object.
+        if (@isArrayConstructor(constructor) && @Array !== constructor)
+            constructor = @undefined;
+        if (@isObject(constructor)) {
+            constructor = constructor[@symbolSpecies];
+            if (constructor === null)
+                constructor = @undefined;
+        }
+    }
+    if (constructor === @undefined) {
+        result = @Array(length);
+    } else
+        result = new constructor(length);
+
     var nextIndex = 0;
     for (var i = 0; i < length; i++) {
         if (!(i in array))

Modified: trunk/Source/_javascript_Core/runtime/ArrayConstructor.cpp (197535 => 197536)


--- trunk/Source/_javascript_Core/runtime/ArrayConstructor.cpp	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/runtime/ArrayConstructor.cpp	2016-03-04 01:56:39 UTC (rev 197536)
@@ -51,7 +51,6 @@
 
 /* Source for ArrayConstructor.lut.h
 @begin arrayConstructorTable
-  isArray   arrayConstructorIsArray     DontEnum|Function 1
   of        JSBuiltin                   DontEnum|Function 0
   from      JSBuiltin                   DontEnum|Function 0
 @end
@@ -62,12 +61,13 @@
 {
 }
 
-void ArrayConstructor::finishCreation(VM& vm, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
+void ArrayConstructor::finishCreation(VM& vm, JSGlobalObject* globalObject, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
 {
     Base::finishCreation(vm, arrayPrototype->classInfo()->className);
     putDirectWithoutTransition(vm, vm.propertyNames->prototype, arrayPrototype, DontEnum | DontDelete | ReadOnly);
     putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
     putDirectNonIndexAccessor(vm, vm.propertyNames->speciesSymbol, speciesSymbol, Accessor | ReadOnly | DontEnum);
+    JSC_NATIVE_FUNCTION_WITHOUT_TRANSITION(vm.propertyNames->isArray, arrayConstructorIsArray, DontEnum, 1);
 }
 
 bool ArrayConstructor::getOwnPropertySlot(JSObject* object, ExecState* exec, PropertyName propertyName, PropertySlot &slot)
@@ -130,4 +130,9 @@
     return JSValue::encode(jsBoolean(exec->argument(0).inherits(JSArray::info())));
 }
 
+EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState* exec)
+{
+    return JSValue::encode(jsBoolean(jsDynamicCast<ArrayConstructor*>(exec->uncheckedArgument(0))));
+}
+
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/ArrayConstructor.h (197535 => 197536)


--- trunk/Source/_javascript_Core/runtime/ArrayConstructor.h	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/runtime/ArrayConstructor.h	2016-03-04 01:56:39 UTC (rev 197536)
@@ -35,10 +35,10 @@
     typedef InternalFunction Base;
     static const unsigned StructureFlags = OverridesGetOwnPropertySlot | InternalFunction::StructureFlags;
 
-    static ArrayConstructor* create(VM& vm, Structure* structure, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
+    static ArrayConstructor* create(VM& vm, JSGlobalObject* globalObject, Structure* structure, ArrayPrototype* arrayPrototype, GetterSetter* speciesSymbol)
     {
         ArrayConstructor* constructor = new (NotNull, allocateCell<ArrayConstructor>(vm.heap)) ArrayConstructor(vm, structure);
-        constructor->finishCreation(vm, arrayPrototype, speciesSymbol);
+        constructor->finishCreation(vm, globalObject, arrayPrototype, speciesSymbol);
         return constructor;
     }
 
@@ -50,7 +50,7 @@
     }
 
 protected:
-    void finishCreation(VM&, ArrayPrototype*, GetterSetter* speciesSymbol);
+    void finishCreation(VM&, JSGlobalObject*, ArrayPrototype*, GetterSetter* speciesSymbol);
 
 private:
     ArrayConstructor(VM&, Structure*);
@@ -62,6 +62,8 @@
 
 JSObject* constructArrayWithSizeQuirk(ExecState*, ArrayAllocationProfile*, JSGlobalObject*, JSValue length, JSValue prototype = JSValue());
 
+EncodedJSValue JSC_HOST_CALL arrayConstructorPrivateFuncIsArrayConstructor(ExecState*);
+
 } // namespace JSC
 
 #endif // ArrayConstructor_h

Modified: trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h (197535 => 197536)


--- trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/runtime/CommonIdentifiers.h	2016-03-04 01:56:39 UTC (rev 197536)
@@ -364,6 +364,8 @@
     macro(isBoundFunction) \
     macro(hasInstanceBoundFunction) \
     macro(instanceOf) \
+    macro(isArray) \
+    macro(isArrayConstructor) \
     macro(isSet) \
     macro(isMap) \
     macro(SetIterator) \

Modified: trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp (197535 => 197536)


--- trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/runtime/JSGlobalObject.cpp	2016-03-04 01:56:39 UTC (rev 197536)
@@ -409,7 +409,7 @@
     m_definePropertyFunction.set(vm, this, definePropertyFunction);
 
     JSCell* functionConstructor = FunctionConstructor::create(vm, FunctionConstructor::createStructure(vm, this, m_functionPrototype.get()), m_functionPrototype.get());
-    JSObject* arrayConstructor = ArrayConstructor::create(vm, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get(), speciesGetterSetter);
+    JSObject* arrayConstructor = ArrayConstructor::create(vm, this, ArrayConstructor::createStructure(vm, this, m_functionPrototype.get()), m_arrayPrototype.get(), speciesGetterSetter);
     
     m_regExpConstructor.set(vm, this, RegExpConstructor::create(vm, RegExpConstructor::createStructure(vm, this, m_functionPrototype.get()), m_regExpPrototype.get(), speciesGetterSetter));
     
@@ -530,6 +530,7 @@
     JSFunction* privateFuncHasInstanceBoundFunction = JSFunction::create(vm, this, 0, String(), hasInstanceBoundFunction);
     JSFunction* privateFuncInstanceOf = JSFunction::create(vm, this, 0, String(), objectPrivateFuncInstanceOf);
     JSFunction* privateFuncThisTimeValue = JSFunction::create(vm, this, 0, String(), dateProtoFuncGetTime);
+    JSFunction* privateFuncIsArrayConstructor = JSFunction::create(vm, this, 0, String(), arrayConstructorPrivateFuncIsArrayConstructor);
 
     GlobalPropertyInfo staticGlobals[] = {
         GlobalPropertyInfo(vm.propertyNames->NaN, jsNaN(), DontEnum | DontDelete | ReadOnly),
@@ -563,6 +564,8 @@
         GlobalPropertyInfo(vm.propertyNames->SetIteratorPrivateName, JSFunction::create(vm, this, 1, String(), privateFuncSetIterator), DontEnum | DontDelete | ReadOnly),
         GlobalPropertyInfo(vm.propertyNames->setIteratorNextPrivateName, JSFunction::create(vm, this, 0, String(), privateFuncSetIteratorNext), DontEnum | DontDelete | ReadOnly),
         GlobalPropertyInfo(vm.propertyNames->isMapPrivateName, JSFunction::create(vm, this, 1, String(), privateFuncIsMap), DontEnum | DontDelete | ReadOnly),
+        GlobalPropertyInfo(vm.propertyNames->isArrayPrivateName, arrayConstructor->getDirect(vm, vm.propertyNames->isArray), DontEnum | DontDelete | ReadOnly),
+        GlobalPropertyInfo(vm.propertyNames->isArrayConstructorPrivateName, privateFuncIsArrayConstructor, DontEnum | DontDelete | ReadOnly),
         GlobalPropertyInfo(vm.propertyNames->MapIteratorPrivateName, JSFunction::create(vm, this, 1, String(), privateFuncMapIterator), DontEnum | DontDelete | ReadOnly),
         GlobalPropertyInfo(vm.propertyNames->mapIteratorNextPrivateName, JSFunction::create(vm, this, 0, String(), privateFuncMapIteratorNext), DontEnum | DontDelete | ReadOnly),
 

Modified: trunk/Source/_javascript_Core/tests/stress/array-species-functions.js (197535 => 197536)


--- trunk/Source/_javascript_Core/tests/stress/array-species-functions.js	2016-03-04 01:56:05 UTC (rev 197535)
+++ trunk/Source/_javascript_Core/tests/stress/array-species-functions.js	2016-03-04 01:56:39 UTC (rev 197536)
@@ -2,11 +2,15 @@
 N = class { }
 N[Symbol.species] = function() { throw "this should never be called"; }
 
+function id(x) { return x; }
+
 testFunctions = [
     [Array.prototype.concat, []],
     [Array.prototype.slice, [1,2]],
     [Array.prototype.splice, []],
-    [Array.prototype.splice, [0,1]]
+    [Array.prototype.splice, [0,1]],
+    [Array.prototype.map, [id]],
+    [Array.prototype.filter, [id]]
 ];
 
 objProp = Object.defineProperty;
@@ -28,7 +32,7 @@
 
     // Test non-array ignores constructor.
     objProp(n, "constructor", { value: C });
-    let bar = protoFunction.call(...[n, ...args]);
+    let bar = protoFunction.call(n, ...args);
     if (!(bar instanceof Array) || bar instanceof N || bar instanceof C)
         throw Error();
 
@@ -40,12 +44,12 @@
     foo = new C(10);
 
     objProp(C, Symbol.species, { value: undefined, writable: true});
-    bar = protoFunction.call(...[foo, ...args]);
+    bar = protoFunction.call(foo, ...args);
     if (!(bar instanceof Array) || bar instanceof C)
         throw Error();
 
     C[Symbol.species] = null;
-    bar = protoFunction.call(...[foo, ...args]);
+    bar = protoFunction.call(foo, ...args);
     if (!(bar instanceof Array) || bar instanceof C)
         throw Error();
 
@@ -57,7 +61,7 @@
     }
 
     C[Symbol.species] = species;
-    bar = protoFunction.call(...[foo, ...args]);
+    bar = protoFunction.call(foo, ...args);
 
     if (!(bar instanceof Array) || !(bar instanceof C) || !called)
         throw Error("failed on " + protoFunction);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to