OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 11 matching lines...) Expand all Loading... | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Test the Worker API of d8. This test only makes sense with d8. A Worker | 28 // Test the Worker API of d8. This test only makes sense with d8. A Worker |
29 // spawns a new OS thread and isolate, and runs it concurrently with the | 29 // spawns a new OS thread and isolate, and runs it concurrently with the |
30 // current running thread. | 30 // current running thread. |
31 | 31 |
32 function f() { | 32 var workerScript = |
33 postMessage("Starting worker"); | 33 "postMessage('Starting worker');\n" + |
Jarin
2015/07/01 07:35:44
You might consider using the new shiny template st
binji
2015/07/01 16:13:53
Ah, I didn't know about this. Thanks!
| |
34 // Set a global variable; should not be visible outside of the worker's | 34 // Set a global variable; should not be visible outside of the worker's |
35 // context. | 35 // context. |
36 foo = 100; | 36 "foo = 100;\n" + |
37 | 37 "var c = 0;\n" + |
38 var c = 0; | 38 "onmessage = function(m) {\n" + |
39 onmessage = function(m) { | 39 " switch (c++) {\n" + |
40 switch (c++) { | 40 " case 0:\n" + |
41 case 0: | 41 " if (m !== undefined) throw new Error('undefined');\n" + |
42 if (m !== undefined) throw new Error("undefined"); | 42 " break;\n" + |
43 break; | 43 " case 1:\n" + |
44 case 1: | 44 " if (m !== null) throw new Error('null');\n" + |
45 if (m !== null) throw new Error("null"); | 45 " break;\n" + |
46 break; | 46 " case 2:\n" + |
47 case 2: | 47 " if (m !== true) throw new Error('true');\n" + |
48 if (m !== true) throw new Error("true"); | 48 " break;\n" + |
49 break; | 49 " case 3:\n" + |
50 case 3: | 50 " if (m !== false) throw new Error('false');\n" + |
51 if (m !== false) throw new Error("false"); | 51 " break;\n" + |
52 break; | 52 " case 4:\n" + |
53 case 4: | 53 " if (m !== 100) throw new Error('Number');\n" + |
54 if (m !== 100) throw new Error("Number"); | 54 " break;\n" + |
55 break; | 55 " case 5:\n" + |
56 case 5: | 56 " if (m !== 'hi') throw new Error('String');\n" + |
57 if (m !== "hi") throw new Error("String"); | 57 " break;\n" + |
58 break; | 58 " case 6:\n" + |
59 case 6: | 59 " if (JSON.stringify(m) !== '[4,true,\"bye\"]') {\n" + |
60 if (JSON.stringify(m) !== '[4,true,"bye"]') throw new Error("Array"); | 60 " throw new Error('Array');\n" + |
61 break; | 61 " }\n" + |
62 case 7: | 62 " break;\n" + |
63 if (JSON.stringify(m) !== '{"a":1,"b":2.5,"c":"three"}') | 63 " case 7:\n" + |
64 throw new Error("Object"); | 64 " if (JSON.stringify(m) !== \"{'a':1,'b':2.5,'c':'three'}\")\n" + |
65 break; | 65 " throw new Error('Object');\n" + |
66 case 8: | 66 " break;\n" + |
67 var ab = m; | 67 " case 8:\n" + |
68 var t = new Uint32Array(ab); | 68 " var ab = m;\n" + |
69 if (ab.byteLength !== 16) | 69 " var t = new Uint32Array(ab);\n" + |
70 throw new Error("ArrayBuffer clone byteLength"); | 70 " if (ab.byteLength !== 16)\n" + |
71 for (var i = 0; i < 4; ++i) | 71 " throw new Error('ArrayBuffer clone byteLength');\n" + |
72 if (t[i] !== i) | 72 " for (var i = 0; i < 4; ++i)\n" + |
73 throw new Error("ArrayBuffer clone value " + i); | 73 " if (t[i] !== i)\n" + |
74 break; | 74 " throw new Error('ArrayBuffer clone value ' + i);\n" + |
75 case 9: | 75 " break;\n" + |
76 var ab = m; | 76 " case 9:\n" + |
77 var t = new Uint32Array(ab); | 77 " var ab = m;\n" + |
78 if (ab.byteLength !== 32) | 78 " var t = new Uint32Array(ab);\n" + |
79 throw new Error("ArrayBuffer transfer byteLength"); | 79 " if (ab.byteLength !== 32)\n" + |
80 for (var i = 0; i < 8; ++i) | 80 " throw new Error('ArrayBuffer transfer byteLength');\n" + |
81 if (t[i] !== i) | 81 " for (var i = 0; i < 8; ++i)\n" + |
82 throw new Error("ArrayBuffer transfer value " + i); | 82 " if (t[i] !== i)\n" + |
83 break; | 83 " throw new Error('ArrayBuffer transfer value ' + i);\n" + |
84 } | 84 " break;\n" + |
85 | 85 " }\n" + |
86 if (c == 10) { | 86 " if (c == 10) {\n" + |
87 postMessage("DONE"); | 87 " postMessage('DONE');\n" + |
88 } | 88 " }\n" + |
89 } | 89 "}\n"; |
90 } | |
91 | 90 |
92 | 91 |
93 if (this.Worker) { | 92 if (this.Worker) { |
94 function createArrayBuffer(byteLength) { | 93 function createArrayBuffer(byteLength) { |
95 var ab = new ArrayBuffer(byteLength); | 94 var ab = new ArrayBuffer(byteLength); |
96 var t = new Uint32Array(ab); | 95 var t = new Uint32Array(ab); |
97 for (var i = 0; i < byteLength / 4; ++i) | 96 for (var i = 0; i < byteLength / 4; ++i) |
98 t[i] = i; | 97 t[i] = i; |
99 return ab; | 98 return ab; |
100 } | 99 } |
101 | 100 |
102 var w = new Worker(f); | 101 var w = new Worker(workerScript); |
103 | 102 |
104 assertEquals("Starting worker", w.getMessage()); | 103 assertEquals("Starting worker", w.getMessage()); |
105 | 104 |
106 w.postMessage(undefined); | 105 w.postMessage(undefined); |
107 w.postMessage(null); | 106 w.postMessage(null); |
108 w.postMessage(true); | 107 w.postMessage(true); |
109 w.postMessage(false); | 108 w.postMessage(false); |
110 w.postMessage(100); | 109 w.postMessage(100); |
111 w.postMessage("hi"); | 110 w.postMessage("hi"); |
112 w.postMessage([4, true, "bye"]); | 111 w.postMessage([4, true, "bye"]); |
(...skipping 12 matching lines...) Expand all Loading... | |
125 assertEquals("undefined", typeof foo); | 124 assertEquals("undefined", typeof foo); |
126 | 125 |
127 // Read a message from the worker. | 126 // Read a message from the worker. |
128 assertEquals("DONE", w.getMessage()); | 127 assertEquals("DONE", w.getMessage()); |
129 | 128 |
130 w.terminate(); | 129 w.terminate(); |
131 | 130 |
132 | 131 |
133 // Make sure that the main thread doesn't block forever in getMessage() if | 132 // Make sure that the main thread doesn't block forever in getMessage() if |
134 // the worker dies without posting a message. | 133 // the worker dies without posting a message. |
135 function f2() {} | 134 var w2 = new Worker(''); |
136 var w2 = new Worker(f2); | |
137 var msg = w2.getMessage(); | 135 var msg = w2.getMessage(); |
138 assertEquals(undefined, msg); | 136 assertEquals(undefined, msg); |
139 } | 137 } |
OLD | NEW |