Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 // Test computed properties syntax. | |
|
Michael Starzinger
2014/06/13 08:14:19
This file should definitely go into the "harmony"
wingo
2014/06/16 08:32:27
Done.
| |
| 6 | |
| 7 // Flags: --harmony-symbols --harmony-strings | |
| 8 | |
| 9 function assertObjectEquals(expected, actual) { | |
| 10 var expected_props = Object.getOwnPropertyNames(expected); | |
| 11 var actual_props = Object.getOwnPropertyNames(actual); | |
| 12 expected_props += Object.getOwnPropertySymbols(expected); | |
| 13 actual_props += Object.getOwnPropertySymbols(actual); | |
| 14 | |
| 15 assertEquals(expected_props.length, actual_props.length); | |
| 16 for (var i = 0; i < expected_props.length; i++) { | |
| 17 var prop = expected_props[i]; | |
| 18 assertEquals(prop, actual_props[i]); | |
| 19 assertEquals(expected[prop], actual[prop]); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 function preparse(str, strict) { | |
| 24 // Trigger the preparser by fooling it into thinking that the | |
| 25 // function is really big. | |
|
marja
2014/06/13 14:10:37
This is not lazily parsed (i.e., parsed with PrePa
| |
| 26 var buffer = '/* ' + ' '.repeat(4096) + '*/'; | |
| 27 var body = (strict ? '"use strict"; ' : '') + 'return ' + buffer + str; | |
| 28 return Function(body)(); | |
| 29 } | |
| 30 | |
| 31 function parse(str, strict) { | |
| 32 var indirect_eval = eval; | |
| 33 var body = (strict ? '"use strict"; ' : '') + '(' + str + ')'; | |
| 34 return indirect_eval(body); | |
| 35 } | |
| 36 | |
| 37 function test(expected, str) { | |
| 38 assertObjectEquals(expected, parse(str, true)); | |
| 39 assertObjectEquals(expected, parse(str, false)); | |
| 40 assertObjectEquals(expected, preparse(str, true)); | |
| 41 assertObjectEquals(expected, preparse(str, false)); | |
| 42 } | |
| 43 | |
| 44 var sym = Symbol(); | |
| 45 var expected_with_sym = {}; | |
| 46 expected_with_sym[sym] = 'ohai'; | |
| 47 | |
| 48 test({foo: 42}, "{['foo']: 42}"); | |
| 49 test({foo: 10}, "{['foo']: 42, foo: 10}"); | |
| 50 test({foo: 42}, "{foo: 10, ['foo']: 42}"); | |
| 51 test({foo: 42, bar: 35}, "{foo: 10, ['foo']: 42, bar: 35}"); | |
| 52 test({foo: 42, bar: 35}, "{foo: 10, bar: 35, ['foo']: 42}"); | |
| 53 test({bar: 35, foo: 42}, "{bar: 35, foo: 10, ['foo']: 42}"); | |
| 54 test({bar: 35, foo: 10}, "{bar: 35, ['foo']: 42, foo: 10}"); | |
| 55 test({bar: 35, foo: 10}, "{['bar']: 35, ['foo']: 42, foo: 10}"); | |
| 56 test({bar: 35, foo: 42}, "{['bar']: 35, foo: 10, ['foo']: 42}"); | |
| 57 test({foo: 42}, "{foo: 10, [1 ? 'foo' : 'bar']: 42}"); | |
| 58 test({foo: 10, bar: 42}, "{foo: 10, [0 ? 'foo' : 'bar']: 42}"); | |
| 59 test(expected_with_sym, "{[sym]: 'ohai'}"); | |
| 60 test(expected_with_sym, "{[1 ? sym : 'foo']: 'ohai'}"); | |
| 61 test({foo: 'ohai'}, "{[0 ? sym : 'foo']: 'ohai'}"); | |
| 62 test({foo: 42}, "{['foo']: 10, get foo() { return 42 }}"); | |
| 63 test({foo: 10}, "{get foo() { return 42 }, ['foo']: 10}"); | |
| 64 | |
| 65 var test_proto = {}; | |
| 66 var test_obj = {['__proto__']: test_proto}; | |
| 67 assertEquals(Object.prototype, Object.getPrototypeOf(test_obj)); | |
| 68 assertTrue(test_obj.hasOwnProperty('__proto__')); | |
| 69 assertEquals(test_proto, test_obj.__proto__); | |
| OLD | NEW |