|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const common = require('../common'); |
| 4 | +const assert = require('assert'); |
| 5 | + |
| 6 | +const Buffer = require('buffer').Buffer; |
| 7 | +const LENGTH = 16; |
| 8 | + |
| 9 | +const ab = new ArrayBuffer(LENGTH); |
| 10 | +const dv = new DataView(ab); |
| 11 | +const ui = new Uint8Array(ab); |
| 12 | +const buf = new Buffer(ab); |
| 13 | + |
| 14 | + |
| 15 | +assert.ok(buf instanceof Buffer); |
| 16 | +// For backwards compatibility of old .parent property test that if buf is not |
| 17 | +// a slice then .parent should be undefined. |
| 18 | +assert.equal(buf.parent, undefined); |
| 19 | +assert.equal(buf.buffer, ab); |
| 20 | +assert.equal(buf.length, ab.byteLength); |
| 21 | + |
| 22 | + |
| 23 | +buf.fill(0xC); |
| 24 | +for (let i = 0; i < LENGTH; i++) { |
| 25 | + assert.equal(ui[i], 0xC); |
| 26 | + ui[i] = 0xF; |
| 27 | + assert.equal(buf[i], 0xF); |
| 28 | +} |
| 29 | + |
| 30 | +buf.writeUInt32LE(0xF00, 0); |
| 31 | +buf.writeUInt32BE(0xB47, 4); |
| 32 | +buf.writeDoubleLE(3.1415, 8); |
| 33 | + |
| 34 | +assert.equal(dv.getUint32(0, true), 0xF00); |
| 35 | +assert.equal(dv.getUint32(4), 0xB47); |
| 36 | +assert.equal(dv.getFloat64(8, true), 3.1415); |
| 37 | + |
| 38 | + |
| 39 | +// Now test protecting users from doing stupid things |
| 40 | + |
| 41 | +assert.throws(function() { |
| 42 | + function AB() { } |
| 43 | + AB.__proto__ = ArrayBuffer; |
| 44 | + AB.prototype.__proto__ = ArrayBuffer.prototype; |
| 45 | + new Buffer(new AB()); |
| 46 | +}, TypeError); |
0 commit comments