Merge pull request #14 from Enyby/patch-6

Fix for proper print OP_SETLIST additional opcode

Closes #7
This commit was merged in pull request #14.
This commit is contained in:
Benjamin P. Jung
2018-09-14 12:12:10 +02:00
committed by GitHub

View File

@@ -128,6 +128,10 @@ public class Print extends Lua {
}
static void printValue( PrintStream ps, LuaValue v ) {
if (v == null) {
ps.print("null");
return;
}
switch ( v.type() ) {
case LuaValue.TSTRING: printString( ps, (LuaString) v ); break;
default: ps.print( v.tojstring() );
@@ -136,7 +140,7 @@ public class Print extends Lua {
}
static void printConstant(PrintStream ps, Prototype f, int i) {
printValue( ps, f.k[i] );
printValue( ps, i < f.k.length ? f.k[i] : LuaValue.valueOf("UNKNOWN_CONST_" + i) );
}
static void printUpvalue(PrintStream ps, Upvaldesc u) {
@@ -152,7 +156,7 @@ public class Print extends Lua {
int[] code = f.code;
int pc, n = code.length;
for (pc = 0; pc < n; pc++) {
printOpCode(f, pc);
pc = printOpCode(f, pc);
ps.println();
}
}
@@ -161,9 +165,10 @@ public class Print extends Lua {
* Print an opcode in a prototype
* @param f the {@link Prototype}
* @param pc the program counter to look up and print
* @return pc same as above or changed
*/
public static void printOpCode(Prototype f, int pc) {
printOpCode(ps,f,pc);
public static int printOpCode(Prototype f, int pc) {
return printOpCode(ps,f,pc);
}
/**
@@ -171,8 +176,9 @@ public class Print extends Lua {
* @param ps the {@link PrintStream} to print to
* @param f the {@link Prototype}
* @param pc the program counter to look up and print
* @return pc same as above or changed
*/
public static void printOpCode(PrintStream ps, Prototype f, int pc) {
public static int printOpCode(PrintStream ps, Prototype f, int pc) {
int[] code = f.code;
int i = code[pc];
int o = GET_OPCODE(i);
@@ -187,6 +193,9 @@ public class Print extends Lua {
ps.print("[" + line + "] ");
else
ps.print("[-] ");
if (o >= OPNAMES.length - 1) {
ps.print("UNKNOWN_OP_" + o + " ");
} else {
ps.print(OPNAMES[o] + " ");
switch (getOpMode(o)) {
case iABC:
@@ -218,11 +227,19 @@ public class Print extends Lua {
case OP_GETUPVAL:
case OP_SETUPVAL:
ps.print(" ; ");
if (b < f.upvalues.length) {
printUpvalue(ps, f.upvalues[b]);
} else {
ps.print("UNKNOWN_UPVALUE_" + b);
}
break;
case OP_GETTABUP:
ps.print(" ; ");
if (b < f.upvalues.length) {
printUpvalue(ps, f.upvalues[b]);
} else {
ps.print("UNKNOWN_UPVALUE_" + b);
}
ps.print(" ");
if (ISK(c))
printConstant(ps, f, INDEXK(c));
@@ -231,7 +248,11 @@ public class Print extends Lua {
break;
case OP_SETTABUP:
ps.print(" ; ");
if (a < f.upvalues.length) {
printUpvalue(ps, f.upvalues[a]);
} else {
ps.print("UNKNOWN_UPVALUE_" + a);
}
ps.print(" ");
if (ISK(b))
printConstant(ps, f, INDEXK(b));
@@ -278,11 +299,15 @@ public class Print extends Lua {
ps.print(" ; to " + (sbx + pc + 2));
break;
case OP_CLOSURE:
if (bx < f.p.length) {
ps.print(" ; " + f.p[bx].getClass().getName());
} else {
ps.print(" ; UNKNOWN_PROTYPE_" + bx);
}
break;
case OP_SETLIST:
if (c == 0)
ps.print(" ; " + ((int) code[++pc]));
ps.print(" ; " + ((int) code[++pc]) + " (stored in the next OP)");
else
ps.print(" ; " + ((int) c));
break;
@@ -293,6 +318,8 @@ public class Print extends Lua {
break;
}
}
return pc;
}
private static int getline(Prototype f, int pc) {
return pc>0 && f.lineinfo!=null && pc<f.lineinfo.length? f.lineinfo[pc]: -1;