summaryrefslogtreecommitdiff
path: root/deps/v8/test/mjsunit/asm/asm-memory.js
blob: 6f9b2fe6397370dd2d5608dbad34f1274ff14414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
// Copyright 2017 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.

// Flags: --allow-natives-syntax

(function TestUnalignedMemory() {
  // Test that a buffer whose length is not a multiple of the element size of a
  // heap view throws the proper {RangeError} during instantiation.
  function Module(stdlib, foreign, heap) {
    "use asm";
    var a = new stdlib.Int32Array(heap);
    function f() {}
    return { f:f };
  }
  assertThrows(() => Module(this, {}, new ArrayBuffer(2)), RangeError);
  assertThrows(() => Module(this, {}, new ArrayBuffer(10)), RangeError);
  assertDoesNotThrow(() => Module(this, {}, new ArrayBuffer(4)));
  assertDoesNotThrow(() => Module(this, {}, new ArrayBuffer(16)));
  assertFalse(%IsAsmWasmCode(Module));
})();

(function TestMissingMemory() {
  // Test that a buffer is required for instantiation of modules containing any
  // heap views. JavaScript needs to create individual buffers for each view.
  function Module(stdlib, foreign, heap) {
    "use asm";
    var a = new stdlib.Int16Array(heap);
    var b = new stdlib.Int32Array(heap);
    function f() {
      a[0] = 0x1234;
      return b[0] | 0;
    }
    return { f:f };
  }
  var m = Module(this, {}, undefined);
  assertFalse(%IsAsmWasmCode(Module));
  assertEquals(0, m.f());
})();

(function TestNonBufferMemory() {
  // Test that a buffer has to be an instance of {ArrayBuffer} in order to be
  // valid. JavaScript will also accept any other array-like object.
  function Module(stdlib, foreign, heap) {
    "use asm";
    var a = new stdlib.Int32Array(heap);
    function f() {
      return a[0] | 0;
    }
    return { f:f };
  }
  var m = Module(this, {}, [ 23, 42 ]);
  assertFalse(%IsAsmWasmCode(Module));
  assertEquals(23, m.f());
})();