OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 |
| 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 |
| 30 // current running thread. |
| 31 |
| 32 function f() { |
| 33 postMessage("Starting worker"); |
| 34 // Set a global variable; should not be visible outside of the worker's |
| 35 // context. |
| 36 foo = 100; |
| 37 |
| 38 var c = 0; |
| 39 onmessage = function(m) { |
| 40 switch (c++) { |
| 41 case 0: |
| 42 if (m !== undefined) throw new Error("undefined"); |
| 43 break; |
| 44 case 1: |
| 45 if (m !== null) throw new Error("null"); |
| 46 break; |
| 47 case 2: |
| 48 if (m !== true) throw new Error("true"); |
| 49 break; |
| 50 case 3: |
| 51 if (m !== false) throw new Error("false"); |
| 52 break; |
| 53 case 4: |
| 54 if (m !== 100) throw new Error("Number"); |
| 55 break; |
| 56 case 5: |
| 57 if (m !== "hi") throw new Error("String"); |
| 58 break; |
| 59 case 6: |
| 60 if (JSON.stringify(m) !== '[4,true,"bye"]') throw new Error("Array"); |
| 61 break; |
| 62 case 7: |
| 63 if (JSON.stringify(m) !== '{"a":1,"b":2.5,"c":"three"}') |
| 64 throw new Error("Object"); |
| 65 break; |
| 66 case 8: |
| 67 var ab = m; |
| 68 var t = new Uint32Array(ab); |
| 69 if (ab.byteLength !== 16) |
| 70 throw new Error("ArrayBuffer clone byteLength"); |
| 71 for (var i = 0; i < 4; ++i) |
| 72 if (t[i] !== i) |
| 73 throw new Error("ArrayBuffer clone value " + i); |
| 74 break; |
| 75 case 9: |
| 76 var ab = m; |
| 77 var t = new Uint32Array(ab); |
| 78 if (ab.byteLength !== 32) |
| 79 throw new Error("ArrayBuffer transfer byteLength"); |
| 80 for (var i = 0; i < 8; ++i) |
| 81 if (t[i] !== i) |
| 82 throw new Error("ArrayBuffer transfer value " + i); |
| 83 break; |
| 84 } |
| 85 |
| 86 if (c == 10) { |
| 87 postMessage("DONE"); |
| 88 } |
| 89 } |
| 90 } |
| 91 |
| 92 |
| 93 if (this.Worker) { |
| 94 function createArrayBuffer(byteLength) { |
| 95 var ab = new ArrayBuffer(byteLength); |
| 96 var t = new Uint32Array(ab); |
| 97 for (var i = 0; i < byteLength / 4; ++i) |
| 98 t[i] = i; |
| 99 return ab; |
| 100 } |
| 101 |
| 102 var w = new Worker(f); |
| 103 |
| 104 assertEquals("Starting worker", w.getMessage()); |
| 105 |
| 106 w.postMessage(undefined); |
| 107 w.postMessage(null); |
| 108 w.postMessage(true); |
| 109 w.postMessage(false); |
| 110 w.postMessage(100); |
| 111 w.postMessage("hi"); |
| 112 w.postMessage([4, true, "bye"]); |
| 113 w.postMessage({a: 1, b: 2.5, c: "three"}); |
| 114 |
| 115 // Clone ArrayBuffer |
| 116 var ab1 = createArrayBuffer(16); |
| 117 w.postMessage(ab1); |
| 118 assertEquals(16, ab1.byteLength); // ArrayBuffer should not be neutered. |
| 119 |
| 120 // Transfer ArrayBuffer |
| 121 var ab2 = createArrayBuffer(32); |
| 122 w.postMessage(ab2, [ab2]); |
| 123 assertEquals(0, ab2.byteLength); // ArrayBuffer should be neutered. |
| 124 |
| 125 assertEquals("undefined", typeof foo); |
| 126 |
| 127 // Read a message from the worker. |
| 128 assertEquals("DONE", w.getMessage()); |
| 129 |
| 130 w.terminate(); |
| 131 } |
OLD | NEW |