• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15
16// Test %GeneratorPrototype%
17(function () {
18  function* generatorFn(){}
19  var ownProto = Object.getPrototypeOf(generatorFn());
20  var sharedProto = Object.getPrototypeOf(ownProto);
21
22  assert(ownProto === generatorFn.prototype);
23  assert(sharedProto !== Object.prototype);
24  assert(sharedProto === Object.getPrototypeOf(function*(){}.prototype));
25  assert(sharedProto.hasOwnProperty('next'));
26})();
27
28// Test %GeneratorPrototype% prototype chain
29(function () {
30  function* generatorFn(){}
31  var g = generatorFn();
32  var ownProto = Object.getPrototypeOf(g);
33  var sharedProto = Object.getPrototypeOf(ownProto);
34  var iterProto = Object.getPrototypeOf(sharedProto);
35
36  assert(ownProto === generatorFn.prototype);
37  assert(iterProto.hasOwnProperty(Symbol.iterator));
38  assert(!sharedProto.hasOwnProperty(Symbol.iterator));
39  assert(!ownProto.hasOwnProperty(Symbol.iterator));
40  assert(g[Symbol.iterator]() === g);
41})();
42
43// Test %GeneratorPrototype% prototype chain
44(function () {
45  function* g(){}
46  var iterator = new g.constructor("a","b","c","() => yield\n yield a; yield b; yield c;")(1,2,3);
47
48  var item = iterator.next();
49  assert(item.value === 1);
50  assert(item.done === false);
51
52  item = iterator.next();
53  assert(item.value === 2);
54  assert(item.done === false);
55
56  item = iterator.next();
57  assert(item.value === 3);
58  assert(item.done === false);
59
60  item = iterator.next();
61  assert(item.value === undefined);
62  assert(item.done === true);
63
64  assert(g.constructor === (function*(){}).constructor);
65})();
66
67// Test GeneratorFunction parsing
68(function () {
69  function *f() {};
70
71  try {
72    Object.getPrototypeOf(f).constructor("yield", "");
73  } catch (e) {
74    assert(e instanceof SyntaxError);
75  }
76})();
77
78// Test correct property membership
79(function () {
80  function *f() {};
81
82  Object.getPrototypeOf(f).xx = 5;
83  assert(Object.getPrototypeOf(f).prototype.constructor.xx === 5);
84})();
85
86// Test GetPrototypeFromConstructor
87(function () {
88  function *f() {};
89
90  var originalProto = f.prototype;
91  f.prototype = 5;
92  assert(Object.getPrototypeOf(f()) === Object.getPrototypeOf(originalProto));
93  var fakeProto = { x : 6 };
94  f.prototype = fakeProto;
95  assert(Object.getPrototypeOf(f()) === fakeProto);
96  assert(f.next === undefined);
97})();
98