summaryrefslogtreecommitdiff
path: root/src/llex.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-02 15:47:19 +0100
committerBenoit Giannangeli <giann008@gmail.com>2017-03-02 20:45:06 +0100
commit319c40c4439a9eda7bd4a68769057cb12b04755a (patch)
tree76541920c3594e9a2e2f4c3a973f1815f9358ee1 /src/llex.js
parent2e83b8d2e3ac6c30a53e7919a1808b732aeff5a4 (diff)
downloadfengari-319c40c4439a9eda7bd4a68769057cb12b04755a.tar.gz
fengari-319c40c4439a9eda7bd4a68769057cb12b04755a.tar.bz2
fengari-319c40c4439a9eda7bd4a68769057cb12b04755a.zip
lua_load use reader function
Diffstat (limited to 'src/llex.js')
-rw-r--r--src/llex.js35
1 files changed, 30 insertions, 5 deletions
diff --git a/src/llex.js b/src/llex.js
index dab384d..711f44b 100644
--- a/src/llex.js
+++ b/src/llex.js
@@ -73,17 +73,42 @@ const luaX_tokens = [
const NUM_RESERVED = Object.keys(RESERVED).length;
class MBuffer {
- constructor(data) {
- this.buffer = typeof data === "string" ? data.split('') : (data ? data : []);
- this.n = this.buffer instanceof DataView ? this.buffer.byteLength : this.buffer.length;
+ constructor(L, data, reader) {
+ this.L = L;
+ this.data = data;
+ this.n = 0;
+ this.buffer = null;
this.off = 0;
+ this.reader = reader ? reader : null;
+
+ if (!this.reader) {
+ this.buffer = typeof data === "string" ? data.split('') : (data ? data : []);
+ this.n = this.buffer instanceof DataView ? this.buffer.byteLength : this.buffer.length;
+ this.off = 0;
+ }
}
getc() {
if (this.buffer instanceof DataView)
- return this.n-- > 0 ? this.buffer.getUint8(this.off++, true) : -1;
+ return this.n-- > 0 ? this.buffer.getUint8(this.off++, true) : this.fill();
+
+ return this.n-- > 0 ? this.buffer[this.off++] : this.fill();
+ }
+
+ fill() {
+ if (this.reader) {
+ this.buffer = this.reader(this.L, this.data);
+ this.buffer = typeof this.buffer === "string" ? this.buffer.split('') : this.buffer;
+ 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.n-- > 0 ? this.buffer[this.off++] : -1;
+ return this.buffer[this.off++];
}
}