OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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-reflect | 5 // Flags: --harmony-reflect |
6 | 6 |
7 // TODO(neis): Test with proxies. | 7 // TODO(neis): Test with proxies. |
8 | 8 |
9 | 9 |
10 | 10 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 Date, | 45 Date, |
46 RegExp, | 46 RegExp, |
47 global | 47 global |
48 ]; | 48 ]; |
49 | 49 |
50 function prepare(target) { | 50 function prepare(target) { |
51 target["bla"] = true; | 51 target["bla"] = true; |
52 target[4] = 42; | 52 target[4] = 42; |
53 target[sym] = "foo"; | 53 target[sym] = "foo"; |
54 target["noconf"] = 43; | 54 target["noconf"] = 43; |
55 Object.defineProperty(target, "noconf", {configurable: false}); | 55 Object.defineProperty(target, "noconf", |
| 56 { configurable: false }); |
| 57 Object.defineProperty(target, "nowrite", |
| 58 { writable: false, configurable: true, value: 44 }); |
56 Object.defineProperty(target, "getter", | 59 Object.defineProperty(target, "getter", |
57 { get: function () {return this.bla}, configurable: true }); | 60 { get: function () {return this.bla}, configurable: true }); |
58 Object.defineProperty(target, "setter", | 61 Object.defineProperty(target, "setter", |
59 { set: function () {}, configurable: true }); | 62 { set: function (x) {this.gaga = x}, configurable: true }); |
| 63 Object.defineProperty(target, "setter2", |
| 64 { set: function (x) {}, configurable: true }); |
60 } | 65 } |
61 | 66 |
62 | 67 |
63 | 68 |
64 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
65 // Reflect.get | 70 // Reflect.get |
66 | 71 |
67 | 72 |
68 (function testReflectGetArity() { | 73 (function testReflectGetArity() { |
69 assertEquals(3, Reflect.get.length); | 74 assertEquals(3, Reflect.get.length); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 assertEquals(true, Reflect.get(target, "foo")); | 128 assertEquals(true, Reflect.get(target, "foo")); |
124 assertEquals(true, Reflect.get(target, "foo", target)); | 129 assertEquals(true, Reflect.get(target, "foo", target)); |
125 assertEquals(false, Reflect.get(target, "foo", receiver)); | 130 assertEquals(false, Reflect.get(target, "foo", receiver)); |
126 target.__proto__ = proto; | 131 target.__proto__ = proto; |
127 } | 132 } |
128 })(); | 133 })(); |
129 | 134 |
130 | 135 |
131 | 136 |
132 //////////////////////////////////////////////////////////////////////////////// | 137 //////////////////////////////////////////////////////////////////////////////// |
| 138 // Reflect.set |
| 139 |
| 140 |
| 141 (function testReflectSetArity() { |
| 142 assertEquals(3, Reflect.set.length); |
| 143 })(); |
| 144 |
| 145 |
| 146 (function testReflectSetOnNonObject() { |
| 147 assertThrows(function() { Reflect.set(); }, TypeError); |
| 148 assertThrows(function() { Reflect.set(42, "bla"); }, TypeError); |
| 149 assertThrows(function() { Reflect.set(null, "bla"); }, TypeError); |
| 150 })(); |
| 151 |
| 152 |
| 153 (function testReflectSetKeyConversion() { |
| 154 var target = {}; |
| 155 var a = { [Symbol.toPrimitive]: function() { return "bla" } }; |
| 156 var b = { [Symbol.toPrimitive]: function() { throw "gaga" } }; |
| 157 assertTrue(Reflect.set(target, a, 42)); |
| 158 assertEquals(42, target.bla); |
| 159 assertThrows(function() { Reflect.set(target, b, 42); }, "gaga"); |
| 160 })(); |
| 161 |
| 162 |
| 163 (function testReflectSetOnObject() { |
| 164 var receiver = {bla: false}; |
| 165 var value = 34234; |
| 166 for (let target of objects) { |
| 167 prepare(target); |
| 168 assertTrue(Reflect.set(target, "bla", value)); |
| 169 assertEquals(value, target.bla); |
| 170 |
| 171 prepare(target); |
| 172 assertTrue(Reflect.set(target, "bla", value, target)); |
| 173 assertEquals(value, target.bla); |
| 174 |
| 175 prepare(target); |
| 176 assertTrue(Reflect.set(target, "bla", value, receiver)); |
| 177 assertEquals(true, target.bla); |
| 178 assertEquals(value, receiver.bla); |
| 179 receiver.bla = false; |
| 180 |
| 181 prepare(target); |
| 182 assertTrue(Reflect.set(target, 4, value)); |
| 183 assertEquals(value, target[4]); |
| 184 |
| 185 prepare(target); |
| 186 assertTrue(Reflect.set(target, 4, value, target)); |
| 187 assertEquals(value, target[4]); |
| 188 |
| 189 prepare(target); |
| 190 assertTrue(Reflect.set(target, 4, value, receiver)); |
| 191 assertEquals(42, target[4]); |
| 192 assertEquals(value, receiver[4]); |
| 193 delete receiver[4]; |
| 194 |
| 195 prepare(target); |
| 196 assertTrue(Reflect.set(target, sym, value)); |
| 197 assertEquals(value, target[sym]); |
| 198 |
| 199 prepare(target); |
| 200 assertTrue(Reflect.set(target, sym, value, target)); |
| 201 assertEquals(value, target[sym]); |
| 202 |
| 203 prepare(target); |
| 204 assertTrue(Reflect.set(target, sym, value, receiver)); |
| 205 assertEquals("foo", target[sym]); |
| 206 assertEquals(value, receiver[sym]); |
| 207 delete receiver[sym]; |
| 208 |
| 209 prepare(target); |
| 210 assertTrue(Reflect.set(target, "noconf", value)); |
| 211 assertEquals(value, target.noconf); |
| 212 |
| 213 prepare(target); |
| 214 assertTrue(Reflect.set(target, "noconf", value, target)); |
| 215 assertEquals(value, target.noconf); |
| 216 |
| 217 prepare(target); |
| 218 assertTrue(Reflect.set(target, "noconf", value, receiver)); |
| 219 assertEquals(43, target.noconf); |
| 220 assertEquals(value, receiver.noconf); |
| 221 delete receiver.noconf; |
| 222 |
| 223 assertTrue(Reflect.set(target, "setter", value)); |
| 224 assertEquals(value, target.gaga) |
| 225 delete target.gaga; |
| 226 |
| 227 assertTrue(Reflect.set(target, "setter", value, target)); |
| 228 assertEquals(value, target.gaga) |
| 229 delete target.gaga; |
| 230 |
| 231 assertTrue(Reflect.set(target, "setter", value, receiver)); |
| 232 assertFalse("gaga" in target); |
| 233 assertEquals(value, receiver.gaga); |
| 234 delete receiver.gaga; |
| 235 |
| 236 assertFalse(Reflect.set(target, "nowrite", value)); |
| 237 assertEquals(44, target.nowrite); |
| 238 |
| 239 assertFalse(Reflect.set(target, "nowrite", value, target)); |
| 240 assertEquals(44, target.nowrite); |
| 241 |
| 242 assertFalse(Reflect.set(target, "nowrite", value, receiver)); |
| 243 assertEquals(44, target.nowrite); |
| 244 assertFalse("nowrite" in receiver); |
| 245 |
| 246 // Data vs Non-Writable |
| 247 // TODO(neis): This must return false but currently doesn't. |
| 248 // assertFalse(Reflect.set({}, "nowrite", value, target)); |
| 249 |
| 250 // Data vs Accessor |
| 251 // TODO(neis): These must return false but currently don't. |
| 252 // assertFalse(Reflect.set(target, "unknown", value, {set bla(x) {}})); |
| 253 // assertFalse(Reflect.set(target, "unknown", value, {get bla() {}})); |
| 254 // assertFalse(Reflect.set(target, "bla", value, {set bla(x) {}})); |
| 255 // assertFalse(Reflect.set(target, "bla", value, {get bla() {}})); |
| 256 |
| 257 // Accessor vs Data |
| 258 assertTrue(Reflect.set({set bla(x) {}}), "bla", value, target); |
| 259 assertFalse(Reflect.set({get bla() {}}, "bla", value, target)); |
| 260 |
| 261 // Data vs Non-Object |
| 262 assertFalse(Reflect.set({}, "bla", value, null)); |
| 263 assertFalse(Reflect.set({bla: 42}, "bla", value, null)); |
| 264 |
| 265 // Accessor vs Non-Object |
| 266 assertTrue(Reflect.set(target, "setter2", value, null)); |
| 267 assertFalse(Reflect.set(target, "getter", value, null)); |
| 268 } |
| 269 })(); |
| 270 |
| 271 |
| 272 |
| 273 //////////////////////////////////////////////////////////////////////////////// |
133 // Reflect.has | 274 // Reflect.has |
134 | 275 |
135 | 276 |
136 (function testReflectHasArity() { | 277 (function testReflectHasArity() { |
137 assertEquals(2, Reflect.has.length); | 278 assertEquals(2, Reflect.has.length); |
138 })(); | 279 })(); |
139 | 280 |
140 | 281 |
141 (function testReflectHasOnNonObject() { | 282 (function testReflectHasOnNonObject() { |
142 assertThrows(function() { Reflect.has(); }, TypeError); | 283 assertThrows(function() { Reflect.has(); }, TypeError); |
(...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 assertThrows(function() { Reflect.preventExtensions(); }, TypeError); | 546 assertThrows(function() { Reflect.preventExtensions(); }, TypeError); |
406 assertThrows(function() { Reflect.preventExtensions(42); }, TypeError); | 547 assertThrows(function() { Reflect.preventExtensions(42); }, TypeError); |
407 assertThrows(function() { Reflect.preventExtensions(null); }, TypeError); | 548 assertThrows(function() { Reflect.preventExtensions(null); }, TypeError); |
408 })(); | 549 })(); |
409 | 550 |
410 | 551 |
411 // See reflect-prevent-extensions.js for further tests. | 552 // See reflect-prevent-extensions.js for further tests. |
412 | 553 |
413 // TODO(neis): Need proxies to test the situation where | 554 // TODO(neis): Need proxies to test the situation where |
414 // [[preventExtensions]] returns false. | 555 // [[preventExtensions]] returns false. |
OLD | NEW |