Title: [206710] trunk
Revision
206710
Author
[email protected]
Date
2016-10-01 16:35:17 -0700 (Sat, 01 Oct 2016)

Log Message

[ES6] GeneratorFunction constructor should instantiate generator function
https://bugs.webkit.org/show_bug.cgi?id=162838

Reviewed by Saam Barati.

JSTests:

* stress/generator-function-constructor-is-subclassible.js: Added.
(shouldBe):
(GeneratorFunction):
(prototype.hello):
* stress/generator-function-constructor.js:
* test262.yaml:

Source/_javascript_Core:

GeneratorFunction's constructor should return an instance of JSGeneratorFunction
instead of JSFunction. In this patch, we fix the following 2 things.

1. GeneratorFunction constructor should use JSGeneratorFunction

    Previously, we used JSFunction to construct a result. It's wrong. We use JSGeneratorFunction.

2. Pass newTarget into GeneratorFunction constructor to make it subclassible

    We did not leverage newTarget when using GeneratorFunction constructor.
    Using it correctly to create the subclass Structure and making GeneratorFunction subclassible.

Test262 test covers (1), but (2) is not covered. We add tests that covers both to stress tests.

* runtime/FunctionConstructor.cpp:
(JSC::constructFunctionSkippingEvalEnabledCheck):
* runtime/GeneratorFunctionConstructor.cpp:
(JSC::constructGeneratorFunctionConstructor):
* runtime/JSGeneratorFunction.cpp:
(JSC::JSGeneratorFunction::JSGeneratorFunction):
(JSC::JSGeneratorFunction::createImpl):
(JSC::JSGeneratorFunction::create):
(JSC::JSGeneratorFunction::createWithInvalidatedReallocationWatchpoint):
* runtime/JSGeneratorFunction.h:

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (206709 => 206710)


--- trunk/JSTests/ChangeLog	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/JSTests/ChangeLog	2016-10-01 23:35:17 UTC (rev 206710)
@@ -1,3 +1,17 @@
+2016-10-01  Yusuke Suzuki  <[email protected]>
+
+        [ES6] GeneratorFunction constructor should instantiate generator function
+        https://bugs.webkit.org/show_bug.cgi?id=162838
+
+        Reviewed by Saam Barati.
+
+        * stress/generator-function-constructor-is-subclassible.js: Added.
+        (shouldBe):
+        (GeneratorFunction):
+        (prototype.hello):
+        * stress/generator-function-constructor.js:
+        * test262.yaml:
+
 2016-09-30  Saam Barati  <[email protected]>
 
         Arrow functions should not allow duplicate parameter names

Added: trunk/JSTests/stress/generator-function-constructor-is-subclassible.js (0 => 206710)


--- trunk/JSTests/stress/generator-function-constructor-is-subclassible.js	                        (rev 0)
+++ trunk/JSTests/stress/generator-function-constructor-is-subclassible.js	2016-10-01 23:35:17 UTC (rev 206710)
@@ -0,0 +1,24 @@
+function shouldBe(actual, expected) {
+    if (actual !== expected)
+        throw new Error('bad value: ' + actual);
+}
+
+var GeneratorFunction = (function *() { }).constructor;
+
+class DerivedGeneratorFunction extends GeneratorFunction {
+    constructor()
+    {
+        super("yield 42");
+    }
+
+    hello()
+    {
+        return 50;
+    }
+}
+
+let DerivedGenerator = new DerivedGeneratorFunction();
+shouldBe(DerivedGenerator.__proto__, DerivedGeneratorFunction.prototype);
+shouldBe(DerivedGenerator.hello(), 50);
+let gen = DerivedGenerator();
+shouldBe(gen.next().value, 42);

Modified: trunk/JSTests/stress/generator-function-constructor.js (206709 => 206710)


--- trunk/JSTests/stress/generator-function-constructor.js	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/JSTests/stress/generator-function-constructor.js	2016-10-01 23:35:17 UTC (rev 206710)
@@ -7,3 +7,6 @@
 var generatorFunctionConstructor = (function *() { }).constructor;
 shouldBe(generatorFunctionConstructor.__proto__, Function);
 shouldBe(generatorFunctionConstructor.prototype.constructor, generatorFunctionConstructor);
+shouldBe(generatorFunctionConstructor() instanceof generatorFunctionConstructor, true);
+shouldBe(generatorFunctionConstructor("a") instanceof generatorFunctionConstructor, true);
+shouldBe(generatorFunctionConstructor("a", "b") instanceof generatorFunctionConstructor, true);

Modified: trunk/JSTests/test262.yaml (206709 => 206710)


--- trunk/JSTests/test262.yaml	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/JSTests/test262.yaml	2016-10-01 23:35:17 UTC (rev 206710)
@@ -18664,9 +18664,9 @@
 - path: test262/test/built-ins/GeneratorFunction/extensibility.js
   cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], [:strict]
 - path: test262/test/built-ins/GeneratorFunction/has-instance.js
-  cmd: runTest262 :fail, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], []
+  cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], []
 - path: test262/test/built-ins/GeneratorFunction/has-instance.js
