summaryrefslogtreecommitdiff
path: root/src/lvm.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-03 15:18:55 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-02-03 15:18:55 +0100
commit2cf246d68c5e658eb6c36a00ac35c7862a54e90e (patch)
treeab9c4587af63b80093d866798383bd4ae6b98dca /src/lvm.js
parent90161c463fc8725d3d39b699dbf7e591874c47fb (diff)
downloadfengari-2cf246d68c5e658eb6c36a00ac35c7862a54e90e.tar.gz
fengari-2cf246d68c5e658eb6c36a00ac35c7862a54e90e.tar.bz2
fengari-2cf246d68c5e658eb6c36a00ac35c7862a54e90e.zip
TValue
Diffstat (limited to 'src/lvm.js')
-rw-r--r--src/lvm.js131
1 files changed, 27 insertions, 104 deletions
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": {