Added a JUnit test case. Unfortunately, it will almost certainly not work on

Windows in its present form, and only one of the test cases passes.
In addition two changes to print() were made: output can be redirected to
an arbitrary OutputStream and tabs are no longer printed at the end of
each line.
This commit is contained in:
Ian Farmer
2007-07-04 04:25:06 +00:00
parent ba26aed248
commit 241edfbf37
4 changed files with 193 additions and 6 deletions

View File

@@ -3,8 +3,10 @@
*/
package lua;
import java.io.OutputStream;
import java.io.PrintStream;
import lua.value.LFunction;
import lua.value.LString;
import lua.value.LTable;
import lua.value.LValue;
@@ -22,6 +24,8 @@ final class Builtin extends LFunction {
private static final String[] NAMES = { "print", "pairs", "getmetatable", "setmetatable" };
private static PrintStream stdout = System.out;
private int id;
private Builtin( int id ) {
this.id = id;
@@ -35,11 +39,13 @@ final class Builtin extends LFunction {
public void luaStackCall(CallFrame call, int base, int top, int nresults) {
switch ( id ) {
case PRINT:
for ( int i=base+1; i<top; i++ ) {
System.out.print( call.stack[i].luaAsString() );
System.out.print( "\t" );
if ( base+1<top )
stdout.print( call.stack[base+1].luaAsString() );
for ( int i=base+2; i<top; i++ ) {
stdout.print( "\t" );
stdout.print( call.stack[i].luaAsString() );
}
System.out.println();
stdout.println();
call.top = base;
break;
case PAIRS:
@@ -62,5 +68,13 @@ final class Builtin extends LFunction {
if (nresults >= 0)
call.adjustTop(base + nresults);
}
static void redirectOutput( OutputStream newStdOut ) {
stdout = new PrintStream( newStdOut );
}
static void restoreStandardOutput() {
stdout = System.out;
}
}
}