diff --git a/src/core/org/luaj/lib/BaseLib.java b/src/core/org/luaj/lib/BaseLib.java index 260ff843..dfa03d55 100644 --- a/src/core/org/luaj/lib/BaseLib.java +++ b/src/core/org/luaj/lib/BaseLib.java @@ -364,7 +364,7 @@ public class BaseLib extends LFunction { break; } default: - throw new RuntimeException( "bad id: "+id ); + LuaState.vmerror( "bad base id" ); } return false; } diff --git a/src/core/org/luaj/lib/MathLib.java b/src/core/org/luaj/lib/MathLib.java index 4bffc896..0372282f 100644 --- a/src/core/org/luaj/lib/MathLib.java +++ b/src/core/org/luaj/lib/MathLib.java @@ -113,7 +113,7 @@ public class MathLib extends LFunction { setResult( vm, LInteger.valueOf( (int) Math.floor( vm.tonumber(2) ) ) ); break; default: - throw new RuntimeException( "bad id: "+id ); + LuaState.vmerror( "bad math id" ); } return false; } diff --git a/src/core/org/luaj/lib/TableLib.java b/src/core/org/luaj/lib/TableLib.java index 1470675c..d7f110bb 100644 --- a/src/core/org/luaj/lib/TableLib.java +++ b/src/core/org/luaj/lib/TableLib.java @@ -175,7 +175,7 @@ public class TableLib extends LFunction { } default: - throw new RuntimeException( "bad id" ); + LuaState.vmerror( "bad table id" ); } return false; } diff --git a/src/core/org/luaj/vm/LDouble.java b/src/core/org/luaj/vm/LDouble.java index 22ae9a19..df83265c 100644 --- a/src/core/org/luaj/vm/LDouble.java +++ b/src/core/org/luaj/vm/LDouble.java @@ -79,7 +79,8 @@ public class LDouble extends LNumber { case Lua.OP_MOD: return new LDouble( lhs - Math.floor(lhs/rhs) * rhs ); case Lua.OP_POW: throw new LuaErrorException("math.pow() not implemented for doubles"); } - throw new RuntimeException("bad opcode"); + LuaState.vmerror( "bad bin opcode" ); + return null; } /* warning: NOT TESTED @@ -134,7 +135,8 @@ public class LDouble extends LNumber { case Lua.OP_LT: return lhs < rhs; case Lua.OP_LE: return lhs <= rhs; } - throw new RuntimeException("bad opcode"); + LuaState.vmerror( "bad cmp opcode" ); + return false; } /** Arithmetic negative */ diff --git a/src/core/org/luaj/vm/LInteger.java b/src/core/org/luaj/vm/LInteger.java index 44410757..2eb55a2a 100644 --- a/src/core/org/luaj/vm/LInteger.java +++ b/src/core/org/luaj/vm/LInteger.java @@ -85,7 +85,8 @@ public class LInteger extends LNumber { case Lua.OP_MOD: return LInteger.valueOf( m_value - ((int) Math.floor(m_value/(double)rhs)) * rhs ); case Lua.OP_POW: return LInteger.valueOf( ipow(m_value, rhs) ); } - throw new RuntimeException("bad opcode"); + LuaState.vmerror( "bad bin opcode" ); + return null; } private static int ipow(int v, int rhs) { @@ -113,7 +114,8 @@ public class LInteger extends LNumber { case Lua.OP_LT: return m_value < rhs; case Lua.OP_LE: return m_value <= rhs; } - throw new RuntimeException("bad opcode"); + LuaState.vmerror( "bad cmp opcode" ); + return false; } // unsupported except for numbers diff --git a/src/core/org/luaj/vm/LString.java b/src/core/org/luaj/vm/LString.java index 5f70685a..5ae6065f 100644 --- a/src/core/org/luaj/vm/LString.java +++ b/src/core/org/luaj/vm/LString.java @@ -315,7 +315,8 @@ public class LString extends LValue { case Lua.OP_LT: return compareTo(rhs) < 0; case Lua.OP_LE: return compareTo(rhs) <= 0; } - throw new RuntimeException("bad opcode"); + LuaState.vmerror( "bad cmp opcode" ); + return false; } public LValue luaBinOpDouble( int opcode, double m_value ) { diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 01d5e9af..54b2b329 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -822,34 +822,30 @@ public class LuaState extends Lua { case LuaState.OP_FORPREP: { init = this.stack[base + a]; step = this.stack[base + a + 2]; - this.stack[base + a] = step.luaBinOpUnknown(Lua.OP_SUB, - init); + this.stack[base + a] = step.luaBinOpUnknown(Lua.OP_SUB, init); b = LuaState.GETARG_sBx(i); ci.pc += b; continue; } case LuaState.OP_TFORLOOP: { - cb = a + 3; /* call base */ - System.arraycopy(this.stack, base + a, this.stack, - base + cb, 3); - base += cb; - try { - top = base + 3; /* func. + 2 args (state and index) */ - - // call the iterator - c = LuaState.GETARG_C(i); - if (this.stack[base].luaStackCall(this)) - execute(); - adjustTop( base + c - 1 ); - - // test for continuation - if (this.stack[base] != LNil.NIL) { // continue? - this.stack[base - 1] = this.stack[base]; // save control variable - } else { - ci.pc++; // skip over jump - } - } finally { - base -= cb; + cb = base + a + 3; /* call base */ + base = cb; + adjustTop( cb + 3 ); + System.arraycopy(this.stack, cb-3, this.stack, cb, 3); + + // call the iterator + c = LuaState.GETARG_C(i); + this.nresults = c; + if (this.stack[cb].luaStackCall(this)) + execute(); + base = ci.base; + adjustTop( cb + c ); + + // test for continuation + if (this.stack[cb] != LNil.NIL ) { // continue? + this.stack[cb-1] = this.stack[cb]; // save control variable + } else { + ci.pc++; // skip over jump } continue; } @@ -2568,22 +2564,14 @@ public class LuaState extends Lua { public Long toboxedlong(int index) { return topointer(index).toJavaBoxedLong(); } - + + /** - * Get the current environment. - * - * @return LTable the current environment + * Method to indicate a vm internal error has occurred. + * Generally, this is not recoverable, so we convert to + * a lua error during production so that the code may recover. */ -// public LTable curr_env() { -// LuaState vm = (LThread.running!=null? -// LThread.running.threadVm: -// this); -// for ( int i=vm.cc; i>=0; i-- ) { -// LClosure c = vm.calls[cc].closure; -// if ( c != null ) -// return c.env; -// } -// return _G; -// } - + public static void vmerror(String description) { + throw new LuaErrorException( "internal error: "+description ); + } } diff --git a/src/sample/org/luaj/sample/LuaRunner.java b/src/sample/org/luaj/sample/LuaRunner.java index 2dd47dad..dd6af5bf 100644 --- a/src/sample/org/luaj/sample/LuaRunner.java +++ b/src/sample/org/luaj/sample/LuaRunner.java @@ -42,7 +42,7 @@ public class LuaRunner { public static void main( String[] args ) throws IOException { // new lua state - LuaState state = new LuaState(); + LuaState state = LuaState.newState(); // get script name String script = (args.length>0? args[0]: "/test2.luac"); diff --git a/src/test/res/loops.luac b/src/test/res/loops.luac deleted file mode 100644 index c7cbe1a9..00000000 Binary files a/src/test/res/loops.luac and /dev/null differ