OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --harmony-object-literals --allow-natives-syntax | 5 // Flags: --harmony-object-literals --allow-natives-syntax |
6 | 6 |
7 | 7 |
8 (function TestDescriptor() { | 8 (function TestBasics() { |
9 var object = { | 9 var object = { |
10 method() { | 10 method() { |
11 return 42; | 11 return 42; |
12 } | 12 } |
13 }; | 13 }; |
14 assertEquals(42, object.method()); | 14 assertEquals(42, object.method()); |
15 })(); | 15 })(); |
16 | 16 |
17 | 17 |
| 18 (function TestThis() { |
| 19 var object = { |
| 20 method() { |
| 21 assertEquals(object, this); |
| 22 } |
| 23 }; |
| 24 object.method(); |
| 25 })(); |
| 26 |
| 27 |
18 (function TestDescriptor() { | 28 (function TestDescriptor() { |
19 var object = { | 29 var object = { |
20 method() { | 30 method() { |
21 return 42; | 31 return 42; |
22 } | 32 } |
23 }; | 33 }; |
24 | 34 |
25 var desc = Object.getOwnPropertyDescriptor(object, 'method'); | 35 var desc = Object.getOwnPropertyDescriptor(object, 'method'); |
26 assertTrue(desc.enumerable); | 36 assertTrue(desc.enumerable); |
27 assertTrue(desc.configurable); | 37 assertTrue(desc.configurable); |
28 assertTrue(desc.writable); | 38 assertTrue(desc.writable); |
29 assertEquals('function', typeof desc.value); | 39 assertEquals('function', typeof desc.value); |
30 | 40 |
31 assertEquals(42, desc.value()); | 41 assertEquals(42, desc.value()); |
32 })(); | 42 })(); |
33 | 43 |
34 | 44 |
35 (function TestProto() { | 45 (function TestProto() { |
36 var object = { | 46 var object = { |
37 method() { | 47 method() {} |
38 return 42; | |
39 } | |
40 }; | 48 }; |
41 | 49 |
42 assertEquals(Function.prototype, Object.getPrototypeOf(object.method)); | 50 assertEquals(Function.prototype, Object.getPrototypeOf(object.method)); |
43 })(); | 51 })(); |
44 | 52 |
45 | 53 |
46 (function TestNotConstructable() { | 54 (function TestNotConstructable() { |
47 var object = { | 55 var object = { |
48 method() { | 56 method() {} |
49 return 42; | |
50 } | |
51 }; | 57 }; |
52 | 58 |
53 assertThrows(function() { | 59 assertThrows(function() { |
54 new object.method; | 60 new object.method; |
55 }); | 61 }); |
56 })(); | 62 })(); |
57 | 63 |
58 | 64 |
59 (function TestFunctionName() { | 65 (function TestFunctionName() { |
60 var object = { | 66 var object = { |
61 method() { | 67 method() {}, |
62 return 42; | |
63 }, | |
64 1() {}, | 68 1() {}, |
65 2.0() {} | 69 2.0() {} |
66 }; | 70 }; |
67 var f = object.method; | 71 var f = object.method; |
68 assertEquals('method', f.name); | 72 assertEquals('method', f.name); |
69 var g = object[1]; | 73 var g = object[1]; |
70 assertEquals('1', g.name); | 74 assertEquals('1', g.name); |
71 | |
72 var h = object[2]; | 75 var h = object[2]; |
73 assertEquals('2', h.name); | 76 assertEquals('2', h.name); |
74 })(); | 77 })(); |
75 | 78 |
76 | 79 |
77 (function TestNoBinding() { | 80 (function TestNoBinding() { |
78 var method = 'local'; | 81 var method = 'local'; |
79 var calls = 0; | 82 var calls = 0; |
80 var object = { | 83 var object = { |
81 method() { | 84 method() { |
82 calls++; | 85 calls++; |
83 assertEquals('local', method); | 86 assertEquals('local', method); |
84 } | 87 } |
85 }; | 88 }; |
86 object.method(); | 89 object.method(); |
87 assertEquals(1, calls); | 90 assertEquals(1, calls); |
88 })(); | 91 })(); |
89 | 92 |
90 | 93 |
91 (function TestNoPrototype() { | 94 (function TestNoPrototype() { |
92 var object = { | 95 var object = { |
93 method() { | 96 method() {} |
94 return 42; | |
95 } | |
96 }; | 97 }; |
97 var f = object.method; | 98 var f = object.method; |
98 assertFalse(f.hasOwnProperty('prototype')); | 99 assertFalse(f.hasOwnProperty('prototype')); |
99 assertEquals(undefined, f.prototype); | 100 assertEquals(undefined, f.prototype); |
100 | 101 |
101 f.prototype = 42; | 102 f.prototype = 42; |
102 assertEquals(42, f.prototype); | 103 assertEquals(42, f.prototype); |
103 })(); | 104 })(); |
104 | 105 |
105 | 106 |
106 (function TestToString() { | 107 (function TestToString() { |
107 var object = { | 108 var object = { |
108 method() { 42; } | 109 method() { 42; } |
109 }; | 110 }; |
110 assertEquals('method() { 42; }', object.method.toString()); | 111 assertEquals('method() { 42; }', object.method.toString()); |
111 })(); | 112 })(); |
112 | 113 |
113 | 114 |
114 (function TestOptimized() { | 115 (function TestOptimized() { |
115 var object = { | 116 var object = { |
116 method() { return 42; } | 117 method() { return 42; } |
117 }; | 118 }; |
118 assertEquals(42, object.method()); | 119 assertEquals(42, object.method()); |
119 assertEquals(42, object.method()); | 120 assertEquals(42, object.method()); |
120 %OptimizeFunctionOnNextCall(object.method); | 121 %OptimizeFunctionOnNextCall(object.method); |
121 assertEquals(42, object.method()); | 122 assertEquals(42, object.method()); |
122 assertFalse(object.method.hasOwnProperty('prototype')); | 123 assertFalse(object.method.hasOwnProperty('prototype')); |
123 })(); | 124 })(); |
| 125 |
| 126 |
| 127 /////////////////////////////////////////////////////////////////////////////// |
| 128 |
| 129 |
| 130 var GeneratorFunction = function*() {}.__proto__.constructor; |
| 131 |
| 132 |
| 133 function 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 })(); |
OLD | NEW |