Skip to content

Commit 59a9a9b

Browse files
committed
buffer: add .read*() and .write*() methods to SlowBuffer prototype
Fixes #2138.
1 parent 06d71ad commit 59a9a9b

9 files changed

+101
-48
lines changed

lib/buffer.js

+29
Original file line numberDiff line numberDiff line change
@@ -1119,3 +1119,32 @@ Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
11191119
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
11201120
writeDouble(this, value, offset, true, noAssert);
11211121
};
1122+
1123+
SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8;
1124+
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE;
1125+
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE;
1126+
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE;
1127+
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE;
1128+
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8;
1129+
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE;
1130+
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE;
1131+
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE;
1132+
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE;
1133+
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE;
1134+
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE;
1135+
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE;
1136+
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE;
1137+
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8;
1138+
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE;
1139+
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE;
1140+
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE;
1141+
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE;
1142+
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8;
1143+
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE;
1144+
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE;
1145+
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE;
1146+
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE;
1147+
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE;
1148+
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE;
1149+
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE;
1150+
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE;

test/simple/test-readdouble.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
* Tests to verify we're reading in doubles correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

67
/*
78
* Test (64 bit) double
89
*/
9-
function test() {
10-
var buffer = new Buffer(8);
10+
function test(clazz) {
11+
var buffer = new clazz(8);
1112

1213
buffer[0] = 0x55;
1314
buffer[1] = 0x55;
@@ -104,4 +105,5 @@ function test() {
104105
}
105106

106107

107-
test();
108+
test(Buffer);
109+
test(SlowBuffer);

test/simple/test-readfloat.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
* Tests to verify we're reading in floats correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

67
/*
78
* Test (32 bit) float
89
*/
9-
function test() {
10-
var buffer = new Buffer(4);
10+
function test(clazz) {
11+
var buffer = new clazz(4);
1112

1213
buffer[0] = 0;
1314
buffer[1] = 0;
@@ -66,4 +67,5 @@ function test() {
6667
}
6768

6869

69-
test();
70+
test(Buffer);
71+
test(SlowBuffer);

test/simple/test-readint.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/*
22
* Tests to verify we're reading in signed integers correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

67
/*
78
* Test 8 bit signed integers
89
*/
9-
function test8() {
10-
var data = new Buffer(4);
10+
function test8(clazz) {
11+
var data = new clazz(4);
1112

1213
data[0] = 0x23;
1314
ASSERT.equal(0x23, data.readInt8(0));
@@ -26,8 +27,8 @@ function test8() {
2627
}
2728

2829

29-
function test16() {
30-
var buffer = new Buffer(6);
30+
function test16(clazz) {
31+
var buffer = new clazz(6);
3132

3233
buffer[0] = 0x16;
3334
buffer[1] = 0x79;
@@ -59,8 +60,8 @@ function test16() {
5960
}
6061

6162

62-
function test32() {
63-
var buffer = new Buffer(6);
63+
function test32(clazz) {
64+
var buffer = new clazz(6);
6465

6566
buffer[0] = 0x43;
6667
buffer[1] = 0x53;
@@ -91,6 +92,9 @@ function test32() {
9192
}
9293

9394

94-
test8();
95-
test16();
96-
test32();
95+
test8(Buffer);
96+
test8(SlowBuffer);
97+
test16(Buffer);
98+
test16(SlowBuffer);
99+
test32(Buffer);
100+
test32(SlowBuffer);

test/simple/test-readuint.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* A battery of tests to help us read a series of uints
33
*/
44

5+
var SlowBuffer = process.binding('buffer').SlowBuffer;
56
var ASSERT = require('assert');
67

78
/*
@@ -11,8 +12,8 @@ var ASSERT = require('assert');
1112
* - Correctly using the offsets
1213
* - Correctly interpreting values that are beyond the signed range as unsigned
1314
*/
14-
function test8() {
15-
var data = new Buffer(4);
15+
function test8(clazz) {
16+
var data = new clazz(4);
1617

1718
data[0] = 23;
1819
data[1] = 23;
@@ -36,8 +37,8 @@ function test8() {
3637
* - Correctly using the offsets
3738
* - Correctly interpreting values that are beyond the signed range as unsigned
3839
*/
39-
function test16() {
40-
var data = new Buffer(4);
40+
function test16(clazz) {
41+
var data = new clazz(4);
4142

4243
data[0] = 0;
4344
data[1] = 0x23;
@@ -64,8 +65,8 @@ function test16() {
6465
* - Correctly using the offsets
6566
* - Correctly interpreting values that are beyond the signed range as unsigned
6667
*/
67-
function test32() {
68-
var data = new Buffer(8);
68+
function test32(clazz) {
69+
var data = new clazz(8);
6970

7071
data[0] = 0x32;
7172
data[1] = 0x65;
@@ -82,6 +83,9 @@ function test32() {
8283
}
8384

8485

85-
test8();
86-
test16();
87-
test32();
86+
test8(Buffer);
87+
test8(SlowBuffer);
88+
test16(Buffer);
89+
test16(SlowBuffer);
90+
test32(Buffer);
91+
test32(SlowBuffer);

test/simple/test-writedouble.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
* Tests to verify we're writing doubles correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

6-
function test() {
7-
var buffer = new Buffer(16);
7+
function test(clazz) {
8+
var buffer = new clazz(16);
89

910
buffer.writeDoubleBE(2.225073858507201e-308, 0);
1011
buffer.writeDoubleLE(2.225073858507201e-308, 8);
@@ -103,4 +104,5 @@ function test() {
103104
}
104105

105106

106-
test();
107+
test(Buffer);
108+
test(SlowBuffer);

test/simple/test-writefloat.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
* Tests to verify we're writing floats correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

6-
function test() {
7-
var buffer = new Buffer(8);
7+
function test(clazz) {
8+
var buffer = new clazz(8);
89

910
buffer.writeFloatBE(1, 0);
1011
buffer.writeFloatLE(1, 4);
@@ -63,4 +64,5 @@ function test() {
6364
}
6465

6566

66-
test();
67+
test(Buffer);
68+
test(SlowBuffer);

test/simple/test-writeint.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
* Tests to verify we're writing signed integers correctly
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

6-
function test8() {
7-
var buffer = new Buffer(2);
7+
function test8(clazz) {
8+
var buffer = new clazz(2);
89

910
buffer.writeInt8(0x23, 0);
1011
buffer.writeInt8(-5, 1);
@@ -35,8 +36,8 @@ function test8() {
3536
}
3637

3738

38-
function test16() {
39-
var buffer = new Buffer(6);
39+
function test16(clazz) {
40+
var buffer = new clazz(6);
4041

4142
buffer.writeInt16BE(0x0023, 0);
4243
buffer.writeInt16LE(0x0023, 2);
@@ -88,8 +89,8 @@ function test16() {
8889
}
8990

9091

91-
function test32() {
92-
var buffer = new Buffer(8);
92+
function test32(clazz) {
93+
var buffer = new clazz(8);
9394

9495
buffer.writeInt32BE(0x23, 0);
9596
buffer.writeInt32LE(0x23, 4);
@@ -161,6 +162,9 @@ function test32() {
161162
}
162163

163164

164-
test8();
165-
test16();
166-
test32();
165+
test8(Buffer);
166+
test8(SlowBuffer);
167+
test16(Buffer);
168+
test16(SlowBuffer);
169+
test32(Buffer);
170+
test32(SlowBuffer);

test/simple/test-writeuint.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* A battery of tests to help us read a series of uints
33
*/
4+
var SlowBuffer = process.binding('buffer').SlowBuffer;
45
var ASSERT = require('assert');
56

67
/*
@@ -10,8 +11,8 @@ var ASSERT = require('assert');
1011
* - Correctly using the offsets
1112
* - Correctly interpreting values that are beyond the signed range as unsigned
1213
*/
13-
function test8() {
14-
var data = new Buffer(4);
14+
function test8(clazz) {
15+
var data = new clazz(4);
1516

1617
data.writeUInt8(23, 0);
1718
data.writeUInt8(23, 1);
@@ -39,9 +40,9 @@ function test8() {
3940
}
4041

4142

42-
function test16() {
43+
function test16(clazz) {
4344
var value = 0x2343;
44-
var data = new Buffer(4);
45+
var data = new clazz(4);
4546

4647
data.writeUInt16BE(value, 0);
4748
ASSERT.equal(0x23, data[0]);
@@ -78,8 +79,8 @@ function test16() {
7879
}
7980

8081

81-
function test32() {
82-
var data = new Buffer(6);
82+
function test32(clazz) {
83+
var data = new clazz(6);
8384
var value = 0xe7f90a6d;
8485

8586
data.writeUInt32BE(value, 0);
@@ -120,6 +121,9 @@ function test32() {
120121
}
121122

122123

123-
test8();
124-
test16();
125-
test32();
124+
test8(Buffer);
125+
test8(SlowBuffer);
126+
test16(Buffer);
127+
test16(SlowBuffer);
128+
test32(Buffer);
129+
test32(SlowBuffer);

0 commit comments

Comments
 (0)