OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --strong-mode --allow-natives-syntax |
| 6 |
| 7 // TODO(conradw): Track implementation of strong bit for other objects, add |
| 8 // tests. |
| 9 |
| 10 function getObjects() { |
| 11 "use strict"; |
| 12 return [ |
| 13 {}, |
| 14 [], |
| 15 (function(){}), |
| 16 (class Foo {}) |
| 17 ]; |
| 18 } |
| 19 |
| 20 function readFromObjectSloppy(o) { |
| 21 return o.foo; |
| 22 } |
| 23 |
| 24 function readFromObjectKeyedSloppy(o) { |
| 25 return o["foo"]; |
| 26 } |
| 27 |
| 28 function readFromObjectKeyedVarSloppy(o) { |
| 29 var a = "foo"; |
| 30 return o[a]; |
| 31 } |
| 32 |
| 33 function readFromObjectKeyedComputedSloppy(o) { |
| 34 var a = "o"; |
| 35 return o["fo" + a]; |
| 36 } |
| 37 |
| 38 function readFromObjectStrong(o) { |
| 39 "use strong"; |
| 40 return o.foo; |
| 41 } |
| 42 |
| 43 function readFromObjectKeyedStrong(o) { |
| 44 "use strong"; |
| 45 return o["foo"]; |
| 46 } |
| 47 |
| 48 function readFromObjectKeyedLetStrong(o) { |
| 49 "use strong"; |
| 50 let a = "foo"; |
| 51 return o[a]; |
| 52 } |
| 53 |
| 54 function readFromObjectKeyedComputedStrong(o) { |
| 55 "use strong"; |
| 56 let a = "o"; |
| 57 return o["fo" + a]; |
| 58 } |
| 59 |
| 60 function getDescs(x) { |
| 61 return [ |
| 62 {value: x}, |
| 63 {configurable: true, value: x}, |
| 64 {configurable: true, enumerable: true, writable: true, value: x}, |
| 65 {configurable: true, enumerable: true, get: (function() {return x}) }, |
| 66 ]; |
| 67 } |
| 68 |
| 69 function assertStrongSemantics(func, object) { |
| 70 %DeoptimizeFunction(func); |
| 71 %ClearFunctionTypeFeedback(func); |
| 72 assertThrows(function(){func(object)}, TypeError); |
| 73 assertThrows(function(){func(object)}, TypeError); |
| 74 assertThrows(function(){func(object)}, TypeError); |
| 75 %OptimizeFunctionOnNextCall(func); |
| 76 assertThrows(function(){func(object)}, TypeError); |
| 77 %DeoptimizeFunction(func); |
| 78 assertThrows(function(){func(object)}, TypeError); |
| 79 } |
| 80 |
| 81 function assertSloppySemantics(func, object) { |
| 82 %DeoptimizeFunction(func); |
| 83 %ClearFunctionTypeFeedback(func); |
| 84 assertDoesNotThrow(function(){func(object)}); |
| 85 assertDoesNotThrow(function(){func(object)}); |
| 86 assertDoesNotThrow(function(){func(object)}); |
| 87 %OptimizeFunctionOnNextCall(func); |
| 88 assertDoesNotThrow(function(){func(object)}); |
| 89 %DeoptimizeFunction(func); |
| 90 assertDoesNotThrow(function(){func(object)}); |
| 91 } |
| 92 |
| 93 (function () { |
| 94 "use strict"; |
| 95 |
| 96 let goodKeys = [ |
| 97 "foo" |
| 98 ] |
| 99 |
| 100 let badKeys = [ |
| 101 "bar", |
| 102 "1", |
| 103 "100001", |
| 104 "3000000001", |
| 105 "5000000001" |
| 106 ]; |
| 107 |
| 108 let values = [ |
| 109 "string", |
| 110 1, |
| 111 100001, |
| 112 30000000001, |
| 113 50000000001, |
| 114 NaN, |
| 115 {}, |
| 116 undefined |
| 117 ]; |
| 118 |
| 119 let badAccessorDescs = [ |
| 120 { set: (function(){}) }, |
| 121 { configurable: true, set: (function(){}) }, |
| 122 { configurable: true, enumerable: true, set: (function(){}) } |
| 123 ]; |
| 124 |
| 125 let readSloppy = [ |
| 126 readFromObjectSloppy, |
| 127 readFromObjectKeyedSloppy, |
| 128 readFromObjectKeyedVarSloppy, |
| 129 readFromObjectKeyedComputedSloppy |
| 130 ]; |
| 131 |
| 132 let readStrong = [ |
| 133 readFromObjectStrong, |
| 134 readFromObjectKeyedStrong, |
| 135 readFromObjectKeyedLetStrong, |
| 136 readFromObjectKeyedComputedStrong |
| 137 ]; |
| 138 |
| 139 let dummyProto = {}; |
| 140 for (let key of goodKeys) { |
| 141 Object.defineProperty(dummyProto, key, { value: undefined }); |
| 142 } |
| 143 |
| 144 let dummyAccessorProto = {}; |
| 145 for (let key of goodKeys) { |
| 146 Object.defineProperty(dummyAccessorProto, key, { set: (function(){}) }) |
| 147 } |
| 148 |
| 149 // Attempting to access a property on an objects with no defined properties |
| 150 // should throw. |
| 151 for (let object of getObjects()) { |
| 152 for (let func of readStrong) { |
| 153 assertStrongSemantics(func, object); |
| 154 } |
| 155 for (let func of readSloppy) { |
| 156 assertSloppySemantics(func, object); |
| 157 } |
| 158 // Accessing a property which is on the prototype chain of the object should |
| 159 // not throw. |
| 160 object.__proto__ = dummyProto; |
| 161 for (let key of goodKeys) { |
| 162 for (let func of readStrong.concat(readSloppy)) { |
| 163 assertSloppySemantics(func, object); |
| 164 } |
| 165 } |
| 166 } |
| 167 // Properties with accessor descriptors missing 'get' should throw on access. |
| 168 for (let desc of badAccessorDescs) { |
| 169 for (let key of goodKeys) { |
| 170 for (let object of getObjects()) { |
| 171 Object.defineProperty(object, key, desc); |
| 172 for (let func of readStrong) { |
| 173 assertStrongSemantics(func, object); |
| 174 } |
| 175 for (let func of readSloppy) { |
| 176 assertSloppySemantics(func, object); |
| 177 } |
| 178 } |
| 179 } |
| 180 } |
| 181 // The same behaviour should be expected for bad accessor properties on the |
| 182 // prototype chain. |
| 183 for (let object of getObjects()) { |
| 184 object.__proto__ = dummyAccessorProto; |
| 185 for (let func of readStrong) { |
| 186 assertStrongSemantics(func, object); |
| 187 } |
| 188 for (let func of readSloppy) { |
| 189 assertSloppySemantics(func, object); |
| 190 } |
| 191 } |
| 192 })(); |
OLD | NEW |