From 2cf246d68c5e658eb6c36a00ac35c7862a54e90e Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Fri, 3 Feb 2017 15:18:55 +0100 Subject: TValue --- luac.out | Bin 241 -> 241 bytes src/lobject.js | 15 ++++++- src/lundump.js | 26 +++--------- src/lvm.js | 131 ++++++++++++--------------------------------------------- 4 files changed, 47 insertions(+), 125 deletions(-) diff --git a/luac.out b/luac.out index 9973c9b..85aec6d 100644 Binary files a/luac.out and b/luac.out differ diff --git a/src/lobject.js b/src/lobject.js index 262b38d..7efbef5 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -1,6 +1,8 @@ /*jshint esversion: 6 */ "use strict"; +const CT = require('./lua.js').constant_types; + class LClosure { constructor(n) { @@ -10,6 +12,17 @@ class LClosure { } + +class TValue { + + constructor(type, value) { + this.type = type; + this.value = value; + } + +} + module.exports = { - LClosure: LClosure + LClosure: LClosure, + TValue: TValue }; \ No newline at end of file diff --git a/src/lundump.js b/src/lundump.js index 896fb31..f2f91f1 100644 --- a/src/lundump.js +++ b/src/lundump.js @@ -6,6 +6,7 @@ const fs = require('fs'); const assert = require('assert'); const LClosure = require('./lobject.js').LClosure; +const TValue = require('./lobject.js').TValue; const Proto = require('./lfunc.js').Proto; const constant_types = require('./lua.js').constant_types; const OpCodes = require('./lopcodes.js'); @@ -163,39 +164,24 @@ class BytecodeParser { switch (t) { case constant_types.LUA_TNIL: - f.k.push({ - type: constant_types.LUA_TNIL, - value: null - }); + f.k.push(new TValue(constant_types.LUA_TNIL, null)); console.log(` LUA_TNIL = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TBOOLEAN: - f.k.push({ - type: constant_types.LUA_TBOOLEAN, - value: this.readByte() - }); + f.k.push(new TValue(constant_types.LUA_TBOOLEAN, this.readByte())); console.log(` LUA_TBOOLEAN = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TNUMFLT: - f.k.push({ - type: constant_types.LUA_TNUMFLT, - value: this.readNumber() - }); + f.k.push(new TValue(constant_types.LUA_TNUMFLT, this.readNumber())); console.log(` LUA_TNUMFLT = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TNUMINT: - f.k.push({ - type: constant_types.LUA_TNUMINT, - value: this.readInteger() - }); + f.k.push(new TValue(constant_types.LUA_TNUMINT, this.readInteger())); console.log(` LUA_TNUMINT = ${f.k[f.k.length - 1].value}`); break; case constant_types.LUA_TSHRSTR: case constant_types.LUA_TLNGSTR: - f.k.push({ - type: constant_types.LUA_TLNGSTR, - value: this.readString() - }); + f.k.push(new TValue(constant_types.LUA_TLNGSTR, this.readString())); console.log(` LUA_TLNGSTR = ${f.k[f.k.length - 1].value}`); break; default: diff --git a/src/lvm.js b/src/lvm.js index f18ede4..3d6f43a 100644 --- a/src/lvm.js +++ b/src/lvm.js @@ -4,6 +4,7 @@ const BytecodeParser = require("./lundump.js"); const OC = require('./lopcodes.js'); const CT = require('./lua.js').constant_types; +const TValue = require('./lobject.js').TValue; class LuaVM { @@ -33,22 +34,13 @@ class LuaVM { static tonumber(v) { if (v.type === CT.LUA_TNUMFLT) - return { - type: v.type, - value: v.value - }; + return new TValue(v.type, v.value); if (v.type === CT.LUA_TNUMINT) - return { - type: CT.LUA_TNUMFLT, - value: v.value - }; + return new TValue(CT.LUA_TNUMFLT, v.value); if (v.type === CT.LUA_TSHRSTR || v.type === CT.LUA_TLNGSTR) - return { - type: CT.LUA_TNUMFLT, - value: parseFloat(v.value) // TODO 0x or other exotic form - }; + return new TValue(CT.LUA_TNUMFLT, parseFloat(v.value)); // TODO 0x or other exotic form return false; } @@ -81,10 +73,7 @@ class LuaVM { break; } case "OP_LOADBOOL": { - L.stack[ra] = { - type: CT.LUA_TBOOLEAN, - value: i.B !== 0 - }; + L.stack[ra] = new TValue(CT.LUA_TBOOLEAN, i.B !== 0); if (i.C !== 0) ci.pcOff++; /* skip next instruction (if C) */ @@ -93,10 +82,7 @@ class LuaVM { } case "OP_LOADNIL": { for (let j = 0; j <= i.B; j++) - L.stack[ra + j] = { - type: CT.LUA_TNIL, - value: null - } + L.stack[ra + j] = new TValue(CT.LUA_TNIL, null); break; } case "OP_GETUPVAL": { @@ -130,15 +116,9 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value + k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value + k[i.C].value); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: k[i.B].value + k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value + k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -152,15 +132,9 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value - k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value - k[i.C].value); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: k[i.B].value - k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value - k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -174,15 +148,9 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value * k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value * k[i.C].value); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: k[i.B].value * k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value * k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -196,15 +164,9 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value % k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value % k[i.C].value); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: k[i.B].value % k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value % k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -218,10 +180,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: Math.pow(k[i.B].value, k[i.C].value) - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.pow(k[i.B].value, k[i.C].value)); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -235,10 +194,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: k[i.B].value / k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, k[i.B].value / k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -252,15 +208,9 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: Math.floor(k[i.B].value / k[i.C].value) - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, Math.floor(k[i.B].value / k[i.C].value)); } else if (numberop1 !== false && numberop2 !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: Math.floor(k[i.B].value / k[i.C].value) - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, Math.floor(k[i.B].value / k[i.C].value)); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -274,10 +224,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value & k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value & k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -291,10 +238,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value | k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value | k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -308,10 +252,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value ^ k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value ^ k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -325,10 +266,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value << k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value << k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -342,10 +280,7 @@ class LuaVM { let numberop2 = LuaVM.tonumber(op2); if (op1.type === CT.LUA_TNUMINT && op2.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: k[i.B].value >> k[i.C].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, k[i.B].value >> k[i.C].value); } else { // Metamethod throw new Error(`Can't perform binary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -357,15 +292,9 @@ class LuaVM { let numberop = LuaVM.tonumber(op); if (op.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: -L.stack[this.RB(base, i)].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, -L.stack[this.RB(base, i)].value); } else if (numberop !== false) { - L.stack[ra] = { - type: CT.LUA_TNUMFLT, - value: -L.stack[this.RB(base, i)].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMFLT, -L.stack[this.RB(base, i)].value); } else { // Metamethod throw new Error(`Can't perform unary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -377,10 +306,7 @@ class LuaVM { let numberop = LuaVM.tonumber(op); if (op.type === CT.LUA_TNUMINT) { - L.stack[ra] = { - type: CT.LUA_TNUMINT, - value: ~L.stack[this.RB(base, i)].value - }; + L.stack[ra] = new TValue(CT.LUA_TNUMINT, ~L.stack[this.RB(base, i)].value); } else { // Metamethod throw new Error(`Can't perform unary operation on ${k[i.B].value} and ${k[i.C].value}`); @@ -389,10 +315,7 @@ class LuaVM { } case "OP_NOT": { let op = L.stack[this.RB(base, i)]; - L.stack[ra] = { - type: CT.LUA_TBOOLEAN, - value: !!((op.type === CT.LUA_TBOOLEAN && !op.value) || op.type === CT.LUA_TNIL) - } + L.stack[ra] = new TValue(CT.LUA_TBOOLEAN, !!((op.type === CT.LUA_TBOOLEAN && !op.value) || op.type === CT.LUA_TNIL)); break; } case "OP_LEN": { -- cgit v1.2.3-70-g09d2