diff options
author | daurnimator <quae@daurnimator.com> | 2017-04-27 01:59:10 +1000 |
---|---|---|
committer | daurnimator <quae@daurnimator.com> | 2017-04-27 02:32:21 +1000 |
commit | b41aad71a6b0d742cb83c16bcf53c0383de8d77e (patch) | |
tree | 97ef0cee8a4b3597107236851e4527dd9a23de2d /src/llex.js | |
parent | 0ad47e19d91dbfc04f890cdbbd09f7cf9f3e9d5f (diff) | |
download | fengari-b41aad71a6b0d742cb83c16bcf53c0383de8d77e.tar.gz fengari-b41aad71a6b0d742cb83c16bcf53c0383de8d77e.tar.bz2 fengari-b41aad71a6b0d742cb83c16bcf53c0383de8d77e.zip |
src/llex.js: getc should return -1 if no more input
If the buffer was exhausted, the previous implementation could return undefined
Diffstat (limited to 'src/llex.js')
-rw-r--r-- | src/llex.js | 32 |
1 files changed, 18 insertions, 14 deletions
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; + } + } } } |