Skip to content

Commit

Permalink
Make Tap#length a getter
Browse files Browse the repository at this point in the history
  • Loading branch information
valadaptive committed Sep 17, 2024
1 parent 2f8a010 commit 3941f74
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,15 @@ class Tap {
}
this.arr = buf;
this.pos = pos | 0;
this.length = buf.length;
if (this.pos < 0) {
throw new Error('negative offset');
}
}

get length() {
return this.arr.length;
}

reinitialize (capacity) {
this.setData(new Uint8Array(capacity));
}
Expand Down Expand Up @@ -584,9 +587,9 @@ class Tap {
* caller to always check that the read, skip, or write was valid by calling
* this method.
*/
isValid () { return this.pos <= this.length; }
isValid () { return this.pos <= this.arr.length; }

_invalidate () { this.pos = this.length + 1; }
_invalidate () { this.pos = this.arr.length + 1; }

// Read, skip, write methods.
//
Expand Down Expand Up @@ -660,7 +663,7 @@ class Tap {
readFloat () {
let pos = this.pos;
this.pos += 4;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return 0;
}
FLOAT_VIEW.setUint32(
Expand All @@ -678,7 +681,7 @@ class Tap {
writeFloat (f) {
let pos = this.pos;
this.pos += 4;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return;
}

Expand All @@ -693,7 +696,7 @@ class Tap {
readDouble () {
let pos = this.pos;
this.pos += 8;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return 0;
}
FLOAT_VIEW.setUint32(
Expand All @@ -720,7 +723,7 @@ class Tap {
writeDouble (d) {
let pos = this.pos;
this.pos += 8;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return;
}
FLOAT_VIEW.setFloat64(0, d, true);
Expand All @@ -739,7 +742,7 @@ class Tap {
readFixed (len) {
let pos = this.pos;
this.pos += len;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return;
}
return this.arr.slice(pos, pos + len);
Expand All @@ -751,7 +754,7 @@ class Tap {
len = len || buf.length;
let pos = this.pos;
this.pos += len;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return;
}
this.arr.set(buf.subarray(0, len), pos);
Expand Down Expand Up @@ -798,7 +801,7 @@ class Tap {
}
let pos = this.pos;
this.pos += len;
if (this.pos > this.length) {
if (this.pos > this.arr.length) {
return;
}

Expand Down

0 comments on commit 3941f74

Please sign in to comment.