Example #1
0
math.frexp = function(x) {
    _checkFloat(x);
    var xx = x.__float__().val;
    // check for 0, -0, NaN, Inf, -Inf
    if (xx === 0 || !isFinite(xx)) {
        return new types.Tuple([x.__float__(), new types.Int(0)]);
    }
    var buff = new Buffer(8);
    buff.writeDoubleLE(x, 0);
    var a = buff.readUInt32LE(0);
    var b = buff.readUInt32LE(4);
    var exp = ((b >> 20) & 0x7ff) - 1022;
    var num;
    // check for denormal number
    if (exp == -1022) {
        // each leading zero increases the exponent
        num = (b & 0xfffff) * 4294967296 + a;
        while ((num != 0) && (num < 0x8000000000000)) {
            exp--;
            num *= 2;
        }
        num = num / 0x10000000000000;
    } else {
      num = 0x10000000000000 + (b & 0xfffff) * 4294967296 + a;
      num = num / 0x20000000000000;
    }
    if (b >> 31) {
        num = -num;
    }
    return new types.Tuple([new types.Float(num), new types.Int(exp)]);
}
Example #2
0
    assert.equal(written, 2);
    assert.equal(buf[0], 0x61);
    assert.equal(buf[1], 0x00);
    assert.equal(buf[2], 0xFF);
    assert.equal(buf[3], 0xFF);
  });
}

{
  // test offset returns are correct
  const b = new Buffer(16);
  assert.equal(4, b.writeUInt32LE(0, 0));
  assert.equal(6, b.writeUInt16LE(0, 4));
  assert.equal(7, b.writeUInt8(0, 6));
  assert.equal(8, b.writeInt8(0, 7));
  assert.equal(16, b.writeDoubleLE(0, 8));
}

{
  // test unmatched surrogates not producing invalid utf8 output
  // ef bf bd = utf-8 representation of unicode replacement character
  // see https://codereview.chromium.org/121173009/
  const buf = new Buffer('ab\ud800cd', 'utf8');
  assert.equal(buf[0], 0x61);
  assert.equal(buf[1], 0x62);
  assert.equal(buf[2], 0xef);
  assert.equal(buf[3], 0xbf);
  assert.equal(buf[4], 0xbd);
  assert.equal(buf[5], 0x63);
  assert.equal(buf[6], 0x64);
}
Example #3
0
assert.throws(function() {
  var buf = new Buffer(16);
  buf.writeDoubleLE(0, 9);
}, RangeError);
Example #4
0
// a slice then .parent should be undefined.
assert.equal(buf.parent, undefined);
assert.equal(buf.buffer, ab);
assert.equal(buf.length, ab.byteLength);


buf.fill(0xC);
for (let i = 0; i < LENGTH; i++) {
  assert.equal(ui[i], 0xC);
  ui[i] = 0xF;
  assert.equal(buf[i], 0xF);
}

buf.writeUInt32LE(0xF00, 0);
buf.writeUInt32BE(0xB47, 4);
buf.writeDoubleLE(3.1415, 8);

assert.equal(dv.getUint32(0, true), 0xF00);
assert.equal(dv.getUint32(4), 0xB47);
assert.equal(dv.getFloat64(8, true), 3.1415);


// Now test protecting users from doing stupid things

assert.throws(function() {
  function AB() { }
  AB.__proto__ = ArrayBuffer;
  AB.prototype.__proto__ = ArrayBuffer.prototype;
  new Buffer(new AB());
}, TypeError);
Example #5
0
NetWriter.prototype.writeDoubleLE = function (value) {
    var buffer = new Buffer(8);
    buffer.writeDoubleLE(value, 0, true);
    this.content.push(buffer);
    this.size += 8;
};