Refactor API's related to compiling and loading scripts and character encoding handling.

This commit is contained in:
James Roseborough
2013-09-18 05:32:30 +00:00
parent a552494b72
commit 2123d3f924
35 changed files with 489 additions and 293 deletions

View File

@@ -107,7 +107,7 @@ public class CompatibiltyTest extends TestSuite {
protected void setUp() throws Exception {
super.setUp();
System.setProperty("JME", "false");
LuaJC.install();
LuaJC.install(globals);
}
// not supported on this platform - don't test
public void testDebugLib() {}

View File

@@ -21,13 +21,13 @@
******************************************************************************/
package org.luaj.vm2;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.jse.JsePlatform;
import org.luaj.vm2.luajc.LuaJC;
/**
@@ -64,16 +64,18 @@ public class FragmentsTest extends TestSuite {
public void runFragment( Varargs expected, String script ) {
try {
String name = getName();
Globals _G = org.luaj.vm2.lib.jse.JsePlatform.debugGlobals();
InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
Globals _G = JsePlatform.debugGlobals();
Reader reader = new StringReader(script);
LuaValue chunk ;
switch ( TEST_TYPE ) {
case TEST_TYPE_LUAJC:
chunk = LuaJC.getInstance().load(is,name,_G);
LuaJC.install(_G);
chunk = _G.load(reader, name);
break;
default:
chunk = LuaC.instance.load( is, name, _G );
Print.print(((LuaClosure)chunk).p);
Prototype p = _G.compilePrototype(reader, name);
chunk = new LuaClosure(p, _G);
Print.print(p);
break;
}
Varargs actual = chunk.invoke();

View File

@@ -21,8 +21,8 @@
******************************************************************************/
package org.luaj.vm2;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import junit.framework.TestCase;
@@ -130,9 +130,9 @@ public class LuaOperationsTest extends TestCase {
public Prototype createPrototype( String script, String name ) {
try {
LuaTable _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
InputStream is = new ByteArrayInputStream(script.getBytes("UTF-8"));
return LuaC.instance.compile(is, name);
Globals _G = org.luaj.vm2.lib.jse.JsePlatform.standardGlobals();
Reader reader = new StringReader(script);
return _G.compilePrototype(reader, name);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

View File

@@ -21,15 +21,10 @@
******************************************************************************/
package org.luaj.vm2;
import java.io.ByteArrayInputStream;
import java.lang.ref.WeakReference;
import junit.framework.TestCase;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -74,8 +69,7 @@ public class OrphanedThreadTest extends TestCase {
"arg = coroutine.yield(0)\n" +
"print('leakage in closure.3, arg is '..arg)\n" +
"return 'done'\n";
LuaC.install();
function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
@@ -90,8 +84,7 @@ public class OrphanedThreadTest extends TestCase {
" return 'done'\n" +
"end\n" +
"print( 'pcall-closre.result:', pcall( f, ... ) )\n";
LuaC.install();
function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ZERO);
}
@@ -107,8 +100,7 @@ public class OrphanedThreadTest extends TestCase {
" return t[i]\n" +
"end\n" +
"load(f)()\n";
LuaC.install();
function = LoadState.load(new ByteArrayInputStream(script.getBytes()), "script", "bt", globals);
function = globals.load(script, "script");
doTest(LuaValue.TRUE, LuaValue.ONE);
}

View File

@@ -177,7 +177,7 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
}
}
protected LuaValue loadScript(String name, LuaTable _G) throws IOException {
protected LuaValue loadScript(String name, Globals _G) throws IOException {
InputStream script = this.findResource(name+".lua");
if ( script == null )
fail("Could not load script for test case: " + name);
@@ -188,10 +188,11 @@ public class ScriptDrivenTest extends TestCase implements ResourceFinder {
LuaValue c = (LuaValue) Class.forName(name).newInstance();
return c;
} else {
return LuaJC.getInstance().load( script, name, _G);
LuaJC.install(_G);
return _G.load(script, name, "bt", _G);
}
default:
return LoadState.load(script, "@"+name+".lua", "bt", _G);
return _G.load(script, "@"+name+".lua", "bt", _G);
}
} catch ( Exception e ) {
e.printStackTrace();

View File

@@ -11,8 +11,8 @@ import java.net.URL;
import junit.framework.TestCase;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.Print;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -21,7 +21,7 @@ abstract public class AbstractUnitTests extends TestCase {
private final String dir;
private final String jar;
private LuaTable _G;
private Globals _G;
public AbstractUnitTests(String zipdir, String zipfile, String dir) {
URL zip = null;
@@ -67,7 +67,7 @@ abstract public class AbstractUnitTests extends TestCase {
// compile in memory
InputStream is = new ByteArrayInputStream(lua);
Prototype p = LuaC.instance.compile(is, "@" + file);
Prototype p = _G.loadPrototype(is, "@" + file, "bt");
String actual = protoToString(p);
// load expected value from jar
@@ -109,7 +109,7 @@ abstract public class AbstractUnitTests extends TestCase {
protected Prototype loadFromBytes(byte[] bytes, String script)
throws IOException {
InputStream is = new ByteArrayInputStream(bytes);
return LoadState.loadBinaryChunk(is.read(), is, script);
return _G.loadPrototype(is, script, "b");
}
protected String protoToString(Prototype p) {

View File

@@ -6,13 +6,15 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import junit.framework.TestCase;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaClosure;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Prototype;
import org.luaj.vm2.lib.jse.JsePlatform;
@@ -28,7 +30,7 @@ public class DumpLoadEndianIntTest extends TestCase {
private static final String withdoubles = "1234-#!-23.75";
private static final String withints = "1234-#!-23";
private LuaTable _G;
private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -83,8 +85,8 @@ public class DumpLoadEndianIntTest extends TestCase {
try {
// compile into prototype
InputStream is = new ByteArrayInputStream(script.getBytes());
Prototype p = LuaC.instance.compile(is, "script");
Reader reader = new StringReader(script);
Prototype p = _G.compilePrototype(reader, "script");
// double check script result before dumping
LuaFunction f = new LuaClosure(p, _G);
@@ -107,8 +109,8 @@ public class DumpLoadEndianIntTest extends TestCase {
byte[] dumped = baos.toByteArray();
// load again using compiler
is = new ByteArrayInputStream(dumped);
f = LoadState.load(is, "dumped", "bt", _G);
InputStream is = new ByteArrayInputStream(dumped);
f = _G.load(is, "dumped", "b", _G).checkfunction();
r = f.call();
actual = r.tojstring();
assertEquals( expectedPostDump, actual );

View File

@@ -1,20 +1,16 @@
package org.luaj.vm2.compiler;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import junit.framework.TestCase;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaDouble;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaInteger;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.JsePlatform;
public class SimpleTests extends TestCase {
private LuaTable _G;
private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -23,8 +19,7 @@ public class SimpleTests extends TestCase {
private void doTest( String script ) {
try {
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
LuaFunction c = LuaC.instance.load( is, "script", _G );
LuaValue c = _G.load(script, "script");
c.call();
} catch ( Exception e ) {
fail("i/o exception: "+e );

View File

@@ -1,20 +1,13 @@
package org.luaj.vm2.lib.jse;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.Permission;
import junit.framework.TestCase;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.jse.JsePlatform;
public class LuajavaAccessibleMembersTest extends TestCase {
private LuaTable _G;
private Globals _G;
protected void setUp() throws Exception {
super.setUp();
@@ -23,8 +16,7 @@ public class LuajavaAccessibleMembersTest extends TestCase {
private String invokeScript(String script) {
try {
InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") );
LuaFunction c = LuaC.instance.load( is, "script", _G );
LuaValue c = _G.load(script, "script");
return c.call().tojstring();
} catch ( Exception e ) {
fail("exception: "+e );

View File

@@ -21,7 +21,6 @@
******************************************************************************/
package org.luaj.vm2.script;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.Reader;
@@ -100,8 +99,8 @@ public class ScriptEngineTests extends TestSuite {
return new SimpleBindings();
}
public void setUp() {
System.setProperty("org.luaj.luajc", "true");
super.setUp();
org.luaj.vm2.luajc.LuaJC.install();
}
public void testCompiledFunctionIsNotClosure() throws ScriptException {
CompiledScript cs = ((Compilable)e).compile("return 'foo'");