dom/workers/test/json_worker.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial