2013-04-11 16:28:19 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Test aspects of the generator runtime.
|
|
|
|
|
|
|
|
// See:
|
2014-06-12 08:53:07 +00:00
|
|
|
// http://people.mozilla.org/~jorendorff/es6-draft.html#sec-generatorfunction-objects
|
2013-04-11 16:28:19 +00:00
|
|
|
|
2015-04-09 22:40:16 +00:00
|
|
|
function f() { "use strict"; }
|
2013-04-11 16:28:19 +00:00
|
|
|
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);
|
2015-04-09 22:40:16 +00:00
|
|
|
if (prop === "prototype") {
|
|
|
|
// ES6 draft 03-17-2015 section 25.2.2.2
|
|
|
|
assertFalse(g_desc.writable, prop);
|
|
|
|
assertFalse(g_desc.enumerable, prop);
|
|
|
|
assertFalse(g_desc.configurable, prop);
|
2014-05-19 10:47:00 +00:00
|
|
|
} else {
|
2015-04-09 22:40:16 +00:00
|
|
|
assertEquals(f_desc.configurable, g_desc.configurable, prop);
|
2014-05-19 10:47:00 +00:00
|
|
|
assertEquals(f_desc.writable, g_desc.writable, prop);
|
2015-04-09 22:40:16 +00:00
|
|
|
assertEquals(f_desc.enumerable, g_desc.enumerable, prop);
|
2014-05-19 10:47:00 +00:00
|
|
|
}
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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* () {}));
|
2015-04-06 21:04:43 +00:00
|
|
|
assertSame("object", typeof GeneratorFunctionPrototype);
|
|
|
|
|
|
|
|
var constructor_desc = Object.getOwnPropertyDescriptor(
|
|
|
|
GeneratorFunctionPrototype, "constructor");
|
|
|
|
assertTrue(constructor_desc !== undefined);
|
|
|
|
assertSame(GeneratorFunction, constructor_desc.value);
|
|
|
|
assertFalse(constructor_desc.writable);
|
|
|
|
assertFalse(constructor_desc.enumerable);
|
|
|
|
assertTrue(constructor_desc.configurable);
|
|
|
|
|
|
|
|
var prototype_desc = Object.getOwnPropertyDescriptor(
|
|
|
|
GeneratorFunctionPrototype, "prototype");
|
|
|
|
assertTrue(prototype_desc !== undefined);
|
|
|
|
assertSame(GeneratorObjectPrototype, prototype_desc.value);
|
|
|
|
assertFalse(prototype_desc.writable);
|
|
|
|
assertFalse(prototype_desc.enumerable);
|
|
|
|
assertTrue(prototype_desc.configurable);
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
|
2013-06-10 09:26:18 +00:00
|
|
|
var expected_property_names = ["next", "throw", "constructor"];
|
2013-04-11 16:28:19 +00:00
|
|
|
var found_property_names =
|
|
|
|
Object.getOwnPropertyNames(GeneratorObjectPrototype);
|
|
|
|
|
|
|
|
expected_property_names.sort();
|
|
|
|
found_property_names.sort();
|
|
|
|
|
|
|
|
assertArrayEquals(expected_property_names, found_property_names);
|
2014-06-12 08:53:07 +00:00
|
|
|
|
2015-04-11 21:14:19 +00:00
|
|
|
var constructor_desc = Object.getOwnPropertyDescriptor(
|
|
|
|
GeneratorObjectPrototype, "constructor");
|
|
|
|
assertTrue(constructor_desc !== undefined);
|
|
|
|
assertFalse(constructor_desc.writable);
|
|
|
|
assertFalse(constructor_desc.enumerable);
|
|
|
|
assertTrue(constructor_desc.configurable);
|
|
|
|
|
|
|
|
var next_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
|
|
|
|
"next");
|
|
|
|
assertTrue(next_desc !== undefined);
|
|
|
|
assertTrue(next_desc.writable);
|
|
|
|
assertFalse(next_desc.enumerable);
|
|
|
|
assertTrue(next_desc.configurable);
|
|
|
|
|
|
|
|
var throw_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
|
|
|
|
"throw");
|
2015-04-12 21:18:29 +00:00
|
|
|
assertTrue(throw_desc !== undefined);
|
|
|
|
assertTrue(throw_desc.writable);
|
|
|
|
assertFalse(throw_desc.enumerable);
|
|
|
|
assertTrue(throw_desc.configurable);
|
2015-04-11 21:14:19 +00:00
|
|
|
|
|
|
|
var iterator_desc = Object.getOwnPropertyDescriptor(GeneratorObjectPrototype,
|
2014-06-12 08:53:07 +00:00
|
|
|
Symbol.iterator);
|
|
|
|
assertTrue(iterator_desc !== undefined);
|
|
|
|
assertFalse(iterator_desc.writable);
|
|
|
|
assertFalse(iterator_desc.enumerable);
|
|
|
|
assertFalse(iterator_desc.configurable);
|
|
|
|
|
|
|
|
// The generator object's "iterator" function is just the identity.
|
|
|
|
assertSame(iterator_desc.value.call(42), 42);
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
2013-05-03 13:01:28 +00:00
|
|
|
assertEquals("function* g() { yield 1; }", g.toString());
|
|
|
|
|
2013-04-11 16:28:19 +00:00
|
|
|
// Not all functions are generators.
|
|
|
|
assertTrue(f instanceof Function); // Sanity check.
|
|
|
|
assertTrue(!(f instanceof GeneratorFunction));
|
2013-06-12 12:52:16 +00:00
|
|
|
|
|
|
|
assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
|
|
|
|
assertTrue(GeneratorFunction() instanceof GeneratorFunction);
|
2013-04-11 16:28:19 +00:00
|
|
|
}
|
|
|
|
TestGeneratorFunction();
|
|
|
|
|
|
|
|
|
|
|
|
function TestPerGeneratorPrototype() {
|
|
|
|
assertTrue((function*(){}).prototype !== (function*(){}).prototype);
|
|
|
|
assertTrue((function*(){}).prototype !== g.prototype);
|
|
|
|
assertSame(GeneratorObjectPrototype, Object.getPrototypeOf(g.prototype));
|
|
|
|
assertTrue(!(g.prototype instanceof Function));
|
|
|
|
assertSame(typeof (g.prototype), "object");
|
|
|
|
|
|
|
|
assertArrayEquals([], Object.getOwnPropertyNames(g.prototype));
|
|
|
|
}
|
|
|
|
TestPerGeneratorPrototype();
|