Changes made for the following:
1) Platform.java stays as abstract class with, but default implementation is removed. Stays in org.luaj.vm package
2) org.luaj.platform package is created to hold concrete Platform implementations
3) Platform.newLuaState() method is introduced to instantiate the LuaState. Other constructors are privatized.
4) Following Platform implementations are created:
J2sePlatform
J2meMidp20Cldc11Platform
J2meMidp10Cldc10Platform
5) All clients of luaj-vm are changed to include startup code that looks something like this:
Platform.setInstance( new J2meMidp20Cldc11Platform() );
LuaState state = Platform.getInstance().newLuaState();
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src/core"/>
|
||||
<classpathentry kind="src" path="src/j2me"/>
|
||||
<classpathentry kind="src" path="src/j2se"/>
|
||||
<classpathentry kind="src" path="src/debug"/>
|
||||
<classpathentry kind="src" path="src/sample"/>
|
||||
|
||||
14
build.xml
14
build.xml
@@ -1,5 +1,5 @@
|
||||
<project default="all">
|
||||
<property name="version" value="0.14"/>
|
||||
<property name="version" value="0.15"/>
|
||||
|
||||
<target name="clean">
|
||||
<delete dir="build"/>
|
||||
@@ -9,7 +9,7 @@
|
||||
<delete file="luaj-vm-${version}.jar"/>
|
||||
<delete file="luaj-vm-core-${version}.jar"/>
|
||||
<delete file="luaj-vm-extras-j2se-${version}.jar"/>
|
||||
<delete file="luaj-vm-debug-j2me-${version}.jar"/>
|
||||
<delete file="luaj-vm-extras-j2me-${version}.jar"/>
|
||||
</target>
|
||||
|
||||
<target name="compile">
|
||||
@@ -17,6 +17,7 @@
|
||||
<javac destdir="build/all/classes" encoding="utf-8" source="1.3" target="1.1">
|
||||
<src path="src/core" />
|
||||
<src path="src/j2se" />
|
||||
<exclude name="org/luaj/platform/**"/>
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
@@ -34,7 +35,7 @@
|
||||
<echo>Skipping steps that require J2ME WTK.</echo>
|
||||
</target>
|
||||
|
||||
<target name="j2me-jars" depends="wtk-or-fail,jar-core,jar-j2se-extras,jar-j2me-debug"/>
|
||||
<target name="j2me-jars" depends="wtk-or-fail,jar-core,jar-j2se-extras,jar-j2me-extra"/>
|
||||
|
||||
<target name="jar-core" depends="compile-core">
|
||||
<jar destfile="luaj-vm-core-${version}.jar" basedir="build/core/classes"/>
|
||||
@@ -44,8 +45,8 @@
|
||||
<jar destfile="luaj-vm-extras-j2se-${version}.jar" basedir="build/extras-j2se/classes"/>
|
||||
</target>
|
||||
|
||||
<target name="jar-j2me-debug" depends="compile-j2me-debug">
|
||||
<jar destfile="luaj-vm-debug-j2me-${version}.jar" basedir="build/extras-j2me/classes"/>
|
||||
<target name="jar-j2me-extra" depends="compile-j2me-extra">
|
||||
<jar destfile="luaj-vm-extras-j2me-${version}.jar" basedir="build/extras-j2me/classes"/>
|
||||
</target>
|
||||
|
||||
<target name="compile-core" depends="wtk-or-fail">
|
||||
@@ -73,10 +74,11 @@
|
||||
</javac>
|
||||
</target>
|
||||
|
||||
<target name="compile-j2me-debug" depends="wtk-or-fail">
|
||||
<target name="compile-j2me-extra" depends="wtk-or-fail">
|
||||
<mkdir dir="build/extras-j2me/classes"/>
|
||||
<javac destdir="build/extras-j2me/classes" encoding="utf-8" source="1.3" target="1.1"
|
||||
bootclasspathref="wtk-libs" classpath="build/core/classes">
|
||||
<src path="src/j2me" />
|
||||
<src path="src/debug" />
|
||||
<exclude name="org/luaj/debug/j2se/**"/>
|
||||
<exclude name="org/luaj/debug/net/j2se/**"/>
|
||||
|
||||
@@ -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, <code>ud</code>, 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.
|
||||
|
||||
@@ -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.
|
||||
* <p>
|
||||
* Here is the sample code to set up the platform instance and create a new
|
||||
* LuaState instance.
|
||||
* <pre>
|
||||
* Platform.setInstance(new J2meMidp20Cldc11Platform());
|
||||
* LuaState vm = Platform.newLuaState();
|
||||
* </pre>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
51
src/j2me/org/luaj/platform/J2meMidp10Cldc10Platform.java
Normal file
51
src/j2me/org/luaj/platform/J2meMidp10Cldc10Platform.java
Normal file
@@ -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.");
|
||||
}
|
||||
}
|
||||
9
src/j2me/org/luaj/platform/J2meMidp20Cldc11Platform.java
Normal file
9
src/j2me/org/luaj/platform/J2meMidp20Cldc11Platform.java
Normal file
@@ -0,0 +1,9 @@
|
||||
package org.luaj.platform;
|
||||
|
||||
import javax.microedition.midlet.MIDlet;
|
||||
|
||||
public class J2meMidp20Cldc11Platform extends J2meMidp10Cldc10Platform {
|
||||
public J2meMidp20Cldc11Platform(MIDlet midlet) {
|
||||
super(midlet);
|
||||
}
|
||||
}
|
||||
51
src/j2se/org/luaj/platform/J2sePlatform.java
Normal file
51
src/j2se/org/luaj/platform/J2sePlatform.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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; i<args.length; i++ ) {
|
||||
String script = args[i];
|
||||
try {
|
||||
System.out.println("loading '"+script+"'");
|
||||
|
||||
// add standard bindings
|
||||
state.installStandardLibs();
|
||||
LuaC.install();
|
||||
|
||||
|
||||
// load the file
|
||||
InputStream is = null;
|
||||
File f = new File(script);
|
||||
|
||||
@@ -24,18 +24,14 @@ package org.luaj.sample;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.luaj.lib.CoroutineLib;
|
||||
import org.luaj.lib.MathLib;
|
||||
import org.luaj.lib.PackageLib;
|
||||
import org.luaj.lib.StringLib;
|
||||
import org.luaj.lib.TableLib;
|
||||
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.LTable;
|
||||
import org.luaj.vm.LValue;
|
||||
import org.luaj.vm.LoadState;
|
||||
import org.luaj.vm.LuaState;
|
||||
import org.luaj.vm.Platform;
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,14 +44,10 @@ public class LuajavaRunner {
|
||||
|
||||
public static void main( String[] args ) throws IOException {
|
||||
|
||||
Platform.setInstance(new J2sePlatform());
|
||||
|
||||
// new lua state
|
||||
LuaState state = new LuaState();
|
||||
|
||||
// add standard bindings
|
||||
state.installStandardLibs();
|
||||
|
||||
// add LuaJava bindings
|
||||
LuajavaLib.install(state._G);
|
||||
LuaState state = Platform.newLuaState();
|
||||
|
||||
// get script name
|
||||
String script = (args.length>0? args[0]: "/swingapp.luac");
|
||||
|
||||
11
src/test/java/org/luaj/TestPlatform.java
Normal file
11
src/test/java/org/luaj/TestPlatform.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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"); }
|
||||
|
||||
@@ -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] );
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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 );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user