1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/Generators/runtime.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,131 @@ 1.4 +// This file was written by Andy Wingo <wingo@igalia.com> and originally 1.5 +// contributed to V8 as generators-runtime.js, available here: 1.6 +// 1.7 +// http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-runtime.js 1.8 + 1.9 +// Test aspects of the generator runtime. 1.10 + 1.11 +// See http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.19.3. 1.12 + 1.13 +function assertSyntaxError(str) { 1.14 + assertThrowsInstanceOf(Function(str), SyntaxError); 1.15 +} 1.16 + 1.17 + 1.18 +function f() { } 1.19 +function* g() { yield 1; } 1.20 +var GeneratorFunctionPrototype = Object.getPrototypeOf(g); 1.21 +var GeneratorFunction = GeneratorFunctionPrototype.constructor; 1.22 +var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype; 1.23 +// FIXME: This should be a symbol. 1.24 +var std_iterator = "@@iterator"; 1.25 + 1.26 + 1.27 +// A generator function should have the same set of properties as any 1.28 +// other function. 1.29 +function TestGeneratorFunctionInstance() { 1.30 + var f_own_property_names = Object.getOwnPropertyNames(f); 1.31 + var g_own_property_names = Object.getOwnPropertyNames(g); 1.32 + 1.33 + f_own_property_names.sort(); 1.34 + g_own_property_names.sort(); 1.35 + 1.36 + assertDeepEq(f_own_property_names, g_own_property_names); 1.37 + var i; 1.38 + for (i = 0; i < f_own_property_names.length; i++) { 1.39 + var prop = f_own_property_names[i]; 1.40 + var f_desc = Object.getOwnPropertyDescriptor(f, prop); 1.41 + var g_desc = Object.getOwnPropertyDescriptor(g, prop); 1.42 + assertEq(f_desc.configurable, g_desc.configurable, prop); 1.43 + assertEq(f_desc.writable, g_desc.writable, prop); 1.44 + assertEq(f_desc.enumerable, g_desc.enumerable, prop); 1.45 + } 1.46 +} 1.47 +TestGeneratorFunctionInstance(); 1.48 + 1.49 + 1.50 +// Generators have an additional object interposed in the chain between 1.51 +// themselves and Function.prototype. 1.52 +function TestGeneratorFunctionPrototype() { 1.53 + // Sanity check. 1.54 + assertEq(Object.getPrototypeOf(f), Function.prototype); 1.55 + assertNotEq(GeneratorFunctionPrototype, Function.prototype); 1.56 + assertEq(Object.getPrototypeOf(GeneratorFunctionPrototype), 1.57 + Function.prototype); 1.58 + assertEq(Object.getPrototypeOf(function* () {}), 1.59 + GeneratorFunctionPrototype); 1.60 +} 1.61 +TestGeneratorFunctionPrototype(); 1.62 + 1.63 + 1.64 +// Functions that we associate with generator objects are actually defined by 1.65 +// a common prototype. 1.66 +function TestGeneratorObjectPrototype() { 1.67 + assertEq(Object.getPrototypeOf(GeneratorObjectPrototype), 1.68 + Object.prototype); 1.69 + assertEq(Object.getPrototypeOf((function*(){yield 1}).prototype), 1.70 + GeneratorObjectPrototype); 1.71 + 1.72 + var expected_property_names = ["next", "throw", "constructor", std_iterator]; 1.73 + var found_property_names = 1.74 + Object.getOwnPropertyNames(GeneratorObjectPrototype); 1.75 + 1.76 + expected_property_names.sort(); 1.77 + found_property_names.sort(); 1.78 + 1.79 + assertDeepEq(found_property_names, expected_property_names); 1.80 +} 1.81 +TestGeneratorObjectPrototype(); 1.82 + 1.83 + 1.84 +// This tests the object that would be called "GeneratorFunction", if it were 1.85 +// like "Function". 1.86 +function TestGeneratorFunction() { 1.87 + assertEq(GeneratorFunctionPrototype, GeneratorFunction.prototype); 1.88 + assertTrue(g instanceof GeneratorFunction); 1.89 + 1.90 + assertEq(Function, Object.getPrototypeOf(GeneratorFunction)); 1.91 + assertTrue(g instanceof Function); 1.92 + 1.93 + assertEq("function* g() { yield 1; }", g.toString()); 1.94 + 1.95 + // Not all functions are generators. 1.96 + assertTrue(f instanceof Function); // Sanity check. 1.97 + assertFalse(f instanceof GeneratorFunction); 1.98 + 1.99 + assertTrue((new GeneratorFunction()) instanceof GeneratorFunction); 1.100 + assertTrue(GeneratorFunction() instanceof GeneratorFunction); 1.101 + 1.102 + assertTrue(GeneratorFunction('yield 1') instanceof GeneratorFunction); 1.103 + assertTrue(GeneratorFunction('return 1') instanceof GeneratorFunction); 1.104 + assertTrue(GeneratorFunction('a', 'yield a') instanceof GeneratorFunction); 1.105 + assertTrue(GeneratorFunction('a', 'return a') instanceof GeneratorFunction); 1.106 + assertTrue(GeneratorFunction('a', 'return a') instanceof GeneratorFunction); 1.107 + assertSyntaxError("GeneratorFunction('yield', 'return yield')"); 1.108 + assertTrue(GeneratorFunction('with (x) return foo;') instanceof GeneratorFunction); 1.109 + assertSyntaxError("GeneratorFunction('\"use strict\"; with (x) return foo;')"); 1.110 + 1.111 + // Doesn't matter particularly what string gets serialized, as long 1.112 + // as it contains "function*" and "yield 10". 1.113 + assertEq(GeneratorFunction('yield 10').toString(), 1.114 + "function* anonymous() {\nyield 10\n}"); 1.115 +} 1.116 +TestGeneratorFunction(); 1.117 + 1.118 + 1.119 +function TestPerGeneratorPrototype() { 1.120 + assertNotEq((function*(){}).prototype, (function*(){}).prototype); 1.121 + assertNotEq((function*(){}).prototype, g.prototype); 1.122 + assertEq(typeof GeneratorFunctionPrototype, "object"); 1.123 + assertEq(g.prototype.__proto__.constructor, GeneratorFunctionPrototype, "object"); 1.124 + assertEq(Object.getPrototypeOf(g.prototype), GeneratorObjectPrototype); 1.125 + assertFalse(g.prototype instanceof Function); 1.126 + assertEq(typeof (g.prototype), "object"); 1.127 + 1.128 + assertDeepEq(Object.getOwnPropertyNames(g.prototype), []); 1.129 +} 1.130 +TestPerGeneratorPrototype(); 1.131 + 1.132 + 1.133 +if (typeof reportCompare == "function") 1.134 + reportCompare(true, true);