From 444182dbbb18f44cf7cafc378f092c28006be365 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 1 Mar 2017 11:51:00 +0100 Subject: Loading tests (binary/text) --- src/lcode.js | 20 ++++++++------------ src/lfunc.js | 4 ++++ src/llex.js | 4 ++-- src/lopcodes.js | 34 ++++++++++++++++++++++++++-------- src/lparser.js | 10 ++++------ src/lua.js | 8 +++++++- src/lualib.js | 25 ++++++++++--------------- 7 files changed, 61 insertions(+), 44 deletions(-) (limited to 'src') diff --git a/src/lcode.js b/src/lcode.js index 9b48d4d..d8b0252 100644 --- a/src/lcode.js +++ b/src/lcode.js @@ -10,7 +10,7 @@ const lparser = require('./lparser.js'); const ltm = require('./ltm.js'); const lua = require('./lua.js'); const lvm = require('./lvm.js'); -const CT = lua.constants_type; +const CT = lua.CT; const OpCodesI = lopcode.OpCodesI; const TValue = lobject.TValue; @@ -483,7 +483,7 @@ const freeexps = function(fs, e1, e2) { const addk = function(fs, key, v) { let f = fs.f; let idx = fs.ls.h.__index(fs.ls.h, key); /* index scanner table */ - if (idx) { /* is there an index there? */ + if (idx && !idx.ttisnil()) { /* is there an index there? */ /* correct value? (warning: must distinguish floats from integers!) */ if (f.k[idx].ttype() === v.ttype() && f.k[idx].value === v.value) return idx; /* reuse index */ @@ -500,8 +500,7 @@ const addk = function(fs, key, v) { ** Add a string to list of constants and return its index. */ const luaK_stringK = function(fs, s) { - let o = new TValue(CT.LUA_TLNGSTR, s); - return addk(fs, o, o); /* use string itself as key */ + return addk(fs, s, s); /* use string itself as key */ }; @@ -512,8 +511,8 @@ const luaK_stringK = function(fs, s) { ** are no "precision" problems. */ const luaK_intK = function(fs, n) { - let k = new TValue(CT.LUA_TLNGSTR, `n`); - let o = new TValue(CT.LUA_TNUMINT, n); + let k = new TValue(CT.LUA_TLNGSTR, `${n.value}`); + let o = new TValue(CT.LUA_TNUMINT, n.value); return addk(fs, k, o); }; @@ -521,8 +520,7 @@ const luaK_intK = function(fs, n) { ** Add a float to list of constants and return its index. */ const luaK_numberK = function(fs, r) { - let o = new TValue(CT.LUA_TNUMFLT, r); - return addk(fs, o, o); /* use number itself as key */ + return addk(fs, r, r); /* use number itself as key */ }; @@ -530,8 +528,7 @@ const luaK_numberK = function(fs, r) { ** Add a boolean to list of constants and return its index. */ const boolK = function(fs, b) { - let o = new TValue(CT.LUA_TBOOLEAN, b); - return addk(fs, o, o); /* use boolean itself as key */ + return addk(fs, b, b); /* use boolean itself as key */ }; @@ -539,8 +536,7 @@ const boolK = function(fs, b) { ** Add nil to list of constants and return its index. */ const nilK = function(fs) { - let o = new TValue(CT.LUA_TNIL, null); - return addk(fs, o, o); + return addk(fs, new TValue(CT.LUA_TLNGSTR, `null`), new TValue(CT.LUA_TNIL, null)); }; /* diff --git a/src/lfunc.js b/src/lfunc.js index 37239b2..227606d 100644 --- a/src/lfunc.js +++ b/src/lfunc.js @@ -102,12 +102,16 @@ const luaF_close = function(L, level) { } }; +/* +** fill a closure with new closed upvalues +*/ const luaF_initupvals = function(L, cl) { for (let i = 0; i < cl.nupvalues; i++) { let uv = new UpVal(L); uv.refcount = 1; uv.u.value = null; uv.v = uv.u.value; + cl.upvals[i] = uv; } }; diff --git a/src/llex.js b/src/llex.js index 73cda58..63a1fbb 100644 --- a/src/llex.js +++ b/src/llex.js @@ -453,7 +453,7 @@ const read_string = function(ls, del, seminfo) { } } save_and_next(ls); /* skip delimiter */ - seminfo.ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.slice(1).join('')); + seminfo.ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.slice(1, ls.buff.buffer.length-1).join('')); }; const isreserved = function(w) { @@ -566,7 +566,7 @@ const llex = function(ls, seminfo) { let ts = new TValue(CT.LUA_TLNGSTR, ls.buff.buffer.join('')); seminfo.ts = ts; - let kidx = luaX_tokens.slice(0, 22).indexOf(ts.value) + let kidx = luaX_tokens.slice(0, 22).indexOf(ts.value); if (kidx >= 0) /* reserved word? */ return kidx + FIRST_RESERVED; else diff --git a/src/lopcodes.js b/src/lopcodes.js index 3f7f63b..bfcd993 100644 --- a/src/lopcodes.js +++ b/src/lopcodes.js @@ -209,6 +209,7 @@ const MAXARG_A = ((1 << SIZE_A) - 1); const MAXARG_B = ((1 << SIZE_B) - 1); const MAXARG_C = ((1 << SIZE_C) - 1); +/* this bit 1 means constant (0 means register) */ const BITRK = (1 << (SIZE_B - 1)); /* @@ -216,14 +217,22 @@ const BITRK = (1 << (SIZE_B - 1)); */ const NO_REG = MAXARG_A; +/* test whether value is a constant */ const ISK = function (x) { return x & BITRK; }; +/* gets the index of the constant */ const INDEXK = function (r) { return r & ~BITRK; }; +/* code a constant index as a RK value */ +const RKASK = function(x) { + return x | BITRK; +}; + + /* creates a mask with 'n' 1 bits at position 'p' */ const MASK1 = function(n, p) { return ((~((~0)<<(n)))<<(p)); @@ -267,7 +276,7 @@ const SETARG_sBx = function(i, b) { ** Pre-calculate all possible part of the instruction */ const fullins = function(ins) { - if (typeof ins === "integer") { + if (typeof ins === "number") { return { code: ins, opcode: (ins >> POS_OP) & MASK1(SIZE_OP, 0), @@ -280,13 +289,13 @@ const fullins = function(ins) { }; } else { let i = ins.code; - ins.opcode = (i >> POS_OP) & MASK1(SIZE_OP, 0), - ins.A = (i >> POS_A) & MASK1(SIZE_A, 0), - ins.B = (i >> POS_B) & MASK1(SIZE_B, 0), - ins.C = (i >> POS_C) & MASK1(SIZE_C, 0), - ins.Bx = (i >> POS_Bx) & MASK1(SIZE_Bx, 0), - ins.Ax = (i >> POS_Ax) & MASK1(SIZE_Ax, 0), - ins.sBx = ((i >> POS_Bx) & MASK1(SIZE_Bx, 0)) - MAXARG_sBx + ins.opcode = (i >> POS_OP) & MASK1(SIZE_OP, 0); + ins.A = (i >> POS_A) & MASK1(SIZE_A, 0); + ins.B = (i >> POS_B) & MASK1(SIZE_B, 0); + ins.C = (i >> POS_C) & MASK1(SIZE_C, 0); + ins.Bx = (i >> POS_Bx) & MASK1(SIZE_Bx, 0); + ins.Ax = (i >> POS_Ax) & MASK1(SIZE_Ax, 0); + ins.sBx = ((i >> POS_Bx) & MASK1(SIZE_Bx, 0)) - MAXARG_sBx; return ins; } }; @@ -320,6 +329,10 @@ module.exports.MAXARG_Bx = MAXARG_Bx; module.exports.MAXARG_C = MAXARG_C; module.exports.MAXARG_sBx = MAXARG_sBx; module.exports.NO_REG = NO_REG; +module.exports.OpArgK = OpArgK; +module.exports.OpArgN = OpArgN; +module.exports.OpArgR = OpArgR; +module.exports.OpArgU = OpArgU; module.exports.OpCodes = OpCodes; module.exports.OpCodesI = OpCodesI; module.exports.POS_A = POS_A; @@ -328,6 +341,7 @@ module.exports.POS_B = POS_B; module.exports.POS_Bx = POS_Bx; module.exports.POS_C = POS_C; module.exports.POS_OP = POS_OP; +module.exports.RKASK = RKASK; module.exports.SETARG_A = SETARG_A; module.exports.SETARG_Ax = SETARG_Ax; module.exports.SETARG_B = SETARG_B; @@ -344,5 +358,9 @@ module.exports.fullins = fullins; module.exports.getBMode = getBMode; module.exports.getCMode = getCMode; module.exports.getOpMode = getOpMode; +module.exports.iABC = iABC; +module.exports.iABx = iABx; +module.exports.iAsBx = iAsBx; +module.exports.iAx = iAx; module.exports.testAMode = testAMode; module.exports.testTMode = testTMode; \ No newline at end of file diff --git a/src/lparser.js b/src/lparser.js index 40d6c88..aa48f10 100644 --- a/src/lparser.js +++ b/src/lparser.js @@ -10,7 +10,6 @@ const lobject = require('./lobject.js'); const lopcode = require('./lopcodes.js'); const lua = require('./lua.js'); const BinOpr = lcode.BinOpr; -const CT = lua.constants_type; const OpCodesI = lopcode.OpCodesI; const Proto = lfunc.Proto; const R = llex.RESERVED; @@ -239,7 +238,7 @@ const new_localvar = function(ls, name) { }; const new_localvarliteral = function(ls, name) { - new_localvar(ls, new TValue(CT.LUA_TLNGSTR, name)); + new_localvar(ls, new TValue(lua.CT.LUA_TLNGSTR, name)); }; const getlocvar = function(fs, i) { @@ -469,7 +468,7 @@ const enterblock = function(fs, bl, isloop) { ** create a label named 'break' to resolve break statements */ const breaklabel = function(ls) { - let n = new TValue(CT.LUA_TLNGSTR, "break"); + let n = new TValue(lua.CT.LUA_TLNGSTR, "break"); let l = newlabelentry(ls, ls.dyd.label, n, 0, ls.fs.pc); findgotos(ls, ls.dyd.label.arr[l]); }; @@ -507,7 +506,6 @@ const codeclosure = function(ls, v) { }; const open_func = function(ls, fs, bl) { - this.f = new Proto(); fs.prev = ls.fs; /* linked list of funcstates */ fs.ls = ls; ls.fs = fs; @@ -1132,7 +1130,7 @@ const gotostat = function(ls, pc) { label = str_checkname(ls); else { llex.luaX_next(ls); /* skip break */ - label = new TValue(CT.LUA_TLNGSTR, "break"); + label = new TValue(lua.CT.LUA_TLNGSTR, "break"); } let g = newlabelentry(ls, ls.dyd.gt, label, line, pc); findlabel(ls, g); /* close it if label already defined */ @@ -1538,7 +1536,7 @@ const luaY_parser = function(L, z, buff, dyd, name, firstchar) { lexstate.h = new Table(); /* create table for scanner */ L.stack[L.top++] = lexstate.h; funcstate.f = cl.p = new Proto(L); - funcstate.f.source = new TValue(CT.LUA_TLNGSTR, name); + funcstate.f.source = new TValue(lua.CT.LUA_TLNGSTR, name); lexstate.buff = buff; lexstate.dyd = dyd; dyd.actvar.n = dyd.gt.n = dyd.label.n = 0; diff --git a/src/lua.js b/src/lua.js index 9367746..ef5385c 100644 --- a/src/lua.js +++ b/src/lua.js @@ -28,8 +28,10 @@ const FENGARI_RELEASE = FENGARI_VERSION + "." + FENGARI_VERSION_RELEASE; const FENGARI_COPYRIGHT = FENGARI_RELEASE + " Copyright (C) 2017 BenoƮt Giannangeli\nBased on: " + LUA_COPYRIGHT; const FENGARI_AUTHORS = "B. Giannangeli"; +const LUA_VERSUFFIX = "_" + LUA_VERSION_MAJOR + "_" + LUA_VERSION_MINOR; + const LUA_INIT_VAR = "LUA_INIT"; -const LUA_INITVARVERSION = LUA_INIT_VAR + lualib.LUA_VERSUFFIX; +const LUA_INITVARVERSION = LUA_INIT_VAR + LUA_VERSUFFIX; const thread_status = { LUA_OK: 0, @@ -55,6 +57,8 @@ const constant_types = { LUA_NUMTAGS: 9 }; +const CT = constant_types; + constant_types.LUA_TSHRSTR = constant_types.LUA_TSTRING | (0 << 4); /* short strings */ constant_types.LUA_TLNGSTR = constant_types.LUA_TSTRING | (1 << 4); /* long strings */ @@ -128,6 +132,7 @@ class lua_Debug { } +module.exports.CT = CT; module.exports.FENGARI_AUTHORS = FENGARI_AUTHORS; module.exports.FENGARI_COPYRIGHT = FENGARI_COPYRIGHT; module.exports.FENGARI_RELEASE = FENGARI_RELEASE; @@ -171,6 +176,7 @@ module.exports.LUA_VERSION_MAJOR = LUA_VERSION_MAJOR; module.exports.LUA_VERSION_MINOR = LUA_VERSION_MINOR; module.exports.LUA_VERSION_NUM = LUA_VERSION_NUM; module.exports.LUA_VERSION_RELEASE = LUA_VERSION_RELEASE; +module.exports.LUA_VERSUFFIX = LUA_VERSUFFIX; module.exports.constant_types = constant_types; module.exports.lua_Debug = lua_Debug; module.exports.lua_upvalueindex = lua_upvalueindex; diff --git a/src/lualib.js b/src/lualib.js index d87cfbc..da9b290 100644 --- a/src/lualib.js +++ b/src/lualib.js @@ -2,21 +2,17 @@ "use strict"; const assert = require('assert'); -const lua = require('./lua.js'); - - -const LUA_VERSUFFIX = "_" + lua.LUA_VERSION_MAJOR + "_" + lua.LUA_VERSION_MINOR; const LUA_COLIBNAME = "coroutine"; -const LUA_TABLIBNAME = "table" -const LUA_IOLIBNAME = "io" -const LUA_OSLIBNAME = "os" -const LUA_STRLIBNAME = "string" -const LUA_UTF8LIBNAME = "utf8" -const LUA_BITLIBNAME = "bit32" -const LUA_MATHLIBNAME = "math" -const LUA_DBLIBNAME = "debug" -const LUA_LOADLIBNAME = "package" +const LUA_TABLIBNAME = "table"; +const LUA_IOLIBNAME = "io"; +const LUA_OSLIBNAME = "os"; +const LUA_STRLIBNAME = "string"; +const LUA_UTF8LIBNAME = "utf8"; +const LUA_BITLIBNAME = "bit32"; +const LUA_MATHLIBNAME = "math"; +const LUA_DBLIBNAME = "debug"; +const LUA_LOADLIBNAME = "package"; module.exports.LUA_BITLIBNAME = LUA_BITLIBNAME; @@ -28,5 +24,4 @@ module.exports.LUA_MATHLIBNAME = LUA_MATHLIBNAME; module.exports.LUA_OSLIBNAME = LUA_OSLIBNAME; module.exports.LUA_STRLIBNAME = LUA_STRLIBNAME; module.exports.LUA_TABLIBNAME = LUA_TABLIBNAME; -module.exports.LUA_UTF8LIBNAME = LUA_UTF8LIBNAME; -module.exports.LUA_VERSUFFIX = LUA_VERSUFFIX; \ No newline at end of file +module.exports.LUA_UTF8LIBNAME = LUA_UTF8LIBNAME; \ No newline at end of file -- cgit v1.2.3-54-g00ecf