1. added a shutdown method for LuaState and DebugLuaState to clean up before shutting down

2. added the capability to set LUA_PATH
3. minor debugging improvement
4. bump up the version to 0.12 because the addition outputRedirect debug message is not compatible with the previous versions
This commit is contained in:
Shu Lei
2007-12-11 01:51:28 +00:00
parent ab8fc4883e
commit 42b94709f0
9 changed files with 94 additions and 28 deletions

View File

@@ -1,5 +1,5 @@
<project default="all"> <project default="all">
<property name="version" value="0.11"/> <property name="version" value="0.12"/>
<target name="clean"> <target name="clean">
<delete dir="build"/> <delete dir="build"/>

View File

@@ -167,6 +167,11 @@ public class LuaState extends Lua {
*/ */
public void init() {} public void init() {}
/**
* Perform any shutdown/clean up tasks if needed
*/
public void shutdown() {}
/** /**
* Install the standard set of libraries used by most implementations: * Install the standard set of libraries used by most implementations:
* BaseLib, CoroutineLib, MathLib, PackageLib, TableLib, StringLib * BaseLib, CoroutineLib, MathLib, PackageLib, TableLib, StringLib

View File

@@ -77,7 +77,11 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
*/ */
public DebugLuaState() {} public DebugLuaState() {}
public void init() { /*
* (non-Javadoc)
* @see org.luaj.vm.LuaState#init()
*/
public void init() {
Platform platform = Platform.getInstance(); Platform platform = Platform.getInstance();
// set if the vm should be suspended at start // set if the vm should be suspended at start
@@ -98,6 +102,14 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
} }
} }
/*
* (non-Javadoc)
* @see org.luaj.vm.LuaState#shutdown()
*/
public void shutdown() {
stop();
}
protected void setDebugSupport(DebugNetSupport debugSupport) protected void setDebugSupport(DebugNetSupport debugSupport)
throws IOException { throws IOException {
if (debugSupport == null) { if (debugSupport == null) {

View File

@@ -52,17 +52,16 @@ public class DebugMessageType extends EnumType {
public static final DebugMessageType suspendedOnWatchpoint = new DebugMessageType("suspendedOnWatchpoint", 20); public static final DebugMessageType suspendedOnWatchpoint = new DebugMessageType("suspendedOnWatchpoint", 20);
public static final DebugMessageType suspendedOnStepping = new DebugMessageType("suspendedOnStepping", 21); public static final DebugMessageType suspendedOnStepping = new DebugMessageType("suspendedOnStepping", 21);
public static final DebugMessageType suspendedOnError = new DebugMessageType("suspendedOnError", 22); public static final DebugMessageType suspendedOnError = new DebugMessageType("suspendedOnError", 22);
public static final DebugMessageType resumedOnSteppingEnd = new DebugMessageType("resumedOnSteppingEnd", 23); public static final DebugMessageType resumedOnSteppingEnd = new DebugMessageType("resumedOnSteppingEnd", 23);
public static final DebugMessageType resumedOnError = new DebugMessageType("resumedOnError", 24); public static final DebugMessageType error = new DebugMessageType("error", 24);
public static final DebugMessageType error = new DebugMessageType("error", 25); public static final DebugMessageType terminated = new DebugMessageType("terminated", 25);
public static final DebugMessageType terminated = new DebugMessageType("terminated", 26); public static final DebugMessageType clientRequestCallgraphReply = new DebugMessageType("clientRequestCallgraphReply", 26);
public static final DebugMessageType clientRequestCallgraphReply = new DebugMessageType("clientRequestCallgraphReply", 27); public static final DebugMessageType clientRequestStackReply = new DebugMessageType("clientRequestStackReply", 27);
public static final DebugMessageType clientRequestStackReply = new DebugMessageType("clientRequestStackReply", 28); public static final DebugMessageType clientRequestGlobalReply = new DebugMessageType("clientRequestGlobalReply", 28);
public static final DebugMessageType clientRequestGlobalReply = new DebugMessageType("clientRequestGlobalReply", 29); public static final DebugMessageType disconnected = new DebugMessageType("disconnected", 29);
public static final DebugMessageType disconnected = new DebugMessageType("disconnected", 30); public static final DebugMessageType sessionId = new DebugMessageType("sessionId", 30);
public static final DebugMessageType sessionId = new DebugMessageType("sessionId", 31); public static final DebugMessageType debugServiceDown = new DebugMessageType("debugServiceDown", 31);
public static final DebugMessageType debugServiceDown = new DebugMessageType("debugServiceDown", 32); public static final DebugMessageType outputRedirect = new DebugMessageType("outputRedirect", 32);
public static final DebugMessageType outputRedirect = new DebugMessageType("outputRedirect", 333);
protected static DebugMessageType[] ENUMS = new DebugMessageType[] { protected static DebugMessageType[] ENUMS = new DebugMessageType[] {
start, start,
@@ -89,7 +88,6 @@ public class DebugMessageType extends EnumType {
suspendedOnStepping, suspendedOnStepping,
suspendedOnError, suspendedOnError,
resumedOnSteppingEnd, resumedOnSteppingEnd,
resumedOnError,
error, error,
terminated, terminated,
clientRequestCallgraphReply, clientRequestCallgraphReply,

View File

@@ -1,5 +1,7 @@
package org.luaj.debug.j2se; package org.luaj.debug.j2se;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@@ -14,8 +16,13 @@ public class J2sePlatform extends Platform {
return new InputStreamReader(inputStream); return new InputStreamReader(inputStream);
} }
public InputStream openFile(String fileName) { public InputStream openFile(String filePath) {
return getClass().getResourceAsStream("/" + fileName); try {
FileInputStream fis = new FileInputStream(new File(filePath));
return fis;
} catch (IOException e) {
return null;
}
} }
/** /**

View File

@@ -28,6 +28,7 @@ import java.io.InputStream;
import org.luaj.compiler.LuaC; import org.luaj.compiler.LuaC;
import org.luaj.debug.DebugLuaState; import org.luaj.debug.DebugLuaState;
import org.luaj.lib.PackageLib;
import org.luaj.lib.j2se.LuajavaLib; import org.luaj.lib.j2se.LuajavaLib;
import org.luaj.vm.LClosure; import org.luaj.vm.LClosure;
import org.luaj.vm.LPrototype; import org.luaj.vm.LPrototype;
@@ -52,6 +53,7 @@ public class StandardLuaJVM {
protected boolean bSuspendOnStart = false; protected boolean bSuspendOnStart = false;
protected String script; protected String script;
protected String[] scriptArgs; protected String[] scriptArgs;
protected String luaPath;
protected LuaState state; protected LuaState state;
protected boolean isReady = false; protected boolean isReady = false;
protected boolean isTerminated = false; protected boolean isTerminated = false;
@@ -67,7 +69,7 @@ public class StandardLuaJVM {
protected void printUsage() { protected void printUsage() {
System.out.println("Usage:"); System.out.println("Usage:");
System.out.println("\t java StandardLuaJVM [-Dport=<port>[,suspendedOnStart=<true|false>]] <script> [<script arguments>]"); System.out.println("\t java StandardLuaJVM [-Dport=<port>[,suspendedOnStart=<true|false>]] [-L<lua path>] <script> [<script arguments>]");
System.out.println("where [] denotes an optional argument and <> denotes a placeholder."); System.out.println("where [] denotes an optional argument and <> denotes a placeholder.");
} }
@@ -76,7 +78,8 @@ public class StandardLuaJVM {
throw new ParseException("Invalid command line arguments."); throw new ParseException("Invalid command line arguments.");
} }
if (args[0] != null && args[0].startsWith("-D")) { int index = 0;
if (args[index] != null && args[index].startsWith("-D")) {
if (args.length < 2) { if (args.length < 2) {
throw new ParseException("Invalid command line arguments."); throw new ParseException("Invalid command line arguments.");
} }
@@ -84,7 +87,7 @@ public class StandardLuaJVM {
this.isDebugMode = true; this.isDebugMode = true;
System.setProperty(LuaState.PROPERTY_LUAJ_DEBUG, "true"); System.setProperty(LuaState.PROPERTY_LUAJ_DEBUG, "true");
String debugOptions = args[0]; String debugOptions = args[index];
debugOptions = debugOptions.substring(2); // remove '-D' debugOptions = debugOptions.substring(2); // remove '-D'
String[] options = debugOptions.split(","); String[] options = debugOptions.split(",");
for (int i = 0; options != null && i < options.length; i++) { for (int i = 0; options != null && i < options.length; i++) {
@@ -118,13 +121,24 @@ public class StandardLuaJVM {
throw new ParseException("Invalid command line: debug port is missing"); throw new ParseException("Invalid command line: debug port is missing");
} }
int tempArgsCount = args.length - 1; index++;
String[] tempArgs = new String[tempArgsCount]; }
System.arraycopy(args, 1, tempArgs, 0, tempArgsCount);
parseScriptArgs(tempArgs); if (args[index] != null && args[index].startsWith("-L")) {
} else { luaPath = args[index].substring(2); //remove -L fromt the arg
parseScriptArgs(args); index++;
} }
String[] scriptArgStrs;
if (index != 0) {
int scriptArgsCount = args.length - index;
scriptArgStrs = new String[scriptArgsCount];
System.arraycopy(args, index, scriptArgStrs, 0, scriptArgsCount);
} else {
scriptArgStrs = args;
}
parseScriptArgs(scriptArgStrs);
} }
private void parseScriptArgs(String[] args) private void parseScriptArgs(String[] args)
@@ -169,6 +183,10 @@ public class StandardLuaJVM {
return this.scriptArgs; return this.scriptArgs;
} }
String getLuaPath() {
return this.luaPath;
}
public void run() { public void run() {
try { try {
// new lua debug state // new lua debug state
@@ -215,6 +233,11 @@ public class StandardLuaJVM {
// add the compiler // add the compiler
LuaC.install(); LuaC.install();
// set the lua path if present
if (luaPath != null && luaPath.trim().length() > 0) {
PackageLib.setLuaPath(luaPath);
}
} }
/** /**

View File

@@ -46,7 +46,7 @@ public class DebugResponseCallgraph extends DebugMessage {
} }
public String toString() { public String toString() {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer("callgraph: ");
for (int i = 0; i < stackFrames.length; i++) { for (int i = 0; i < stackFrames.length; i++) {
StackFrame frame = stackFrames[i]; StackFrame frame = stackFrames[i];
buffer.append(frame.toString()); buffer.append(frame.toString());

View File

@@ -50,7 +50,7 @@ public class DebugResponseVariables extends DebugMessage {
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {
StringBuffer buffer = new StringBuffer(); StringBuffer buffer = new StringBuffer(getType().toString() + ":");
for (int i = 0; variables != null && i < variables.length; i++) { for (int i = 0; variables != null && i < variables.length; i++) {
buffer.append("\t" + variables[i].getName() + ":" + variables[i].getIndex() + "\n"); buffer.append("\t" + variables[i].getName() + ":" + variables[i].getIndex() + "\n");
} }

View File

@@ -216,7 +216,28 @@ public class LuaJVMTest extends TestCase {
assertEquals("dummy.lua", vm.getScript()); assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) { } catch (ParseException e) {
fail("Should never reach this line."); fail("Should never reach this line.");
} }
args = new String[] { "-LC:\\lua\\scripts", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals("C:\\lua\\scripts", vm.getLuaPath());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
args = new String[] { "-Dport=1044", "-LC:\\lua\\scripts", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals(1044, vm.getDebugPort());
assertEquals("C:\\lua\\scripts", vm.getLuaPath());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
} }
public void testRun() { public void testRun() {