-  cmd: runTest262 :fail, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], [:strict]
+  cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], [:strict]
 - path: test262/test/built-ins/GeneratorFunction/instance-length.js
   cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], []
 - path: test262/test/built-ins/GeneratorFunction/instance-length.js
@@ -18676,9 +18676,9 @@
 - path: test262/test/built-ins/GeneratorFunction/instance-name.js
   cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], [:strict]
 - path: test262/test/built-ins/GeneratorFunction/instance-prototype.js
-  cmd: runTest262 :fail, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], []
+  cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], []
 - path: test262/test/built-ins/GeneratorFunction/instance-prototype.js
-  cmd: runTest262 :fail, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], [:strict]
+  cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js", "../../../harness/propertyHelper.js"], [:strict]
 - path: test262/test/built-ins/GeneratorFunction/instance-restricted-properties.js
   cmd: runTest262 :normal, "NoException", ["../../../harness/assert.js", "../../../harness/sta.js"], []
 - path: test262/test/built-ins/GeneratorFunction/instance-restricted-properties.js
@@ -76530,9 +76530,9 @@
 - path: test262/test/language/statements/class/subclass/builtin-objects/GeneratorFunction/regular-subclassing.js
   cmd: runTest262 :normal, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], [:strict]
 - path: test262/test/language/statements/class/subclass/builtin-objects/GeneratorFunction/super-must-be-called.js
-  cmd: runTest262 :fail, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], []
+  cmd: runTest262 :normal, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], []
 - path: test262/test/language/statements/class/subclass/builtin-objects/GeneratorFunction/super-must-be-called.js
-  cmd: runTest262 :fail, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], [:strict]
+  cmd: runTest262 :normal, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], [:strict]
 - path: test262/test/language/statements/class/subclass/builtin-objects/Map/regular-subclassing.js
   cmd: runTest262 :normal, "NoException", ["../../../../../../../harness/assert.js", "../../../../../../../harness/sta.js"], []
 - path: test262/test/language/statements/class/subclass/builtin-objects/Map/regular-subclassing.js

Modified: trunk/Source/_javascript_Core/ChangeLog (206709 => 206710)


--- trunk/Source/_javascript_Core/ChangeLog	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-10-01 23:35:17 UTC (rev 206710)
@@ -1,3 +1,35 @@
+2016-10-01  Yusuke Suzuki  <[email protected]>
+
+        [ES6] GeneratorFunction constructor should instantiate generator function
+        https://bugs.webkit.org/show_bug.cgi?id=162838
+
+        Reviewed by Saam Barati.
+
+        GeneratorFunction's constructor should return an instance of JSGeneratorFunction
+        instead of JSFunction. In this patch, we fix the following 2 things.
+
+        1. GeneratorFunction constructor should use JSGeneratorFunction
+
+            Previously, we used JSFunction to construct a result. It's wrong. We use JSGeneratorFunction.
+
+        2. Pass newTarget into GeneratorFunction constructor to make it subclassible
+
+            We did not leverage newTarget when using GeneratorFunction constructor.
+            Using it correctly to create the subclass Structure and making GeneratorFunction subclassible.
+
+        Test262 test covers (1), but (2) is not covered. We add tests that covers both to stress tests.
+
+        * runtime/FunctionConstructor.cpp:
+        (JSC::constructFunctionSkippingEvalEnabledCheck):
+        * runtime/GeneratorFunctionConstructor.cpp:
+        (JSC::constructGeneratorFunctionConstructor):
+        * runtime/JSGeneratorFunction.cpp:
+        (JSC::JSGeneratorFunction::JSGeneratorFunction):
+        (JSC::JSGeneratorFunction::createImpl):
+        (JSC::JSGeneratorFunction::create):
+        (JSC::JSGeneratorFunction::createWithInvalidatedReallocationWatchpoint):
+        * runtime/JSGeneratorFunction.h:
+
 2016-10-01  Filip Pizlo  <[email protected]>
 
         Get rid of isMarkedOrNewlyAllocated

Modified: trunk/Source/_javascript_Core/runtime/FunctionConstructor.cpp (206709 => 206710)


--- trunk/Source/_javascript_Core/runtime/FunctionConstructor.cpp	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/Source/_javascript_Core/runtime/FunctionConstructor.cpp	2016-10-01 23:35:17 UTC (rev 206710)
@@ -24,6 +24,7 @@
 #include "ExceptionHelpers.h"
 #include "FunctionPrototype.h"
 #include "JSFunction.h"
+#include "JSGeneratorFunction.h"
 #include "JSGlobalObject.h"
 #include "JSCInlines.h"
 #include <wtf/text/StringBuilder.h>
@@ -125,9 +126,11 @@
         return throwException(exec, scope, exception);
     }
 
-    Structure* subclassStructure = InternalFunction::createSubclassStructure(exec, newTarget, globalObject->functionStructure());
+    Structure* subclassStructure = InternalFunction::createSubclassStructure(exec, newTarget, functionConstructionMode == FunctionConstructionMode::Generator ? globalObject->generatorFunctionStructure() : globalObject->functionStructure());
     RETURN_IF_EXCEPTION(scope, nullptr);
 
