diff options
| author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-23 09:59:38 +0100 | 
|---|---|---|
| committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-23 09:59:38 +0100 | 
| commit | cd46ac257c623b4966ebf9f86523feff65746c38 (patch) | |
| tree | 310e985e47d2731558095a3c58e4a2eb8cb2ff28 | |
| parent | 99564913987698cdfc2528a93b5157030e637ced (diff) | |
| download | fengari-cd46ac257c623b4966ebf9f86523feff65746c38.tar.gz fengari-cd46ac257c623b4966ebf9f86523feff65746c38.tar.bz2 fengari-cd46ac257c623b4966ebf9f86523feff65746c38.zip | |
Handle binary in luaL_loadfilex
| -rw-r--r-- | src/lauxlib.js | 32 | ||||
| -rw-r--r-- | tests/load.js | 33 | ||||
| -rw-r--r-- | tests/loadfile-test.bc | bin | 0 -> 142 bytes | 
3 files changed, 58 insertions, 7 deletions
| diff --git a/src/lauxlib.js b/src/lauxlib.js index dbdb251..689abbb 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -410,9 +410,18 @@ if (typeof window === "undefined") {              this.f = null;  /* file being read */              this.buff = new Buffer(1024);  /* area for reading file */              this.pos = 0;  /* current position in file */ +            this.binary = false;          }      } +    const toDataView = function(buffer) { +        let ab = new ArrayBuffer(buffer.length); +        let au = new Uint8Array(ab); +        for (let i = 0; i < buffer.length; i++) +            au[i] = buffer[i]; +        return new DataView(ab); +    }; +      const getF = function(L, ud) {          let lf = ud;          let bytes = 0; @@ -423,7 +432,9 @@ if (typeof window === "undefined") {              bytes = fs.readSync(lf.f, lf.buff, 0, lf.buff.length, lf.pos); /* read block */              lf.pos += bytes;          } -        return bytes > 0 ? new lobject.TValue(0, lf.buff).jsstring() : null; // TODO: Here reading utf8 only +        if (bytes > 0) +            return lf.binary ? toDataView(lf.buff) : new lobject.TValue(0, lf.buff).jsstring(); // TODO: Here reading utf8 only +        else return null;      };      const errfile = function(L, what, fnameindex, error) { @@ -468,10 +479,17 @@ if (typeof window === "undefined") {              do {  /* skip first line */                  c = getc(lf);              } while (c && c !== '\n'.charCodeAt(0)); -            return getc(lf);  /* skip end-of-line, if present */ + +            return { +                skipped: true, +                c: getc(lf)  /* skip end-of-line, if present */ +            };          } else {              lf.pos--; -            return false; +            return { +                skipped: false, +                c: c +            };          }      }; @@ -491,12 +509,12 @@ if (typeof window === "undefined") {          }          try { -            let c; -            if ((c = skipcomment(lf)))  /* read initial portion */ +            let com; +            if ((com = skipcomment(lf)).skipped)  /* read initial portion */                  lf.buff[lf.n++] = '\n'.charCodeAt(0);  /* add line to correct line numbers */ -            if (c === lua.LUA_SIGNATURE.charCodeAt(0) && filename) {  /* binary file? */ -                // ... +            if (com.c === lua.LUA_SIGNATURE.charCodeAt(0) && filename) {  /* binary file? */ +                lf.binary = true;              }              let status = lapi.lua_load(L, getF, lf, lapi.lua_tostring(L, -1), mode); diff --git a/tests/load.js b/tests/load.js index ef37309..143549a 100644 --- a/tests/load.js +++ b/tests/load.js @@ -150,6 +150,39 @@ test('loadfile', function (t) {  }); +test('loadfile (binary)', function (t) { +    let luaCode = ` +        local f = loadfile("tests/loadfile-test.bc") +        return f() +    `, L; +     +    t.plan(3); + +    t.doesNotThrow(function () { + +        L = lauxlib.luaL_newstate(); + +        linit.luaL_openlibs(L); + +        lauxlib.luaL_loadstring(L, luaCode); + +    }, "Lua program loaded without error"); + +    t.doesNotThrow(function () { + +        lapi.lua_call(L, 0, -1); + +    }, "Lua program ran without error"); + +    t.strictEqual( +        lapi.lua_tostring(L, -1), +        "hello world", +        "Correct element(s) on the stack" +    ); + +}); + +  test('dofile', function (t) {      let luaCode = `          return dofile("tests/loadfile-test.lua") diff --git a/tests/loadfile-test.bc b/tests/loadfile-test.bcBinary files differ new file mode 100644 index 0000000..502b0ce --- /dev/null +++ b/tests/loadfile-test.bc | 
