(ia, index_, value_, timeout_)
| 72 | // rejected with an error string. |
| 73 | |
| 74 | function waitAsync(ia, index_, value_, timeout_) { |
| 75 | if (typeof ia != "object" || !(ia instanceof Int32Array) || !(ia.buffer instanceof SharedArrayBuffer)) |
| 76 | throw new TypeError("Expected shared memory"); |
| 77 | |
| 78 | // These conversions only approximate the desired semantics but are |
| 79 | // close enough for the polyfill. |
| 80 | |
| 81 | let index = index_|0; |
| 82 | let value = value_|0; |
| 83 | let timeout = timeout_ === undefined ? Infinity : +timeout_; |
| 84 | |
| 85 | // Range checking for the index. |
| 86 | |
| 87 | ia[index]; |
| 88 | |
| 89 | // Optimization, avoid the helper thread in this common case. |
| 90 | |
| 91 | if (Atomics.load(ia, index) != value) |
| 92 | return { value: Promise.resolve("not-equal") }; |
| 93 | |
| 94 | // General case, we must wait. |
| 95 | |
| 96 | return { value: new Promise(function (resolve, reject) { |
| 97 | let h = allocHelper(); |
| 98 | h.onmessage = function (ev) { |
| 99 | // Free the helper early so that it can be reused if the resolution |
| 100 | // needs a helper. |
| 101 | freeHelper(h); |
| 102 | switch (ev.data[0]) { |
| 103 | case 'ok': |
| 104 | resolve(ev.data[1]); |
| 105 | break; |
| 106 | case 'error': |
| 107 | // Note, rejection is not in the spec, it is an artifact of the polyfill. |
| 108 | // The helper already printed an error to the console. |
| 109 | reject(ev.data[1]); |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // It's possible to do better here if the ia is already known to the |
| 115 | // helper. In that case we can communicate the other data through |
| 116 | // shared memory and wake the agent. And it is possible to make ia |
| 117 | // known to the helper by waking it with a special value so that it |
| 118 | // checks its messages, and then posting the ia to the helper. Some |
| 119 | // caching / decay scheme is useful no doubt, to improve performance |
| 120 | // and avoid leaks. |
| 121 | // |
| 122 | // In the event we wake the helper directly, we can micro-wait here |
| 123 | // for a quick result. We'll need to restructure some code to make |
| 124 | // that work out properly, and some synchronization is necessary for |
| 125 | // the helper to know that we've picked up the result and no |
| 126 | // postMessage is necessary. |
| 127 | |
| 128 | h.postMessage(['wait', ia, index, value, timeout]); |
| 129 | }) }; |
| 130 | } |
| 131 |
nothing calls this directly
no test coverage detected