dom/workers/test/json_worker.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/workers/test/json_worker.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,338 @@
     1.4 +/**
     1.5 + * Any copyright is dedicated to the Public Domain.
     1.6 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.7 + */
     1.8 +var cyclicalObject = {};
     1.9 +cyclicalObject.foo = cyclicalObject;
    1.10 +
    1.11 +var cyclicalArray = [];
    1.12 +cyclicalArray.push(cyclicalArray);
    1.13 +
    1.14 +function makeCrazyNested(obj, count) {
    1.15 +  var innermostobj;
    1.16 +  for (var i = 0; i < count; i++) {
    1.17 +    obj.foo = { bar: 5 }
    1.18 +    innermostobj = obj.foo;
    1.19 +    obj = innermostobj;
    1.20 +  }
    1.21 +  return innermostobj;
    1.22 +}
    1.23 +
    1.24 +var crazyNestedObject = {};
    1.25 +makeCrazyNested(crazyNestedObject, 100);
    1.26 +
    1.27 +var crazyCyclicalObject = {};
    1.28 +var innermost = makeCrazyNested(crazyCyclicalObject, 1000);
    1.29 +innermost.baz = crazyCyclicalObject;
    1.30 +
    1.31 +var objectWithSaneGetter = { };
    1.32 +objectWithSaneGetter.__defineGetter__("foo", function() { return 5; });
    1.33 +
    1.34 +// We don't walk prototype chains for cloning so this won't actually do much...
    1.35 +function objectWithSaneGetter2() { }
    1.36 +objectWithSaneGetter2.prototype = {
    1.37 +  get foo() {
    1.38 +    return 5;
    1.39 +  }
    1.40 +};
    1.41 +
    1.42 +const throwingGetterThrownString = "bad";
    1.43 +
    1.44 +var objectWithThrowingGetter = { };
    1.45 +objectWithThrowingGetter.__defineGetter__("foo", function() {
    1.46 +  throw throwingGetterThrownString;
    1.47 +});
    1.48 +
    1.49 +var typedArrayWithValues = new Int8Array(5);
    1.50 +for (var index in typedArrayWithValues) {
    1.51 +  typedArrayWithValues[index] = index;
    1.52 +}
    1.53 +
    1.54 +var typedArrayWithFunBuffer = new Int8Array(4);
    1.55 +for (var index in typedArrayWithFunBuffer) {
    1.56 +  typedArrayWithFunBuffer[index] = 255;
    1.57 +}
    1.58 +
    1.59 +var typedArrayWithFunBuffer2 = new Int32Array(typedArrayWithFunBuffer.buffer);
    1.60 +
    1.61 +var xhr = new XMLHttpRequest();
    1.62 +
    1.63 +var messages = [
    1.64 +  {
    1.65 +    type: "object",
    1.66 +    value: { },
    1.67 +    jsonValue: '{}'
    1.68 +  },
    1.69 +  {
    1.70 +    type: "object",
    1.71 +    value: {foo: "bar"},
    1.72 +    jsonValue: '{"foo":"bar"}'
    1.73 +  },
    1.74 +  {
    1.75 +    type: "object",
    1.76 +    value: {foo: "bar", foo2: {bee: "bop"}},
    1.77 +    jsonValue: '{"foo":"bar","foo2":{"bee":"bop"}}'
    1.78 +  },
    1.79 +  {
    1.80 +    type: "object",
    1.81 +    value: {foo: "bar", foo2: {bee: "bop"}, foo3: "baz"},
    1.82 +    jsonValue: '{"foo":"bar","foo2":{"bee":"bop"},"foo3":"baz"}'
    1.83 +  },
    1.84 +  {
    1.85 +    type: "object",
    1.86 +    value: {foo: "bar", foo2: [1,2,3]},
    1.87 +    jsonValue: '{"foo":"bar","foo2":[1,2,3]}'
    1.88 +  },
    1.89 +  {
    1.90 +    type: "object",
    1.91 +    value: cyclicalObject,
    1.92 +  },
    1.93 +  {
    1.94 +    type: "object",
    1.95 +    value: [null, 2, false, cyclicalObject],
    1.96 +  },
    1.97 +  {
    1.98 +    type: "object",
    1.99 +    value: cyclicalArray,
   1.100 +  },
   1.101 +  {
   1.102 +    type: "object",
   1.103 +    value: {foo: 1, bar: cyclicalArray},
   1.104 +  },
   1.105 +  {
   1.106 +    type: "object",
   1.107 +    value: crazyNestedObject,
   1.108 +    jsonValue: JSON.stringify(crazyNestedObject)
   1.109 +  },
   1.110 +  {
   1.111 +    type: "object",
   1.112 +    value: crazyCyclicalObject,
   1.113 +  },
   1.114 +  {
   1.115 +    type: "object",
   1.116 +    value: objectWithSaneGetter,
   1.117 +    jsonValue: '{"foo":5}'
   1.118 +  },
   1.119 +  {
   1.120 +    type: "object",
   1.121 +    value: new objectWithSaneGetter2(),
   1.122 +    jsonValue: '{}'
   1.123 +  },
   1.124 +  {
   1.125 +    type: "object",
   1.126 +    value: objectWithThrowingGetter,
   1.127 +    exception: true
   1.128 +  },
   1.129 +  {
   1.130 +    type: "object",
   1.131 +    array: true,
   1.132 +    value: [9, 8, 7],
   1.133 +    jsonValue: '[9,8,7]'
   1.134 +  },
   1.135 +  {
   1.136 +    type: "object",
   1.137 +    array: true,
   1.138 +    value: [9, false, 10.5, {foo: "bar"}],
   1.139 +    jsonValue: '[9,false,10.5,{"foo":"bar"}]'
   1.140 +  },
   1.141 +  {
   1.142 +    type: "object",
   1.143 +    shouldEqual: true,
   1.144 +    value: null
   1.145 +  },
   1.146 +  {
   1.147 +    type: "undefined",
   1.148 +    shouldEqual: true,
   1.149 +    value: undefined
   1.150 +  },
   1.151 +  {
   1.152 +    type: "string",
   1.153 +    shouldEqual: true,
   1.154 +    value: "Hello"
   1.155 +  },
   1.156 +  {
   1.157 +    type: "string",
   1.158 +    shouldEqual: true,
   1.159 +    value: JSON.stringify({ foo: "bar" }),
   1.160 +    compareValue: '{"foo":"bar"}'
   1.161 +  },
   1.162 +  {
   1.163 +    type: "number",
   1.164 +    shouldEqual: true,
   1.165 +    value: 1
   1.166 +  },
   1.167 +  {
   1.168 +    type: "number",
   1.169 +    shouldEqual: true,
   1.170 +    value: 0
   1.171 +  },
   1.172 +  {
   1.173 +    type: "number",
   1.174 +    shouldEqual: true,
   1.175 +    value: -1
   1.176 +  },
   1.177 +  {
   1.178 +    type: "number",
   1.179 +    shouldEqual: true,
   1.180 +    value: 238573459843702923492399923049
   1.181 +  },
   1.182 +  {
   1.183 +    type: "number",
   1.184 +    shouldEqual: true,
   1.185 +    value: -238573459843702923492399923049
   1.186 +  },
   1.187 +  {
   1.188 +    type: "number",
   1.189 +    shouldEqual: true,
   1.190 +    value: 0.25
   1.191 +  },
   1.192 +  {
   1.193 +    type: "number",
   1.194 +    shouldEqual: true,
   1.195 +    value: -0.25
   1.196 +  },
   1.197 +  {
   1.198 +    type: "boolean",
   1.199 +    shouldEqual: true,
   1.200 +    value: true
   1.201 +  },
   1.202 +  {
   1.203 +    type: "boolean",
   1.204 +    shouldEqual: true,
   1.205 +    value: false
   1.206 +  },
   1.207 +  {
   1.208 +    type: "object",
   1.209 +    value: function (foo) { return "Bad!"; },
   1.210 +    exception: true
   1.211 +  },
   1.212 +  {
   1.213 +    type: "number",
   1.214 +    isNaN: true,
   1.215 +    value: NaN
   1.216 +  },
   1.217 +  {
   1.218 +    type: "number",
   1.219 +    isInfinity: true,
   1.220 +    value: Infinity
   1.221 +  },
   1.222 +  {
   1.223 +    type: "number",
   1.224 +    isNegativeInfinity: true,
   1.225 +    value: -Infinity
   1.226 +  },
   1.227 +  {
   1.228 +    type: "object",
   1.229 +    value: new Int32Array(10),
   1.230 +    jsonValue: '{"0":0,"1":0,"2":0,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0}'
   1.231 +  },
   1.232 +  {
   1.233 +    type: "object",
   1.234 +    value: new Float32Array(5),
   1.235 +    jsonValue: '{"0":0,"1":0,"2":0,"3":0,"4":0}'
   1.236 +  },
   1.237 +  {
   1.238 +    type: "object",
   1.239 +    value: typedArrayWithValues,
   1.240 +    jsonValue: '{"0":0,"1":1,"2":2,"3":3,"4":4}'
   1.241 +  },
   1.242 +  {
   1.243 +    type: "number",
   1.244 +    value: typedArrayWithValues[2],
   1.245 +    compareValue: 2,
   1.246 +    shouldEqual: true
   1.247 +  },
   1.248 +  {
   1.249 +    type: "object",
   1.250 +    value: typedArrayWithValues.buffer,
   1.251 +    jsonValue: '{}'
   1.252 +  },
   1.253 +  {
   1.254 +    type: "object",
   1.255 +    value: typedArrayWithFunBuffer2,
   1.256 +    jsonValue: '{"0":-1}'
   1.257 +  },
   1.258 +  {
   1.259 +    type: "object",
   1.260 +    value: { foo: typedArrayWithFunBuffer2 },
   1.261 +    jsonValue: '{"foo":{"0":-1}}'
   1.262 +  },
   1.263 +  {
   1.264 +    type: "object",
   1.265 +    value: [ typedArrayWithFunBuffer2 ],
   1.266 +    jsonValue: '[{"0":-1}]'
   1.267 +  },
   1.268 +  {
   1.269 +    type: "object",
   1.270 +    value: { foo: function(a) { alert(b); } },
   1.271 +    exception: true
   1.272 +  },
   1.273 +  {
   1.274 +    type: "object",
   1.275 +    value: xhr,
   1.276 +    exception: true
   1.277 +  },
   1.278 +  {
   1.279 +    type: "number",
   1.280 +    value: xhr.readyState,
   1.281 +    shouldEqual: true
   1.282 +  },
   1.283 +  {
   1.284 +    type: "object",
   1.285 +    value: { xhr: xhr },
   1.286 +    exception: true
   1.287 +  },
   1.288 +  {
   1.289 +    type: "object",
   1.290 +    value: self,
   1.291 +    exception: true
   1.292 +  },
   1.293 +  {
   1.294 +    type: "object",
   1.295 +    value: { p: ArrayBuffer.prototype },
   1.296 +    exception: true
   1.297 +  },
   1.298 +  {
   1.299 +    type: "string",
   1.300 +    shouldEqual: true,
   1.301 +    value: "testFinished"
   1.302 +  }
   1.303 +];
   1.304 +
   1.305 +for (var index = 0; index < messages.length; index++) {
   1.306 +  var message = messages[index];
   1.307 +  if (message.hasOwnProperty("compareValue")) {
   1.308 +    continue;
   1.309 +  }
   1.310 +  if (message.hasOwnProperty("shouldEqual") ||
   1.311 +      message.hasOwnProperty("shouldCompare")) {
   1.312 +    message.compareValue = message.value;
   1.313 +  }
   1.314 +}
   1.315 +
   1.316 +onmessage = function(event) {
   1.317 +  for (var index = 0; index < messages.length; index++) {
   1.318 +    var exception = undefined;
   1.319 +
   1.320 +    try {
   1.321 +      postMessage(messages[index].value);
   1.322 +    }
   1.323 +    catch (e) {
   1.324 +      if (e instanceof DOMException) {
   1.325 +        if (e.code != DOMException.DATA_CLONE_ERR) {
   1.326 +          throw "DOMException with the wrong code: " + e.code;
   1.327 +        }
   1.328 +      }
   1.329 +      else if (e != throwingGetterThrownString) {
   1.330 +        throw "Exception of the wrong type: " + e;
   1.331 +      }
   1.332 +      exception = e;
   1.333 +    }
   1.334 +
   1.335 +    if ((exception !== undefined && !messages[index].exception) ||
   1.336 +        (exception === undefined && messages[index].exception)) {
   1.337 +      throw "Exception inconsistency [index = " + index + ", " +
   1.338 +            messages[index].toSource() + "]: " + exception;
   1.339 +    }
   1.340 +  }
   1.341 +}

mercurial