aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md2
-rw-r--r--src/lauxlib.js10
-rw-r--r--src/ldebug.js2
-rw-r--r--src/lfunc.js5
-rw-r--r--src/lobject.js19
-rw-r--r--src/lundump.js18
-rw-r--r--tests/tests.js6
7 files changed, 29 insertions, 33 deletions
diff --git a/README.md b/README.md
index c14ff90..c0fd4f3 100644
--- a/README.md
+++ b/README.md
@@ -73,8 +73,6 @@
- [ ] luaL_addsize
- [ ] luaL_checkoption
- [ ] luaL_checkversion
- - [ ] luaL_dofile
- - [ ] luaL_dostring
- [ ] luaL_gsub
- [ ] luaL_newlibtable
- [ ] luaL_optnumber
diff --git a/src/lauxlib.js b/src/lauxlib.js
index 68e7c12..1921237 100644
--- a/src/lauxlib.js
+++ b/src/lauxlib.js
@@ -403,6 +403,10 @@ const luaL_loadstring = function(L, s) {
return luaL_loadbuffer(L, s, s.length, s);
};
+const luaL_dostring = function(L, s) {
+ return (luaL_loadstring(L, s) || lua.lua_pcall(L, 0, lua.LUA_MULTRET, 0));
+};
+
const luaL_getmetafield = function(L, obj, event) {
if (!lua.lua_getmetatable(L, obj))
return lua.LUA_TNIL;
@@ -699,6 +703,11 @@ if (typeof require === "function") {
return luaL_loadfilex(L, filename, null);
};
+ const luaL_dofile = function(L, filename) {
+ return (luaL_loadfile(L, filename) || lua.lua_pcall(L, 0, lua.LUA_MULTRET, 0));
+ };
+
+ module.exports.luaL_dofile = luaL_dofile;
module.exports.luaL_loadfilex = luaL_loadfilex;
module.exports.luaL_loadfile = luaL_loadfile;
}
@@ -729,6 +738,7 @@ module.exports.luaL_checkstack = luaL_checkstack;
module.exports.luaL_checkstring = luaL_checkstring;
module.exports.luaL_checktype = luaL_checktype;
module.exports.luaL_checkudata = luaL_checkudata;
+module.exports.luaL_dostring = luaL_dostring;
module.exports.luaL_error = luaL_error;
module.exports.luaL_execresult = luaL_execresult;
module.exports.luaL_fileresult = luaL_fileresult;
diff --git a/src/ldebug.js b/src/ldebug.js
index c9c4c1e..d429457 100644
--- a/src/ldebug.js
+++ b/src/ldebug.js
@@ -183,7 +183,7 @@ const collectvalidlines = function(L, f) {
let t = new TValue(CT.LUA_TTABLE, ltable.luaH_new(L));
L.stack[L.top++] = t;
assert(L.top <= L.ci.top, "stack overflow");
- let v = new TValue(true, CT.LUA_TBOOLEAN);
+ let v = new TValue(CT.LUA_TBOOLEAN, true);
for (let i = 0; i < f.l.p.length; i++)
lobject.table_newindex(t, lineinfo[i], v);
}
diff --git a/src/lfunc.js b/src/lfunc.js
index 3f8fe2d..69c7581 100644
--- a/src/lfunc.js
+++ b/src/lfunc.js
@@ -56,10 +56,7 @@ class UpVal {
}
const luaF_newLclosure = function(L, n) {
- let c = new lobject.LClosure();
- c.p = null;
- c.nupvalues = n;
- while (n--) c.upvals[n] = null;
+ let c = new lobject.LClosure(L, n);
return c;
};
diff --git a/src/lobject.js b/src/lobject.js
index 5a202d4..784720b 100644
--- a/src/lobject.js
+++ b/src/lobject.js
@@ -7,9 +7,7 @@ const defs = require('./defs.js');
const ljstype = require('./ljstype.js');
const luaconf = require('./luaconf.js');
const llimit = require('./llimit.js');
-const ltable = require('./ltable.js');
const CT = defs.constant_types;
-const UpVal = require('./lfunc.js').UpVal;
const char = defs.char;
let tvalueCount = 0;
@@ -147,6 +145,11 @@ class TValue {
this.value = x;
}
+ sethvalue(x) {
+ this.type = CT.LUA_TTABLE;
+ this.value = x;
+ }
+
setfrom(tv) { /* in lua C source setobj2t is often used for this */
this.type = tv.type;
this.value = tv.value;
@@ -201,17 +204,7 @@ class LClosure {
constructor(L, n) {
this.p = null;
this.nupvalues = n;
-
- let _ENV = new UpVal(L);
- _ENV.refcount = 0;
- _ENV.v = null;
- _ENV.u.open.next = null;
- _ENV.u.open.touched = true;
- _ENV.u.value = new TValue(CT.LUA_TTABLE, ltable.luaH_new(L));
-
- this.upvals = [
- _ENV
- ];
+ this.upvals = Array(n);
}
}
diff --git a/src/lundump.js b/src/lundump.js
index 9162501..1d2d079 100644
--- a/src/lundump.js
+++ b/src/lundump.js
@@ -1,12 +1,12 @@
/*jshint esversion: 6 */
"use strict";
-const assert = require('assert');
+const assert = require('assert');
-const defs = require('./defs.js');
-const lobject = require('./lobject.js');
-const Proto = require('./lfunc.js').Proto;
-const OpCodes = require('./lopcodes.js');
+const defs = require('./defs.js');
+const lfunc = require('./lfunc.js');
+const lobject = require('./lobject.js');
+const lopcodes = require('./lopcodes.js');
const LUAI_MAXSHORTLEN = 40;
@@ -128,7 +128,7 @@ class BytecodeParser {
readCode(f) {
let n = this.readInt();
- let o = OpCodes;
+ let o = lopcodes;
let p = BytecodeParser;
for (let i = 0; i < n; i++) {
@@ -191,7 +191,7 @@ class BytecodeParser {
let n = this.readInt();
for (let i = 0; i < n; i++) {
- f.p[i] = new Proto(this.L);
+ f.p[i] = new lfunc.Proto(this.L);
this.readFunction(f.p[i], f.source);
}
}
@@ -280,12 +280,12 @@ class BytecodeParser {
luaU_undump() {
this.checkHeader();
- let cl = new lobject.LClosure(this.L, this.readByte());
+ let cl = lfunc.luaF_newLclosure(this.L, this.readByte());
this.L.stack[this.L.top] = new lobject.TValue(defs.CT.LUA_TLCL, cl);
this.L.top++;
- cl.p = new Proto();
+ cl.p = new lfunc.Proto();
this.readFunction(cl.p);
diff --git a/tests/tests.js b/tests/tests.js
index 6de0db3..217f1b8 100644
--- a/tests/tests.js
+++ b/tests/tests.js
@@ -4,7 +4,7 @@ const fs = require('fs');
const child_process = require('child_process');
const tmp = require('tmp');
-const BytecodeParser = require("../src/lundump.js");
+const lua = require("../src/lua.js");
const lauxlib = require("../src/lauxlib.js");
const toByteCode = function (luaCode) {
@@ -21,11 +21,9 @@ const toByteCode = function (luaCode) {
};
const getState = function(luaCode) {
- var dv = toByteCode(luaCode);
-
let L = lauxlib.luaL_newstate();
- let p = new BytecodeParser(L, dv).luaU_undump();
+ lauxlib.luaL_loadstring(L, lua.to_luastring(luaCode));
return L;
};