From 319c40c4439a9eda7bd4a68769057cb12b04755a Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 2 Mar 2017 15:47:19 +0100 Subject: lua_load use reader function --- src/lapi.js | 2 +- src/ldo.js | 1 + src/llex.js | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 32 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lapi.js b/src/lapi.js index 36da7c6..72ff554 100644 --- a/src/lapi.js +++ b/src/lapi.js @@ -628,7 +628,7 @@ const lua_rawequal = function(L, index1, index2) { // TODO: reader is ignored because we don't implement ZIO const lua_load = function(L, reader, data, chunckname, mode) { - let z = new llex.MBuffer(data); + let z = new llex.MBuffer(L, data, reader); if (!chunckname) chunckname = "?"; let status = ldo.luaD_protectedparser(L, z, chunckname, mode); if (status === TS.LUA_OK) { /* no errors? */ diff --git a/src/ldo.js b/src/ldo.js index efb971d..668ea9b 100644 --- a/src/ldo.js +++ b/src/ldo.js @@ -547,6 +547,7 @@ const luaD_protectedparser = function(L, z, name, mode) { L.nny++; /* cannot yield during parsing */ p.z = z; + p.buff.L = L; p.name = name; p.mode = mode; p.dyd.actvar.arr = []; 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++]; } } -- cgit v1.2.3-70-g09d2