diff --git a/.classpath b/.classpath index 1009c29a..1bd1e178 100644 --- a/.classpath +++ b/.classpath @@ -1,6 +1,7 @@ + diff --git a/build.xml b/build.xml index dd877173..8873f8c4 100644 --- a/build.xml +++ b/build.xml @@ -1,5 +1,5 @@ - + @@ -9,7 +9,7 @@ - + @@ -17,6 +17,7 @@ + @@ -34,7 +35,7 @@ Skipping steps that require J2ME WTK. - + @@ -44,8 +45,8 @@ - - + + @@ -73,10 +74,11 @@ - + + diff --git a/src/core/org/luaj/vm/LuaState.java b/src/core/org/luaj/vm/LuaState.java index 18c96372..f4bc5c2d 100644 --- a/src/core/org/luaj/vm/LuaState.java +++ b/src/core/org/luaj/vm/LuaState.java @@ -69,14 +69,6 @@ import org.luaj.lib.TableLib; * */ public class LuaState extends Lua { - - protected static final String DEBUG_CLASS_NAME = "org.luaj.debug.DebugLuaState"; - - public static final String PROPERTY_LUAJ_DEBUG = "Luaj-Debug"; - public static final String PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START = "Luaj-Debug-SuspendAtStart"; - public static final String PROPERTY_LUAJ_DEBUG_HOST = "Luaj-Debug-Host"; - public static final String PROPERTY_LUAJ_DEBUG_PORT = "Luaj-Debug-Port"; - /* thread status; 0 is OK */ private static final int LUA_YIELD = 1; private static final int LUA_ERRRUN = 2; @@ -117,10 +109,8 @@ public class LuaState extends Lua { * does all memory allocation for this state through this function. The * second argument, ud, is an opaque pointer that Lua simply * passes to the allocator in every call. - * - * @deprecated As of version 0.10, replaced by {@link #newState()} */ - public LuaState() { + protected LuaState() { _G = new LTable(); _G.put("_G", _G); mainState = this; @@ -134,33 +124,6 @@ public class LuaState extends Lua { LuaState(LTable globals) { _G = globals; } - - /** - * Factory method to return an instance of LuaState. If debug property is - * present, it will create a DebugLuaState instance. - * @return - */ - public static LuaState newState() { - String isDebugStr - = Platform.getInstance().getProperty(PROPERTY_LUAJ_DEBUG); - boolean isDebug = (isDebugStr != null && "true".equalsIgnoreCase(isDebugStr)); - - LuaState vm = null; - if ( isDebug ) { - try { - vm = (LuaState) Class.forName( DEBUG_CLASS_NAME ).newInstance(); - } catch (Exception e) { - System.out.println("Warning: no debug support, " + e ); - } - } - - if ( vm == null ) - vm = new LuaState(); - - vm.init(); - - return vm; - } /** * Performs the initialization. diff --git a/src/core/org/luaj/vm/Platform.java b/src/core/org/luaj/vm/Platform.java index 07e68893..61251508 100644 --- a/src/core/org/luaj/vm/Platform.java +++ b/src/core/org/luaj/vm/Platform.java @@ -23,16 +23,27 @@ package org.luaj.vm; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.Reader; /** - * Singleton to manage platform-specific behaviors. - * - * @deprecated - will probably be replaced with Config, LuaConfig or something - * similar. + * Singleton to manage platform-specific behaviors. + *

+ * Here is the sample code to set up the platform instance and create a new + * LuaState instance. + *

+ *    Platform.setInstance(new J2meMidp20Cldc11Platform());
+ *    LuaState vm = Platform.newLuaState();
+ * 
*/ abstract public class Platform { + + protected static final String DEBUG_CLASS_NAME = "org.luaj.debug.DebugLuaState"; + + public static final String PROPERTY_LUAJ_DEBUG = "Luaj-Debug"; + public static final String PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START = "Luaj-Debug-SuspendAtStart"; + public static final String PROPERTY_LUAJ_DEBUG_HOST = "Luaj-Debug-Host"; + public static final String PROPERTY_LUAJ_DEBUG_PORT = "Luaj-Debug-Port"; + private static Platform instance; /** @@ -43,31 +54,9 @@ abstract public class Platform { */ public static Platform getInstance() { if (instance == null) { - instance = new Platform() { - public Reader createReader(InputStream inputStream) { - return new InputStreamReader(inputStream); - } - - public InputStream openFile(String fileName) { - return getClass().getResourceAsStream("/" + fileName); - } - - /** - * Assumes J2SE platform, return the corresponding system - * property - */ - public String getProperty(String propertyName) { - return System.getProperty(propertyName); - } - - /** - * Provides a J2SE DebugSupport instance. - */ - public DebugNetSupport getDebugSupport() throws IOException { - return null; - } - }; + throw new RuntimeException("Platform instance is null. Use Platform.setInstance(Platform p) to set the instance first."); } + return instance; } @@ -81,6 +70,35 @@ abstract public class Platform { instance = platform; } + /** + * Creates a new instance of LuaState. If debug properties are present, + * DebugLuaState (a LuaState with debugging capabilities) will be created. + * + * @return a new instance of LuaState + */ + public static LuaState newLuaState() { + Platform p = Platform.getInstance(); + String isDebugStr = p.getProperty(PROPERTY_LUAJ_DEBUG); + boolean isDebug = (isDebugStr != null && "true".equalsIgnoreCase(isDebugStr)); + + LuaState vm = null; + if (isDebug) { + try { + vm = (LuaState) Class.forName(DEBUG_CLASS_NAME).newInstance(); + } catch (Exception e) { + System.out.println("Warning: no debug support, " + e); + } + } + + if (vm == null) + vm = new LuaState(); + + vm.init(); + p.installOptionalLibs(vm); + + return vm; + } + /** * Return an InputStream or null if not found for a particular file name. * @@ -115,12 +133,26 @@ abstract public class Platform { */ abstract public DebugNetSupport getDebugSupport() throws IOException; + /** + * Install optional libraries on the LuaState. + * @param vm LuaState instance + */ + abstract protected void installOptionalLibs(LuaState vm); + + /** + * Compute math.pow() for two numbers using double math when available. + * @param lhs LNumber base + * @param rhs LNumber exponent + * @return base ^ exponent as a LNumber, throw RuntimeException if not implemented + */ + abstract public LNumber mathPow(double lhs, double rhs); + /** * Convenience method for the subclasses to figure out the debug host. * @return the debug host property. If it is not present, null is returned. */ protected String getDebugHost() { - String host = getProperty(LuaState.PROPERTY_LUAJ_DEBUG_HOST); + String host = getProperty(PROPERTY_LUAJ_DEBUG_HOST); return host; } @@ -130,7 +162,7 @@ abstract public class Platform { * as an integer if it is present in the platform properties and valid. */ protected int getDebugPort() { - String portStr = getProperty(LuaState.PROPERTY_LUAJ_DEBUG_PORT); + String portStr = getProperty(PROPERTY_LUAJ_DEBUG_PORT); int port = -1; if (portStr != null) { try { @@ -139,14 +171,4 @@ abstract public class Platform { } return port; } - - /** - * Compute math.pow() for two numbers using double math when available. - * @param lhs LNumber base - * @param rhs LNumber exponent - * @return base ^ exponent as a LNumber, or null if not implemented - */ - public LNumber mathPow(double lhs, double rhs) { - return null; - } } diff --git a/src/debug/org/luaj/debug/DebugLuaState.java b/src/debug/org/luaj/debug/DebugLuaState.java index 9f1e895b..5b295851 100644 --- a/src/debug/org/luaj/debug/DebugLuaState.java +++ b/src/debug/org/luaj/debug/DebugLuaState.java @@ -176,7 +176,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { /** * Creates an instance of DebugLuaState. * - * @deprecated As of version 0.10, replaced by {@link #LuaState.newState()} + * @deprecated As of version 0.10, replaced by {@link #Platform.newLuaState()} */ public DebugLuaState() {} @@ -185,10 +185,12 @@ public class DebugLuaState extends LuaState implements DebugRequestListener { * @see org.luaj.vm.LuaState#init() */ public void init() { + super.init(); + Platform platform = Platform.getInstance(); // set if the vm should be suspended at start - String suspendOnStartStr = platform.getProperty(PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START); + String suspendOnStartStr = platform.getProperty(Platform.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START); boolean bSuspendOnStart = (suspendOnStartStr != null && "true".equalsIgnoreCase(suspendOnStartStr)); setSuspendAtStart(bSuspendOnStart); diff --git a/src/debug/org/luaj/debug/j2se/J2sePlatform.java b/src/debug/org/luaj/debug/j2se/J2sePlatform.java deleted file mode 100644 index 38297d69..00000000 --- a/src/debug/org/luaj/debug/j2se/J2sePlatform.java +++ /dev/null @@ -1,44 +0,0 @@ -package org.luaj.debug.j2se; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; - -import org.luaj.debug.net.j2se.DebugSupportImpl; -import org.luaj.vm.DebugNetSupport; -import org.luaj.vm.Platform; - -public class J2sePlatform extends Platform { - public Reader createReader(InputStream inputStream) { - return new InputStreamReader(inputStream); - } - - public InputStream openFile(String filePath) { - try { - FileInputStream fis = new FileInputStream(new File(filePath)); - return fis; - } catch (IOException e) { - return null; - } - } - - /** - * Assumes J2SE platform, return the corresponding system - * property - */ - public String getProperty(String propertyName) { - return System.getProperty(propertyName); - } - - /** - * Provides a J2SE DebugSupport instance. - */ - public DebugNetSupport getDebugSupport() throws IOException { - int port = getDebugPort(); - DebugSupportImpl debugSupport = new DebugSupportImpl(port); - return debugSupport; - } -} diff --git a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java index b444d678..ea27c20c 100644 --- a/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java +++ b/src/debug/org/luaj/debug/j2se/StandardLuaJVM.java @@ -29,7 +29,7 @@ import java.io.InputStream; import org.luaj.compiler.LuaC; import org.luaj.debug.DebugLuaState; import org.luaj.lib.PackageLib; -import org.luaj.lib.j2se.LuajavaLib; +import org.luaj.platform.J2sePlatform; import org.luaj.vm.LClosure; import org.luaj.vm.LPrototype; import org.luaj.vm.LString; @@ -84,7 +84,7 @@ public class StandardLuaJVM { } this.isDebugMode = true; - System.setProperty(LuaState.PROPERTY_LUAJ_DEBUG, "true"); + System.setProperty(Platform.PROPERTY_LUAJ_DEBUG, "true"); String debugOptions = args[index]; debugOptions = debugOptions.substring(2); // remove '-D' @@ -94,7 +94,7 @@ public class StandardLuaJVM { String portString = options[i].substring(CMD_LINE_DEBUG_OPTION_PORT.length()); try { this.debugPort = Integer.parseInt(portString); - System.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, String.valueOf(debugPort)); + System.setProperty(Platform.PROPERTY_LUAJ_DEBUG_PORT, String.valueOf(debugPort)); if (this.debugPort <= 0) { throw new ParseException( "Invalid debug port: it must be greater than zero."); @@ -110,7 +110,7 @@ public class StandardLuaJVM { throw new ParseException("invalid debug flag: suspendOnStart"); } this.bSuspendOnStart = Boolean.parseBoolean(suspendOnStartStr); - System.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START, suspendOnStartStr); + System.setProperty(Platform.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START, suspendOnStartStr); } else { throw new ParseException("Invalid command line argument: " + debugOptions); } @@ -184,9 +184,8 @@ public class StandardLuaJVM { public void run() { try { // new lua debug state - Platform.setInstance(new J2sePlatform()); - - state = LuaState.newState(); + Platform.setInstance(new J2sePlatform()); + state = Platform.newLuaState(); init(state); // load the Lua file @@ -218,13 +217,6 @@ public class StandardLuaJVM { } protected void init(LuaState state) { - - // add standard bindings - state.installStandardLibs(); - - // add LuaJava bindings - LuajavaLib.install(state._G); - // add the compiler LuaC.install(); diff --git a/src/j2me/org/luaj/platform/J2meMidp10Cldc10Platform.java b/src/j2me/org/luaj/platform/J2meMidp10Cldc10Platform.java new file mode 100644 index 00000000..168da932 --- /dev/null +++ b/src/j2me/org/luaj/platform/J2meMidp10Cldc10Platform.java @@ -0,0 +1,51 @@ +package org.luaj.platform; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import javax.microedition.midlet.MIDlet; + +import org.luaj.debug.net.j2me.DebugSupportImpl; +import org.luaj.vm.DebugNetSupport; +import org.luaj.vm.LNumber; +import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; + +public class J2meMidp10Cldc10Platform extends Platform { + protected MIDlet midlet; + + public J2meMidp10Cldc10Platform(MIDlet midlet) { + this.midlet = midlet; + } + + public Reader createReader(InputStream inputStream) { + return new InputStreamReader(inputStream); + } + + public InputStream openFile(String fileName) { + if (!fileName.startsWith("/")) + fileName = "/" + fileName; + InputStream is = this.getClass().getResourceAsStream(fileName); + return is; + } + + public DebugNetSupport getDebugSupport() throws IOException { + String host = getDebugHost(); + int port = getDebugPort(); + return new DebugSupportImpl(host, port); + } + + public String getProperty(String key) { + return midlet.getAppProperty(key); + } + + protected void installOptionalLibs(LuaState vm) { + vm.installStandardLibs(); + } + + public LNumber mathPow(double lhs, double rhs) { + throw new RuntimeException("mathPow(double lhs, double rhs) is not supported."); + } +} diff --git a/src/j2me/org/luaj/platform/J2meMidp20Cldc11Platform.java b/src/j2me/org/luaj/platform/J2meMidp20Cldc11Platform.java new file mode 100644 index 00000000..edb5ddf8 --- /dev/null +++ b/src/j2me/org/luaj/platform/J2meMidp20Cldc11Platform.java @@ -0,0 +1,9 @@ +package org.luaj.platform; + +import javax.microedition.midlet.MIDlet; + +public class J2meMidp20Cldc11Platform extends J2meMidp10Cldc10Platform { + public J2meMidp20Cldc11Platform(MIDlet midlet) { + super(midlet); + } +} diff --git a/src/j2se/org/luaj/platform/J2sePlatform.java b/src/j2se/org/luaj/platform/J2sePlatform.java new file mode 100644 index 00000000..a13ac910 --- /dev/null +++ b/src/j2se/org/luaj/platform/J2sePlatform.java @@ -0,0 +1,51 @@ +package org.luaj.platform; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; + +import org.luaj.debug.net.j2se.DebugSupportImpl; +import org.luaj.lib.j2se.LuajavaLib; +import org.luaj.vm.DebugNetSupport; +import org.luaj.vm.LDouble; +import org.luaj.vm.LNumber; +import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; + +public class J2sePlatform extends Platform { + public Reader createReader(InputStream inputStream) { + return new InputStreamReader(inputStream); + } + + public DebugNetSupport getDebugSupport() throws IOException { + DebugNetSupport debugNetSupport = new DebugSupportImpl(getDebugPort()); + return debugNetSupport; + } + + public String getProperty(String propertyName) { + return System.getProperty(propertyName); + } + + protected void installOptionalLibs(LuaState vm) { + vm.installStandardLibs(); + LuajavaLib.install(vm._G); + } + + public InputStream openFile(String fileName) { + File file = new File(fileName); + try { + return new FileInputStream(file); + } catch (FileNotFoundException e) { + return null; + } + } + + public LNumber mathPow(double lhs, double rhs) { + double d = Math.pow(lhs, rhs); + return LDouble.valueOf(d); + } +} diff --git a/src/sample/org/luaj/sample/LuaRunner.java b/src/sample/org/luaj/sample/LuaRunner.java index 2bd065d7..f8c9f190 100644 --- a/src/sample/org/luaj/sample/LuaRunner.java +++ b/src/sample/org/luaj/sample/LuaRunner.java @@ -27,12 +27,14 @@ import java.io.IOException; import java.io.InputStream; import org.luaj.compiler.LuaC; +import org.luaj.platform.J2sePlatform; import org.luaj.vm.LClosure; import org.luaj.vm.LPrototype; import org.luaj.vm.LValue; import org.luaj.vm.LoadState; import org.luaj.vm.LuaErrorException; import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; /** @@ -45,18 +47,16 @@ public class LuaRunner { public static void main( String[] args ) throws IOException { // new lua state - LuaState state = LuaState.newState(); + Platform.setInstance(new J2sePlatform()); + LuaState state = Platform.newLuaState(); + LuaC.install(); // get script name for ( int i=0; i0? args[0]: "/swingapp.luac"); diff --git a/src/test/java/org/luaj/TestPlatform.java b/src/test/java/org/luaj/TestPlatform.java new file mode 100644 index 00000000..ad00dea0 --- /dev/null +++ b/src/test/java/org/luaj/TestPlatform.java @@ -0,0 +1,11 @@ +package org.luaj; + +import java.io.InputStream; + +import org.luaj.platform.J2sePlatform; + +public class TestPlatform extends J2sePlatform { + public InputStream openFile(String fileName) { + return getClass().getResourceAsStream("/" + fileName); + } +} diff --git a/src/test/java/org/luaj/compiler/AbstractUnitTests.java b/src/test/java/org/luaj/compiler/AbstractUnitTests.java index fcb8337f..1de26a0f 100644 --- a/src/test/java/org/luaj/compiler/AbstractUnitTests.java +++ b/src/test/java/org/luaj/compiler/AbstractUnitTests.java @@ -9,84 +9,89 @@ import java.net.URL; import junit.framework.TestCase; +import org.luaj.TestPlatform; import org.luaj.debug.Print; import org.luaj.vm.LPrototype; import org.luaj.vm.LoadState; import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; +abstract public class AbstractUnitTests extends TestCase { -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; - } + private final String zipfile; + private final String 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 ); - LPrototype p = LuaC.compile(is, dir+"/"+file); - String actual = protoToString( p ); - - // load expected value from jar - byte[] luac = bytesFromJar( path + "c" ); - LPrototype e = loadFromBytes( luac, file ); - String expected = protoToString( e ); + public AbstractUnitTests(String zipfile, String dir) { + this.zipfile = zipfile; + this.dir = dir; + } - // compare results - assertEquals( expected, actual ); - - // dump into memory - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - DumpState.dump(p, baos, false); - byte[] dumped = baos.toByteArray(); - - // re-undump - LPrototype 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 LPrototype loadFromBytes(byte[] bytes, String script) throws IOException { - LuaState state = new LuaState(); - InputStream is = new ByteArrayInputStream( bytes ); - return LoadState.undump(state, is, script); - } - - protected String protoToString(LPrototype p) { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - PrintStream ps = new PrintStream( baos ); - Print.ps = ps; - new Print().printFunction(p, true); - return baos.toString(); - } - + protected void setUp() throws Exception { + super.setUp(); + Platform.setInstance(new TestPlatform()); + } + + 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); + LPrototype p = LuaC.compile(is, dir + "/" + file); + String actual = protoToString(p); + + // load expected value from jar + byte[] luac = bytesFromJar(path + "c"); + LPrototype 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 + LPrototype 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 LPrototype loadFromBytes(byte[] bytes, String script) + throws IOException { + LuaState state = Platform.newLuaState(); + InputStream is = new ByteArrayInputStream(bytes); + return LoadState.undump(state, is, script); + } + + protected String protoToString(LPrototype p) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + Print.ps = ps; + new Print().printFunction(p, true); + return baos.toString(); + } } diff --git a/src/test/java/org/luaj/compiler/CompilerUnitTests.java b/src/test/java/org/luaj/compiler/CompilerUnitTests.java index 584aeb26..1923e50b 100644 --- a/src/test/java/org/luaj/compiler/CompilerUnitTests.java +++ b/src/test/java/org/luaj/compiler/CompilerUnitTests.java @@ -1,29 +1,14 @@ package org.luaj.compiler; -import org.luaj.debug.j2se.J2sePlatform; -import org.luaj.vm.LDouble; -import org.luaj.vm.LNumber; -import org.luaj.vm.Platform; public class CompilerUnitTests extends AbstractUnitTests { - static { - // override platform to test with standard debug features. - Platform.setInstance( new J2sePlatform() { - public LNumber mathPow(double lhs, double rhs) { - double d = Math.pow(lhs, rhs); - return LDouble.valueOf(d); - } - }); - } - - public CompilerUnitTests() { - super( "src/test/compile/lua5.1-tests.zip", - "lua5.1-tests" ); - } - - public void testAll() { doTest("all.lua"); } + 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"); } diff --git a/src/test/java/org/luaj/compiler/SimpleTests.java b/src/test/java/org/luaj/compiler/SimpleTests.java index 031f5048..f4664e6c 100644 --- a/src/test/java/org/luaj/compiler/SimpleTests.java +++ b/src/test/java/org/luaj/compiler/SimpleTests.java @@ -5,15 +5,22 @@ import java.io.InputStream; import junit.framework.TestCase; +import org.luaj.TestPlatform; import org.luaj.debug.Print; import org.luaj.lib.BaseLib; import org.luaj.vm.LClosure; import org.luaj.vm.LPrototype; import org.luaj.vm.LValue; import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; public class SimpleTests extends TestCase { + protected void setUp() throws Exception { + super.setUp(); + Platform.setInstance(new TestPlatform()); + } + private void doTest( String script ) { try { InputStream is = new ByteArrayInputStream( script.getBytes("UTF8") ); @@ -22,7 +29,7 @@ public class SimpleTests extends TestCase { Print.printCode( p ); // try running the code! - LuaState state = new LuaState(); + LuaState state = Platform.newLuaState(); BaseLib.install( state._G ); LClosure c = new LClosure( state, p ); state.doCall( c, new LValue[0] ); diff --git a/src/test/java/org/luaj/debug/DebugStackStateTest.java b/src/test/java/org/luaj/debug/DebugStackStateTest.java index d75dcb21..bad30226 100644 --- a/src/test/java/org/luaj/debug/DebugStackStateTest.java +++ b/src/test/java/org/luaj/debug/DebugStackStateTest.java @@ -24,21 +24,33 @@ package org.luaj.debug; import java.io.IOException; import java.io.InputStream; -import org.luaj.debug.DebugLuaState; +import junit.framework.TestCase; + +import org.luaj.TestPlatform; +import org.luaj.compiler.LuaC; +import org.luaj.vm.DebugNetSupport; import org.luaj.vm.LClosure; +import org.luaj.vm.LPrototype; import org.luaj.vm.LValue; import org.luaj.vm.LoadState; -import org.luaj.vm.LPrototype; - -import junit.framework.TestCase; +import org.luaj.vm.LuaState; +import org.luaj.vm.Platform; public class DebugStackStateTest extends TestCase { public void testDebugStackState() throws InterruptedException, IOException { - String script = "/test6.luac"; + String script = "/test6.lua"; // set up the vm - final DebugLuaState state = new DebugLuaState(); + System.setProperty(Platform.PROPERTY_LUAJ_DEBUG, "true"); + Platform.setInstance(new TestPlatform() { + public DebugNetSupport getDebugSupport() throws IOException { + return null; + } + }); + + final DebugLuaState state = (DebugLuaState) Platform.newLuaState(); + LuaC.install(); InputStream is = getClass().getResourceAsStream( script ); LPrototype p = LoadState.undump(state, is, script); diff --git a/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java index 9df6c369..33a7b77d 100644 --- a/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java +++ b/src/test/java/org/luaj/debug/j2se/LuaJVMTest.java @@ -21,20 +21,13 @@ ******************************************************************************/ package org.luaj.debug.j2se; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; import java.net.URL; import java.util.Properties; import junit.framework.TestCase; -import org.luaj.debug.DebugLuaState; +import org.luaj.TestPlatform; import org.luaj.debug.j2se.StandardLuaJVM.ParseException; -import org.luaj.debug.net.j2se.DebugSupportImpl; -import org.luaj.vm.DebugNetSupport; -import org.luaj.vm.LuaState; import org.luaj.vm.Platform; /** @@ -44,33 +37,8 @@ public class LuaJVMTest extends TestCase { protected void setUp() throws Exception { super.setUp(); - System.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "1999"); - - Platform.setInstance(new Platform() { - public Reader createReader(InputStream inputStream) { - return new InputStreamReader(inputStream); - } - - public InputStream openFile(String fileName) { - return getClass().getResourceAsStream("/" + fileName); - } - - /** - * Assumes J2SE platform, return the corresponding system property - */ - public String getProperty(String propertyName) { - return System.getProperty(propertyName); - } - - /** - * Provides a J2SE DebugSupport instance. - */ - public DebugNetSupport getDebugSupport() throws IOException { - int port = getDebugPort(); - DebugSupportImpl debugSupport = new DebugSupportImpl(port); - return debugSupport; - } - }); + System.setProperty(Platform.PROPERTY_LUAJ_DEBUG_PORT, "1999"); + Platform.setInstance(new TestPlatform()); } public void testCommandLineParse() { @@ -244,10 +212,10 @@ public class LuaJVMTest extends TestCase { public void testRun() { Properties props = System.getProperties(); - props.remove(LuaState.PROPERTY_LUAJ_DEBUG); - props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_HOST); - props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT); - props.remove(DebugLuaState.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START); + props.remove(Platform.PROPERTY_LUAJ_DEBUG); + props.remove(Platform.PROPERTY_LUAJ_DEBUG_HOST); + props.remove(Platform.PROPERTY_LUAJ_DEBUG_PORT); + props.remove(Platform.PROPERTY_LUAJ_DEBUG_SUSPEND_AT_START); System.setProperties(props); String[] tests = new String[] { "autoload", "boolean", "calls", @@ -261,8 +229,8 @@ public class LuaJVMTest extends TestCase { public void testDebugRun() { Properties props = System.getProperties(); - props.setProperty(LuaState.PROPERTY_LUAJ_DEBUG, "true"); - props.setProperty(DebugLuaState.PROPERTY_LUAJ_DEBUG_PORT, "1999"); + props.setProperty(Platform.PROPERTY_LUAJ_DEBUG, "true"); + props.setProperty(Platform.PROPERTY_LUAJ_DEBUG_PORT, "1999"); System.setProperties(props); String[] tests = new String[] { "boolean", "calls", diff --git a/src/test/java/org/luaj/vm/LuaJTest.java b/src/test/java/org/luaj/vm/LuaJTest.java index 88d48ec5..f7fdd729 100644 --- a/src/test/java/org/luaj/vm/LuaJTest.java +++ b/src/test/java/org/luaj/vm/LuaJTest.java @@ -6,301 +6,302 @@ import java.io.OutputStream; import junit.framework.TestCase; +import org.luaj.TestPlatform; import org.luaj.compiler.LuaC; -import org.luaj.debug.DebugLuaState; import org.luaj.lib.BaseLib; -import org.luaj.lib.j2se.LuajavaLib; public class LuaJTest extends TestCase { + - public void testTest1() throws IOException, InterruptedException { - runTest( "test1" ); - } + protected void setUp() throws Exception { + super.setUp(); + Platform.setInstance(new TestPlatform()); + } - public void testTest2() throws IOException, InterruptedException { - runTest( "test2" ); - } + public void testTest1() throws IOException, InterruptedException { + runTest( "test1" ); + } - public void testTest3() throws IOException, InterruptedException { - runTest( "test3" ); - } + public void testTest2() throws IOException, InterruptedException { + runTest( "test2" ); + } - public void testTest4() throws IOException, InterruptedException { - runTest( "test4" ); - } + public void testTest3() throws IOException, InterruptedException { + runTest( "test3" ); + } - public void testTest5() throws IOException, InterruptedException { - runTest( "test5" ); - } + public void testTest4() throws IOException, InterruptedException { + runTest( "test4" ); + } - public void testTest6() throws IOException, InterruptedException { - runTest( "test6" ); - } + public void testTest5() throws IOException, InterruptedException { + runTest( "test5" ); + } - public void testTest7() throws IOException, InterruptedException { - runTest( "test7" ); - } + public void testTest6() throws IOException, InterruptedException { + runTest( "test6" ); + } - public void testTest8() throws IOException, InterruptedException { - runTest( "test8" ); - } + public void testTest7() throws IOException, InterruptedException { + runTest( "test7" ); + } - public void testArgtypes() throws IOException, InterruptedException { - runTest( "argtypes" ); - } + public void testTest8() throws IOException, InterruptedException { + runTest( "test8" ); + } - public void testAutoload() throws IOException, InterruptedException { - runTest( "autoload" ); - } + public void testArgtypes() throws IOException, InterruptedException { + runTest( "argtypes" ); + } - public void testBaseLib() throws IOException, InterruptedException { - runTest( "baselib" ); - } - - public void testBoolean() throws IOException, InterruptedException { - runTest( "boolean" ); - } + public void testAutoload() throws IOException, InterruptedException { + runTest( "autoload" ); + } - public void testCalls() throws IOException, InterruptedException { - runTest( "calls" ); - } + public void testBaseLib() throws IOException, InterruptedException { + runTest( "baselib" ); + } + + public void testBoolean() throws IOException, InterruptedException { + runTest( "boolean" ); + } - public void testCoercions() throws IOException, InterruptedException { - runTest( "coercions" ); - } - - public void testCoroutines() throws IOException, InterruptedException { - runTest( "coroutines" ); - } - - public void testCompare() throws IOException, InterruptedException { - runTest( "compare" ); - } + public void testCalls() throws IOException, InterruptedException { + runTest( "calls" ); + } - public void testErrors() throws IOException, InterruptedException { - runTest( "errors" ); - } + public void testCoercions() throws IOException, InterruptedException { + runTest( "coercions" ); + } + + public void testCoroutines() throws IOException, InterruptedException { + runTest( "coroutines" ); + } + + public void testCompare() throws IOException, InterruptedException { + runTest( "compare" ); + } - public void testLoops() throws IOException, InterruptedException { - runTest( "loops" ); - } + public void testErrors() throws IOException, InterruptedException { + runTest( "errors" ); + } - public void testMathLib() throws IOException, InterruptedException { - runTest( "mathlib" ); - } + public void testLoops() throws IOException, InterruptedException { + runTest( "loops" ); + } - public void testMetatables() throws IOException, InterruptedException { - runTest( "metatables" ); - } + public void testMathLib() throws IOException, InterruptedException { + runTest( "mathlib" ); + } - public void testModule() throws IOException, InterruptedException { - runTest( "module" ); - } + public void testMetatables() throws IOException, InterruptedException { + runTest( "metatables" ); + } - public void testNext() throws IOException, InterruptedException { - runTest( "next" ); - } + public void testModule() throws IOException, InterruptedException { + runTest( "module" ); + } - public void testPcalls() throws IOException, InterruptedException { - runTest( "pcalls" ); - } - - public void testRequire() throws IOException, InterruptedException { - runTest( "require" ); - } - - public void testSelect() throws IOException, InterruptedException { - runTest( "select" ); - } + public void testNext() throws IOException, InterruptedException { + runTest( "next" ); + } - public void testSetfenv() throws IOException, InterruptedException { - runTest( "setfenv" ); - } + public void testPcalls() throws IOException, InterruptedException { + runTest( "pcalls" ); + } + + public void testRequire() throws IOException, InterruptedException { + runTest( "require" ); + } + + public void testSelect() throws IOException, InterruptedException { + runTest( "select" ); + } - public void testSetlist() throws IOException, InterruptedException { - runTest( "setlist" ); - } - - public void testSimpleMetatables() throws IOException, InterruptedException { - runTest( "simplemetatables" ); - } - - public void testStrLib() throws IOException, InterruptedException { - runTest( "strlib" ); - } - - public void testTable() throws IOException, InterruptedException { - runTest( "table" ); - } + public void testSetfenv() throws IOException, InterruptedException { + runTest( "setfenv" ); + } - public void testType() throws IOException, InterruptedException { - runTest( "type" ); - } - - public void testUpvalues() throws IOException, InterruptedException { - runTest( "upvalues" ); - } - - public void testUpvalues2() throws IOException, InterruptedException { - runTest( "upvalues2" ); - } + public void testSetlist() throws IOException, InterruptedException { + runTest( "setlist" ); + } + + public void testSimpleMetatables() throws IOException, InterruptedException { + runTest( "simplemetatables" ); + } + + public void testStrLib() throws IOException, InterruptedException { + runTest( "strlib" ); + } + + public void testTable() throws IOException, InterruptedException { + runTest( "table" ); + } + + public void testType() throws IOException, InterruptedException { + runTest( "type" ); + } + + public void testUpvalues() throws IOException, InterruptedException { + runTest( "upvalues" ); + } + + public void testUpvalues2() throws IOException, InterruptedException { + runTest( "upvalues2" ); + } //*/ - private void runTest( String testName ) throws IOException, InterruptedException { + private void runTest( String testName ) throws IOException, InterruptedException { - // new lua state - LuaState state = LuaState.newState(); - - // add standard bindings - state.installStandardLibs(); - - // add luajava - LuajavaLib.install( state._G ); - LuaC.install(); - - // load the file - LPrototype p = loadScriptResource( state, testName ); - - // Replace System.out with a ByteArrayOutputStream - ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - BaseLib.redirectOutput( outputStream ); - try { - // create closure and execute - LClosure c = new LClosure( p, state._G ); - state.doCall(c, new LValue[0]); - - final String actualOutput = new String( outputStream.toByteArray() ); - final String expectedOutput = getExpectedOutput( testName ); - - assertEquals( expectedOutput, actualOutput ); - } finally { - BaseLib.restoreStandardOutput(); - outputStream.close(); - } - } - - private LPrototype loadScriptResource( LuaState state, String name ) throws IOException { - InputStream script = getClass().getResourceAsStream( "/"+name+".luac" ); - if ( script == null ) { - script = getClass().getResourceAsStream( "/"+name+".lua" ); - if ( script == null ) { - fail( "Could not load script for test case: "+name ); - } - } - - try { - return LoadState.undump(state, script, name); - } finally { - script.close(); - } - } - - private String getExpectedOutput( final String testName ) throws IOException, InterruptedException { - String expectedOutputName = "/" + testName + "-expected.out"; - InputStream is = getClass().getResourceAsStream( expectedOutputName ); - if ( is != null ) { - try { - return readString( is ); - } finally { - is.close(); - } - } else { - InputStream script; -// script = getClass().getResourceAsStream( "/" + testName + ".luac" ); -// if ( script == null ) { - script = getClass().getResourceAsStream( "/" + testName + ".lua" ); - if ( script == null ) { - fail( "Could not find script for test case: "+testName ); - } -// } - try { - return collectProcessOutput( new String[] { "lua", "-" }, script ); - } finally { - script.close(); - } - } - } - - private String collectProcessOutput( String[] cmd, final InputStream input ) throws IOException, InterruptedException { - Runtime r = Runtime.getRuntime(); - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final Process p = r.exec( cmd ); - try { - // start a thread to write the given input to the subprocess. - Thread inputCopier = (new Thread() { - public void run() { - try { - OutputStream processStdIn = p.getOutputStream(); - try { - copy( input, processStdIn ); - } finally { - processStdIn.close(); - } - } catch ( IOException e ) { - e.printStackTrace(); - } - } - }); - inputCopier.start(); - - // start another thread to read output from the subprocess. - Thread outputCopier = (new Thread() { - public void run() { - try { - InputStream processStdOut = p.getInputStream(); - try { - copy( processStdOut, baos ); - } finally { - processStdOut.close(); - } - } catch ( IOException ioe ) { - ioe.printStackTrace(); - } - } - }); - outputCopier.start(); - - // start another thread to read output from the subprocess. - Thread errorCopier = (new Thread() { - public void run() { - try { - InputStream processError = p.getErrorStream(); - try { - copy( processError, System.err ); - } finally { - processError.close(); - } - } catch ( IOException ioe ) { - ioe.printStackTrace(); - } - } - }); - errorCopier.start(); - - p.waitFor(); - inputCopier.join(); - outputCopier.join(); - errorCopier.join(); - - return new String( baos.toByteArray() ); - - } finally { - p.destroy(); - } - } - - private String readString( InputStream is ) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - copy( is, baos ); - return new String( baos.toByteArray() ); - } - - private void copy( InputStream is, OutputStream os ) throws IOException { - byte[] buf = new byte[ 1024 ]; - int r; - while ( ( r = is.read( buf ) ) >= 0 ) { - os.write( buf, 0, r ); - } - } + // new lua state + LuaState state = Platform.newLuaState(); + + // install the compiler + LuaC.install(); + + // load the file + LPrototype p = loadScriptResource( state, testName ); + + // Replace System.out with a ByteArrayOutputStream + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + BaseLib.redirectOutput( outputStream ); + try { + // create closure and execute + LClosure c = new LClosure( p, state._G ); + state.doCall(c, new LValue[0]); + + final String actualOutput = new String( outputStream.toByteArray() ); + final String expectedOutput = getExpectedOutput( testName ); + + assertEquals( expectedOutput, actualOutput ); + } finally { + BaseLib.restoreStandardOutput(); + outputStream.close(); + } + } + + private LPrototype loadScriptResource( LuaState state, String name ) throws IOException { + InputStream script = getClass().getResourceAsStream( "/"+name+".luac" ); + if ( script == null ) { + script = getClass().getResourceAsStream( "/"+name+".lua" ); + if ( script == null ) { + fail( "Could not load script for test case: "+name ); + } + } + + try { + return LoadState.undump(state, script, name); + } finally { + script.close(); + } + } + + private String getExpectedOutput( final String testName ) throws IOException, InterruptedException { + String expectedOutputName = "/" + testName + "-expected.out"; + InputStream is = getClass().getResourceAsStream( expectedOutputName ); + if ( is != null ) { + try { + return readString( is ); + } finally { + is.close(); + } + } else { + InputStream script; +// script = getClass().getResourceAsStream( "/" + testName + ".luac" ); +// if ( script == null ) { + script = getClass().getResourceAsStream( "/" + testName + ".lua" ); + if ( script == null ) { + fail( "Could not find script for test case: "+testName ); + } +// } + try { + return collectProcessOutput( new String[] { "lua", "-" }, script ); + } finally { + script.close(); + } + } + } + + private String collectProcessOutput( String[] cmd, final InputStream input ) throws IOException, InterruptedException { + Runtime r = Runtime.getRuntime(); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + final Process p = r.exec( cmd ); + try { + // start a thread to write the given input to the subprocess. + Thread inputCopier = (new Thread() { + public void run() { + try { + OutputStream processStdIn = p.getOutputStream(); + try { + copy( input, processStdIn ); + } finally { + processStdIn.close(); + } + } catch ( IOException e ) { + e.printStackTrace(); + } + } + }); + inputCopier.start(); + + // start another thread to read output from the subprocess. + Thread outputCopier = (new Thread() { + public void run() { + try { + InputStream processStdOut = p.getInputStream(); + try { + copy( processStdOut, baos ); + } finally { + processStdOut.close(); + } + } catch ( IOException ioe ) { + ioe.printStackTrace(); + } + } + }); + outputCopier.start(); + + // start another thread to read output from the subprocess. + Thread errorCopier = (new Thread() { + public void run() { + try { + InputStream processError = p.getErrorStream(); + try { + copy( processError, System.err ); + } finally { + processError.close(); + } + } catch ( IOException ioe ) { + ioe.printStackTrace(); + } + } + }); + errorCopier.start(); + + p.waitFor(); + inputCopier.join(); + outputCopier.join(); + errorCopier.join(); + + return new String( baos.toByteArray() ); + + } finally { + p.destroy(); + } + } + + private String readString( InputStream is ) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + copy( is, baos ); + return new String( baos.toByteArray() ); + } + + private void copy( InputStream is, OutputStream os ) throws IOException { + byte[] buf = new byte[ 1024 ]; + int r; + while ( ( r = is.read( buf ) ) >= 0 ) { + os.write( buf, 0, r ); + } + } } diff --git a/src/test/java/org/luaj/vm/StandardTest.java b/src/test/java/org/luaj/vm/StandardTest.java index 1f5d88a2..077ed42d 100644 --- a/src/test/java/org/luaj/vm/StandardTest.java +++ b/src/test/java/org/luaj/vm/StandardTest.java @@ -13,7 +13,7 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; -import org.luaj.debug.DebugLuaState; +import org.luaj.TestPlatform; import org.luaj.lib.BaseLib; public class StandardTest extends TestCase { @@ -65,10 +65,8 @@ public class StandardTest extends TestCase { } public void runTest() { - LuaState state = new DebugLuaState(); - - // add standard bindings - state.installStandardLibs(); + Platform.setInstance(new TestPlatform()); + LuaState state = Platform.newLuaState(); // hack: it's unpleasant when the test cases fail to terminate; // unfortunately, there is a test in the standard suite that