js/src/jit-test/tests/basic/splice-check-steps.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Check the order of splice's internal operations, because the ordering is
michael@0 3 * visible externally.
michael@0 4 */
michael@0 5
michael@0 6 function handlerMaker(obj, expected_exceptions) {
michael@0 7 var order = [];
michael@0 8 function note(trap, name)
michael@0 9 {
michael@0 10 order.push(trap + '-' + name);
michael@0 11 if (expected_exceptions[trap] === name) {
michael@0 12 throw ("fail");
michael@0 13 }
michael@0 14 }
michael@0 15
michael@0 16 return [{
michael@0 17 /* this is the only trap we care about */
michael@0 18 delete: function(name) {
michael@0 19 note("del", name);
michael@0 20 return delete obj[name];
michael@0 21 },
michael@0 22
michael@0 23 // Fundamental traps
michael@0 24 getOwnPropertyDescriptor: function(name) {
michael@0 25 var desc = Object.getOwnPropertyDescriptor(obj, name);
michael@0 26 // a trapping proxy's properties must always be configurable
michael@0 27 if (desc !== undefined)
michael@0 28 desc.configurable = true;
michael@0 29 return desc;
michael@0 30 },
michael@0 31 getPropertyDescriptor: function(name) {
michael@0 32 var desc = Object.getPropertyDescriptor(obj, name); // not in ES5
michael@0 33 // a trapping proxy's properties must always be configurable
michael@0 34 if (desc !== undefined)
michael@0 35 desc.configurable = true;
michael@0 36 return desc;
michael@0 37 },
michael@0 38 getOwnPropertyNames: function() {
michael@0 39 return Object.getOwnPropertyNames(obj);
michael@0 40 },
michael@0 41 getPropertyNames: function() {
michael@0 42 return Object.getPropertyNames(obj); // not in ES5
michael@0 43 },
michael@0 44 defineProperty: function(name, desc) {
michael@0 45 note("def", name);
michael@0 46 Object.defineProperty(obj, name, desc);
michael@0 47 },
michael@0 48 fix: function() {
michael@0 49 if (Object.isFrozen(obj)) {
michael@0 50 return Object.getOwnPropertyNames(obj).map(function(name) {
michael@0 51 return Object.getOwnPropertyDescriptor(obj, name);
michael@0 52 });
michael@0 53 }
michael@0 54 // As long as obj is not frozen, the proxy won't allow itself to be fixed
michael@0 55 return undefined; // will cause a TypeError to be thrown
michael@0 56 },
michael@0 57
michael@0 58 // derived traps
michael@0 59 has: function(name) {
michael@0 60 note("has", name);
michael@0 61 return name in obj;
michael@0 62 },
michael@0 63 hasOwn: function(name) { return Object.prototype.hasOwnProperty.call(obj, name); },
michael@0 64 get: function(receiver, name) {
michael@0 65 note("get", name);
michael@0 66 return obj[name];
michael@0 67 },
michael@0 68 set: function(receiver, name, val) {
michael@0 69 note("set", name);
michael@0 70 obj[name] = val;
michael@0 71 return true; // bad behavior when set fails in non-strict mode
michael@0 72 },
michael@0 73 enumerate: function() {
michael@0 74 var result = [];
michael@0 75 for (name in obj)
michael@0 76 result.push(name);
michael@0 77 return result;
michael@0 78 },
michael@0 79 keys: function() { return Object.keys(obj) }
michael@0 80 }, order];
michael@0 81 }
michael@0 82
michael@0 83 // arr: the array to splice
michael@0 84 // expected_order: the expected order of operations on arr, stringified
michael@0 85 function check_splice_proxy(arr, expected_order, expected_exceptions, expected_array, expected_result) {
michael@0 86 print (arr);
michael@0 87 var [handler, store] = handlerMaker(arr, expected_exceptions);
michael@0 88 var proxy = Proxy.create(handler);
michael@0 89
michael@0 90 try {
michael@0 91 var args = Array.prototype.slice.call(arguments, 5);
michael@0 92 var result = Array.prototype.splice.apply(proxy, args);
michael@0 93 assertEq(Object.keys(expected_exceptions).length, 0);
michael@0 94 } catch (e) {
michael@0 95 assertEq(Object.keys(expected_exceptions).length > 0, true);
michael@0 96 }
michael@0 97
michael@0 98 // check the order of the property accesses, etc
michael@0 99 assertEq(store.toString(), expected_order);
michael@0 100
michael@0 101 // The deleted elements are returned in an object that's always an Array.
michael@0 102 assertEq(Array.isArray(result) || result === undefined, true);
michael@0 103
michael@0 104 // check the return value
michael@0 105 for (var i in expected_result) {
michael@0 106 assertEq(result[i], expected_result[i]);
michael@0 107 }
michael@0 108 for (var i in result) {
michael@0 109 assertEq(result[i], expected_result[i]);
michael@0 110 }
michael@0 111
michael@0 112 // check the value of arr
michael@0 113 for (var i in expected_array) {
michael@0 114 assertEq(arr[i], expected_array[i]);
michael@0 115 }
michael@0 116 for (var i in arr) {
michael@0 117 assertEq(arr[i], expected_array[i]);
michael@0 118 }
michael@0 119
michael@0 120 return result;
michael@0 121 }
michael@0 122
michael@0 123 // Shrinking array
michael@0 124 check_splice_proxy(
michael@0 125 [10,1,2,3,4,5],
michael@0 126 "get-length," +
michael@0 127 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 128 "has-3,get-3,set-0,has-4,get-4,set-1,has-5,get-5,set-2," +
michael@0 129 "del-5,del-4,del-3," +
michael@0 130 "set-length",
michael@0 131 {},
michael@0 132 [3,4,5],
michael@0 133 [10,1,2],
michael@0 134 0, 3
michael@0 135 );
michael@0 136
michael@0 137 // Growing array
michael@0 138 check_splice_proxy(
michael@0 139 [11,1,2,3,4,5],
michael@0 140 "get-length," +
michael@0 141 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 142 "has-5,get-5,set-9,has-4,get-4,set-8,has-3,get-3,set-7," +
michael@0 143 "set-0,set-1,set-2,set-3,set-4,set-5,set-6," +
michael@0 144 "set-length",
michael@0 145 {},
michael@0 146 [9,9,9,9,9,9,9,3,4,5],
michael@0 147 [11,1,2],
michael@0 148 0, 3, 9, 9, 9, 9, 9, 9, 9
michael@0 149 );
michael@0 150
michael@0 151 // Same sized array
michael@0 152 check_splice_proxy(
michael@0 153 [12,1,2,3,4,5],
michael@0 154 "get-length," +
michael@0 155 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 156 "set-0,set-1,set-2," +
michael@0 157 "set-length",
michael@0 158 {},
michael@0 159 [9,9,9,3,4,5],
michael@0 160 [12,1,2],
michael@0 161 0, 3, 9, 9, 9
michael@0 162 );
michael@0 163
michael@0 164
michael@0 165 /*
michael@0 166 * Check that if we fail at a particular step in the algorithm, we don't
michael@0 167 * continue with the algorithm beyond that step.
michael@0 168 */
michael@0 169
michael@0 170
michael@0 171 // Step 3: fail when getting length
michael@0 172 check_splice_proxy(
michael@0 173 [13,1,2,3,4,5],
michael@0 174 "get-length",
michael@0 175 {get: 'length'},
michael@0 176 [13,1,2,3,4,5],
michael@0 177 undefined,
michael@0 178 0, 3, 9, 9, 9
michael@0 179 );
michael@0 180
michael@0 181 // Step 9b: fail when [[HasProperty]]
michael@0 182 check_splice_proxy(
michael@0 183 [14,1,2,3,4,5],
michael@0 184 "get-length," +
michael@0 185 "has-0,get-0,has-1",
michael@0 186 {has: '1'},
michael@0 187 [14,1,2,3,4,5],
michael@0 188 undefined,
michael@0 189 0, 3, 9, 9, 9
michael@0 190 );
michael@0 191
michael@0 192 // Step 9c(i): fail when [[Get]]
michael@0 193 check_splice_proxy(
michael@0 194 [15,1,2,3,4,5],
michael@0 195 "get-length," +
michael@0 196 "has-0,get-0,has-1,get-1",
michael@0 197 {get: '1'},
michael@0 198 [15,1,2,3,4,5],
michael@0 199 undefined,
michael@0 200 0, 3, 9, 9, 9
michael@0 201 );
michael@0 202
michael@0 203 // Step 12b(iii): fail when [[HasProperty]]
michael@0 204 check_splice_proxy(
michael@0 205 [16,1,2,3,4,5],
michael@0 206 "get-length," +
michael@0 207 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 208 "has-3,get-3,set-0,has-4",
michael@0 209 {has: '4'},
michael@0 210 [3,1,2,3,4,5],
michael@0 211 undefined,
michael@0 212 0, 3
michael@0 213 );
michael@0 214
michael@0 215
michael@0 216 // Step 12b(iv)1: fail when [[Get]]
michael@0 217 check_splice_proxy(
michael@0 218 [17,1,2,3,4,5],
michael@0 219 "get-length," +
michael@0 220 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 221 "has-3,get-3,set-0,has-4,get-4",
michael@0 222 {get: '4'},
michael@0 223 [3,1,2,3,4,5],
michael@0 224 undefined,
michael@0 225 0, 3
michael@0 226 );
michael@0 227
michael@0 228
michael@0 229 // Step 12b(iv)2: fail when [[Put]]
michael@0 230 check_splice_proxy(
michael@0 231 [18,1,2,3,4,5],
michael@0 232 "get-length," +
michael@0 233 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 234 "has-3,get-3,set-0,has-4,get-4,set-1",
michael@0 235 {set: '1'},
michael@0 236 [3,1,2,3,4,5],
michael@0 237 undefined,
michael@0 238 0, 3
michael@0 239 );
michael@0 240
michael@0 241 // Step 12b(v)1: fail when [[Delete]]
michael@0 242 check_splice_proxy(
michael@0 243 [19,1,2,3,,5],
michael@0 244 "get-length," +
michael@0 245 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 246 "has-3,get-3,set-0,has-4,del-1",
michael@0 247 {del: '1'},
michael@0 248 [3,1,2,3,,5],
michael@0 249 undefined,
michael@0 250 0, 3
michael@0 251 );
michael@0 252
michael@0 253 // Step 12d(i): fail when [[Delete]]
michael@0 254 check_splice_proxy(
michael@0 255 [20,1,2,3,4,5],
michael@0 256 "get-length," +
michael@0 257 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 258 "has-3,get-3,set-0,has-4,get-4,set-1,has-5,get-5,set-2," +
michael@0 259 "del-5,del-4",
michael@0 260 {del: '4'},
michael@0 261 [3,4,5,3,4],
michael@0 262 undefined,
michael@0 263 0, 3
michael@0 264 );
michael@0 265
michael@0 266 // Step 13b(iii): fail when [[HasProperty]]
michael@0 267 check_splice_proxy(
michael@0 268 [21,1,2,3,4,5],
michael@0 269 "get-length," +
michael@0 270 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 271 "has-5,get-5,set-8,has-4",
michael@0 272 {has: '4'},
michael@0 273 [21,1,2,3,4,5,,,5],
michael@0 274 undefined,
michael@0 275 0, 3, 9,9,9,9,9,9
michael@0 276 );
michael@0 277
michael@0 278
michael@0 279 // Step 13b(iv)1: fail when [[Get]]
michael@0 280 check_splice_proxy(
michael@0 281 [22,1,2,3,4,5],
michael@0 282 "get-length," +
michael@0 283 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 284 "has-5,get-5,set-8,has-4,get-4",
michael@0 285 {get: '4'},
michael@0 286 [22,1,2,3,4,5,,,5],
michael@0 287 undefined,
michael@0 288 0, 3, 9,9,9,9,9,9
michael@0 289 );
michael@0 290
michael@0 291
michael@0 292 // Step 13b(iv)2: fail when [[Put]]
michael@0 293 check_splice_proxy(
michael@0 294 [23,1,2,3,4,5],
michael@0 295 "get-length," +
michael@0 296 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 297 "has-5,get-5,set-8,has-4,get-4,set-7",
michael@0 298 {set: '7'},
michael@0 299 [23,1,2,3,4,5,,,5],
michael@0 300 undefined,
michael@0 301 0, 3, 9,9,9,9,9,9
michael@0 302 );
michael@0 303
michael@0 304 // Step 13b(v)1: fail when [[Delete]]
michael@0 305 check_splice_proxy(
michael@0 306 [24,1,2,3,,5],
michael@0 307 "get-length," +
michael@0 308 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 309 "has-5,get-5,set-8,has-4,del-7",
michael@0 310 {del: '7'},
michael@0 311 [24,1,2,3,,5,,,5],
michael@0 312 undefined,
michael@0 313 0, 3, 9,9,9,9,9,9
michael@0 314 );
michael@0 315
michael@0 316 // Step 15b: fail when [[Put]]
michael@0 317 check_splice_proxy(
michael@0 318 [25,1,2,3,4,5],
michael@0 319 "get-length," +
michael@0 320 "has-0,get-0,has-1,get-1,has-2,get-2," +
michael@0 321 "has-5,get-5,set-8,has-4,get-4,set-7,has-3,get-3,set-6," +
michael@0 322 "set-0,set-1,set-2",
michael@0 323 {set: '2'},
michael@0 324 [9,9,2,3,4,5,3,4,5],
michael@0 325 undefined,
michael@0 326 0, 3, 9,9,9,9,9,9
michael@0 327 );

mercurial