1// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --harmony-object-literals --allow-natives-syntax
6
7
8(function TestBasics() {
9  var object = {
10    method() {
11      return 42;
12    }
13  };
14  assertEquals(42, object.method());
15})();
16
17
18(function TestThis() {
19  var object = {
20    method() {
21      assertEquals(object, this);
22    }
23  };
24  object.method();
25})();
26
27
28(function TestDescriptor() {
29  var object = {
30    method() {
31      return 42;
32    }
33  };
34
35  var desc = Object.getOwnPropertyDescriptor(object, 'method');
36  assertTrue(desc.enumerable);
37  assertTrue(desc.configurable);
38  assertTrue(desc.writable);
39  assertEquals('function', typeof desc.value);
40
41  assertEquals(42, desc.value());
42})();
43
44
45(function TestProto() {
46  var object = {
47    method() {}
48  };
49
50  assertEquals(Function.prototype, Object.getPrototypeOf(object.method));
51})();
52
53
54(function TestNotConstructable() {
55  var object = {
56    method() {}
57  };
58
59  assertThrows(function() {
60    new object.method;
61  });
62})();
63
64
65(function TestFunctionName() {
66  var object = {
67    method() {},
68    1() {},
69    2.0() {}
70  };
71  var f = object.method;
72  assertEquals('method', f.name);
73  var g = object[1];
74  assertEquals('1', g.name);
75  var h = object[2];
76  assertEquals('2', h.name);
77})();
78
79
80(function TestNoBinding() {
81  var method = 'local';
82  var calls = 0;
83  var object = {
84    method() {
85      calls++;
86      assertEquals('local', method);
87    }
88  };
89  object.method();
90  assertEquals(1, calls);
91})();
92
93
94(function TestNoPrototype() {
95  var object = {
96    method() {}
97  };
98  var f = object.method;
99  assertFalse(f.hasOwnProperty('prototype'));
100  assertEquals(undefined, f.prototype);
101
102  f.prototype = 42;
103  assertEquals(42, f.prototype);
104})();
105
106
107(function TestToString() {
108  var object = {
109    method() { 42; }
110  };
111  assertEquals('method() { 42; }', object.method.toString());
112})();
113
114
115(function TestOptimized() {
116  var object = {
117    method() { return 42; }
118  };
119  assertEquals(42, object.method());
120  assertEquals(42, object.method());
121  %OptimizeFunctionOnNextCall(object.method);
122  assertEquals(42, object.method());
123  assertFalse(object.method.hasOwnProperty('prototype'));
124})();
125
126
127///////////////////////////////////////////////////////////////////////////////
128
129
130var GeneratorFunction = function*() {}.__proto__.constructor;
131
132
133function assertIteratorResult(value, done, result) {
134  assertEquals({value: value, done: done}, result);
135}
136
137
138(function TestGeneratorBasics() {
139  var object = {
140    *method() {
141      yield 1;
142    }
143  };
144  var g = object.method();
145  assertIteratorResult(1, false, g.next());
146  assertIteratorResult(undefined, true, g.next());
147})();
148
149
150(function TestGeneratorThis() {
151  var object = {
152    *method() {
153      yield this;
154    }
155  };
156  var g = object.method();
157  assertIteratorResult(object, false, g.next());
158  assertIteratorResult(undefined, true, g.next());
159})();
160
161
162(function TestGeneratorSymbolIterator() {
163  var object = {
164    *method() {}
165  };
166  var g = object.method();
167  assertEquals(g, g[Symbol.iterator]());
168})();
169
170
171(function TestGeneratorDescriptor() {
172  var object = {
173    *method() {
174      yield 1;
175    }
176  };
177
178  var desc = Object.getOwnPropertyDescriptor(object, 'method');
179  assertTrue(desc.enumerable);
180  assertTrue(desc.configurable);
181  assertTrue(desc.writable);
182  assertEquals('function', typeof desc.value);
183
184  var g = desc.value();
185  assertIteratorResult(1, false, g.next());
186  assertIteratorResult(undefined, true, g.next());
187})();
188
189
190(function TestGeneratorProto() {
191  var object = {
192    *method() {}
193  };
194
195  assertEquals(GeneratorFunction.prototype,
196               Object.getPrototypeOf(object.method));
197})();
198
199
200(function TestGeneratorConstructable() {
201  var object = {
202    *method() {
203      yield 1;
204    }
205  };
206
207  var g = new object.method();
208  assertIteratorResult(1, false, g.next());
209  assertIteratorResult(undefined, true, g.next());
210})();
211
212
213(function TestGeneratorName() {
214  var object = {
215    *method() {},
216    *1() {},
217    *2.0() {}
218  };
219  var f = object.method;
220  assertEquals('method', f.name);
221  var g = object[1];
222  assertEquals('1', g.name);
223  var h = object[2];
224  assertEquals('2', h.name);
225})();
226
227
228(function TestGeneratorNoBinding() {
229  var method = 'local';
230  var calls = 0;
231  var object = {
232    *method() {
233      calls++;
234      assertEquals('local', method);
235    }
236  };
237  var g = object.method();
238  assertIteratorResult(undefined, true, g.next());
239  assertEquals(1, calls);
240})();
241
242
243(function TestGeneratorToString() {
244  var object = {
245    *method() { yield 1; }
246  };
247  assertEquals('*method() { yield 1; }', object.method.toString());
248})();
249