summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/asm.js/arraybuffer-transfer.js
blob: c96df0536a9b617c30a3950b3be2bedd1ac4ac80 (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
56
57
58
59
60
61
62
63
64
65
load(libdir + "asm.js");

function module(stdlib, foreign, buffer) {
  "use asm";

  var i32 = new stdlib.Int32Array(buffer);

  function zero() {
    return i32[0] | 0;
  }
  return zero;
}

const byteLength = BUF_MIN;
let buffer = new ArrayBuffer(byteLength);
let i32 = new Int32Array(buffer);
let zero = module(globalThis, null, buffer);

const magic = 0xbadf00d;

assertEq(zero(), 0);
assertEq(i32[0], 0);

i32[0] = magic;

assertEq(zero(), magic);
assertEq(i32[0], magic);

assertEq(buffer.detached, false);
assertEq(buffer.byteLength, byteLength);

if (isAsmJSCompilationAvailable()) {
  // asm.js compilation is enabled.

  assertEq(isAsmJSModule(module), true);
  assertEq(isAsmJSFunction(zero), true);

  // Can't transfer asm.js prepared array buffers.
  assertThrowsInstanceOf(() => buffer.transfer(), TypeError);

  // |buffer| is still attached.
  assertEq(buffer.detached, false);
  assertEq(buffer.byteLength, byteLength);

  // Access still returns the original value.
  assertEq(zero(), magic);
  assertEq(i32[0], magic);
} else {
  // asm.js compilation is disabled.

  assertEq(isAsmJSModule(module), false);
  assertEq(isAsmJSFunction(zero), false);

  let copy = buffer.transfer();

  assertEq(buffer.detached, true);
  assertEq(buffer.byteLength, 0);

  assertEq(copy.detached, false);
  assertEq(copy.byteLength, byteLength);

  // Access returns undefined when detached.
  assertEq(zero(), 0);
  assertEq(i32[0], undefined);
}