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. |
| 6 |
| 7 // Flags: --harmony-object-literals --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. |
| 26 var buffer = '/* ' + ' '.repeat(4096) + '*/'; |
| 27 var body = (strict ? '"use strict"; ' : '') + 'return ' + buffer + str; |
| 28 var indirect_eval = eval; |
| 29 var f = 'function preparsed_function() {' + body + '}' |
| 30 indirect_eval(f); |
| 31 return preparsed_function(); |
| 32 } |
| 33 |
| 34 function parse(str, strict) { |
| 35 var indirect_eval = eval; |
| 36 var body = (strict ? '"use strict"; ' : '') + '(' + str + ')'; |
| 37 return indirect_eval(body); |
| 38 } |
| 39 |
| 40 function test(expected, str) { |
| 41 assertObjectEquals(expected, parse(str, true)); |
| 42 assertObjectEquals(expected, parse(str, false)); |
| 43 assertObjectEquals(expected, preparse(str, true)); |
| 44 assertObjectEquals(expected, preparse(str, false)); |
| 45 } |
| 46 |
| 47 var sym = Symbol(); |
| 48 var expected_with_sym = {}; |
| 49 expected_with_sym[sym] = 'ohai'; |
| 50 |
| 51 test({foo: 42}, "{['foo']: 42}"); |
| 52 test({foo: 10}, "{['foo']: 42, foo: 10}"); |
| 53 test({foo: 42}, "{foo: 10, ['foo']: 42}"); |
| 54 test({foo: 42, bar: 35}, "{foo: 10, ['foo']: 42, bar: 35}"); |
| 55 test({foo: 42, bar: 35}, "{foo: 10, bar: 35, ['foo']: 42}"); |
| 56 test({bar: 35, foo: 42}, "{bar: 35, foo: 10, ['foo']: 42}"); |
| 57 test({bar: 35, foo: 10}, "{bar: 35, ['foo']: 42, foo: 10}"); |
| 58 test({bar: 35, foo: 10}, "{['bar']: 35, ['foo']: 42, foo: 10}"); |
| 59 test({bar: 35, foo: 42}, "{['bar']: 35, foo: 10, ['foo']: 42}"); |
| 60 test({foo: 42}, "{foo: 10, [1 ? 'foo' : 'bar']: 42}"); |
| 61 test({foo: 10, bar: 42}, "{foo: 10, [0 ? 'foo' : 'bar']: 42}"); |
| 62 test(expected_with_sym, "{[sym]: 'ohai'}"); |
| 63 test(expected_with_sym, "{[1 ? sym : 'foo']: 'ohai'}"); |
| 64 test({foo: 'ohai'}, "{[0 ? sym : 'foo']: 'ohai'}"); |
| 65 test({foo: 42}, "{['foo']: 10, get foo() { return 42 }}"); |
| 66 test({foo: 10}, "{get foo() { return 42 }, ['foo']: 10}"); |
| 67 test({0: 10}, "{0: 42, [0]: 10}"); |
| 68 test({0: 10}, "{[0]: 42, 0: 10}"); |
| 69 |
| 70 var test_proto = {}; |
| 71 var test_obj = {['__proto__']: test_proto}; |
| 72 assertEquals(Object.prototype, Object.getPrototypeOf(test_obj)); |
| 73 assertTrue(test_obj.hasOwnProperty('__proto__')); |
| 74 assertEquals(test_proto, test_obj.__proto__); |
OLD | NEW |