From b41aad71a6b0d742cb83c16bcf53c0383de8d77e Mon Sep 17 00:00:00 2001 From: daurnimator Date: Thu, 27 Apr 2017 01:59:10 +1000 Subject: src/llex.js: getc should return -1 if no more input If the buffer was exhausted, the previous implementation could return undefined --- src/llex.js | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/llex.js b/src/llex.js index 703db49..ee7f993 100644 --- a/src/llex.js +++ b/src/llex.js @@ -90,26 +90,30 @@ class MBuffer { } getc() { - if (this.buffer instanceof DataView) - return this.n-- > 0 ? this.buffer.getUint8(this.off++, true) : this.fill(); - - return this.n-- > 0 ? this.buffer[this.off++] : this.fill(); + if (this.n <= 0) + this.fill(); + if (this.n <= 0) + return -1; + let r; + if (this.buffer instanceof DataView) { + r = this.buffer.getUint8(this.off++, true); + } else { + r = this.buffer[this.off++]; + } + if (this.n-- === 0) // remove reference to input so it can get freed + this.buffer = null; + return r; } fill() { if (this.reader) { this.buffer = this.reader(this.L, this.data); assert(typeof this.buffer !== "string", "Should only load binary of array of bytes"); - if (this.buffer === null) - return -1; - this.n = this.buffer instanceof DataView ? this.buffer.byteLength - 1 : this.buffer.length - 1; - this.off = 0; - } else return -1; - - if (this.buffer instanceof DataView) - return this.buffer.getUint8(this.off++, true); - - return this.buffer[this.off++]; + if (this.buffer !== null) { + this.n = ((this.buffer instanceof DataView) ? this.buffer.byteLength : this.buffer.length); + this.off = 0; + } + } } } -- cgit v1.2.3-70-g09d2