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 7function 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 load = stdlib.Atomics.load; 16 var fround = stdlib.Math.fround; 17 18 function loadi8(i) { 19 i = i | 0; 20 return load(MEM8, i)|0; 21 } 22 23 function loadi16(i) { 24 i = i | 0; 25 return load(MEM16, i)|0; 26 } 27 28 function loadi32(i) { 29 i = i | 0; 30 return load(MEM32, i)|0; 31 } 32 33 function loadu8(i) { 34 i = i | 0; 35 return load(MEMU8, i)>>>0; 36 } 37 38 function loadu16(i) { 39 i = i | 0; 40 return load(MEMU16, i)>>>0; 41 } 42 43 function loadu32(i) { 44 i = i | 0; 45 return load(MEMU32, i)>>>0; 46 } 47 48 return { 49 loadi8: loadi8, 50 loadi16: loadi16, 51 loadi32: loadi32, 52 loadu8: loadu8, 53 loadu16: loadu16, 54 loadu32: loadu32, 55 }; 56} 57 58function clearArray() { 59 var ui8 = new Uint8Array(sab); 60 for (var i = 0; i < sab.byteLength; ++i) { 61 ui8[i] = 0; 62 } 63} 64 65function testElementType(taConstr, f, oobValue, offset) { 66 clearArray(); 67 68 var ta = new taConstr(sab, offset); 69 var name = Object.prototype.toString.call(ta); 70 ta[0] = 10; 71 assertEquals(10, f(0), name); 72 assertEquals(0, f(1), name); 73 // out of bounds 74 assertEquals(oobValue, f(-1), name); 75 assertEquals(oobValue, f(ta.length), name); 76} 77 78function testElement(m, offset) { 79 testElementType(Int8Array, m.loadi8, 0, offset); 80 testElementType(Int16Array, m.loadi16, 0, offset); 81 testElementType(Int32Array, m.loadi32, 0, offset); 82 testElementType(Uint8Array, m.loadu8, 0, offset); 83 testElementType(Uint16Array, m.loadu16, 0, offset); 84 testElementType(Uint32Array, m.loadu32, 0, offset); 85} 86 87var offset = 0; 88var sab = new SharedArrayBuffer(16); 89var m1 = Module(this, {}, sab, offset); 90testElement(m1, offset); 91 92offset = 32; 93sab = new SharedArrayBuffer(64); 94var m2 = Module(this, {}, sab, offset); 95testElement(m2, offset); 96