OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 266 |
267 function TestKeyGet(obj) { | 267 function TestKeyGet(obj) { |
268 var obj2 = Object.create(obj) | 268 var obj2 = Object.create(obj) |
269 for (var i in symbols) { | 269 for (var i in symbols) { |
270 assertEquals(i|0, obj[symbols[i]]) | 270 assertEquals(i|0, obj[symbols[i]]) |
271 assertEquals(i|0, obj2[symbols[i]]) | 271 assertEquals(i|0, obj2[symbols[i]]) |
272 } | 272 } |
273 } | 273 } |
274 | 274 |
275 | 275 |
276 function TestKeyHas() { | 276 function TestKeyHas(obj) { |
277 for (var i in symbols) { | 277 for (var i in symbols) { |
278 assertTrue(symbols[i] in obj) | 278 assertTrue(symbols[i] in obj) |
279 assertTrue(Object.hasOwnProperty.call(obj, symbols[i])) | 279 assertTrue(Object.hasOwnProperty.call(obj, symbols[i])) |
280 } | 280 } |
281 } | 281 } |
282 | 282 |
283 | 283 |
284 function TestKeyEnum(obj) { | 284 function TestKeyEnum(obj) { |
285 for (var name in obj) { | 285 for (var name in obj) { |
286 assertEquals("string", typeof name) | 286 assertEquals("string", typeof name) |
287 } | 287 } |
288 } | 288 } |
289 | 289 |
290 | 290 |
291 function TestKeyNames(obj) { | 291 function TestKeyNames(obj) { |
292 assertEquals(0, Object.keys(obj).length) | 292 assertEquals(0, Object.keys(obj).length) |
293 | 293 |
294 var names = Object.getOwnPropertyNames(obj) | 294 var names = Object.getOwnPropertyNames(obj) |
295 for (var i in names) { | 295 for (var i in names) { |
296 assertEquals("string", typeof names[i]) | 296 assertEquals("string", typeof names[i]) |
297 } | 297 } |
298 } | 298 } |
299 | 299 |
300 | 300 |
| 301 function TestGetOwnPropertySymbols(obj) { |
| 302 var syms = Object.getOwnPropertySymbols(obj) |
| 303 assertEquals(syms.length, symbols.length) |
| 304 for (var i in syms) { |
| 305 assertEquals("symbol", typeof syms[i]) |
| 306 } |
| 307 } |
| 308 |
| 309 |
301 function TestKeyDescriptor(obj) { | 310 function TestKeyDescriptor(obj) { |
302 for (var i in symbols) { | 311 for (var i in symbols) { |
303 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); | 312 var desc = Object.getOwnPropertyDescriptor(obj, symbols[i]); |
304 assertEquals(i|0, desc.value) | 313 assertEquals(i|0, desc.value) |
305 assertTrue(desc.configurable) | 314 assertTrue(desc.configurable) |
306 assertEquals(i % 2 == 0, desc.writable) | 315 assertEquals(i % 2 == 0, desc.writable) |
307 assertEquals(i % 2 == 0, desc.enumerable) | 316 assertEquals(i % 2 == 0, desc.enumerable) |
308 assertEquals(i % 2 == 0, | 317 assertEquals(i % 2 == 0, |
309 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) | 318 Object.prototype.propertyIsEnumerable.call(obj, symbols[i])) |
310 } | 319 } |
(...skipping 13 matching lines...) Expand all Loading... |
324 var objs = [{}, [], Object.create(null), Object(1), new Map, function(){}] | 333 var objs = [{}, [], Object.create(null), Object(1), new Map, function(){}] |
325 | 334 |
326 for (var i in objs) { | 335 for (var i in objs) { |
327 var obj = objs[i] | 336 var obj = objs[i] |
328 TestKeySet(obj) | 337 TestKeySet(obj) |
329 TestKeyDefine(obj) | 338 TestKeyDefine(obj) |
330 TestKeyGet(obj) | 339 TestKeyGet(obj) |
331 TestKeyHas(obj) | 340 TestKeyHas(obj) |
332 TestKeyEnum(obj) | 341 TestKeyEnum(obj) |
333 TestKeyNames(obj) | 342 TestKeyNames(obj) |
| 343 TestGetOwnPropertySymbols(obj) |
334 TestKeyDescriptor(obj) | 344 TestKeyDescriptor(obj) |
335 TestKeyDelete(obj) | 345 TestKeyDelete(obj) |
336 } | 346 } |
337 | 347 |
338 | 348 |
339 function TestCachedKeyAfterScavenge() { | 349 function TestCachedKeyAfterScavenge() { |
340 gc(); | 350 gc(); |
341 // Keyed property lookup are cached. Hereby we assume that the keys are | 351 // Keyed property lookup are cached. Hereby we assume that the keys are |
342 // tenured, so that we only have to clear the cache between mark compacts, | 352 // tenured, so that we only have to clear the cache between mark compacts, |
343 // but not between scavenges. This must also apply for symbol keys. | 353 // but not between scavenges. This must also apply for symbol keys. |
344 var key = Symbol("key"); | 354 var key = Symbol("key"); |
345 var a = {}; | 355 var a = {}; |
346 a[key] = "abc"; | 356 a[key] = "abc"; |
347 | 357 |
348 for (var i = 0; i < 100000; i++) { | 358 for (var i = 0; i < 100000; i++) { |
349 a[key] += "a"; // Allocations cause a scavenge. | 359 a[key] += "a"; // Allocations cause a scavenge. |
350 } | 360 } |
351 } | 361 } |
352 TestCachedKeyAfterScavenge(); | 362 TestCachedKeyAfterScavenge(); |
| 363 |
| 364 |
| 365 function TestGetOwnPropertySymbolsWithProto() { |
| 366 // We need to be have fast properties to have insertion order for property |
| 367 // keys. The current limit is currently 30 properties. |
| 368 var syms = symbols.slice(0, 30); |
| 369 var proto = {} |
| 370 var object = Object.create(proto) |
| 371 for (var i = 0; i < syms.length; i++) { |
| 372 // Even on object, odd on proto. |
| 373 if (i % 2) { |
| 374 proto[syms[i]] = i |
| 375 } else { |
| 376 object[syms[i]] = i |
| 377 } |
| 378 } |
| 379 |
| 380 assertTrue(%HasFastProperties(object)); |
| 381 |
| 382 var objectOwnSymbols = Object.getOwnPropertySymbols(object) |
| 383 assertEquals(objectOwnSymbols.length, syms.length / 2) |
| 384 |
| 385 for (var i = 0; i < objectOwnSymbols.length; i++) { |
| 386 assertEquals(objectOwnSymbols[i], syms[i * 2]) |
| 387 } |
| 388 } |
| 389 TestGetOwnPropertySymbolsWithProto() |
| 390 |
| 391 |
| 392 function TestGetOwnPropertySymbolsWithPrivateSymbols() { |
| 393 var privateSymbol = %CreatePrivateSymbol("private") |
| 394 var publicSymbol = Symbol() |
| 395 var publicSymbol2 = Symbol() |
| 396 var obj = {} |
| 397 obj[publicSymbol] = 1 |
| 398 obj[privateSymbol] = 2 |
| 399 obj[publicSymbol2] = 3 |
| 400 var syms = Object.getOwnPropertySymbols(obj) |
| 401 assertEquals(syms, [publicSymbol, publicSymbol2]) |
| 402 } |
| 403 TestGetOwnPropertySymbolsWithPrivateSymbols() |
OLD | NEW |