Add tests for pcall, add error() builtin, fix assert(), error levels.

This commit is contained in:
James Roseborough
2007-10-23 00:34:04 +00:00
parent 8248cca2bf
commit 932960c846
12 changed files with 92 additions and 49 deletions

View File

@@ -24,6 +24,8 @@ final class Builtin extends JavaFunction {
"type",
"pcall",
"ipairs",
"error",
"assert",
};
private static final int PRINT = 0;
private static final int PAIRS = 1;
@@ -32,6 +34,8 @@ final class Builtin extends JavaFunction {
private static final int TYPE = 4;
private static final int PCALL = 5;
private static final int IPAIRS = 6;
private static final int ERROR = 7;
private static final int ASSERT = 8;
private static PrintStream stdout = System.out;
@@ -90,6 +94,16 @@ final class Builtin extends JavaFunction {
return 2;
}
}
case ERROR: {
vm.error(vm.tostring(1), vm.gettop()>1? vm.tointeger(2): 1);
}
case ASSERT: {
if ( ! vm.toboolean(1) ) {
vm.error( vm.gettop()>1? vm.tostring(2): "assertion failed!", 0 );
} else {
return vm.gettop();
}
}
default:
luaUnsupportedOperation();
return 0;

View File

@@ -357,6 +357,14 @@ public interface VM {
*/
public void error();
/**
* Raises an error with the default level.
*
* In the java implementation this throws a RuntimeException, possibly filling
* line number information first.
*/
public void error(String message);
/**
* Raises an error. The message is pushed onto the stack and used as the error message.
* It also adds at the beginning of the message the file name and the line number where
@@ -365,7 +373,7 @@ public interface VM {
* In the java implementation this throws a RuntimeException, possibly filling
* line number information first.
*/
public void error(String message);
public void error(String message, int level);
/**
* Controls the garbage collector. <span class="apii">[-0, +0, <em>e</em>]</span>

View File

@@ -94,11 +94,16 @@ public class DebugStackState extends StackState implements DebugRequestListener
return source+":"+line+"("+func+")";
}
// override and fill in line number info
public void error(String message) {
super.error( getFileLine(cc)+": "+message );
public void error(String message, int level) {
super.error( level<=0? message: getFileLine(cc+1-level)+": "+message );
}
// use line numbers by default
public void error(String message) {
error(message, 1);
}
private void printLuaTrace() {
System.out.println( "Lua location: "+getFileLine(cc) );
@@ -112,11 +117,10 @@ public class DebugStackState extends StackState implements DebugRequestListener
super.exec();
} catch (AbortException e) {
// ignored. Client aborts the debugging session.
} catch ( Exception t ) {
t.printStackTrace();
printLuaTrace();
System.out.flush();
}
// let other exceptions be processed
// the same as the base class to minimize differences
// between the debug and non-debug behavior
}

View File

@@ -54,10 +54,9 @@ public class LThread extends LValue {
public void resumeFrom(VM vm, int nargs) {
if ( status == STATUS_DEAD ) {
vm.error("cannot resume dead coroutine");
// vm.settop(0);
// vm.pushboolean(false);
// vm.pushstring("cannot resume dead coroutine");
vm.settop(0);
vm.pushboolean(false);
vm.pushstring("cannot resume dead coroutine");
return;
}