8000 Properly work around empty backing buffers · protobufjs/bytebuffer.js@d5db121 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5db121

Browse files
committed
Properly work around empty backing buffers
1 parent 576870c commit d5db121

File tree

8 files changed

+91
-73
lines changed

8 files changed

+91
-73
lines changed

ByteBuffer.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
* @const
111111
* @expose
112112
*/
113-
ByteBuffer.VERSION = "2.0.2";
113+
ByteBuffer.VERSION = "2.1.0";
114114

115115
/**
116116
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -259,7 +259,7 @@
259259
*/
260260
ByteBuffer.prototype.resize = function(capacity) {
261261
if (capacity < 1) return false;
262-
if (this.array == null) { // Silently recreate
262+
if (this.array === null) { // Silently recreate
263263
this.array = new ArrayBuffer(capacity);
264264
this.view = new DataView(this.array);
265265
}
@@ -310,10 +310,10 @@
310310
* @expose
311311
*/
312312
ByteBuffer.prototype.ensureCapacity = function(capacity) {
313-
if (this.array == null) {
313+
if (this.array === null)
314314
return this.resize(capacity);
315-
}
316-
if (this.array.byteLength < capacity) return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
315+
if (this.array.byteLength < capacity)
316+
return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
317317
return this;
318318
};
319319

@@ -440,7 +440,9 @@
440440
this.flip();
441441
}
442442
if (this.offset === this.length) {
443-
throw(new Error(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"));
443+
this.array = new ArrayBuffer(0);
444+
this.view = null; // A DataView on a zero-length AB would throw
445+
return this;
444446
}
445447
if (this.offset === 0 && this.length === this.array.byteLength) {
446448
return this; // Already compacted
@@ -1593,11 +1595,10 @@
15931595
/**
15941596
* Base64 alphabet.
15951597
* @type {string}
1596-
* @const
15971598
* @inner
15981599
*/
15991600
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1600-
// FIXME: CC inlines this, which is not so smart regarding script size. Any ideas?
1601+
B64 = B64+""; // Prevent CC from inlining this for less code size
16011602

16021603
/**
16031604
* Encodes a ByteBuffer's contents to a base64 string.

ByteBuffer.min.js

Lines changed: 44 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ByteBuffer.min.map

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

ByteBuffer.noexpose.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
* @type {string}
103103
* 9E88 @const
104104
*/
105-
ByteBuffer.VERSION = "2.0.2";
105+
ByteBuffer.VERSION = "2.1.0";
106106

107107
/**
108108
* Default buffer capacity of `16`. The ByteBuffer will be automatically resized by a factor of 2 if required.
@@ -242,7 +242,7 @@
242242
*/
243243
ByteBuffer.prototype.resize = function(capacity) {
244244
if (capacity < 1) return false;
245-
if (this.array == null) { // Silently recreate
245+
if (this.array === null) { // Silently recreate
246246
this.array = new ArrayBuffer(capacity);
247247
this.view = new DataView(this.array);
248248
}
@@ -291,10 +291,10 @@
291291
* @returns {!ByteBuffer} this
292292
*/
293293
ByteBuffer.prototype.ensureCapacity = function(capacity) {
294-
if (this.array == null) {
294+
if (this.array === null)
295295
return this.resize(capacity);
296-
}
297-
if (this.array.byteLength < capacity) return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
296+
if (this.array.byteLength < capacity)
297+
return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
298298
return this;
299299
};
300300

@@ -413,7 +413,9 @@
413413
this.flip();
414414
}
415415
if (this.offset === this.length) {
416-
throw(new Error(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"));
416+
this.array = new ArrayBuffer(0);
417+
this.view = null; // A DataView on a zero-length AB would throw
418+
return this;
417419
}
418420
if (this.offset === 0 && this.length === this.array.byteLength) {
419421
return this; // Already compacted
@@ -1507,11 +1509,10 @@
15071509
/**
15081510
* Base64 alphabet.
15091511
* @type {string}
1510-
* @const
15111512
* @inner
15121513
*/
15131514
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1514-
// FIXME: CC inlines this, which is not so smart regarding script size. Any ideas?
1515+
B64 = B64+""; // Prevent CC from inlining this for less code size
15151516

15161517
/**
15171518
* Encodes a ByteBuffer's contents to a base64 string.

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bytebuffer",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"author": "Daniel Wirtz <dcode@dcode.io>",
55
"description": "A full-featured ByteBuffer implementation using typed arrays.",
66
"main": "ByteBuffer.js",

src/ByteBuffer.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@
259259
*/
260260
ByteBuffer.prototype.resize = function(capacity) {
261261
if (capacity < 1) return false;
262-
if (this.array == null) { // Silently recreate
262+
if (this.array === null) { // Silently recreate
263263
this.array = new ArrayBuffer(capacity);
264264
this.view = new DataView(this.array);
265265
}
@@ -310,10 +310,10 @@
310310
* @expose
311311
*/
312312
ByteBuffer.prototype.ensureCapacity = function(capacity) {
313-
if (this.array == null) {
313+
if (this.array === null)
314314
return this.resize(capacity);
315-
}
316-
if (this.array.byteLength < capacity) return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
315+
if (this.array.byteLength < capacity)
316+
return this.resize(this.array.byteLength*2 >= capacity ? this.array.byteLength*2 : capacity);
317317
return this;
318318
};
319319

@@ -440,7 +440,9 @@
440440
this.flip();
441441
}
442442
if (this.offset === this.length) {
443-
throw(new Error(this+" cannot be compacted: Offset ("+this.offset+") is equal to its length ("+this.length+")"));
443+
this.array = new ArrayBuffer(0);
444+
this.view = null; // A DataView on a zero-length AB would throw
445+
return this;
444446
}
445447
if (this.offset === 0 && this.length === this.array.byteLength) {
446448
return this; // Already compacted
@@ -1593,11 +1595,10 @@
15931595
/**
15941596
* Base64 alphabet.
15951597
* @type {string}
1596-
* @const
15971598
* @inner
15981599
*/
15991600
var B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
1600-
// FIXME: CC inlines this, which is not so smart regarding script size. Any ideas?
1601+
B64 = B64+""; // Prevent CC from inlining this for less code size
16011602

16021603
/**
16031604
* Encodes a ByteBuffer's contents to a base64 string.

tests/suite.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,22 @@ var suite = {
233233
test.equal(bb.length, 1);
234234
test.done();
235235
},
236+
237+
"compactEmpty": function(test) {
238+
var bb = new ByteBuffer(2);
239+
bb.compact();
240+
test.strictEqual(bb.offset, 0);
241+
test.strictEqual(bb.length, 0);
242+
test.strictEqual(bb.view, null); // Special case
243+
test.strictEqual(bb.array.byteLength, 0);
244+
bb.writeInt32(0xFFFFFFFF);
245+
bb.flip();
246+
test.strictEqual(bb.offset, 0);
247+
test.strictEqual(bb.length, 4);
248+
test.notStrictEqual(bb.view, null);
249+
test.strictEqual(bb.array.byteLength, 4); // Cannot double 0, so it takes 32bits
250+
test.done();
251+
},
236252

237253
"destroy": function(test) {
238254
var bb = new ByteBuffer(1);

0 commit comments

Comments
 (0)
0