add compiler to vm as add-on

This commit is contained in:
James Roseborough
2007-09-21 16:50:29 +00:00
parent 2db26b0844
commit be87758fe5
16 changed files with 3586 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
package lua.addon.compile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.net.URL;
import junit.framework.TestCase;
import lua.Print;
import lua.StackState;
import lua.addon.compile.Compiler;
import lua.addon.compile.DumpState;
import lua.io.LoadState;
import lua.io.Proto;
abstract
public class AbstractUnitTests extends TestCase {
private final String zipfile;
private final String dir;
public AbstractUnitTests(String zipfile, String dir) {
this.zipfile = zipfile;
this.dir = dir;
}
protected void doTest( String file ) {
try {
// load source from jar
String path = "jar:file:" + zipfile + "!/" + dir + "/" + file;
byte[] lua = bytesFromJar( path );
// compile in memory
InputStream is = new ByteArrayInputStream( lua );
Reader r = new InputStreamReader( is );
Proto p = Compiler.compile(r, dir+"/"+file);
String actual = protoToString( p );
// load expected value from jar
byte[] luac = bytesFromJar( path + "c" );
Proto e = loadFromBytes( luac, file );
String expected = protoToString( e );
// compare results
assertEquals( expected, actual );
// dump into memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DumpState.dump(p, baos, false);
byte[] dumped = baos.toByteArray();
// re-undump
Proto p2 = loadFromBytes( dumped, file );
String actual2 = protoToString( p2 );
// compare again
assertEquals( actual, actual2 );
} catch (IOException e) {
fail( e.toString() );
}
}
protected byte[] bytesFromJar(String path) throws IOException {
URL url = new URL(path);
InputStream is = url.openStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int n;
while ( (n = is.read(buffer)) >= 0 )
baos.write( buffer, 0, n );
is.close();
return baos.toByteArray();
}
protected Proto loadFromBytes(byte[] bytes, String script) throws IOException {
StackState state = new StackState();
InputStream is = new ByteArrayInputStream( bytes );
return LoadState.undump(state, is, script);
}
protected String protoToString(Proto p) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream( baos );
Print.ps = ps;
new Print().printFunction(p, true);
return baos.toString();
}
}

View File

@@ -0,0 +1,35 @@
package lua.addon.compile;
public class CompilerUnitTests extends AbstractUnitTests {
public CompilerUnitTests() {
super( "src/test/compile/lua5.1-tests.zip",
"lua5.1-tests" );
}
public void testAll() { doTest("all.lua"); }
public void testApi() { doTest("api.lua"); }
public void testAttrib() { doTest("attrib.lua"); }
public void testBig() { doTest("big.lua"); }
public void testCalls() { doTest("calls.lua"); }
public void testChecktable() { doTest("checktable.lua"); }
public void testClosure() { doTest("closure.lua"); }
public void testCode() { doTest("code.lua"); }
public void testConstruct() { doTest("constructs.lua"); }
public void testDb() { doTest("db.lua"); }
public void testErrors() { doTest("errors.lua"); }
public void testEvents() { doTest("events.lua"); }
public void testFiles() { doTest("files.lua"); }
public void testGc() { doTest("gc.lua"); }
public void testLiterals() { doTest("literals.lua"); }
public void testLocals() { doTest("locals.lua"); }
public void testMain() { doTest("main.lua"); }
public void testMath() { doTest("math.lua"); }
public void testNextvar() { doTest("nextvar.lua"); }
public void testPm() { doTest("pm.lua"); }
public void testSort() { doTest("sort.lua"); }
public void testStrings() { doTest("strings.lua"); }
public void testVararg() { doTest("vararg.lua"); }
public void testVerybig() { doTest("verybig.lua"); }
}

View File

@@ -0,0 +1,31 @@
package lua.addon.compile;
/**
* Framework to add regression tests as problem areas are found.
*
* To add a new regression test:
* 1) run "unpack.sh" in the project root
* 2) add a new "lua" file in the "regressions" subdirectory
* 3) run "repack.sh" in the project root
* 4) add a line to the source file naming the new test
*
* After adding a test, check in the zip file
* rather than the individual regression test files.
*
* @author jrosebor
*/
public class RegressionTests extends AbstractUnitTests {
public RegressionTests() {
super( "src/test/compile/regressions.zip",
"regressions" );
}
public void testModulo() { doTest("modulo.lua"); }
public void testConstruct() { doTest("construct.lua"); }
public void testBigAttrs() { doTest("bigattr.lua"); }
public void testControlChars() { doTest("controlchars.lua"); }
public void testComparators() { doTest("comparators.lua"); }
public void testMathRandomseed() { doTest("mathrandomseed.lua"); }
}

View File

@@ -0,0 +1,76 @@
package lua.addon.compile;
import java.io.Reader;
import java.io.StringReader;
import junit.framework.TestCase;
import lua.Print;
import lua.StackState;
import lua.addon.compile.Compiler;
import lua.io.Closure;
import lua.io.Proto;
import lua.value.LValue;
public class SimpleTests extends TestCase {
private void doTest( String script ) {
Reader r = new StringReader( script );
Proto p = Compiler.compile( r, "script" );
assertNotNull( p );
Print.printCode( p );
// try running the code!
StackState state = new StackState();
Closure c = new Closure( state, p );
state.doCall( c, new LValue[0] );
}
public void testTrivial() {
String s = "print( 2 )\n";
doTest( s );
}
public void testAlmostTrivial() {
String s = "print( 2 )\n" +
"print( 3 )\n";
doTest( s );
}
public void testSimple() {
String s = "print( 'hello, world' )\n"+
"for i = 2,4 do\n" +
" print( 'i', i )\n" +
"end\n";
doTest( s );
}
public void testBreak() {
String s = "a=1\n"+
"while true do\n"+
" if a>10 then\n"+
" break\n"+
" end\n"+
" a=a+1\n"+
" print( a )\n"+
"end\n";
doTest( s );
}
public void testShebang() {
String s = "#!../lua\n"+
"print( 2 )\n";
doTest( s );
}
public void testInlineTable() {
String s = "A = {g=10}\n"+
"print( A )\n";
doTest( s );
}
public void testEqualsAnd() {
String s = "print( 1 == b and b )\n";
doTest( s );
}
}