From 59a9a9b5b02a933cb48f86f625faf2587d38b20c Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Thu, 17 Nov 2011 14:14:43 +0100 Subject: [PATCH] buffer: add .read*() and .write*() methods to SlowBuffer prototype Fixes #2138. --- lib/buffer.js | 29 +++++++++++++++++++++++++++++ test/simple/test-readdouble.js | 8 +++++--- test/simple/test-readfloat.js | 8 +++++--- test/simple/test-readint.js | 22 +++++++++++++--------- test/simple/test-readuint.js | 22 +++++++++++++--------- test/simple/test-writedouble.js | 8 +++++--- test/simple/test-writefloat.js | 8 +++++--- test/simple/test-writeint.js | 22 +++++++++++++--------- test/simple/test-writeuint.js | 22 +++++++++++++--------- 9 files changed, 101 insertions(+), 48 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index dbf1b3b3f6c..ebeab7d5226 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -1119,3 +1119,32 @@ Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) { Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) { writeDouble(this, value, offset, true, noAssert); }; + +SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8; +SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE; +SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE; +SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE; +SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE; +SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8; +SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE; +SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE; +SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE; +SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE; +SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE; +SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE; +SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE; +SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE; +SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8; +SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE; +SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE; +SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE; +SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE; +SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8; +SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE; +SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE; +SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE; +SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE; +SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE; +SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE; +SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE; +SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE; diff --git a/test/simple/test-readdouble.js b/test/simple/test-readdouble.js index 9008f8673c2..d0b3605ddc2 100644 --- a/test/simple/test-readdouble.js +++ b/test/simple/test-readdouble.js @@ -1,13 +1,14 @@ /* * Tests to verify we're reading in doubles correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); /* * Test (64 bit) double */ -function test() { - var buffer = new Buffer(8); +function test(clazz) { + var buffer = new clazz(8); buffer[0] = 0x55; buffer[1] = 0x55; @@ -104,4 +105,5 @@ function test() { } -test(); +test(Buffer); +test(SlowBuffer); diff --git a/test/simple/test-readfloat.js b/test/simple/test-readfloat.js index bdfa7cf9cd3..91607c6451d 100644 --- a/test/simple/test-readfloat.js +++ b/test/simple/test-readfloat.js @@ -1,13 +1,14 @@ /* * Tests to verify we're reading in floats correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); /* * Test (32 bit) float */ -function test() { - var buffer = new Buffer(4); +function test(clazz) { + var buffer = new clazz(4); buffer[0] = 0; buffer[1] = 0; @@ -66,4 +67,5 @@ function test() { } -test(); +test(Buffer); +test(SlowBuffer); diff --git a/test/simple/test-readint.js b/test/simple/test-readint.js index 99569fae803..bd39078e87d 100644 --- a/test/simple/test-readint.js +++ b/test/simple/test-readint.js @@ -1,13 +1,14 @@ /* * Tests to verify we're reading in signed integers correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); /* * Test 8 bit signed integers */ -function test8() { - var data = new Buffer(4); +function test8(clazz) { + var data = new clazz(4); data[0] = 0x23; ASSERT.equal(0x23, data.readInt8(0)); @@ -26,8 +27,8 @@ function test8() { } -function test16() { - var buffer = new Buffer(6); +function test16(clazz) { + var buffer = new clazz(6); buffer[0] = 0x16; buffer[1] = 0x79; @@ -59,8 +60,8 @@ function test16() { } -function test32() { - var buffer = new Buffer(6); +function test32(clazz) { + var buffer = new clazz(6); buffer[0] = 0x43; buffer[1] = 0x53; @@ -91,6 +92,9 @@ function test32() { } -test8(); -test16(); -test32(); +test8(Buffer); +test8(SlowBuffer); +test16(Buffer); +test16(SlowBuffer); +test32(Buffer); +test32(SlowBuffer); diff --git a/test/simple/test-readuint.js b/test/simple/test-readuint.js index 4fdda1b3fe0..a3926c1d17b 100644 --- a/test/simple/test-readuint.js +++ b/test/simple/test-readuint.js @@ -2,6 +2,7 @@ * A battery of tests to help us read a series of uints */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); /* @@ -11,8 +12,8 @@ var ASSERT = require('assert'); * - Correctly using the offsets * - Correctly interpreting values that are beyond the signed range as unsigned */ -function test8() { - var data = new Buffer(4); +function test8(clazz) { + var data = new clazz(4); data[0] = 23; data[1] = 23; @@ -36,8 +37,8 @@ function test8() { * - Correctly using the offsets * - Correctly interpreting values that are beyond the signed range as unsigned */ -function test16() { - var data = new Buffer(4); +function test16(clazz) { + var data = new clazz(4); data[0] = 0; data[1] = 0x23; @@ -64,8 +65,8 @@ function test16() { * - Correctly using the offsets * - Correctly interpreting values that are beyond the signed range as unsigned */ -function test32() { - var data = new Buffer(8); +function test32(clazz) { + var data = new clazz(8); data[0] = 0x32; data[1] = 0x65; @@ -82,6 +83,9 @@ function test32() { } -test8(); -test16(); -test32(); +test8(Buffer); +test8(SlowBuffer); +test16(Buffer); +test16(SlowBuffer); +test32(Buffer); +test32(SlowBuffer); diff --git a/test/simple/test-writedouble.js b/test/simple/test-writedouble.js index de8c9378fa6..1d04974372a 100644 --- a/test/simple/test-writedouble.js +++ b/test/simple/test-writedouble.js @@ -1,10 +1,11 @@ /* * Tests to verify we're writing doubles correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); -function test() { - var buffer = new Buffer(16); +function test(clazz) { + var buffer = new clazz(16); buffer.writeDoubleBE(2.225073858507201e-308, 0); buffer.writeDoubleLE(2.225073858507201e-308, 8); @@ -103,4 +104,5 @@ function test() { } -test(); +test(Buffer); +test(SlowBuffer); diff --git a/test/simple/test-writefloat.js b/test/simple/test-writefloat.js index 93e2596e201..2a08667b444 100644 --- a/test/simple/test-writefloat.js +++ b/test/simple/test-writefloat.js @@ -1,10 +1,11 @@ /* * Tests to verify we're writing floats correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); -function test() { - var buffer = new Buffer(8); +function test(clazz) { + var buffer = new clazz(8); buffer.writeFloatBE(1, 0); buffer.writeFloatLE(1, 4); @@ -63,4 +64,5 @@ function test() { } -test(); +test(Buffer); +test(SlowBuffer); diff --git a/test/simple/test-writeint.js b/test/simple/test-writeint.js index 77a4fea0b86..eff92e06ea5 100644 --- a/test/simple/test-writeint.js +++ b/test/simple/test-writeint.js @@ -1,10 +1,11 @@ /* * Tests to verify we're writing signed integers correctly */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); -function test8() { - var buffer = new Buffer(2); +function test8(clazz) { + var buffer = new clazz(2); buffer.writeInt8(0x23, 0); buffer.writeInt8(-5, 1); @@ -35,8 +36,8 @@ function test8() { } -function test16() { - var buffer = new Buffer(6); +function test16(clazz) { + var buffer = new clazz(6); buffer.writeInt16BE(0x0023, 0); buffer.writeInt16LE(0x0023, 2); @@ -88,8 +89,8 @@ function test16() { } -function test32() { - var buffer = new Buffer(8); +function test32(clazz) { + var buffer = new clazz(8); buffer.writeInt32BE(0x23, 0); buffer.writeInt32LE(0x23, 4); @@ -161,6 +162,9 @@ function test32() { } -test8(); -test16(); -test32(); +test8(Buffer); +test8(SlowBuffer); +test16(Buffer); +test16(SlowBuffer); +test32(Buffer); +test32(SlowBuffer); diff --git a/test/simple/test-writeuint.js b/test/simple/test-writeuint.js index e67c153a9e9..a9c97e04c1c 100644 --- a/test/simple/test-writeuint.js +++ b/test/simple/test-writeuint.js @@ -1,6 +1,7 @@ /* * A battery of tests to help us read a series of uints */ +var SlowBuffer = process.binding('buffer').SlowBuffer; var ASSERT = require('assert'); /* @@ -10,8 +11,8 @@ var ASSERT = require('assert'); * - Correctly using the offsets * - Correctly interpreting values that are beyond the signed range as unsigned */ -function test8() { - var data = new Buffer(4); +function test8(clazz) { + var data = new clazz(4); data.writeUInt8(23, 0); data.writeUInt8(23, 1); @@ -39,9 +40,9 @@ function test8() { } -function test16() { +function test16(clazz) { var value = 0x2343; - var data = new Buffer(4); + var data = new clazz(4); data.writeUInt16BE(value, 0); ASSERT.equal(0x23, data[0]); @@ -78,8 +79,8 @@ function test16() { } -function test32() { - var data = new Buffer(6); +function test32(clazz) { + var data = new clazz(6); var value = 0xe7f90a6d; data.writeUInt32BE(value, 0); @@ -120,6 +121,9 @@ function test32() { } -test8(); -test16(); -test32(); +test8(Buffer); +test8(SlowBuffer); +test16(Buffer); +test16(SlowBuffer); +test32(Buffer); +test32(SlowBuffer);