Reviewers: Michael Starzinger, rossberg,

Message:
This is another attempt to land https://codereview.chromium.org/14857009/, this time making it so that SyntaxErrors are raised from the constructor itself and not a helper. Except for the paren error, anyway. Hopefully that fixes the WK
layout tests, though I haven't tried.

We might want to wait until after the roll for this one.

Description:
GeneratorFunction() makes generator instances

The current specification has GeneratorFunction() be like Function(),
except that it makes generator instances.  This commit implements that
behavior.  It also fills in a piece of the implementation where
otherwise calling GeneratorFunction or GeneratorFunctionPrototype would
cause an abort because they have no code.

[email protected], [email protected]
TEST=mjsunit/harmony/generators-iteration
TEST=mjsunit/harmony/generators-runtime
BUG=v8:2355
BUG=v8:2680

Please review this at https://codereview.chromium.org/15218004/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/generator.js
  M src/v8natives.js
  M test/mjsunit/harmony/generators-iteration.js
  M test/mjsunit/harmony/generators-runtime.js


Index: src/generator.js
diff --git a/src/generator.js b/src/generator.js
index 5e61091565567bf9d516a45d0efd72add6aa33af..fe70981fc303b4bcba1177c484ff7922adfcb00b 100644
--- a/src/generator.js
+++ b/src/generator.js
@@ -65,6 +65,23 @@ function GeneratorObjectThrow(exn) {
   return %_GeneratorThrow(this, exn);
 }

