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