1. some refacotring

2. fixed a few files so that they will compile for j2me
This commit is contained in:
Shu Lei
2007-10-12 20:39:26 +00:00
parent 3138fdee65
commit 38dafdb190
11 changed files with 77 additions and 94 deletions

View File

@@ -1,10 +1,15 @@
<project default="all"> <project default="all">
<property name="version" value="1.0"/> <property name="version" value="1.0"/>
<target name="clean"> <target name="clean">
<delete dir="build"/> <delete dir="build"/>
</target> </target>
<target name="real-clean" depends="clean">
<delete file="luaj-vm-${version}.jar"/>
<delete file="luaj-vm-j2me-${version}.jar"/>
</target>
<target name="compile"> <target name="compile">
<mkdir dir="build/classes"/> <mkdir dir="build/classes"/>
<javac destdir="build/classes" encoding="utf-8" source="1.3" target="1.1"> <javac destdir="build/classes" encoding="utf-8" source="1.3" target="1.1">
@@ -36,7 +41,7 @@
<mkdir dir="build/src-j2me" /> <mkdir dir="build/src-j2me" />
<copy todir="build/src-j2me"> <copy todir="build/src-j2me">
<fileset dir="../luaj-vm/src/main/java"> <fileset dir="../luaj-vm/src/main/java">
<exclude name="lua/debug/**" /> <exclude name="lua/debug/j2se/**" />
</fileset> </fileset>
<fileset dir="../luaj-vm/src/addon/java"> <fileset dir="../luaj-vm/src/addon/java">
<exclude name="lua/addon/luajava/**" /> <exclude name="lua/addon/luajava/**" />

View File

