js/src/tests/ecma_6/Generators/runtime.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // This file was written by Andy Wingo <wingo@igalia.com> and originally
michael@0 2 // contributed to V8 as generators-runtime.js, available here:
michael@0 3 //
michael@0 4 // http://code.google.com/p/v8/source/browse/branches/bleeding_edge/test/mjsunit/harmony/generators-runtime.js
michael@0 5
michael@0 6 // Test aspects of the generator runtime.
michael@0 7
michael@0 8 // See http://people.mozilla.org/~jorendorff/es6-draft.html#sec-15.19.3.
michael@0 9
michael@0 10 function assertSyntaxError(str) {
michael@0 11 assertThrowsInstanceOf(Function(str), SyntaxError);
michael@0 12 }
michael@0 13
michael@0 14
michael@0 15 function f() { }
michael@0 16 function* g() { yield 1; }
michael@0 17 var GeneratorFunctionPrototype = Object.getPrototypeOf(g);
michael@0 18 var GeneratorFunction = GeneratorFunctionPrototype.constructor;
michael@0 19 var GeneratorObjectPrototype = GeneratorFunctionPrototype.prototype;
michael@0 20 // FIXME: This should be a symbol.
michael@0 21 var std_iterator = "@@iterator";
michael@0 22
michael@0 23
michael@0 24 // A generator function should have the same set of properties as any
michael@0 25 // other function.
michael@0 26 function TestGeneratorFunctionInstance() {
michael@0 27 var f_own_property_names = Object.getOwnPropertyNames(f);
michael@0 28 var g_own_property_names = Object.getOwnPropertyNames(g);
michael@0 29
michael@0 30 f_own_property_names.sort();
michael@0 31 g_own_property_names.sort();
michael@0 32
michael@0 33 assertDeepEq(f_own_property_names, g_own_property_names);
michael@0 34 var i;
michael@0 35 for (i = 0; i < f_own_property_names.length; i++) {
michael@0 36 var prop = f_own_property_names[i];
michael@0 37 var f_desc = Object.getOwnPropertyDescriptor(f, prop);
michael@0 38 var g_desc = Object.getOwnPropertyDescriptor(g, prop);
michael@0 39 assertEq(f_desc.configurable, g_desc.configurable, prop);
michael@0 40 assertEq(f_desc.writable, g_desc.writable, prop);
michael@0 41 assertEq(f_desc.enumerable, g_desc.enumerable, prop);
michael@0 42 }
michael@0 43 }
michael@0 44 TestGeneratorFunctionInstance();
michael@0 45
michael@0 46
michael@0 47 // Generators have an additional object interposed in the chain between
michael@0 48 // themselves and Function.prototype.
michael@0 49 function TestGeneratorFunctionPrototype() {
michael@0 50 // Sanity check.
michael@0 51 assertEq(Object.getPrototypeOf(f), Function.prototype);
michael@0 52 assertNotEq(GeneratorFunctionPrototype, Function.prototype);
michael@0 53 assertEq(Object.getPrototypeOf(GeneratorFunctionPrototype),
michael@0 54 Function.prototype);
michael@0 55 assertEq(Object.getPrototypeOf(function* () {}),
michael@0 56 GeneratorFunctionPrototype);
michael@0 57 }
michael@0 58 TestGeneratorFunctionPrototype();
michael@0 59
michael@0 60
michael@0 61 // Functions that we associate with generator objects are actually defined by
michael@0 62 // a common prototype.
michael@0 63 function TestGeneratorObjectPrototype() {
michael@0 64 assertEq(Object.getPrototypeOf(GeneratorObjectPrototype),
michael@0 65 Object.prototype);
michael@0 66 assertEq(Object.getPrototypeOf((function*(){yield 1}).prototype),
michael@0 67 GeneratorObjectPrototype);
michael@0 68
michael@0 69 var expected_property_names = ["next", "throw", "constructor", std_iterator];
michael@0 70 var found_property_names =
michael@0 71 Object.getOwnPropertyNames(GeneratorObjectPrototype);
michael@0 72
michael@0 73 expected_property_names.sort();
michael@0 74 found_property_names.sort();
michael@0 75
michael@0 76 assertDeepEq(found_property_names, expected_property_names);
michael@0 77 }
michael@0 78 TestGeneratorObjectPrototype();
michael@0 79
michael@0 80
michael@0 81 // This tests the object that would be called "GeneratorFunction", if it were
michael@0 82 // like "Function".
michael@0 83 function TestGeneratorFunction() {
michael@0 84 assertEq(GeneratorFunctionPrototype, GeneratorFunction.prototype);
michael@0 85 assertTrue(g instanceof GeneratorFunction);
michael@0 86
michael@0 87 assertEq(Function, Object.getPrototypeOf(GeneratorFunction));
michael@0 88 assertTrue(g instanceof Function);
michael@0 89
michael@0 90 assertEq("function* g() { yield 1; }", g.toString());
michael@0 91
michael@0 92 // Not all functions are generators.
michael@0 93 assertTrue(f instanceof Function); // Sanity check.
michael@0 94 assertFalse(f instanceof GeneratorFunction);
michael@0 95
michael@0 96 assertTrue((new GeneratorFunction()) instanceof GeneratorFunction);
michael@0 97 assertTrue(GeneratorFunction() instanceof GeneratorFunction);
michael@0 98
michael@0 99 assertTrue(GeneratorFunction('yield 1') instanceof GeneratorFunction);
michael@0 100 assertTrue(GeneratorFunction('return 1') instanceof GeneratorFunction);
michael@0 101 assertTrue(GeneratorFunction('a', 'yield a') instanceof GeneratorFunction);
michael@0 102 assertTrue(GeneratorFunction('a', 'return a') instanceof GeneratorFunction);
michael@0 103 assertTrue(GeneratorFunction('a', 'return a') instanceof GeneratorFunction);
michael@0 104 assertSyntaxError("GeneratorFunction('yield', 'return yield')");
michael@0 105 assertTrue(GeneratorFunction('with (x) return foo;') instanceof GeneratorFunction);
michael@0 106 assertSyntaxError("GeneratorFunction('\"use strict\"; with (x) return foo;')");
michael@0 107
michael@0 108 // Doesn't matter particularly what string gets serialized, as long
michael@0 109 // as it contains "function*" and "yield 10".
michael@0 110 assertEq(GeneratorFunction('yield 10').toString(),
michael@0 111 "function* anonymous() {\nyield 10\n}");
michael@0 112 }
michael@0 113 TestGeneratorFunction();
michael@0 114
michael@0 115
michael@0 116 function TestPerGeneratorPrototype() {
michael@0 117 assertNotEq((function*(){}).prototype, (function*(){}).prototype);
michael@0 118 assertNotEq((function*(){}).prototype, g.prototype);
michael@0 119 assertEq(typeof GeneratorFunctionPrototype, "object");
michael@0 120 assertEq(g.prototype.__proto__.constructor, GeneratorFunctionPrototype, "object");
michael@0 121 assertEq(Object.getPrototypeOf(g.prototype), GeneratorObjectPrototype);
michael@0 122 assertFalse(g.prototype instanceof Function);
michael@0 123 assertEq(typeof (g.prototype), "object");
michael@0 124
michael@0 125 assertDeepEq(Object.getOwnPropertyNames(g.prototype), []);
michael@0 126 }
michael@0 127 TestPerGeneratorPrototype();
michael@0 128
michael@0 129
michael@0 130 if (typeof reportCompare == "function")
michael@0 131 reportCompare(true, true);

mercurial