summaryrefslogtreecommitdiff
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
parent2e83b8d2e3ac6c30a53e7919a1808b732aeff5a4 (diff)
downloadfengari-319c40c4439a9eda7bd4a68769057cb12b04755a.tar.gz
fengari-319c40c4439a9eda7bd4a68769057cb12b04755a.tar.bz2
fengari-319c40c4439a9eda7bd4a68769057cb12b04755a.zip
lua_load use reader function
-rw-r--r--.vscode/launch.json27
-rw-r--r--.vscode/settings.json3
-rw-r--r--src/lapi.js2
-rw-r--r--src/ldo.js1
-rw-r--r--src/llex.js35
-rw-r--r--tests/lexparse.js216
-rw-r--r--tests/llex.js97
-rw-r--r--tests/load.js68
8 files changed, 221 insertions, 228 deletions
diff --git a/.vscode/launch.json b/.vscode/launch.json
deleted file mode 100644
index 8d42b62..0000000
--- a/.vscode/launch.json
+++ /dev/null
@@ -1,27 +0,0 @@
-{
- // Use IntelliSense to learn about possible Node.js debug attributes.
- // Hover to view descriptions of existing attributes.
- // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
- "version": "0.2.0",
- "configurations": [
- {
- "type": "node",
- "request": "launch",
- "name": "Launch Program",
- "program": "${workspaceRoot}/index.js"
- },
- {
- "type": "node",
- "request": "launch",
- "name": "Tests",
- "program": "${workspaceRoot}/tests/lexparse.js"
- },
- {
- "type": "node",
- "request": "attach",
- "name": "Attach to Process",
- "address": "localhost",
- "port": 5858
- }
- ]
-} \ No newline at end of file
diff --git a/.vscode/settings.json b/.vscode/settings.json
deleted file mode 100644
index 0c4a669..0000000
--- a/.vscode/settings.json
+++ /dev/null
@@ -1,3 +0,0 @@
-{
- "vsicons.presets.angular": false
-} \ No newline at end of file
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++];
}
}
diff --git a/tests/lexparse.js b/tests/lexparse.js
index 0226e92..cd80e43 100644
--- a/tests/lexparse.js
+++ b/tests/lexparse.js
@@ -27,7 +27,13 @@ test('LOADK, RETURN', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -61,7 +67,13 @@ test('MOVE', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -95,7 +107,13 @@ test('Binary op', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -129,7 +147,13 @@ test('Unary op, LOADBOOL', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -161,7 +185,13 @@ test('NEWTABLE', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -197,7 +227,13 @@ test('CALL', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -237,7 +273,13 @@ test('Multiple return', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -272,7 +314,13 @@ test('TAILCALL', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -307,7 +355,13 @@ test('VARARG', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -340,7 +394,13 @@ test('LE, JMP', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -373,7 +433,13 @@ test('LT', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -406,7 +472,13 @@ test('EQ', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -440,7 +512,13 @@ test('TESTSET (and)', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -474,7 +552,13 @@ test('TESTSET (or)', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -512,7 +596,13 @@ test('TEST (false)', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -549,7 +639,13 @@ test('FORPREP, FORLOOP (int)', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -586,7 +682,13 @@ test('FORPREP, FORLOOP (float)', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -622,7 +724,13 @@ test('SETTABLE, GETTABLE', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -667,7 +775,13 @@ test('SETUPVAL, GETUPVAL', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -703,7 +817,13 @@ test('SETTABUP, GETTABUP', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -747,7 +867,13 @@ test('SELF', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -780,7 +906,13 @@ test('SETLIST', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -817,7 +949,13 @@ test('Variable SETLIST', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -849,7 +987,13 @@ test('Long SETLIST', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -898,7 +1042,13 @@ test('TFORCALL, TFORLOOP', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -933,7 +1083,13 @@ test('LEN', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
@@ -976,7 +1132,13 @@ test('CONCAT', function (t) {
linit.luaL_openlibs(L);
- lapi.lua_load(L, null, luaCode, "test", "text");
+ let reader = function(L, data) {
+ let code = luaCode;
+ luaCode = null;
+ return code;
+ };
+
+ lapi.lua_load(L, reader, luaCode, "test", "text");
}, "Lua program loaded without error");
diff --git a/tests/llex.js b/tests/llex.js
deleted file mode 100644
index 8b69acc..0000000
--- a/tests/llex.js
+++ /dev/null
@@ -1,97 +0,0 @@
-"use strict";
-
-const test = require('tape');
-const beautify = require('js-beautify').js_beautify;
-
-const tests = require("./tests.js");
-
-const lapi = require("../src/lapi.js");
-const lauxlib = require("../src/lauxlib.js");
-const llex = require("../src/llex.js");
-const lua = require('../src/lua.js');
-const R = llex.RESERVED;
-
-
-test('basic lexing: TK_RETURN, TK_STRING', function (t) {
- let luaCode = `
- return "hello lex !"
- `, L;
-
- t.plan(2);
-
- let readTokens = [];
-
- t.doesNotThrow(function () {
-
- L = lauxlib.luaL_newstate();
-
- let ls = new llex.LexState();
- llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0));
-
- llex.luaX_next(ls);
-
- while (ls.t.token !== R.TK_EOS) {
- // console.log(llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]);
-
- readTokens.push(ls.t.token);
- llex.luaX_next(ls);
- }
-
-
- }, "JS Lua program ran without error");
-
- t.deepEqual(
- readTokens,
- [R.TK_RETURN, R.TK_STRING],
- "Correct tokens found"
- );
-
-});
-
-
-test('TK_LOCAL, TK_NAME, TK_INT', function (t) {
- let luaCode = `
- local f = testing(aBunch)
- return "of"
- end
-
- return f("things") + 12
- `, L;
-
- t.plan(2);
-
- let readTokens = [];
-
- t.doesNotThrow(function () {
-
- L = lauxlib.luaL_newstate();
-
- let ls = new llex.LexState();
- llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0));
-
- llex.luaX_next(ls);
-
- while (ls.t.token !== R.TK_EOS) {
- // console.log(ls.t.token >= llex.FIRST_RESERVED ?
- // llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]
- // : ls.t.token);
-
- readTokens.push(ls.t.token);
- llex.luaX_next(ls);
- }
-
-
- }, "JS Lua program ran without error");
-
- t.deepEqual(
- readTokens,
- [
- R.TK_LOCAL, R.TK_NAME, '=', R.TK_NAME, '(', R.TK_NAME, ')',
- R.TK_RETURN, R.TK_STRING,
- R.TK_END,
- R.TK_RETURN, R.TK_NAME, '(', R.TK_STRING, ')', '+', R.TK_INT
- ],
- "Correct tokens found"
- );
-
-}); \ No newline at end of file
diff --git a/tests/load.js b/tests/load.js
deleted file mode 100644
index a71de6b..0000000
--- a/tests/load.js
+++ /dev/null
@@ -1,68 +0,0 @@
-"use strict";
-
-const test = require('tape');
-const beautify = require('js-beautify').js_beautify;
-
-const tests = require("./tests.js");
-const toByteCode = tests.toByteCode;
-
-const lapi = require("../src/lapi.js");
-const lauxlib = require("../src/lauxlib.js");
-const linit = require('../src/linit.js');
-
-test('lua_load, binary mode', function (t) {
- let luaCode = `
- return "hello world"
- `, L;
-
- t.plan(2);
-
- t.doesNotThrow(function () {
-
- let bc = toByteCode(luaCode).dataView;
-
- L = lauxlib.luaL_newstate();
-
- linit.luaL_openlibs(L);
-
- lapi.lua_load(L, null, bc, "test-load", "binary");
-
- lapi.lua_call(L, 0, -1);
-
- }, "JS Lua program ran without error");
-
- t.strictEqual(
- lapi.lua_tostring(L, -1),
- "hello world",
- "Correct element(s) on the stack"
- );
-
-});
-
-
-test('lua_load, text mode', function (t) {
- let luaCode = `
- return "hello world"
- `, L;
-
- t.plan(2);
-
- t.doesNotThrow(function () {
-
- L = lauxlib.luaL_newstate();
-
- linit.luaL_openlibs(L);
-
- lapi.lua_load(L, null, luaCode, "test-load", "text");
-
- lapi.lua_call(L, 0, -1);
-
- }, "JS Lua program ran without error");
-
- t.strictEqual(
- lapi.lua_tostring(L, -1),
- "hello world",
- "Correct element(s) on the stack"
- );
-
-}); \ No newline at end of file