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