+    if (functionConstructionMode == FunctionConstructionMode::Generator)
+        return JSGeneratorFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
     return JSFunction::create(vm, function, globalObject->globalScope(), subclassStructure);
 }
 

Modified: trunk/Source/_javascript_Core/runtime/GeneratorFunctionConstructor.cpp (206709 => 206710)


--- trunk/Source/_javascript_Core/runtime/GeneratorFunctionConstructor.cpp	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/Source/_javascript_Core/runtime/GeneratorFunctionConstructor.cpp	2016-10-01 23:35:17 UTC (rev 206710)
@@ -59,7 +59,7 @@
 static EncodedJSValue JSC_HOST_CALL constructGeneratorFunctionConstructor(ExecState* exec)
 {
     ArgList args(exec);
-    return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args, FunctionConstructionMode::Generator));
+    return JSValue::encode(constructFunction(exec, asInternalFunction(exec->callee())->globalObject(), args, FunctionConstructionMode::Generator, exec->newTarget()));
 }
 
 CallType GeneratorFunctionConstructor::getCallData(JSCell*, CallData& callData)

Modified: trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.cpp (206709 => 206710)


--- trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.cpp	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.cpp	2016-10-01 23:35:17 UTC (rev 206710)
@@ -39,14 +39,14 @@
 
 const ClassInfo JSGeneratorFunction::s_info = { "GeneratorFunction", &Base::s_info, nullptr, CREATE_METHOD_TABLE(JSGeneratorFunction) };
 
-JSGeneratorFunction::JSGeneratorFunction(VM& vm, FunctionExecutable* executable, JSScope* scope)
-    : Base(vm, executable, scope, scope->globalObject()->generatorFunctionStructure())
+JSGeneratorFunction::JSGeneratorFunction(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
+    : Base(vm, executable, scope, structure)
 {
 }
 
-JSGeneratorFunction* JSGeneratorFunction::createImpl(VM& vm, FunctionExecutable* executable, JSScope* scope)
+JSGeneratorFunction* JSGeneratorFunction::createImpl(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
 {
-    JSGeneratorFunction* generatorFunction = new (NotNull, allocateCell<JSGeneratorFunction>(vm.heap)) JSGeneratorFunction(vm, executable, scope);
+    JSGeneratorFunction* generatorFunction = new (NotNull, allocateCell<JSGeneratorFunction>(vm.heap)) JSGeneratorFunction(vm, executable, scope, structure);
     ASSERT(generatorFunction->structure()->globalObject());
     generatorFunction->finishCreation(vm);
     return generatorFunction;
@@ -54,7 +54,12 @@
 
 JSGeneratorFunction* JSGeneratorFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope)
 {
-    JSGeneratorFunction* generatorFunction = createImpl(vm, executable, scope);
+    return create(vm, executable, scope, scope->globalObject()->generatorFunctionStructure());
+}
+
+JSGeneratorFunction* JSGeneratorFunction::create(VM& vm, FunctionExecutable* executable, JSScope* scope, Structure* structure)
+{
+    JSGeneratorFunction* generatorFunction = createImpl(vm, executable, scope, structure);
     executable->singletonFunction()->notifyWrite(vm, generatorFunction, "Allocating a generator function");
     return generatorFunction;
 }
@@ -61,7 +66,7 @@
 
 JSGeneratorFunction* JSGeneratorFunction::createWithInvalidatedReallocationWatchpoint(VM& vm, FunctionExecutable* executable, JSScope* scope)
 {
-    return createImpl(vm, executable, scope);
+    return createImpl(vm, executable, scope, scope->globalObject()->generatorFunctionStructure());
 }
 
 } // namespace JSC

Modified: trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.h (206709 => 206710)


--- trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.h	2016-10-01 21:14:19 UTC (rev 206709)
+++ trunk/Source/_javascript_Core/runtime/JSGeneratorFunction.h	2016-10-01 23:35:17 UTC (rev 206710)
@@ -69,6 +69,7 @@
     DECLARE_EXPORT_INFO;
 
     static JSGeneratorFunction* create(VM&, FunctionExecutable*, JSScope*);
+    static JSGeneratorFunction* create(VM&, FunctionExecutable*, JSScope*, Structure*);
     static JSGeneratorFunction* createWithInvalidatedReallocationWatchpoint(VM&, FunctionExecutable*, JSScope*);
 
     static size_t allocationSize(size_t inlineCapacity)
@@ -84,9 +85,9 @@
     }
 
 private:
-    JSGeneratorFunction(VM&, FunctionExecutable*, JSScope*);
+    JSGeneratorFunction(VM&, FunctionExecutable*, JSScope*, Structure*);
 
-    static JSGeneratorFunction* createImpl(VM&, FunctionExecutable*, JSScope*);
+    static JSGeneratorFunction* createImpl(VM&, FunctionExecutable*, JSScope*, Structure*);
 
     friend class LLIntOffsetsExtractor;
 };
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to