Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(890)

Unified Diff: test/mjsunit/harmony/generators-runtime.js

Issue 13192004: arrange to create prototypes for generators (Closed) Base URL: git://github.com/v8/v8.git@master
Patch Set: Explicitly add constructor properties in generator.js Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: test/mjsunit/harmony/generators-runtime.js
diff --git a/test/mjsunit/harmony/generators-runtime.js b/test/mjsunit/harmony/generators-runtime.js
new file mode 100644
index 0000000000000000000000000000000000000000..d28140c193d5605f7c81aad26ba158f0ecae2a40
--- /dev/null
+++ b/test/mjsunit/harmony/generators-runtime.js
@@ -0,0 +1,126 @@
+// Copyright 2013 the V8 project authors. All rights reserved.
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided
+// with the distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived
+// from this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+// Flags: --harmony-generators
+
+// Test aspects of the generator runtime.
+
+// FIXME(wingo): Replace this reference with a more official link.
+// See:
+// http://wiki.ecmascript.org/lib/exe/fetch.php?cache=cache&media=harmony:es6_generator_object_model_3-29-13.png
+
+function f() { }
+function* g() { yield 1; }
+var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
+var GeneratorFunction = GeneratorFunctionPrototype.constructor;
+var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
+
+// A generator function should have the same set of properties as any
+// other function.
+function TestGeneratorFunctionInstance() {
+ var f_own_property_names = Object.getOwnPropertyNames(f);
+ var g_own_property_names = Object.getOwnPropertyNames(g);
+
+ f_own_property_names.sort();
+ g_own_property_names.sort();
+
+ assertArrayEquals(f_own_property_names, g_own_property_names);
+ var i;
+ for (i = 0; i < f_own_property_names.length; i++) {
+ var prop = f_own_property_names[i];
+ var f_desc = Object.getOwnPropertyDescriptor(f, prop);
+ var g_desc = Object.getOwnPropertyDescriptor(g, prop);
+ assertEquals(f_desc.configurable, g_desc.configurable, prop);
+ assertEquals(f_desc.writable, g_desc.writable, prop);
+ assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
+ }
+}
+TestGeneratorFunctionInstance();
+
+
+// Generators have an additional object interposed in the chain between
+// themselves and Function.prototype.
+function TestGeneratorFunctionPrototype() {
+ // Sanity check.
+ assertSame(Object.getPrototypeOf(f), Function.prototype);
+ assertFalse(GeneratorFunctionPrototype === Function.prototype);
+ assertSame(Function.prototype,
+ Object.getPrototypeOf(GeneratorFunctionPrototype));
+ assertSame(GeneratorFunctionPrototype,
+ Object.getPrototypeOf(function* () {}));
+}
+TestGeneratorFunctionPrototype();
+
+
+// Functions that we associate with generator objects are actually defined by
+// a common prototype.
+function TestGeneratorObjectPrototype() {
+ assertSame(Object.prototype,
+ Object.getPrototypeOf(GeneratorObjectPrototype));
+ assertSame(GeneratorObjectPrototype,
+ Object.getPrototypeOf((function*(){yield 1}).prototype));
+
+ var expected_property_names = ["next", "send", "throw", "close",
+ "constructor"];
+ var found_property_names =
+ Object.getOwnPropertyNames(GeneratorObjectPrototype);
+
+ expected_property_names.sort();
+ found_property_names.sort();
+
+ assertArrayEquals(expected_property_names, found_property_names);
+}
+TestGeneratorObjectPrototype();
+
+
+// This tests the object that would be called "GeneratorFunction", if it were
+// like "Function".
+function TestGeneratorFunction() {
+ assertSame(GeneratorFunctionPrototype, GeneratorFunction.prototype);
+ assertTrue(g instanceof GeneratorFunction);
+
+ assertSame(Function, Object.getPrototypeOf(GeneratorFunction));
+ assertTrue(g instanceof Function);
+
+ // Not all functions are generators.
+ assertTrue(f instanceof Function); // Sanity check.
+ assertTrue(!(f instanceof GeneratorFunction));
+}
+TestGeneratorFunction();
+
+
+function TestPerGeneratorPrototype() {
+ assertTrue((function*(){}).prototype !== (function*(){}).prototype);
+ assertTrue((function*(){}).prototype !== g.prototype);
+ assertTrue(g.prototype instanceof GeneratorFunctionPrototype);
+ assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
+ assertTrue(!(g.prototype instanceof Function));
+ assertSame(typeof (g.prototype), "object");
+
+ assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
+}
+TestPerGeneratorPrototype();
« src/generator.js ('K') | « test/mjsunit/builtins.js ('k') | tools/gyp/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698