dom/workers/test/json_worker.js

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

mercurial