Fix for loop processing, add vmerror()

This commit is contained in:
James Roseborough
2007-12-06 22:59:16 +00:00
parent 6871903303
commit e8e1aaf892
9 changed files with 41 additions and 48 deletions

View File

@@ -364,7 +364,7 @@ public class BaseLib extends LFunction {
break;
}
default:
throw new RuntimeException( "bad id: "+id );
LuaState.vmerror( "bad base id" );
}
return false;
}

View File

@@ -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;
}

View File

@@ -175,7 +175,7 @@ public class TableLib extends LFunction {
}
default:
throw new RuntimeException( "bad id" );
LuaState.vmerror( "bad table id" );
}
return false;
}

View File

@@ -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 */

View File

@@ -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

View File

@@ -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 ) {

View File

@@ -822,35 +822,31 @@ 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) */
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);
if (this.stack[base].luaStackCall(this))
this.nresults = c;
if (this.stack[cb].luaStackCall(this))
execute();
adjustTop( base + c - 1 );
base = ci.base;
adjustTop( cb + c );
// test for continuation
if (this.stack[base] != LNil.NIL) { // continue?
this.stack[base - 1] = this.stack[base]; // save control variable
if (this.stack[cb] != LNil.NIL ) { // continue?
this.stack[cb-1] = this.stack[cb]; // save control variable
} else {
ci.pc++; // skip over jump
}
} finally {
base -= cb;
}
continue;
}
case LuaState.OP_SETLIST: {
@@ -2569,21 +2565,13 @@ public class LuaState extends Lua {
return topointer(index).toJavaBoxedLong();
}
/**
* Get the current environment.
*
* @return LTable the current environment
*/
// 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;
// }
/**
* 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 static void vmerror(String description) {
throw new LuaErrorException( "internal error: "+description );
}
}

View File

@@ -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");

Binary file not shown.