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

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

View File

@@ -66,19 +66,19 @@ public class DebugStackState extends StackState implements DebugRequestListener
public void addDebugEventListener(DebugEventListener listener) {
if (!debugEventListeners.contains(listener)) {
debugEventListeners.add(listener);
debugEventListeners.addElement(listener);
}
}
public void removeDebugEventListener(DebugEventListener listener) {
if (debugEventListeners.contains(listener)) {
debugEventListeners.remove(listener);
debugEventListeners.removeElement(listener);
}
}
protected void notifyDebugEventListeners(DebugEvent event) {
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);
}
}
@@ -336,7 +336,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
DebugUtils.print("\tType: " + Lua.TYPE_NAMES[type]);
if (type == Lua.LUA_TTABLE) {
DebugUtils.println(" (selected)");
variables.add(
variables.addElement(
new TableVariable(localVariableCount++,
varName,
type,
@@ -344,7 +344,7 @@ public class DebugStackState extends StackState implements DebugRequestListener
} else if (type != Lua.LUA_TFUNCTION &&
type != LUA_TTHREAD) {
DebugUtils.println(" (selected)");
variables.add(
variables.addElement(
new Variable(localVariableCount++,
varName,
type,
@@ -359,7 +359,12 @@ public class DebugStackState extends StackState implements DebugRequestListener
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;
import java.io.File;
import lua.io.LoadState;
import lua.value.LString;
public class DebugUtils {
public static final boolean IS_DEBUG = true;
public static final boolean IS_DEBUG = false;
public static void println(String message) {
if (IS_DEBUG) {
@@ -42,13 +40,14 @@ public class DebugUtils {
}
public static String getSourceFileName(LString source) {
String sourceStr = source.toJavaString();
sourceStr = LoadState.getSourceName(sourceStr);
String sourceStr = LoadState.getSourceName(source.toJavaString());
if (!LoadState.SOURCE_BINARY_STRING.equals(sourceStr)) {
File sourceFile = new File(sourceStr);
return sourceFile.getName();
} else {
sourceStr = sourceStr.replace('\\', '/');
int index = sourceStr.lastIndexOf('/');
if (index != -1) {
sourceStr = sourceStr.substring(index + 1);
}
}
return sourceStr;
}
}
}

View File

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

View File

@@ -39,8 +39,8 @@ public class TableVariable extends Variable {
int size = table.size();
DebugUtils.println("table size:" + size);
Vector keyArray = new Vector();
Vector valueArray = new Vector();
Vector keyList = new Vector();
Vector valueList = new Vector();
LValue[] keyValues = table.getKeys();
for (int i = 0; i < size; i++) {
@@ -49,18 +49,26 @@ public class TableVariable extends Variable {
continue;
}
keyArray.add(keyValues[i].toString());
keyList.addElement(keyValues[i].toString());
if (value instanceof LTable) {
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 {
valueArray.add(value.toString());
valueList.addElement(value.toString());
}
DebugUtils.println("["+ keyValues[i].toString() + "," + value.toString() + "]");
}
this.keys = (String[])keyArray.toArray(new String[0]);
this.values = (Object[]) valueArray.toArray(new Object[0]);
this.keys = new String[keyList.size()];
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) {
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
* THE SOFTWARE.
******************************************************************************/
package lua.debug;
package lua.debug.j2se;
import java.io.DataInputStream;
import java.io.DataOutputStream;
@@ -28,6 +28,8 @@ import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import lua.debug.DebugUtils;
import lua.debug.SerializationHelper;
import lua.debug.event.DebugEvent;
import lua.debug.event.DebugEventListener;
import lua.debug.request.DebugRequest;
@@ -35,35 +37,15 @@ import lua.debug.request.DebugRequestListener;
import lua.debug.response.DebugResponse;
public class DebugSupport implements DebugEventListener {
public static class State extends EnumType {
public static final State UNKNOWN = new State("UNKNOWN", 0);
public static final State RUNNING = new State("RUNNING", 1);
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 static final int UNKNOWN = 0;
protected static final int RUNNING = 1;
protected static final int STOPPED = 2;
protected DebugRequestListener listener;
protected int requestPort;
protected int eventPort;
protected Thread requestWatcherThread;
protected State state = State.UNKNOWN;
protected int state = UNKNOWN;
protected ServerSocket requestSocket;
protected Socket clientRequestSocket;
@@ -146,7 +128,7 @@ public class DebugSupport implements DebugEventListener {
this.requestWatcherThread = new Thread(new Runnable() {
public void run() {
if (getState() != State.STOPPED) {
if (getState() != STOPPED) {
handleRequest();
} else {
releaseServer();
@@ -154,21 +136,21 @@ public class DebugSupport implements DebugEventListener {
}
});
this.requestWatcherThread.start();
this.state = State.RUNNING;
this.state = RUNNING;
}
public synchronized State getState() {
public synchronized int getState() {
return this.state;
}
public synchronized void stop() {
this.state = State.STOPPED;
this.state = STOPPED;
}
public void handleRequest() {
synchronized (clientRequestSocket) {
try {
while (getState() != State.STOPPED) {
while (getState() != STOPPED) {
int size = requestReader.readInt();
byte[] data = new byte[size];
requestReader.readFully(data);
@@ -184,7 +166,7 @@ public class DebugSupport implements DebugEventListener {
DebugUtils.println("SERVER sends response: " + response);
}
if (getState() == State.STOPPED) {
if (getState() == STOPPED) {
cleanup();
}
} catch (EOFException e) {

View File

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

View File

@@ -43,7 +43,7 @@ public class DebugResponseCallgraph implements DebugResponse {
}
public String toString() {
StringBuilder buffer = new StringBuilder();
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < stackFrames.length; i++) {
StackFrame frame = stackFrames[i];
buffer.append(frame.toString());

View File

@@ -47,7 +47,7 @@ public class DebugResponseStack implements DebugResponse {
* @see java.lang.Object#toString()
*/
public String toString() {
StringBuilder buffer = new StringBuilder();
StringBuffer buffer = new StringBuffer();
for (int i = 0; variables != null && i < variables.length; i++) {
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;
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() {
try {
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
* THE SOFTWARE.
******************************************************************************/
package lua.debug;
package lua.debug.j2se;
import java.io.IOException;
import java.net.URL;
import junit.framework.TestCase;
import lua.debug.StandardLuaJVM.ParseException;
import lua.debug.j2se.StandardLuaJVM;
import lua.debug.j2se.StandardLuaJVM.ParseException;
/**
* Sanity test for StandardLuaJVM.