@@ -66,19 +66,19 @@ public class DebugStackState extends StackState implements DebugRequestListener
public void addDebugEventListener(DebugEventListener listener) { public void addDebugEventListener(DebugEventListener listener) {
if (!debugEventListeners.contains(listener)) { if (!debugEventListeners.contains(listener)) {
debugEventListeners.add(listener); debugEventListeners.addElement(listener);
} }
} }
public void removeDebugEventListener(DebugEventListener listener) { public void removeDebugEventListener(DebugEventListener listener) {
if (debugEventListeners.contains(listener)) { if (debugEventListeners.contains(listener)) {
debugEventListeners.remove(listener); debugEventListeners.removeElement(listener);
} }
} }
protected void notifyDebugEventListeners(DebugEvent event) { protected void notifyDebugEventListeners(DebugEvent event) {
for (int i = 0; debugEventListeners != null && i < debugEventListeners.size(); i++) { for (int i = 0; debugEventListeners != null && i < debugEventListeners.size(); i++) {
DebugEventListener listener = (DebugEventListener)debugEventListeners.get(i); DebugEventListener listener = (DebugEventListener)debugEventListeners.elementAt(i);
listener.notifyDebugEvent(event); listener.notifyDebugEvent(event);
} }
} }
@@ -336,7 +336,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
DebugUtils.print("\tType: " + Lua.TYPE_NAMES[type]); DebugUtils.print("\tType: " + Lua.TYPE_NAMES[type]);
if (type == Lua.LUA_TTABLE) { if (type == Lua.LUA_TTABLE) {
DebugUtils.println(" (selected)"); DebugUtils.println(" (selected)");
variables.add( variables.addElement(
new TableVariable(localVariableCount++, new TableVariable(localVariableCount++,
varName, varName,
type, type,
@@ -344,7 +344,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
} else if (type != Lua.LUA_TFUNCTION && } else if (type != Lua.LUA_TFUNCTION &&
type != LUA_TTHREAD) { type != LUA_TTHREAD) {
DebugUtils.println(" (selected)"); DebugUtils.println(" (selected)");
variables.add( variables.addElement(
new Variable(localVariableCount++, new Variable(localVariableCount++,
varName, varName,
type, type,
@@ -358,8 +358,13 @@ public class DebugStackState extends StackState implements DebugRequestListener
} else { } else {
DebugUtils.println(""); DebugUtils.println("");
} }
} }
return (Variable[])variables.toArray(new Variable[0]);
Variable[] result = new Variable[variables.size()];
for (int i = 0; i < variables.size(); i++) {
result[i] = (Variable) variables.elementAt(i);
}
return result;
} }

View File

@@ -21,13 +21,11 @@
******************************************************************************/ ******************************************************************************/
package lua.debug; package lua.debug;
import java.io.File;
import lua.io.LoadState; import lua.io.LoadState;
import lua.value.LString; import lua.value.LString;
public class DebugUtils { public class DebugUtils {
public static final boolean IS_DEBUG = true; public static final boolean IS_DEBUG = false;
public static void println(String message) { public static void println(String message) {
if (IS_DEBUG) { if (IS_DEBUG) {
@@ -41,14 +39,15 @@ public class DebugUtils {
} }
} }
public static String getSourceFileName(LString source) { public static String getSourceFileName(LString source) {
String sourceStr = source.toJavaString(); String sourceStr = LoadState.getSourceName(source.toJavaString());
sourceStr = LoadState.getSourceName(sourceStr); if (!LoadState.SOURCE_BINARY_STRING.equals(sourceStr)) {
if (!LoadState.SOURCE_BINARY_STRING.equals(sourceStr)) { sourceStr = sourceStr.replace('\\', '/');
File sourceFile = new File(sourceStr); int index = sourceStr.lastIndexOf('/');
return sourceFile.getName(); if (index != -1) {
} else { sourceStr = sourceStr.substring(index + 1);
return sourceStr; }
} }
} return sourceStr;
}
} }

View File

@@ -52,15 +52,14 @@ public class SerializationHelper {
static final int SERIAL_TYPE_DebugResponseCallgraph = 3; static final int SERIAL_TYPE_DebugResponseCallgraph = 3;
static final int SERIAL_TYPE_DebugResponseStack = 4; static final int SERIAL_TYPE_DebugResponseStack = 4;
static final int SERIAL_TYPE_DebugResponseSimple = 5; static final int SERIAL_TYPE_DebugResponseSimple = 5;
static final int SERIAL_TYPE_DebugSupportState = 6; static final int SERIAL_TYPE_StackFrame = 6;
static final int SERIAL_TYPE_StackFrame = 7; static final int SERIAL_TYPE_DebugRequestType = 7;
static final int SERIAL_TYPE_DebugRequestType = 8; static final int SERIAL_TYPE_DebugRequest = 8;
static final int SERIAL_TYPE_DebugRequest = 9; static final int SERIAL_TYPE_DebugRequestStack = 9;
static final int SERIAL_TYPE_DebugRequestStack = 10; static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10;
static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 11; static final int SERIAL_TYPE_DebugEventType = 11;
static final int SERIAL_TYPE_DebugEventType = 12; static final int SERIAL_TYPE_DebugEvent = 12;
static final int SERIAL_TYPE_DebugEvent = 13; static final int SERIAL_TYPE_DebugEventBreakpoint = 13;
static final int SERIAL_TYPE_DebugEventBreakpoint = 14;
public static void serialize(Serializable object, DataOutputStream dout) public static void serialize(Serializable object, DataOutputStream dout)
throws IOException { throws IOException {
@@ -85,9 +84,6 @@ public class SerializationHelper {
} else if (object instanceof DebugResponseCallgraph) { } else if (object instanceof DebugResponseCallgraph) {
dout.writeInt(SERIAL_TYPE_DebugResponseCallgraph); dout.writeInt(SERIAL_TYPE_DebugResponseCallgraph);
DebugResponseCallgraph.serialize(dout, (DebugResponseCallgraph)object); DebugResponseCallgraph.serialize(dout, (DebugResponseCallgraph)object);
} else if (object instanceof DebugSupport.State) {
dout.writeInt(SERIAL_TYPE_DebugSupportState);
DebugSupport.State.serialize(dout, (DebugSupport.State)object);
} else if (object instanceof DebugRequestType) { } else if (object instanceof DebugRequestType) {
dout.writeInt(SERIAL_TYPE_DebugRequestType); dout.writeInt(SERIAL_TYPE_DebugRequestType);
DebugRequestType.serialize(dout, (DebugRequestType)object); DebugRequestType.serialize(dout, (DebugRequestType)object);
@@ -141,9 +137,6 @@ public class SerializationHelper {
case SERIAL_TYPE_DebugResponseStack: case SERIAL_TYPE_DebugResponseStack:
object = DebugResponseStack.deserialize(din); object = DebugResponseStack.deserialize(din);
break; break;
case SERIAL_TYPE_DebugSupportState:
object = DebugSupport.State.deserialize(din);
break;
case SERIAL_TYPE_DebugRequestType: case SERIAL_TYPE_DebugRequestType:
object = DebugRequestType.deserialize(din); object = DebugRequestType.deserialize(din);
break; break;

View File

@@ -39,8 +39,8 @@ public class TableVariable extends Variable {
int size = table.size(); int size = table.size();
DebugUtils.println("table size:" + size); DebugUtils.println("table size:" + size);
Vector keyArray = new Vector(); Vector keyList = new Vector();
Vector valueArray = new Vector(); Vector valueList = new Vector();
LValue[] keyValues = table.getKeys(); LValue[] keyValues = table.getKeys();
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@@ -49,18 +49,26 @@ public class TableVariable extends Variable {
continue; continue;
} }
keyArray.add(keyValues[i].toString()); keyList.addElement(keyValues[i].toString());
if (value instanceof LTable) { if (value instanceof LTable) {
DebugUtils.println("table: value[" + i + "]=" + value.toString()); DebugUtils.println("table: value[" + i + "]=" + value.toString());
valueArray.add(new TableVariable(i, "element[" + keyValues[i].toString() + "]", Lua.LUA_TTABLE, (LTable)value)); valueList.addElement(new TableVariable(i, "element[" + keyValues[i].toString() + "]", Lua.LUA_TTABLE, (LTable)value));
} else { } else {
valueArray.add(value.toString()); valueList.addElement(value.toString());
} }
DebugUtils.println("["+ keyValues[i].toString() + "," + value.toString() + "]"); DebugUtils.println("["+ keyValues[i].toString() + "," + value.toString() + "]");
} }
this.keys = (String[])keyArray.toArray(new String[0]); this.keys = new String[keyList.size()];
this.values = (Object[]) valueArray.toArray(new Object[0]); for (int i = 0; i < keyList.size(); i++) {
this.keys[i] = (String)keyList.elementAt(i);
}
this.values = new Object[valueList.size()];
for (int i = 0; i < valueList.size(); i++) {
this.values[i] = valueList.elementAt(i);
}
if (this.keys.length != this.values.length) { if (this.keys.length != this.values.length) {
throw new RuntimeException("Internal Error: key.length must equal to values.length"); throw new RuntimeException("Internal Error: key.length must equal to values.length");
} }

View File

@@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
package lua.debug; package lua.debug.j2se;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
@@ -28,6 +28,8 @@ import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import lua.debug.DebugUtils;
import lua.debug.SerializationHelper;
import lua.debug.event.DebugEvent; import lua.debug.event.DebugEvent;
import lua.debug.event.DebugEventListener; import lua.debug.event.DebugEventListener;
import lua.debug.request.DebugRequest; import lua.debug.request.DebugRequest;
@@ -35,35 +37,15 @@ import lua.debug.request.DebugRequestListener;
import lua.debug.response.DebugResponse; import lua.debug.response.DebugResponse;
public class DebugSupport implements DebugEventListener { public class DebugSupport implements DebugEventListener {
public static class State extends EnumType { protected static final int UNKNOWN = 0;
public static final State UNKNOWN = new State("UNKNOWN", 0); protected static final int RUNNING = 1;
public static final State RUNNING = new State("RUNNING", 1); protected static final int STOPPED = 2;
public static final State STOPPED = new State("STOPPED", 2);
protected static final State[] ENUMS = new State[] {
UNKNOWN,
RUNNING,
STOPPED
};
public State(String name, int ordinal) {
super(name, ordinal);
}
public static State deserialize(DataInputStream in) throws IOException {
int ordinal = in.readInt();
if (ordinal < 0 || ordinal >= ENUMS.length) {
throw new RuntimeException("ordinal is out of the range.");
}
return ENUMS[ordinal];
}
}
protected DebugRequestListener listener; protected DebugRequestListener listener;
protected int requestPort; protected int requestPort;
protected int eventPort; protected int eventPort;
protected Thread requestWatcherThread; protected Thread requestWatcherThread;
protected State state = State.UNKNOWN; protected int state = UNKNOWN;
protected ServerSocket requestSocket; protected ServerSocket requestSocket;
protected Socket clientRequestSocket; protected Socket clientRequestSocket;
@@ -146,7 +128,7 @@ public class DebugSupport implements DebugEventListener {
this.requestWatcherThread = new Thread(new Runnable() { this.requestWatcherThread = new Thread(new Runnable() {
public void run() { public void run() {
if (getState() != State.STOPPED) { if (getState() != STOPPED) {
handleRequest(); handleRequest();
} else { } else {
releaseServer(); releaseServer();
@@ -154,21 +136,21 @@ public class DebugSupport implements DebugEventListener {
} }
}); });
this.requestWatcherThread.start(); this.requestWatcherThread.start();
this.state = State.RUNNING; this.state = RUNNING;
} }
public synchronized State getState() { public synchronized int getState() {
return this.state; return this.state;
} }
public synchronized void stop() { public synchronized void stop() {
this.state = State.STOPPED; this.state = STOPPED;
} }
public void handleRequest() { public void handleRequest() {
synchronized (clientRequestSocket) { synchronized (clientRequestSocket) {
try { try {
while (getState() != State.STOPPED) { while (getState() != STOPPED) {
int size = requestReader.readInt(); int size = requestReader.readInt();
byte[] data = new byte[size]; byte[] data = new byte[size];
requestReader.readFully(data); requestReader.readFully(data);
@@ -184,7 +166,7 @@ public class DebugSupport implements DebugEventListener {
DebugUtils.println("SERVER sends response: " + response); DebugUtils.println("SERVER sends response: " + response);
} }
if (getState() == State.STOPPED) { if (getState() == STOPPED) {
cleanup(); cleanup();
} }
} catch (EOFException e) { } catch (EOFException e) {

View File

@@ -19,7 +19,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
package lua.debug; package lua.debug.j2se;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -34,6 +34,8 @@ import lua.GlobalState;
import lua.StackState; import lua.StackState;
import lua.addon.luacompat.LuaCompat; import lua.addon.luacompat.LuaCompat;
import lua.addon.luajava.LuaJava; import lua.addon.luajava.LuaJava;
import lua.debug.DebugStackState;
import lua.debug.DebugUtils;
import lua.debug.event.DebugEvent; import lua.debug.event.DebugEvent;
import lua.debug.event.DebugEventType; import lua.debug.event.DebugEventType;
import lua.debug.request.DebugRequest; import lua.debug.request.DebugRequest;
@@ -118,7 +120,7 @@ public class StandardLuaJVM implements DebugRequestListener {
} }
private void parseScriptArgs(String[] args) private void parseScriptArgs(String[] args)
throws lua.debug.StandardLuaJVM.ParseException { throws lua.debug.j2se.StandardLuaJVM.ParseException {
if (args == null || args.length < 1) { if (args == null || args.length < 1) {
throw new ParseException("script is missing."); throw new ParseException("script is missing.");
} }

View File

@@ -43,7 +43,7 @@ public class DebugResponseCallgraph implements DebugResponse {
} }
public String toString() { public String toString() {
StringBuilder buffer = new StringBuilder(); StringBuffer buffer = new StringBuffer();
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

@@ -47,7 +47,7 @@ public class DebugResponseStack implements DebugResponse {
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {
StringBuilder buffer = new StringBuilder(); StringBuffer buffer = new StringBuffer();
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

@@ -5,18 +5,6 @@ import lua.debug.event.DebugEventType;
import lua.debug.request.DebugRequestType; import lua.debug.request.DebugRequestType;
public class EnumTypeTest extends TestCase { public class EnumTypeTest extends TestCase {
public void testDebugSupportStateSerialization() {
try {
DebugSupport.State stateIn = DebugSupport.State.RUNNING;
byte[] data = SerializationHelper.serialize(stateIn);
DebugSupport.State stateOut
= (DebugSupport.State) SerializationHelper.deserialize(data);
assertEquals(stateIn, stateOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
public void testDebugRequestTypeSerialization() { public void testDebugRequestTypeSerialization() {
try { try {
DebugRequestType type = DebugRequestType.lineBreakpointClear; DebugRequestType type = DebugRequestType.lineBreakpointClear;

View File

@@ -19,13 +19,14 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
******************************************************************************/ ******************************************************************************/
package lua.debug; package lua.debug.j2se;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import junit.framework.TestCase; import junit.framework.TestCase;
import lua.debug.StandardLuaJVM.ParseException; import lua.debug.j2se.StandardLuaJVM;
import lua.debug.j2se.StandardLuaJVM.ParseException;
/** /**
* Sanity test for StandardLuaJVM. * Sanity test for StandardLuaJVM.