json_worker.js (6808B)
1 /** 2 * Any copyright is dedicated to the Public Domain. 3 * http://creativecommons.org/publicdomain/zero/1.0/ 4 */ 5 var cyclicalObject = {}; 6 cyclicalObject.foo = cyclicalObject; 7 8 var cyclicalArray = []; 9 cyclicalArray.push(cyclicalArray); 10 11 function makeNested(obj, count) { 12 var innermostobj; 13 for (var i = 0; i < count; i++) { 14 obj.foo = { bar: 5 }; 15 innermostobj = obj.foo; 16 obj = innermostobj; 17 } 18 return innermostobj; 19 } 20 21 var nestedObject = {}; 22 makeNested(nestedObject, 100); 23 24 var cyclicalObject = {}; 25 var innermost = makeNested(cyclicalObject, 1000); 26 innermost.baz = cyclicalObject; 27 28 var objectWithSaneGetter = {}; 29 objectWithSaneGetter.__defineGetter__("foo", function () { 30 return 5; 31 }); 32 33 // We don't walk prototype chains for cloning so this won't actually do much... 34 function objectWithSaneGetter2() {} 35 objectWithSaneGetter2.prototype = { 36 get foo() { 37 return 5; 38 }, 39 }; 40 41 const throwingGetterThrownString = "bad"; 42 43 var objectWithThrowingGetter = {}; 44 objectWithThrowingGetter.__defineGetter__("foo", function () { 45 throw throwingGetterThrownString; 46 }); 47 48 var typedArrayWithValues = new Int8Array(5); 49 for (let index in typedArrayWithValues) { 50 typedArrayWithValues[index] = index; 51 } 52 53 var typedArrayWithFunBuffer = new Int8Array(4); 54 for (let index in typedArrayWithFunBuffer) { 55 typedArrayWithFunBuffer[index] = 255; 56 } 57 58 var typedArrayWithFunBuffer2 = new Int32Array(typedArrayWithFunBuffer.buffer); 59 60 var xhr = new XMLHttpRequest(); 61 62 var messages = [ 63 { 64 type: "object", 65 value: {}, 66 jsonValue: "{}", 67 }, 68 { 69 type: "object", 70 value: { foo: "bar" }, 71 jsonValue: '{"foo":"bar"}', 72 }, 73 { 74 type: "object", 75 value: { foo: "bar", foo2: { bee: "bop" } }, 76 jsonValue: '{"foo":"bar","foo2":{"bee":"bop"}}', 77 }, 78 { 79 type: "object", 80 value: { foo: "bar", foo2: { bee: "bop" }, foo3: "baz" }, 81 jsonValue: '{"foo":"bar","foo2":{"bee":"bop"},"foo3":"baz"}', 82 }, 83 { 84 type: "object", 85 value: { foo: "bar", foo2: [1, 2, 3] }, 86 jsonValue: '{"foo":"bar","foo2":[1,2,3]}', 87 }, 88 { 89 type: "object", 90 value: cyclicalObject, 91 }, 92 { 93 type: "object", 94 value: [null, 2, false, cyclicalObject], 95 }, 96 { 97 type: "object", 98 value: cyclicalArray, 99 }, 100 { 101 type: "object", 102 value: { foo: 1, bar: cyclicalArray }, 103 }, 104 { 105 type: "object", 106 value: nestedObject, 107 jsonValue: JSON.stringify(nestedObject), 108 }, 109 { 110 type: "object", 111 value: cyclicalObject, 112 }, 113 { 114 type: "object", 115 value: objectWithSaneGetter, 116 jsonValue: '{"foo":5}', 117 }, 118 { 119 type: "object", 120 value: new objectWithSaneGetter2(), 121 jsonValue: "{}", 122 }, 123 { 124 type: "object", 125 value: objectWithThrowingGetter, 126 exception: true, 127 }, 128 { 129 type: "object", 130 array: true, 131 value: [9, 8, 7], 132 jsonValue: "[9,8,7]", 133 }, 134 { 135 type: "object", 136 array: true, 137 value: [9, false, 10.5, { foo: "bar" }], 138 jsonValue: '[9,false,10.5,{"foo":"bar"}]', 139 }, 140 { 141 type: "object", 142 shouldEqual: true, 143 value: null, 144 }, 145 { 146 type: "undefined", 147 shouldEqual: true, 148 value: undefined, 149 }, 150 { 151 type: "string", 152 shouldEqual: true, 153 value: "Hello", 154 }, 155 { 156 type: "string", 157 shouldEqual: true, 158 value: JSON.stringify({ foo: "bar" }), 159 compareValue: '{"foo":"bar"}', 160 }, 161 { 162 type: "number", 163 shouldEqual: true, 164 value: 1, 165 }, 166 { 167 type: "number", 168 shouldEqual: true, 169 value: 0, 170 }, 171 { 172 type: "number", 173 shouldEqual: true, 174 value: -1, 175 }, 176 { 177 type: "number", 178 shouldEqual: true, 179 value: 12345678901234567000, 180 }, 181 { 182 type: "number", 183 shouldEqual: true, 184 value: -12345678901234567000, 185 }, 186 { 187 type: "number", 188 shouldEqual: true, 189 value: 0.25, 190 }, 191 { 192 type: "number", 193 shouldEqual: true, 194 value: -0.25, 195 }, 196 { 197 type: "boolean", 198 shouldEqual: true, 199 value: true, 200 }, 201 { 202 type: "boolean", 203 shouldEqual: true, 204 value: false, 205 }, 206 { 207 type: "object", 208 value(foo) { 209 return "Bad!"; 210 }, 211 exception: true, 212 }, 213 { 214 type: "number", 215 isNaN: true, 216 value: NaN, 217 }, 218 { 219 type: "number", 220 isInfinity: true, 221 value: Infinity, 222 }, 223 { 224 type: "number", 225 isNegativeInfinity: true, 226 value: -Infinity, 227 }, 228 { 229 type: "object", 230 value: new Int32Array(10), 231 jsonValue: '{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0}', 232 }, 233 { 234 type: "object", 235 value: new Float32Array(5), 236 jsonValue: '{"0":0,"1":0,"2":0,"3":0,"4":0}', 237 }, 238 { 239 type: "object", 240 value: typedArrayWithValues, 241 jsonValue: '{"0":0,"1":1,"2":2,"3":3,"4":4}', 242 }, 243 { 244 type: "number", 245 value: typedArrayWithValues[2], 246 compareValue: 2, 247 shouldEqual: true, 248 }, 249 { 250 type: "object", 251 value: typedArrayWithValues.buffer, 252 jsonValue: "{}", 253 }, 254 { 255 type: "object", 256 value: typedArrayWithFunBuffer2, 257 jsonValue: '{"0":-1}', 258 }, 259 { 260 type: "object", 261 value: { foo: typedArrayWithFunBuffer2 }, 262 jsonValue: '{"foo":{"0":-1}}', 263 }, 264 { 265 type: "object", 266 value: [typedArrayWithFunBuffer2], 267 jsonValue: '[{"0":-1}]', 268 }, 269 { 270 type: "object", 271 value: { 272 foo(a) { 273 alert(b); 274 }, 275 }, 276 exception: true, 277 }, 278 { 279 type: "object", 280 value: xhr, 281 exception: true, 282 }, 283 { 284 type: "number", 285 value: xhr.readyState, 286 shouldEqual: true, 287 }, 288 { 289 type: "object", 290 value: { xhr }, 291 exception: true, 292 }, 293 { 294 type: "object", 295 value: self, 296 exception: true, 297 }, 298 { 299 type: "object", 300 value: { p: ArrayBuffer.prototype }, 301 exception: true, 302 }, 303 { 304 type: "string", 305 shouldEqual: true, 306 value: "testFinished", 307 }, 308 ]; 309 310 for (let index = 0; index < messages.length; index++) { 311 var message = messages[index]; 312 if (message.hasOwnProperty("compareValue")) { 313 continue; 314 } 315 if ( 316 message.hasOwnProperty("shouldEqual") || 317 message.hasOwnProperty("shouldCompare") 318 ) { 319 message.compareValue = message.value; 320 } 321 } 322 323 onmessage = function (event) { 324 for (let index = 0; index < messages.length; index++) { 325 var exception = undefined; 326 327 try { 328 postMessage(messages[index].value); 329 } catch (e) { 330 if (e instanceof DOMException) { 331 if (e.code != DOMException.DATA_CLONE_ERR) { 332 throw "DOMException with the wrong code: " + e.code; 333 } 334 } else if (e != throwingGetterThrownString) { 335 throw "Exception of the wrong type: " + e; 336 } 337 exception = e; 338 } 339 340 if ( 341 (exception !== undefined && !messages[index].exception) || 342 (exception === undefined && messages[index].exception) 343 ) { 344 throw ( 345 "Exception inconsistency [index = " + 346 index + 347 ", " + 348 messages[index].toSource() + 349 "]: " + 350 exception 351 ); 352 } 353 } 354 };