|
1 /** |
|
2 * Any copyright is dedicated to the Public Domain. |
|
3 * http://creativecommons.org/publicdomain/zero/1.0/ |
|
4 */ |
|
5 "use strict"; |
|
6 |
|
7 if (!("self" in this)) { |
|
8 throw new Error("No 'self' exists on SharedWorkerGlobalScope!"); |
|
9 } |
|
10 if (this !== self) { |
|
11 throw new Error("'self' not equal to global object!"); |
|
12 } |
|
13 if (!(self instanceof SharedWorkerGlobalScope)) { |
|
14 throw new Error("self not a SharedWorkerGlobalScope instance!"); |
|
15 } |
|
16 |
|
17 var propsToCheck = [ |
|
18 "location", |
|
19 "navigator", |
|
20 "close", |
|
21 "importScripts", |
|
22 "setTimeout", |
|
23 "clearTimeout", |
|
24 "setInterval", |
|
25 "clearInterval", |
|
26 "dump", |
|
27 "atob", |
|
28 "btoa" |
|
29 ]; |
|
30 |
|
31 for (var index = 0; index < propsToCheck.length; index++) { |
|
32 var prop = propsToCheck[index]; |
|
33 if (!(prop in self)) { |
|
34 throw new Error("SharedWorkerGlobalScope has no '" + prop + "' property!"); |
|
35 } |
|
36 } |
|
37 |
|
38 onconnect = function(event) { |
|
39 if (!("SharedWorkerGlobalScope" in self)) { |
|
40 throw new Error("SharedWorkerGlobalScope should be visible!"); |
|
41 } |
|
42 if (!(self instanceof SharedWorkerGlobalScope)) { |
|
43 throw new Error("The global should be a SharedWorkerGlobalScope!"); |
|
44 } |
|
45 if (!(self instanceof WorkerGlobalScope)) { |
|
46 throw new Error("The global should be a WorkerGlobalScope!"); |
|
47 } |
|
48 if ("DedicatedWorkerGlobalScope" in self) { |
|
49 throw new Error("DedicatedWorkerGlobalScope should not be visible!"); |
|
50 } |
|
51 if (!(event instanceof MessageEvent)) { |
|
52 throw new Error("'connect' event is not a MessageEvent!"); |
|
53 } |
|
54 if (!("ports" in event)) { |
|
55 throw new Error("'connect' event doesn't have a 'ports' property!"); |
|
56 } |
|
57 if (event.ports.length != 1) { |
|
58 throw new Error("'connect' event has a 'ports' property with length '" + |
|
59 event.ports.length + "'!"); |
|
60 } |
|
61 if (!event.ports[0]) { |
|
62 throw new Error("'connect' event has a null 'ports[0]' property!"); |
|
63 } |
|
64 if (!(event.ports[0] instanceof MessagePort)) { |
|
65 throw new Error("'connect' event has a 'ports[0]' property that isn't a " + |
|
66 "MessagePort!"); |
|
67 } |
|
68 if (!(event.ports[0] == event.source)) { |
|
69 throw new Error("'connect' event source property is incorrect!"); |
|
70 } |
|
71 if (event.data) { |
|
72 throw new Error("'connect' event has data: " + event.data); |
|
73 } |
|
74 |
|
75 event.ports[0].onmessage = function(event) { |
|
76 if (!(event instanceof MessageEvent)) { |
|
77 throw new Error("'message' event is not a MessageEvent!"); |
|
78 } |
|
79 if (!("ports" in event)) { |
|
80 throw new Error("'message' event doesn't have a 'ports' property!"); |
|
81 } |
|
82 if (!(event.ports === null)) { |
|
83 throw new Error("'message' event has a non-null 'ports' property!"); |
|
84 } |
|
85 event.target.postMessage(event.data); |
|
86 throw new Error(event.data); |
|
87 }; |
|
88 }; |