• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals
2'use strict';
3require('../common');
4const assert = require('assert');
5const { internalBinding } = require('internal/test/binding');
6const { arrayBufferViewHasBuffer } = internalBinding('util');
7
8const tests = [
9  { length: 0, expectOnHeap: true },
10  { length: 48, expectOnHeap: true },
11  { length: 96, expectOnHeap: false },
12  { length: 1024, expectOnHeap: false },
13];
14
15for (const { length, expectOnHeap } of tests) {
16  const arrays = [
17    new Uint8Array(length),
18    new Uint16Array(length / 2),
19    new Uint32Array(length / 4),
20    new Float32Array(length / 4),
21    new Float64Array(length / 8),
22    Buffer.alloc(length),
23    Buffer.allocUnsafeSlow(length),
24    // Buffer.allocUnsafe() is missing because it may use pooled allocations.
25  ];
26
27  for (const array of arrays) {
28    const isOnHeap = !arrayBufferViewHasBuffer(array);
29    assert.strictEqual(isOnHeap, expectOnHeap,
30                       `mismatch: ${isOnHeap} vs ${expectOnHeap} ` +
31                       `for ${array.constructor.name}, length = ${length}`);
32
33    // Consistency check: Accessing .buffer should create it.
34    array.buffer; // eslint-disable-line no-unused-expressions
35    assert(arrayBufferViewHasBuffer(array));
36  }
37}
38