1425bb815Sopenharmony_ci// Copyright JS Foundation and other contributors, http://js.foundation
2425bb815Sopenharmony_ci//
3425bb815Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4425bb815Sopenharmony_ci// you may not use this file except in compliance with the License.
5425bb815Sopenharmony_ci// You may obtain a copy of the License at
6425bb815Sopenharmony_ci//
7425bb815Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8425bb815Sopenharmony_ci//
9425bb815Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10425bb815Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS
11425bb815Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12425bb815Sopenharmony_ci// See the License for the specific language governing permissions and
13425bb815Sopenharmony_ci// limitations under the License.
14425bb815Sopenharmony_ci
15425bb815Sopenharmony_ci
16425bb815Sopenharmony_ci// Test %GeneratorPrototype%
17425bb815Sopenharmony_ci(function () {
18425bb815Sopenharmony_ci  function* generatorFn(){}
19425bb815Sopenharmony_ci  var ownProto = Object.getPrototypeOf(generatorFn());
20425bb815Sopenharmony_ci  var sharedProto = Object.getPrototypeOf(ownProto);
21425bb815Sopenharmony_ci
22425bb815Sopenharmony_ci  assert(ownProto === generatorFn.prototype);
23425bb815Sopenharmony_ci  assert(sharedProto !== Object.prototype);
24425bb815Sopenharmony_ci  assert(sharedProto === Object.getPrototypeOf(function*(){}.prototype));
25425bb815Sopenharmony_ci  assert(sharedProto.hasOwnProperty('next'));
26425bb815Sopenharmony_ci})();
27425bb815Sopenharmony_ci
28425bb815Sopenharmony_ci// Test %GeneratorPrototype% prototype chain
29425bb815Sopenharmony_ci(function () {
30425bb815Sopenharmony_ci  function* generatorFn(){}
31425bb815Sopenharmony_ci  var g = generatorFn();
32425bb815Sopenharmony_ci  var ownProto = Object.getPrototypeOf(g);
33425bb815Sopenharmony_ci  var sharedProto = Object.getPrototypeOf(ownProto);
34425bb815Sopenharmony_ci  var iterProto = Object.getPrototypeOf(sharedProto);
35425bb815Sopenharmony_ci
36425bb815Sopenharmony_ci  assert(ownProto === generatorFn.prototype);
37425bb815Sopenharmony_ci  assert(iterProto.hasOwnProperty(Symbol.iterator));
38425bb815Sopenharmony_ci  assert(!sharedProto.hasOwnProperty(Symbol.iterator));
39425bb815Sopenharmony_ci  assert(!ownProto.hasOwnProperty(Symbol.iterator));
40425bb815Sopenharmony_ci  assert(g[Symbol.iterator]() === g);
41425bb815Sopenharmony_ci})();
42425bb815Sopenharmony_ci
43425bb815Sopenharmony_ci// Test %GeneratorPrototype% prototype chain
44425bb815Sopenharmony_ci(function () {
45425bb815Sopenharmony_ci  function* g(){}
46425bb815Sopenharmony_ci  var iterator = new g.constructor("a","b","c","() => yield\n yield a; yield b; yield c;")(1,2,3);
47425bb815Sopenharmony_ci
48425bb815Sopenharmony_ci  var item = iterator.next();
49425bb815Sopenharmony_ci  assert(item.value === 1);
50425bb815Sopenharmony_ci  assert(item.done === false);
51425bb815Sopenharmony_ci
52425bb815Sopenharmony_ci  item = iterator.next();
53425bb815Sopenharmony_ci  assert(item.value === 2);
54425bb815Sopenharmony_ci  assert(item.done === false);
55425bb815Sopenharmony_ci
56425bb815Sopenharmony_ci  item = iterator.next();
57425bb815Sopenharmony_ci  assert(item.value === 3);
58425bb815Sopenharmony_ci  assert(item.done === false);
59425bb815Sopenharmony_ci
60425bb815Sopenharmony_ci  item = iterator.next();
61425bb815Sopenharmony_ci  assert(item.value === undefined);
62425bb815Sopenharmony_ci  assert(item.done === true);
63425bb815Sopenharmony_ci
64425bb815Sopenharmony_ci  assert(g.constructor === (function*(){}).constructor);
65425bb815Sopenharmony_ci})();
66425bb815Sopenharmony_ci
67425bb815Sopenharmony_ci// Test GeneratorFunction parsing
68425bb815Sopenharmony_ci(function () {
69425bb815Sopenharmony_ci  function *f() {};
70425bb815Sopenharmony_ci
71425bb815Sopenharmony_ci  try {
72425bb815Sopenharmony_ci    Object.getPrototypeOf(f).constructor("yield", "");
73425bb815Sopenharmony_ci  } catch (e) {
74425bb815Sopenharmony_ci    assert(e instanceof SyntaxError);
75425bb815Sopenharmony_ci  }
76425bb815Sopenharmony_ci})();
77425bb815Sopenharmony_ci
78425bb815Sopenharmony_ci// Test correct property membership
79425bb815Sopenharmony_ci(function () {
80425bb815Sopenharmony_ci  function *f() {};
81425bb815Sopenharmony_ci
82425bb815Sopenharmony_ci  Object.getPrototypeOf(f).xx = 5;
83425bb815Sopenharmony_ci  assert(Object.getPrototypeOf(f).prototype.constructor.xx === 5);
84425bb815Sopenharmony_ci})();
85425bb815Sopenharmony_ci
86425bb815Sopenharmony_ci// Test GetPrototypeFromConstructor
87425bb815Sopenharmony_ci(function () {
88425bb815Sopenharmony_ci  function *f() {};
89425bb815Sopenharmony_ci
90425bb815Sopenharmony_ci  var originalProto = f.prototype;
91425bb815Sopenharmony_ci  f.prototype = 5;
92425bb815Sopenharmony_ci  assert(Object.getPrototypeOf(f()) === Object.getPrototypeOf(originalProto));
93425bb815Sopenharmony_ci  var fakeProto = { x : 6 };
94425bb815Sopenharmony_ci  f.prototype = fakeProto;
95425bb815Sopenharmony_ci  assert(Object.getPrototypeOf(f()) === fakeProto);
96425bb815Sopenharmony_ci  assert(f.next === undefined);
97425bb815Sopenharmony_ci})();
98