|
| 1 | +// Flags: --expose-internals |
| 2 | +'use strict'; |
| 3 | +require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | +const { internalBinding } = require('internal/test/binding'); |
| 6 | +const { arrayBufferViewHasBuffer } = internalBinding('util'); |
| 7 | + |
| 8 | +const tests = [ |
| 9 | + { length: 0, expectOnHeap: true }, |
| 10 | + { length: 48, expectOnHeap: true }, |
| 11 | + { length: 96, expectOnHeap: false }, |
| 12 | + { length: 1024, expectOnHeap: false }, |
| 13 | +]; |
| 14 | + |
| 15 | +for (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; |
| 35 | + assert(arrayBufferViewHasBuffer(array)); |
| 36 | + } |
| 37 | +} |
0 commit comments