Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Flags: --harmony-sharedarraybuffer |
| 6 | |
| 7 | function Module(stdlib, foreign, heap, offset) { |
| 8 | "use asm"; |
| 9 | var MEM8 = new stdlib.Int8Array(heap, offset); |
| 10 | var MEM16 = new stdlib.Int16Array(heap, offset); |
| 11 | var MEM32 = new stdlib.Int32Array(heap, offset); |
| 12 | var MEMU8 = new stdlib.Uint8Array(heap, offset); |
| 13 | var MEMU16 = new stdlib.Uint16Array(heap, offset); |
| 14 | var MEMU32 = new stdlib.Uint32Array(heap, offset); |
| 15 | var store = stdlib.Atomics.store; |
| 16 | var fround = stdlib.Math.fround; |
| 17 | |
| 18 | function storei8(i, x) { |
| 19 | i = i | 0; |
| 20 | x = x | 0; |
| 21 | return store(MEM8, i, x)|0; |
| 22 | } |
| 23 | |
| 24 | function storei16(i, x) { |
| 25 | i = i | 0; |
| 26 | x = x | 0; |
| 27 | return store(MEM16, i, x)|0; |
| 28 | } |
| 29 | |
| 30 | function storei32(i, x) { |
| 31 | i = i | 0; |
| 32 | x = x | 0; |
| 33 | return store(MEM32, i, x)|0; |
| 34 | } |
| 35 | |
| 36 | function storeu8(i, x) { |
| 37 | i = i | 0; |
| 38 | x = x >>> 0; |
| 39 | return store(MEMU8, i, x)>>>0; |
| 40 | } |
| 41 | |
| 42 | function storeu16(i, x) { |
| 43 | i = i | 0; |
| 44 | x = x >>> 0; |
| 45 | return store(MEMU16, i, x)>>>0; |
| 46 | } |
| 47 | |
| 48 | function storeu32(i, x) { |
| 49 | i = i | 0; |
| 50 | x = x >>> 0; |
| 51 | return store(MEMU32, i, x)>>>0; |
| 52 | } |
| 53 | |
| 54 | return { |
| 55 | storei8: storei8, |
| 56 | storei16: storei16, |
| 57 | storei32: storei32, |
| 58 | storeu8: storeu8, |
| 59 | storeu16: storeu16, |
| 60 | storeu32: storeu32, |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | function clearArray() { |
| 65 | var ui8 = new Uint8Array(sab); |
| 66 | for (var i = 0; i < sab.byteLength; ++i) { |
| 67 | ui8[i] = 0; |
| 68 | } |
| 69 | } |
| 70 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 71 | function testElementType(taConstr, f, offset) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | clearArray(); |
| 73 | |
| 74 | var ta = new taConstr(sab, offset); |
| 75 | var name = Object.prototype.toString.call(ta); |
| 76 | assertEquals(10, f(0, 10), name); |
| 77 | assertEquals(10, ta[0]); |
| 78 | // out of bounds |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 79 | assertThrows(function() { f(-1, 0); }); |
| 80 | assertThrows(function() { f(ta.length, 0); }); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | function testElement(m, offset) { |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame^] | 84 | testElementType(Int8Array, m.storei8, offset); |
| 85 | testElementType(Int16Array, m.storei16, offset); |
| 86 | testElementType(Int32Array, m.storei32, offset); |
| 87 | testElementType(Uint8Array, m.storeu8, offset); |
| 88 | testElementType(Uint16Array, m.storeu16, offset); |
| 89 | testElementType(Uint32Array, m.storeu32, offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | var offset = 0; |
| 93 | var sab = new SharedArrayBuffer(16); |
| 94 | var m1 = Module(this, {}, sab, offset); |
| 95 | testElement(m1, offset); |
| 96 | |
| 97 | offset = 32; |
| 98 | sab = new SharedArrayBuffer(64); |
| 99 | var m2 = Module(this, {}, sab, offset); |
| 100 | testElement(m2, offset); |