+function GeneratorFunctionPrototypeConstructor(x) {
+  if (%_IsConstructCall()) {
+    throw MakeTypeError('not_constructor', ['GeneratorFunctionPrototype']);
+  }
+}
+
+function GeneratorFunctionConstructor(arg1) {  // length == 1
+  var source = NewFunctionString(arguments, 'function*');
+  var global_receiver = %GlobalReceiver(global);
+  // Compile the string in the constructor and not a helper so that errors
+  // appear to come from here.
+  var f = %_CallFunction(global_receiver, %CompileString(source, true));
+  %FunctionMarkNameShouldPrintAsAnonymous(f);
+  return f;
+}
+
+
 function SetUpGenerators() {
   %CheckIsBootstrapping();
   var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
@@ -76,9 +93,11 @@ function SetUpGenerators() {
   %SetProperty(GeneratorObjectPrototype, "constructor",
GeneratorFunctionPrototype, DONT_ENUM | DONT_DELETE | READ_ONLY);
   %SetPrototype(GeneratorFunctionPrototype, $Function.prototype);
+ %SetCode(GeneratorFunctionPrototype, GeneratorFunctionPrototypeConstructor);
   %SetProperty(GeneratorFunctionPrototype, "constructor",
                GeneratorFunction, DONT_ENUM | DONT_DELETE | READ_ONLY);
   %SetPrototype(GeneratorFunction, $Function);
+  %SetCode(GeneratorFunction, GeneratorFunctionConstructor);
 }

 SetUpGenerators();
Index: src/v8natives.js
diff --git a/src/v8natives.js b/src/v8natives.js
index b2ea749c73030e82d4e7323ce9637f721dc593e7..eea844a212d94daa304cf18945771a1a8e6a3b5f 100644
--- a/src/v8natives.js
+++ b/src/v8natives.js
@@ -1757,30 +1757,36 @@ function FunctionBind(this_arg) { // Length is 1.
 }


-function NewFunction(arg1) {  // length == 1
-  var n = %_ArgumentsLength();
+function NewFunctionString(arguments, function_token) {
+  var n = arguments.length;
   var p = '';
   if (n > 1) {
-    p = new InternalArray(n - 1);
-    for (var i = 0; i < n - 1; i++) p[i] = %_Arguments(i);
-    p = Join(p, n - 1, ',', NonStringToString);
+    p = ToString(arguments[0]);
+    for (var i = 1; i < n - 1; i++) {
+      p += ',' + ToString(arguments[i]);
+    }
     // If the formal parameters string include ) - an illegal
     // character - it may make the combined function expression
     // compile. We avoid this problem by checking for this early on.
     if (%_CallFunction(p, ')', StringIndexOf) != -1) {
-      throw MakeSyntaxError('paren_in_arg_string',[]);
+      throw MakeSyntaxError('paren_in_arg_string', []);
     }
     // If the formal parameters include an unbalanced block comment, the
     // function must be rejected. Since JavaScript does not allow nested
     // comments we can include a trailing block comment to catch this.
     p += '\n/' + '**/';
   }
-  var body = (n > 0) ? ToString(%_Arguments(n - 1)) : '';
-  var source = '(function(' + p + ') {\n' + body + '\n})';
+  var body = (n > 0) ? ToString(arguments[n - 1]) : '';
+  return '(' + function_token + '(' + p + ') {\n' + body + '\n})';
+}

+
+function FunctionConstructor(arg1) {  // length == 1
+  var source = NewFunctionString(arguments, 'function');
   var global_receiver = %GlobalReceiver(global);
+  // Compile the string in the constructor and not a helper so that errors
+  // appear to come from here.
   var f = %_CallFunction(global_receiver, %CompileString(source, true));
-
   %FunctionMarkNameShouldPrintAsAnonymous(f);
   return f;
 }
@@ -1791,7 +1797,7 @@ function NewFunction(arg1) {  // length == 1
 function SetUpFunction() {
   %CheckIsBootstrapping();

-  %SetCode($Function, NewFunction);
+  %SetCode($Function, FunctionConstructor);
   %SetProperty($Function.prototype, "constructor", $Function, DONT_ENUM);

   InstallFunctions($Function.prototype, DONT_ENUM, $Array(
Index: test/mjsunit/harmony/generators-iteration.js
diff --git a/test/mjsunit/harmony/generators-iteration.js b/test/mjsunit/harmony/generators-iteration.js index e717f1b4a3207518d69a14506f1f9827b5bd406a..39e686604ab7cdff43d390c16bf9d8d3d74007c7 100644
--- a/test/mjsunit/harmony/generators-iteration.js
+++ b/test/mjsunit/harmony/generators-iteration.js
@@ -324,6 +324,28 @@ TestGenerator(
     "foo",
     [2, "1foo3", 5, "4foo6", "foofoo"]);

+// Generator function instances.
+TestGenerator(GeneratorFunction(),
+              [undefined],
+              "foo",
+              [undefined]);
+
+TestGenerator(new GeneratorFunction(),
+              [undefined],
+              "foo",
+              [undefined]);
+
+TestGenerator(GeneratorFunction('yield 1;'),
+              [1, undefined],
+              "foo",
+              [1, undefined]);
+
+TestGenerator(
+ function() { return GeneratorFunction('x', 'y', 'yield x + y;')(1, 2) },
+    [3, undefined],
+    "foo",
+    [3, undefined]);
+
 function TestTryCatch(instantiate) {
function* g() { yield 1; try { yield 2; } catch (e) { yield e; } yield 3; }
   function Sentinel() {}
Index: test/mjsunit/harmony/generators-runtime.js
diff --git a/test/mjsunit/harmony/generators-runtime.js b/test/mjsunit/harmony/generators-runtime.js index b4e8f950e1358898d31d16beddd6e36da9228a30..4c589a6dba1ceb6d1363c8ccd36630f71ee7ecdc 100644
--- a/test/mjsunit/harmony/generators-runtime.js
+++ b/test/mjsunit/harmony/generators-runtime.js
@@ -110,6 +110,9 @@ function TestGeneratorFunction() {
   // Not all functions are generators.
   assertTrue(f instanceof Function);  // Sanity check.
   assertTrue(!(f instanceof GeneratorFunction));
+
+  assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
+  assertTrue(GeneratorFunction() instanceof GeneratorFunction);
 }
 TestGeneratorFunction();



--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to