blob: 90bc51ec03d55f352775a9a9728a7a48e31080ff [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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: --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 TestNoRestrictedPropertiesStrict() {
108 var obj = {
109 method() { "use strict"; }
110 };
111 assertFalse(obj.method.hasOwnProperty("arguments"));
112 assertThrows(function() { return obj.method.arguments; }, TypeError);
113 assertThrows(function() { obj.method.arguments = {}; }, TypeError);
114
115 assertFalse(obj.method.hasOwnProperty("caller"));
116 assertThrows(function() { return obj.method.caller; }, TypeError);
117 assertThrows(function() { obj.method.caller = {}; }, TypeError);
118})();
119
120
121(function TestNoRestrictedPropertiesSloppy() {
122 var obj = {
123 method() {}
124 };
125 assertFalse(obj.method.hasOwnProperty("arguments"));
126 assertThrows(function() { return obj.method.arguments; }, TypeError);
127 assertThrows(function() { obj.method.arguments = {}; }, TypeError);
128
129 assertFalse(obj.method.hasOwnProperty("caller"));
130 assertThrows(function() { return obj.method.caller; }, TypeError);
131 assertThrows(function() { obj.method.caller = {}; }, TypeError);
132})();
133
134
135(function TestToString() {
136 var object = {
137 method() { 42; }
138 };
139 assertEquals('method() { 42; }', object.method.toString());
140})();
141
142
143(function TestOptimized() {
144 var object = {
145 method() { return 42; }
146 };
147 assertEquals(42, object.method());
148 assertEquals(42, object.method());
149 %OptimizeFunctionOnNextCall(object.method);
150 assertEquals(42, object.method());
151 assertFalse(object.method.hasOwnProperty('prototype'));
152})();
153
154
155///////////////////////////////////////////////////////////////////////////////
156
157
158var GeneratorFunction = function*() {}.__proto__.constructor;
159var GeneratorPrototype = Object.getPrototypeOf(function*() {}).prototype;
160
161
162function assertIteratorResult(value, done, result) {
163 assertEquals({value: value, done: done}, result);
164}
165
166
167(function TestGeneratorBasics() {
168 var object = {
169 *method() {
170 yield 1;
171 }
172 };
173 var g = object.method();
174 assertIteratorResult(1, false, g.next());
175 assertIteratorResult(undefined, true, g.next());
176})();
177
178
179(function TestGeneratorThis() {
180 var object = {
181 *method() {
182 yield this;
183 }
184 };
185 var g = object.method();
186 assertIteratorResult(object, false, g.next());
187 assertIteratorResult(undefined, true, g.next());
188})();
189
190
191(function TestGeneratorSymbolIterator() {
192 var object = {
193 *method() {}
194 };
195 var g = object.method();
196 assertEquals(g, g[Symbol.iterator]());
197})();
198
199
200(function TestGeneratorDescriptor() {
201 var object = {
202 *method() {
203 yield 1;
204 }
205 };
206
207 var desc = Object.getOwnPropertyDescriptor(object, 'method');
208 assertTrue(desc.enumerable);
209 assertTrue(desc.configurable);
210 assertTrue(desc.writable);
211 assertEquals('function', typeof desc.value);
212
213 var g = desc.value();
214 assertIteratorResult(1, false, g.next());
215 assertIteratorResult(undefined, true, g.next());
216})();
217
218
219(function TestGeneratorPrototypeDescriptor() {
220 var object = {
221 *method() {}
222 };
223
224 var desc = Object.getOwnPropertyDescriptor(object.method, 'prototype');
225 assertFalse(desc.enumerable);
226 assertFalse(desc.configurable);
227 assertTrue(desc.writable);
228 assertEquals(GeneratorPrototype, Object.getPrototypeOf(desc.value));
229})();
230
231
232(function TestGeneratorProto() {
233 var object = {
234 *method() {}
235 };
236
237 assertEquals(GeneratorFunction.prototype,
238 Object.getPrototypeOf(object.method));
239})();
240
241
Ben Murdoch097c5b22016-05-18 11:27:45 +0100242(function TestGeneratorNotConstructable() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243 var object = {
244 *method() {
245 yield 1;
246 }
247 };
248
Ben Murdoch097c5b22016-05-18 11:27:45 +0100249 assertThrows(()=>new object.method());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250})();
251
252
253(function TestGeneratorName() {
254 var object = {
255 *method() {},
256 *1() {},
257 *2.0() {}
258 };
259 var f = object.method;
260 assertEquals('method', f.name);
261 var g = object[1];
262 assertEquals('1', g.name);
263 var h = object[2];
264 assertEquals('2', h.name);
265})();
266
267
268(function TestGeneratorNoBinding() {
269 var method = 'local';
270 var calls = 0;
271 var object = {
272 *method() {
273 calls++;
274 assertEquals('local', method);
275 }
276 };
277 var g = object.method();
278 assertIteratorResult(undefined, true, g.next());
279 assertEquals(1, calls);
280})();
281
282
283(function TestGeneratorToString() {
284 var object = {
285 *method() { yield 1; }
286 };
287 assertEquals('*method() { yield 1; }', object.method.toString());
288})();
289
290
291(function TestProtoName() {
292 var object = {
293 __proto__() {
294 return 1;
295 }
296 };
297 assertEquals(Object.prototype, Object.getPrototypeOf(object));
298 assertEquals(1, object.__proto__());
299})();
300
301
302(function TestProtoName2() {
303 var p = {};
304 var object = {
305 __proto__() {
306 return 1;
307 },
308 __proto__: p
309 };
310 assertEquals(p, Object.getPrototypeOf(object));
311 assertEquals(1, object.__proto__());
312})();