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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 break; | 54 break; |
55 case 5: | 55 case 5: |
56 if (m !== 'hi') throw new Error('String'); | 56 if (m !== 'hi') throw new Error('String'); |
57 break; | 57 break; |
58 case 6: | 58 case 6: |
59 if (JSON.stringify(m) !== '[4,true,\"bye\"]') { | 59 if (JSON.stringify(m) !== '[4,true,\"bye\"]') { |
60 throw new Error('Array'); | 60 throw new Error('Array'); |
61 } | 61 } |
62 break; | 62 break; |
63 case 7: | 63 case 7: |
64 if (JSON.stringify(m) !== \"{'a':1,'b':2.5,'c':'three'}\") | 64 if (JSON.stringify(m) !== '{"a":1,"b":2.5,"c":"three"}') |
65 throw new Error('Object'); | 65 throw new Error('Object' + JSON.stringify(m)); |
66 break; | 66 break; |
67 case 8: | 67 case 8: |
68 var ab = m; | 68 var ab = m; |
69 var t = new Uint32Array(ab); | 69 var t = new Uint32Array(ab); |
70 if (ab.byteLength !== 16) | 70 if (ab.byteLength !== 16) |
71 throw new Error('ArrayBuffer clone byteLength'); | 71 throw new Error('ArrayBuffer clone byteLength'); |
72 for (var i = 0; i < 4; ++i) | 72 for (var i = 0; i < 4; ++i) |
73 if (t[i] !== i) | 73 if (t[i] !== i) |
74 throw new Error('ArrayBuffer clone value ' + i); | 74 throw new Error('ArrayBuffer clone value ' + i); |
75 break; | 75 break; |
76 case 9: | 76 case 9: |
77 var ab = m; | 77 var ab = m; |
78 var t = new Uint32Array(ab); | 78 var t = new Uint32Array(ab); |
79 if (ab.byteLength !== 32) | 79 if (ab.byteLength !== 32) |
80 throw new Error('ArrayBuffer transfer byteLength'); | 80 throw new Error('ArrayBuffer transfer byteLength'); |
81 for (var i = 0; i < 8; ++i) | 81 for (var i = 0; i < 8; ++i) |
82 if (t[i] !== i) | 82 if (t[i] !== i) |
83 throw new Error('ArrayBuffer transfer value ' + i); | 83 throw new Error('ArrayBuffer transfer value ' + i); |
84 break; | 84 break; |
85 } | 85 } |
86 if (c == 10) { | 86 if (c == 10) { |
87 postMessage('DONE'); | 87 postMessage('DONE'); |
88 } | 88 } |
89 };`; | 89 };`; |
90 | 90 |
91 | |
92 if (this.Worker) { | 91 if (this.Worker) { |
93 function createArrayBuffer(byteLength) { | 92 function createArrayBuffer(byteLength) { |
94 var ab = new ArrayBuffer(byteLength); | 93 var ab = new ArrayBuffer(byteLength); |
95 var t = new Uint32Array(ab); | 94 var t = new Uint32Array(ab); |
96 for (var i = 0; i < byteLength / 4; ++i) | 95 for (var i = 0; i < byteLength / 4; ++i) |
97 t[i] = i; | 96 t[i] = i; |
98 return ab; | 97 return ab; |
99 } | 98 } |
100 | 99 |
101 var w = new Worker(workerScript); | 100 var w = new Worker(workerScript); |
102 | 101 |
103 assertEquals("Starting worker", w.getMessage()); | 102 assertEquals("Starting worker", w.getMessage()); |
104 | 103 |
105 w.postMessage(undefined); | 104 w.postMessage(undefined); |
106 w.postMessage(null); | 105 w.postMessage(null); |
107 w.postMessage(true); | 106 w.postMessage(true); |
108 w.postMessage(false); | 107 w.postMessage(false); |
109 w.postMessage(100); | 108 w.postMessage(100); |
110 w.postMessage("hi"); | 109 w.postMessage("hi"); |
111 w.postMessage([4, true, "bye"]); | 110 w.postMessage([4, true, "bye"]); |
112 w.postMessage({a: 1, b: 2.5, c: "three"}); | 111 w.postMessage({a: 1, b: 2.5, c: "three"}); |
113 | 112 |
| 113 // Test bad get in transfer list. |
| 114 var transferList = [undefined]; |
| 115 Object.defineProperty(transferList, '0', { |
| 116 get: function() { |
| 117 throw 'unexpected!'; |
| 118 } |
| 119 }); |
| 120 assertThrows(function() { |
| 121 w.postMessage([], transferList); |
| 122 }); |
| 123 |
114 // Clone ArrayBuffer | 124 // Clone ArrayBuffer |
115 var ab1 = createArrayBuffer(16); | 125 var ab1 = createArrayBuffer(16); |
116 w.postMessage(ab1); | 126 w.postMessage(ab1); |
117 assertEquals(16, ab1.byteLength); // ArrayBuffer should not be neutered. | 127 assertEquals(16, ab1.byteLength); // ArrayBuffer should not be neutered. |
118 | 128 |
119 // Transfer ArrayBuffer | 129 // Transfer ArrayBuffer |
120 var ab2 = createArrayBuffer(32); | 130 var ab2 = createArrayBuffer(32); |
121 w.postMessage(ab2, [ab2]); | 131 w.postMessage(ab2, [ab2]); |
122 assertEquals(0, ab2.byteLength); // ArrayBuffer should be neutered. | 132 assertEquals(0, ab2.byteLength); // ArrayBuffer should be neutered. |
123 | 133 |
124 assertEquals("undefined", typeof foo); | 134 assertEquals("undefined", typeof foo); |
125 | 135 |
126 // Read a message from the worker. | 136 // Read a message from the worker. |
127 assertEquals("DONE", w.getMessage()); | 137 assertEquals("DONE", w.getMessage()); |
128 | 138 |
129 w.terminate(); | 139 w.terminate(); |
130 | 140 |
131 | 141 |
132 // Make sure that the main thread doesn't block forever in getMessage() if | 142 // Make sure that the main thread doesn't block forever in getMessage() if |
133 // the worker dies without posting a message. | 143 // the worker dies without posting a message. |
134 var w2 = new Worker(''); | 144 var w2 = new Worker(''); |
135 var msg = w2.getMessage(); | 145 var msg = w2.getMessage(); |
136 assertEquals(undefined, msg); | 146 assertEquals(undefined, msg); |
137 } | 147 } |
OLD | NEW |