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 // Flags: --harmony-object-literals --allow-natives-syntax |
| 6 |
| 7 |
| 8 (function TestDescriptor() { |
| 9 var object = { |
| 10 method() { |
| 11 return 42; |
| 12 } |
| 13 }; |
| 14 assertEquals(42, object.method()); |
| 15 })(); |
| 16 |
| 17 |
| 18 (function TestDescriptor() { |
| 19 var object = { |
| 20 method() { |
| 21 return 42; |
| 22 } |
| 23 }; |
| 24 |
| 25 var desc = Object.getOwnPropertyDescriptor(object, 'method'); |
| 26 assertTrue(desc.enumerable); |
| 27 assertTrue(desc.configurable); |
| 28 assertTrue(desc.writable); |
| 29 assertEquals('function', typeof desc.value); |
| 30 |
| 31 assertEquals(42, desc.value()); |
| 32 })(); |
| 33 |
| 34 |
| 35 (function TestProto() { |
| 36 var object = { |
| 37 method() { |
| 38 return 42; |
| 39 } |
| 40 }; |
| 41 |
| 42 assertEquals(Function.prototype, Object.getPrototypeOf(object.method)); |
| 43 })(); |
| 44 |
| 45 |
| 46 (function TestNotConstructable() { |
| 47 var object = { |
| 48 method() { |
| 49 return 42; |
| 50 } |
| 51 }; |
| 52 |
| 53 assertThrows(function() { |
| 54 new object.method; |
| 55 }); |
| 56 })(); |
| 57 |
| 58 |
| 59 (function TestFunctionName() { |
| 60 var object = { |
| 61 method() { |
| 62 return 42; |
| 63 }, |
| 64 1() {}, |
| 65 // https://code.google.com/p/v8/issues/detail?id=3507 |
| 66 // 2.0() {} |
| 67 }; |
| 68 var f = object.method; |
| 69 assertEquals('method', f.name); |
| 70 var g = object[1]; |
| 71 assertEquals('1', g.name); |
| 72 |
| 73 // https://code.google.com/p/v8/issues/detail?id=3507 |
| 74 // var h = object[2]; |
| 75 // assertEquals('2', h.name); |
| 76 })(); |
| 77 |
| 78 |
| 79 (function TestNoBinding() { |
| 80 var method = 'local'; |
| 81 var calls = 0; |
| 82 var object = { |
| 83 method() { |
| 84 calls++; |
| 85 assertEquals('local', method); |
| 86 } |
| 87 }; |
| 88 object.method(); |
| 89 assertEquals(1, calls); |
| 90 })(); |
| 91 |
| 92 |
| 93 (function TestNoPrototype() { |
| 94 var object = { |
| 95 method() { |
| 96 return 42; |
| 97 } |
| 98 }; |
| 99 var f = object.method; |
| 100 assertFalse(f.hasOwnProperty('prototype')); |
| 101 assertEquals(undefined, f.prototype); |
| 102 |
| 103 f.prototype = 42; |
| 104 assertEquals(42, f.prototype); |
| 105 })(); |
| 106 |
| 107 |
| 108 (function TestKeywordAsName() { |
| 109 var keywords = [ |
| 110 'await', |
| 111 'break', |
| 112 'case', |
| 113 'catch', |
| 114 'class', |
| 115 'const', |
| 116 'continue', |
| 117 'debugger', |
| 118 'default', |
| 119 'delete', |
| 120 'do', |
| 121 'else', |
| 122 'enum', |
| 123 'export', |
| 124 'extends', |
| 125 'finally', |
| 126 'for', |
| 127 'function', |
| 128 'if', |
| 129 'implements', |
| 130 'import', |
| 131 'in', |
| 132 'instanceof', |
| 133 'interface', |
| 134 'new', |
| 135 'package', |
| 136 'private', |
| 137 'protected', |
| 138 'public', |
| 139 'return', |
| 140 'static', |
| 141 'super', |
| 142 'switch', |
| 143 'this', |
| 144 'throw', |
| 145 'try', |
| 146 'typeof', |
| 147 'var', |
| 148 'void', |
| 149 'while', |
| 150 'with', |
| 151 'yield', |
| 152 ]; |
| 153 |
| 154 for (var kw of keywords) { |
| 155 assertEquals(kw, |
| 156 eval('({' + kw + '() { return "' + kw + '"; }}).' + kw + '()')); |
| 157 } |
| 158 })(); |
| 159 |
| 160 |
| 161 (function TestToString() { |
| 162 var object = { |
| 163 method() { 42; } |
| 164 }; |
| 165 assertEquals('method() { 42; }', object.method.toString()); |
| 166 })(); |
| 167 |
| 168 |
| 169 (function TestOptimized() { |
| 170 var object = { |
| 171 method() { return 42; } |
| 172 }; |
| 173 assertEquals(42, object.method()); |
| 174 assertEquals(42, object.method()); |
| 175 %OptimizeFunctionOnNextCall(object.method); |
| 176 assertEquals(42, object.method()); |
| 177 assertFalse(object.method.hasOwnProperty('prototype')); |
| 178 })(); |
OLD | NEW |