atomics-load.js 2.21 KB
Newer Older
binji's avatar
binji committed
1 2 3 4
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --harmony-sharedarraybuffer
binji's avatar
binji committed
6

7
function Module(stdlib, foreign, heap, offset) {
binji's avatar
binji committed
8
  "use asm";
9 10 11 12 13 14
  var MEM8 = new stdlib.Int8Array(heap, offset);
  var MEM16 = new stdlib.Int16Array(heap, offset);
  var MEM32 = new stdlib.Int32Array(heap, offset);
  var MEMU8 = new stdlib.Uint8Array(heap, offset);
  var MEMU16 = new stdlib.Uint16Array(heap, offset);
  var MEMU32 = new stdlib.Uint32Array(heap, offset);
binji's avatar
binji committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  var load = stdlib.Atomics.load;
  var fround = stdlib.Math.fround;

  function loadi8(i) {
    i = i | 0;
    return load(MEM8, i)|0;
  }

  function loadi16(i) {
    i = i | 0;
    return load(MEM16, i)|0;
  }

  function loadi32(i) {
    i = i | 0;
    return load(MEM32, i)|0;
  }

  function loadu8(i) {
    i = i | 0;
    return load(MEMU8, i)>>>0;
  }

  function loadu16(i) {
    i = i | 0;
    return load(MEMU16, i)>>>0;
  }

  function loadu32(i) {
    i = i | 0;
    return load(MEMU32, i)>>>0;
  }

  return {
    loadi8: loadi8,
    loadi16: loadi16,
    loadi32: loadi32,
    loadu8: loadu8,
    loadu16: loadu16,
    loadu32: loadu32,
  };
}

function clearArray() {
  var ui8 = new Uint8Array(sab);
  for (var i = 0; i < sab.byteLength; ++i) {
    ui8[i] = 0;
  }
}

65
function testElementType(taConstr, f, offset) {
binji's avatar
binji committed
66 67
  clearArray();

68
  var ta = new taConstr(sab, offset);
binji's avatar
binji committed
69 70 71 72 73
  var name = Object.prototype.toString.call(ta);
  ta[0] = 10;
  assertEquals(10, f(0), name);
  assertEquals(0, f(1), name);
  // out of bounds
74 75
  assertThrows(function() { f(-1); });
  assertThrows(function() { f(ta.length); });
binji's avatar
binji committed
76 77
}

78
function testElement(m, offset) {
79 80 81 82 83 84
  testElementType(Int8Array, m.loadi8, offset);
  testElementType(Int16Array, m.loadi16, offset);
  testElementType(Int32Array, m.loadi32, offset);
  testElementType(Uint8Array, m.loadu8, offset);
  testElementType(Uint16Array, m.loadu16, offset);
  testElementType(Uint32Array, m.loadu32, offset);
85 86 87 88 89 90 91 92 93 94 95
}

var offset = 0;
var sab = new SharedArrayBuffer(16);
var m1 = Module(this, {}, sab, offset);
testElement(m1, offset);

offset = 32;
sab = new SharedArrayBuffer(64);
var m2 = Module(this, {}, sab, offset);
testElement(m2, offset);