1. updated DebugSupport to use one port for two-way communication

2. added a command line debug option to pause the VM on start
3. minor code clean up - replaced tabs with 4 white spaces
This commit is contained in:
Shu Lei
2007-11-12 22:08:26 +00:00
parent a68fcb6743
commit 7ddfb80116
17 changed files with 696 additions and 738 deletions

View File

@@ -37,9 +37,8 @@ import org.luaj.debug.request.DebugRequestLineBreakpointToggle;
import org.luaj.debug.request.DebugRequestListener; import org.luaj.debug.request.DebugRequestListener;
import org.luaj.debug.request.DebugRequestStack; import org.luaj.debug.request.DebugRequestStack;
import org.luaj.debug.request.DebugRequestType; import org.luaj.debug.request.DebugRequestType;
import org.luaj.debug.response.DebugResponse;
import org.luaj.debug.response.DebugResponseCallgraph; import org.luaj.debug.response.DebugResponseCallgraph;
import org.luaj.debug.response.DebugResponseSimple; import org.luaj.debug.response.DebugResponseStack;
import org.luaj.debug.response.DebugResponseVariables; import org.luaj.debug.response.DebugResponseVariables;
import org.luaj.vm.CallInfo; import org.luaj.vm.CallInfo;
import org.luaj.vm.LClosure; import org.luaj.vm.LClosure;
@@ -192,7 +191,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
if (DebugUtils.IS_DEBUG) if (DebugUtils.IS_DEBUG)
DebugUtils.println("debugHook - executing line: " + line); DebugUtils.println("debugHook - executing line: " + line);
int i = currentProto.code[pc]; int i = currentProto.code[pc];
int opCode = LuaState.GET_OPCODE(i); int opCode = LuaState.GET_OPCODE(i);
if (isStepping() && opCode == LuaState.OP_RETURN && cc == 0) { if (isStepping() && opCode == LuaState.OP_RETURN && cc == 0) {
@@ -298,7 +297,7 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
// ------------------ commands coming from the debugger ------------------- // ------------------ commands coming from the debugger -------------------
public DebugResponse handleRequest(DebugRequest request) { public void handleRequest(DebugRequest request) {
if (this.debugSupport == null) { if (this.debugSupport == null) {
throw new IllegalStateException( throw new IllegalStateException(
"DebugStackState is not equiped with DebugSupport."); "DebugStackState is not equiped with DebugSupport.");
@@ -313,60 +312,59 @@ public class DebugLuaState extends LuaState implements DebugRequestListener {
DebugEvent event = new DebugEvent(DebugEventType.started); DebugEvent event = new DebugEvent(DebugEventType.started);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
cancelSuspendOnStart(); cancelSuspendOnStart();
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.exit == requestType) { } else if (DebugRequestType.exit == requestType) {
stop(); stop();
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.suspend == requestType) { } else if (DebugRequestType.suspend == requestType) {
suspend(); suspend();
DebugEvent event = new DebugEvent(DebugEventType.suspendedByClient); DebugEvent event = new DebugEvent(DebugEventType.suspendedByClient);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.resume == requestType) { } else if (DebugRequestType.resume == requestType) {
resume(); resume();
DebugEvent event = new DebugEvent(DebugEventType.resumedByClient); DebugEvent event = new DebugEvent(DebugEventType.resumedByClient);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.lineBreakpointSet == requestType) { } else if (DebugRequestType.lineBreakpointSet == requestType) {
DebugRequestLineBreakpointToggle setBreakpointRequest = (DebugRequestLineBreakpointToggle) request; DebugRequestLineBreakpointToggle setBreakpointRequest
= (DebugRequestLineBreakpointToggle) request;
setBreakpoint(setBreakpointRequest.getSource(), setBreakpoint(setBreakpointRequest.getSource(),
setBreakpointRequest.getLineNumber()); setBreakpointRequest.getLineNumber());
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.lineBreakpointClear == requestType) { } else if (DebugRequestType.lineBreakpointClear == requestType) {
DebugRequestLineBreakpointToggle clearBreakpointRequest = (DebugRequestLineBreakpointToggle) request; DebugRequestLineBreakpointToggle clearBreakpointRequest
= (DebugRequestLineBreakpointToggle) request;
clearBreakpoint(clearBreakpointRequest.getSource(), clearBreakpoint(clearBreakpointRequest.getSource(),
clearBreakpointRequest.getLineNumber()); clearBreakpointRequest.getLineNumber());
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.callgraph == requestType) { } else if (DebugRequestType.callgraph == requestType) {
return new DebugResponseCallgraph(getCallgraph()); DebugResponseCallgraph callgraph
= new DebugResponseCallgraph(getCallgraph());
debugSupport.notifyDebugEvent(callgraph);
} else if (DebugRequestType.stack == requestType) { } else if (DebugRequestType.stack == requestType) {
DebugRequestStack stackRequest = (DebugRequestStack) request; DebugRequestStack stackRequest = (DebugRequestStack) request;
int index = stackRequest.getIndex(); int index = stackRequest.getIndex();
return new DebugResponseVariables(getStack(index)); DebugResponseStack stackState
= new DebugResponseStack(index, getStack(index));
debugSupport.notifyDebugEvent(stackState);
} else if (DebugRequestType.global == requestType) { } else if (DebugRequestType.global == requestType) {
return new DebugResponseVariables(getGlobals()); DebugResponseVariables globals
= new DebugResponseVariables(getGlobals(), DebugEventType.clientRequestGlobalReply);
debugSupport.notifyDebugEvent(globals);
} else if (DebugRequestType.stepInto == requestType) { } else if (DebugRequestType.stepInto == requestType) {
DebugEvent event = new DebugEvent( DebugEvent event = new DebugEvent(
DebugEventType.resumedOnSteppingInto); DebugEventType.resumedOnSteppingInto);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
stepInto(); stepInto();
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.stepOver == requestType) { } else if (DebugRequestType.stepOver == requestType) {
DebugEvent event = new DebugEvent( DebugEvent event = new DebugEvent(
DebugEventType.resumedOnSteppingOver); DebugEventType.resumedOnSteppingOver);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
stepOver(); stepOver();
return DebugResponseSimple.SUCCESS;
} else if (DebugRequestType.stepReturn == requestType) { } else if (DebugRequestType.stepReturn == requestType) {
DebugEvent event = new DebugEvent( DebugEvent event = new DebugEvent(
DebugEventType.resumedOnSteppingReturn); DebugEventType.resumedOnSteppingReturn);
debugSupport.notifyDebugEvent(event); debugSupport.notifyDebugEvent(event);
stepReturn(); stepReturn();
return DebugResponseSimple.SUCCESS; } else {
throw new java.lang.IllegalArgumentException("unkown request type: "
+ request.getType());
} }
throw new java.lang.IllegalArgumentException("unkown request type: "
+ request.getType());
} }
/** /**

View File

@@ -9,37 +9,28 @@ import org.luaj.debug.event.DebugEvent;
import org.luaj.debug.event.DebugEventListener; import org.luaj.debug.event.DebugEventListener;
import org.luaj.debug.request.DebugRequest; import org.luaj.debug.request.DebugRequest;
import org.luaj.debug.request.DebugRequestListener; import org.luaj.debug.request.DebugRequestListener;
import org.luaj.debug.response.DebugResponse;
/** /**
* DebugSupport provides the network communication support for the debugger and * DebugSupport provides the network communication support for the debugger and
* debugee. * debugee.
*/ */
public class DebugSupport implements DebugRequestListener, DebugEventListener { public abstract class DebugSupport implements DebugRequestListener, DebugEventListener {
protected static final int UNKNOWN = 0; protected static final int UNKNOWN = 0;
protected static final int RUNNING = 1; protected static final int RUNNING = 1;
protected static final int STOPPED = 2; protected static final int STOPPED = 2;
protected DebugLuaState vm; protected DebugLuaState vm;
protected int requestPort; protected int debugPort;
protected int eventPort;
protected Thread requestWatcherThread; protected Thread requestWatcherThread;
protected int state = UNKNOWN; protected int state = UNKNOWN;
protected DataInputStream requestReader; protected DataInputStream requestReader;
protected DataOutputStream requestWriter;
protected DataOutputStream eventWriter; protected DataOutputStream eventWriter;
public DebugSupport(int requestPort, int eventPort) { public DebugSupport(int debugPort) {
if (requestPort == -1) { if (debugPort == -1) {
throw new IllegalArgumentException("requestPort is invalid"); throw new IllegalArgumentException("requestPort is invalid");
} }
this.debugPort = debugPort;
if (eventPort == -1) {
throw new IllegalArgumentException("eventPort is invalid");
}
this.requestPort = requestPort;
this.eventPort = eventPort;
} }
public void setDebugStackState(DebugLuaState vm) { public void setDebugStackState(DebugLuaState vm) {
@@ -57,14 +48,6 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener {
} }
} }
if (requestWriter != null) {
try {
requestWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (eventWriter != null) { if (eventWriter != null) {
try { try {
eventWriter.close(); eventWriter.close();
@@ -81,7 +64,7 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener {
public synchronized void start() throws IOException { public synchronized void start() throws IOException {
if (this.vm == null) { if (this.vm == null) {
throw new IllegalStateException( throw new IllegalStateException(
"DebugStackState is not set. Please call setDebugStackState first."); "DebugLuaState is not set. Please call setDebugStackState first.");
} }
this.requestWatcherThread = new Thread(new Runnable() { this.requestWatcherThread = new Thread(new Runnable() {
@@ -93,8 +76,7 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener {
this.requestWatcherThread.start(); this.requestWatcherThread.start();
this.state = RUNNING; this.state = RUNNING;
System.out.println("LuaJ debug server is started on ports: " System.out.println("LuaJ debug server is listening on port: " + debugPort);
+ requestPort + ", " + eventPort);
} }
protected synchronized int getState() { protected synchronized int getState() {
@@ -107,28 +89,26 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener {
this.state = STOPPED; this.state = STOPPED;
} }
public abstract Object getClientConnection();
protected void loopForRequests() { protected void loopForRequests() {
try { try {
while (getState() != STOPPED) { while (getState() != STOPPED) {
byte[] data = null;
int size = requestReader.readInt(); int size = requestReader.readInt();
byte[] data = new byte[size]; data = new byte[size];
requestReader.readFully(data); requestReader.readFully(data);
DebugRequest request = (DebugRequest) SerializationHelper DebugRequest request = (DebugRequest) SerializationHelper
.deserialize(data); .deserialize(data);
if (DebugUtils.IS_DEBUG) if (DebugUtils.IS_DEBUG) {
DebugUtils.println("SERVER receives request: " DebugUtils.println("SERVER receives request: " + request.toString());
+ request.toString()); }
DebugResponse response = handleRequest(request); handleRequest(request);
data = SerializationHelper.serialize(response);
requestWriter.writeInt(data.length);
requestWriter.write(data);
requestWriter.flush();
if (DebugUtils.IS_DEBUG)
DebugUtils.println("SERVER sends response: " + response);
} }
} catch (EOFException e) { } catch (EOFException e) {
// expected during shutdown // expected. it may occur depending on the timing during the termination
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@@ -184,12 +164,11 @@ public class DebugSupport implements DebugRequestListener, DebugEventListener {
* *
* @see org.luaj.debug.request.DebugRequestListener#handleRequest(org.luaj.debug.request.DebugRequest) * @see org.luaj.debug.request.DebugRequestListener#handleRequest(org.luaj.debug.request.DebugRequest)
*/ */
public DebugResponse handleRequest(DebugRequest request) { public void handleRequest(DebugRequest request) {
if (DebugUtils.IS_DEBUG) { if (DebugUtils.IS_DEBUG) {
DebugUtils.println("handling request: " + request.toString()); DebugUtils.println("SERVER handling request: " + request.toString());
} }
DebugResponse response = vm.handleRequest(request); vm.handleRequest(request);
return response;
} }
} }

View File

@@ -15,7 +15,7 @@ import org.luaj.debug.request.DebugRequestLineBreakpointToggle;
import org.luaj.debug.request.DebugRequestStack; import org.luaj.debug.request.DebugRequestStack;
import org.luaj.debug.request.DebugRequestType; import org.luaj.debug.request.DebugRequestType;
import org.luaj.debug.response.DebugResponseCallgraph; import org.luaj.debug.response.DebugResponseCallgraph;
import org.luaj.debug.response.DebugResponseSimple; import org.luaj.debug.response.DebugResponseStack;
import org.luaj.debug.response.DebugResponseVariables; import org.luaj.debug.response.DebugResponseVariables;
public class SerializationHelper { public class SerializationHelper {
@@ -46,21 +46,21 @@ public class SerializationHelper {
return object; return object;
} }
static final int SERIAL_TYPE_NullableString = 0; static final int SERIAL_TYPE_NullableString = 0;
static final int SERIAL_TYPE_TableVariable = 1; static final int SERIAL_TYPE_TableVariable = 1;
static final int SERIAL_TYPE_Variable = 2; static final int SERIAL_TYPE_Variable = 2;
static final int SERIAL_TYPE_DebugResponseCallgraph = 3; static final int SERIAL_TYPE_DebugResponseCallgraph = 3;
static final int SERIAL_TYPE_DebugResponseVariables = 4; static final int SERIAL_TYPE_DebugResponseVariables = 4;
static final int SERIAL_TYPE_DebugResponseSimple = 5; static final int SERIAL_TYPE_DebugResponseStack = 5;
static final int SERIAL_TYPE_StackFrame = 6; static final int SERIAL_TYPE_StackFrame = 6;
static final int SERIAL_TYPE_DebugRequestType = 7; static final int SERIAL_TYPE_DebugRequestType = 7;
static final int SERIAL_TYPE_DebugRequest = 8; static final int SERIAL_TYPE_DebugRequest = 8;
static final int SERIAL_TYPE_DebugRequestStack = 9; static final int SERIAL_TYPE_DebugRequestStack = 9;
static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10; static final int SERIAL_TYPE_DebugRequestLineBreakpointToggle = 10;
static final int SERIAL_TYPE_DebugEventType = 11; static final int SERIAL_TYPE_DebugEventType = 11;
static final int SERIAL_TYPE_DebugEvent = 12; static final int SERIAL_TYPE_DebugEvent = 12;
static final int SERIAL_TYPE_DebugEventBreakpoint = 13; static final int SERIAL_TYPE_DebugEventBreakpoint = 13;
static final int SERIAL_TYPE_DebugEventError = 14; static final int SERIAL_TYPE_DebugEventError = 14;
public static void serialize(Serializable object, DataOutputStream dout) public static void serialize(Serializable object, DataOutputStream dout)
throws IOException { throws IOException {
@@ -76,9 +76,9 @@ public class SerializationHelper {
} else if (object instanceof StackFrame) { } else if (object instanceof StackFrame) {
dout.writeInt(SERIAL_TYPE_StackFrame); dout.writeInt(SERIAL_TYPE_StackFrame);
StackFrame.serialize(dout, (StackFrame) object); StackFrame.serialize(dout, (StackFrame) object);
} else if (object instanceof DebugResponseSimple) { } else if (object instanceof DebugResponseStack) {
dout.writeInt(SERIAL_TYPE_DebugResponseSimple); dout.writeInt(SERIAL_TYPE_DebugResponseStack);
DebugResponseSimple.serialize(dout, (DebugResponseSimple) object); DebugResponseStack.serialize(dout, (DebugResponseStack) object);
} else if (object instanceof DebugResponseVariables) { } else if (object instanceof DebugResponseVariables) {
dout.writeInt(SERIAL_TYPE_DebugResponseVariables); dout.writeInt(SERIAL_TYPE_DebugResponseVariables);
DebugResponseVariables.serialize(dout, (DebugResponseVariables) object); DebugResponseVariables.serialize(dout, (DebugResponseVariables) object);
@@ -136,12 +136,12 @@ public class SerializationHelper {
case SERIAL_TYPE_StackFrame: case SERIAL_TYPE_StackFrame:
object = StackFrame.deserialize(din); object = StackFrame.deserialize(din);
break; break;
case SERIAL_TYPE_DebugResponseSimple:
object = DebugResponseSimple.deserialize(din);
break;
case SERIAL_TYPE_DebugResponseCallgraph: case SERIAL_TYPE_DebugResponseCallgraph:
object = DebugResponseCallgraph.deserialize(din); object = DebugResponseCallgraph.deserialize(din);
break; break;
case SERIAL_TYPE_DebugResponseStack:
object = DebugResponseStack.deserialize(din);
break;
case SERIAL_TYPE_DebugResponseVariables: case SERIAL_TYPE_DebugResponseVariables:
object = DebugResponseVariables.deserialize(din); object = DebugResponseVariables.deserialize(din);
break; break;

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved. * Copyright (c) 2007 LuaJ. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* 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 org.luaj.debug.event; package org.luaj.debug.event;
import java.io.DataInputStream; import java.io.DataInputStream;
@@ -29,14 +29,14 @@ import org.luaj.debug.Serializable;
import org.luaj.debug.SerializationHelper; import org.luaj.debug.SerializationHelper;
public class DebugEvent implements Serializable { public class DebugEvent implements Serializable {
protected DebugEventType type; protected DebugEventType type;
public DebugEvent(DebugEventType type) { public DebugEvent(DebugEventType type) {
this.type = type; this.type = type;
} }
public DebugEventType getType() { public DebugEventType getType() {
return type; return type;
} }
@@ -45,20 +45,23 @@ public class DebugEvent implements Serializable {
this.type = type; this.type = type;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
public String toString() { public String toString() {
return type.toString(); return type.toString();
} }
public static void serialize(DataOutputStream out, DebugEvent object)
throws IOException {
SerializationHelper.serialize(object.getType(), out);
}
public static DebugEvent deserialize(DataInputStream in) throws IOException { public static void serialize(DataOutputStream out, DebugEvent object)
DebugEventType type = (DebugEventType) SerializationHelper.deserialize(in); throws IOException {
return new DebugEvent(type); SerializationHelper.serialize(object.getType(), out);
} }
public static DebugEvent deserialize(DataInputStream in) throws IOException {
DebugEventType type = (DebugEventType) SerializationHelper
.deserialize(in);
return new DebugEvent(type);
}
} }

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved. * Copyright (c) 2007 LuaJ. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* 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 org.luaj.debug.event; package org.luaj.debug.event;
import java.io.DataInputStream; import java.io.DataInputStream;
@@ -27,47 +27,52 @@ import java.io.IOException;
public class DebugEventBreakpoint extends DebugEvent { public class DebugEventBreakpoint extends DebugEvent {
protected String source; protected String source;
protected int lineNumber; protected int lineNumber;
public DebugEventBreakpoint(String source, int lineNumber) { public DebugEventBreakpoint(String source, int lineNumber) {
super(DebugEventType.suspendedOnBreakpoint); super(DebugEventType.suspendedOnBreakpoint);
if (source == null) { if (source == null) {
throw new IllegalArgumentException("argument source cannot be null"); throw new IllegalArgumentException("argument source cannot be null");
} }
if (lineNumber <= 0) { if (lineNumber <= 0) {
throw new IllegalArgumentException("argument lineNumber must be positive integer"); throw new IllegalArgumentException(
"argument lineNumber must be positive integer");
} }
this.source = source; this.source = source;
this.lineNumber = lineNumber; this.lineNumber = lineNumber;
} }
public String getSource() { public String getSource() {
return this.source; return this.source;
} }
public int getLineNumber() { public int getLineNumber() {
return this.lineNumber; return this.lineNumber;
} }
/* (non-Javadoc) /*
* (non-Javadoc)
*
* @see lua.debug.DebugEvent#toString() * @see lua.debug.DebugEvent#toString()
*/ */
public String toString() { public String toString() {
return super.toString() + " source:" + getSource() + " line:" + getLineNumber(); return super.toString() + " source:" + getSource() + " line:"
+ getLineNumber();
} }
public static void serialize(DataOutputStream out, DebugEventBreakpoint object)
throws IOException {
out.writeUTF(object.getSource());
out.writeInt(object.getLineNumber());
}
public static DebugEvent deserialize(DataInputStream in) throws IOException { public static void serialize(DataOutputStream out,
String source = in.readUTF(); DebugEventBreakpoint object) throws IOException {
int lineNo = in.readInt(); out.writeUTF(object.getSource());
out.writeInt(object.getLineNumber());
return new DebugEventBreakpoint(source, lineNo); }
}
public static DebugEvent deserialize(DataInputStream in) throws IOException {
String source = in.readUTF();
int lineNo = in.readInt();
return new DebugEventBreakpoint(source, lineNo);
}
} }

View File

@@ -29,7 +29,7 @@ import org.luaj.debug.EnumType;
public class DebugEventType extends EnumType { public class DebugEventType extends EnumType {
public static DebugEventType started = new DebugEventType("started", 0); public static DebugEventType started = new DebugEventType("started", 0);
public static DebugEventType suspendedByClient = new DebugEventType("suspendedByClient", 1); public static DebugEventType suspendedByClient = new DebugEventType("suspendedByClient", 1);
public static DebugEventType suspendedOnBreakpoint = new DebugEventType("suspendedOnBreakpoint", 2); public static DebugEventType suspendedOnBreakpoint = new DebugEventType("suspendedOnBreakpoint", 2);
public static DebugEventType suspendedOnWatchpoint = new DebugEventType("suspendedOnWatchpoint", 3); public static DebugEventType suspendedOnWatchpoint = new DebugEventType("suspendedOnWatchpoint", 3);
@@ -43,6 +43,12 @@ public class DebugEventType extends EnumType {
public static DebugEventType resumedOnError = new DebugEventType("resumedOnError", 11); public static DebugEventType resumedOnError = new DebugEventType("resumedOnError", 11);
public static DebugEventType error = new DebugEventType("error", 12); public static DebugEventType error = new DebugEventType("error", 12);
public static DebugEventType terminated = new DebugEventType("terminated", 13); public static DebugEventType terminated = new DebugEventType("terminated", 13);
public static DebugEventType clientRequestCallgraphReply
= new DebugEventType("clientRequestCallgraphReply", 14);
public static DebugEventType clientRequestStackReply
= new DebugEventType("clientRequestStackReply", 15);
public static DebugEventType clientRequestGlobalReply
= new DebugEventType("clientRequestGlobalReply", 16);
protected static DebugEventType[] ENUMS = new DebugEventType[] { protected static DebugEventType[] ENUMS = new DebugEventType[] {
started, started,
@@ -58,7 +64,10 @@ public class DebugEventType extends EnumType {
resumedOnSteppingEnd, resumedOnSteppingEnd,
resumedOnError, resumedOnError,
error, error,
terminated terminated,
clientRequestCallgraphReply,
clientRequestStackReply,
clientRequestGlobalReply
}; };
protected DebugEventType(String name, int ordinal) { protected DebugEventType(String name, int ordinal) {

View File

@@ -9,90 +9,63 @@ import javax.microedition.io.SocketConnection;
import org.luaj.debug.DebugSupport; import org.luaj.debug.DebugSupport;
import org.luaj.debug.event.DebugEvent; import org.luaj.debug.event.DebugEvent;
public class DebugSupportImpl extends DebugSupport { public class DebugSupportImpl extends DebugSupport {
protected ServerSocketConnection requestServerConnection; protected ServerSocketConnection serverConnection;
protected SocketConnection requestSocketConnection; protected SocketConnection clientDebugConnection;
protected SocketConnection eventSocketConnection;
protected ServerSocketConnection eventServerConnection; public DebugSupportImpl(int debugPort) {
protected SocketConnection eventSocketConnection; super(debugPort);
}
public DebugSupportImpl(int requestPort,
int eventPort) { /*
super(requestPort, eventPort); * (non-Javadoc)
} *
* @see lua.debug.j2se.DebugSupport#start()
/* (non-Javadoc) */
* @see lua.debug.j2se.DebugSupport#start()
*/
public synchronized void start() throws IOException { public synchronized void start() throws IOException {
System.out.println("Starting the sockets...."); // Set up the request socket and request input + event output streams
// Set up the request socket and request input + output streams this.serverConnection = (ServerSocketConnection) Connector
this.requestServerConnection .open("socket://:" + this.debugPort);
= (ServerSocketConnection)Connector.open("socket://:" + this.requestPort); this.clientDebugConnection = (SocketConnection) serverConnection
this.requestSocketConnection = .acceptAndOpen();
(SocketConnection) requestServerConnection.acceptAndOpen(); clientDebugConnection.setSocketOption(SocketConnection.DELAY, 0);
requestSocketConnection.setSocketOption(SocketConnection.DELAY, 0); clientDebugConnection.setSocketOption(SocketConnection.LINGER, 0);
requestSocketConnection.setSocketOption(SocketConnection.LINGER, 0); clientDebugConnection.setSocketOption(SocketConnection.KEEPALIVE, 1);
requestSocketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1); clientDebugConnection.setSocketOption(SocketConnection.RCVBUF, 1024);
requestSocketConnection.setSocketOption(SocketConnection.RCVBUF, 1024); clientDebugConnection.setSocketOption(SocketConnection.SNDBUF, 1024);
requestSocketConnection.setSocketOption(SocketConnection.SNDBUF, 1024); this.requestReader = clientDebugConnection.openDataInputStream();
this.requestReader = requestSocketConnection.openDataInputStream(); this.eventWriter = clientDebugConnection.openDataOutputStream();
this.requestWriter = requestSocketConnection.openDataOutputStream();
// Set up the event socket and event output stream System.out.println("Lua debug server is started on ports: " + debugPort);
this.eventServerConnection
= (ServerSocketConnection)Connector.open("socket://:" + this.eventPort);
this.eventSocketConnection
= (SocketConnection) eventServerConnection.acceptAndOpen();
eventSocketConnection.setSocketOption(SocketConnection.DELAY, 0);
eventSocketConnection.setSocketOption(SocketConnection.LINGER, 0);
eventSocketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1);
eventSocketConnection.setSocketOption(SocketConnection.RCVBUF, 1024);
eventSocketConnection.setSocketOption(SocketConnection.SNDBUF, 1024);
this.eventWriter = eventSocketConnection.openDataOutputStream();;
System.out.println("Lua debug server is started on ports: " + requestPort + ", " + eventPort);
super.start(); super.start();
} }
protected void dispose() { protected void dispose() {
super.dispose(); super.dispose();
if (requestSocketConnection != null) { if (this.clientDebugConnection != null) {
try { try {
requestSocketConnection.close(); clientDebugConnection.close();
} catch (IOException e) {} } catch (IOException e) {
}
} }
if (requestServerConnection != null) { if (this.serverConnection != null) {
try { try {
requestServerConnection.close(); serverConnection.close();
} catch (IOException e) {} } catch (IOException e) {
} }
if (eventSocketConnection != null) {
try {
eventSocketConnection.close();
} catch (IOException e) {}
}
if (eventServerConnection != null){
try {
eventServerConnection.close();
} catch (IOException e) {}
} }
} }
protected void loopForRequests() { public Object getClientConnection() {
synchronized (requestSocketConnection) { return clientDebugConnection;
super.loopForRequests();
}
} }
protected void sendEvent(DebugEvent event) { protected void sendEvent(DebugEvent event) {
synchronized (eventSocketConnection) { synchronized (eventSocketConnection) {
super.sendEvent(event); super.sendEvent(event);
} }
} }
} }

View File

@@ -32,31 +32,23 @@ import org.luaj.debug.event.DebugEvent;
public class DebugSupportImpl extends DebugSupport { public class DebugSupportImpl extends DebugSupport {
protected ServerSocket requestSocket; protected ServerSocket serverSocket;
protected Socket clientRequestSocket; protected Socket clientSocket;
protected ServerSocket eventSocket;
protected Socket clientEventSocket;
public DebugSupportImpl(int requestPort, int eventPort) { public DebugSupportImpl(int debugPort) {
super(requestPort, eventPort); super(debugPort);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see lua.debug.j2se.DebugSupport#start() * @see lua.debug.j2se.DebugSupport#start()
*/ */
public synchronized void start() throws IOException { public synchronized void start() throws IOException {
this.requestSocket = new ServerSocket(requestPort); this.serverSocket = new ServerSocket(debugPort, 1);
this.clientRequestSocket = requestSocket.accept(); this.clientSocket = serverSocket.accept();
this.requestReader this.requestReader
= new DataInputStream(clientRequestSocket.getInputStream()); = new DataInputStream(clientSocket.getInputStream());
this.requestWriter
= new DataOutputStream(clientRequestSocket.getOutputStream());
this.eventSocket = new ServerSocket(eventPort);
this.clientEventSocket = eventSocket.accept();
this.eventWriter this.eventWriter
= new DataOutputStream(clientEventSocket.getOutputStream()); = new DataOutputStream(clientSocket.getOutputStream());
super.start(); super.start();
} }
@@ -64,35 +56,21 @@ public class DebugSupportImpl extends DebugSupport {
protected void dispose() { protected void dispose() {
super.dispose(); super.dispose();
if (clientRequestSocket != null) { if (this.clientSocket != null) {
try { try {
clientRequestSocket.close(); clientSocket.close();
} catch (IOException e) {} } catch (IOException e) {}
} }
if (requestSocket != null) { if (this.serverSocket != null) {
try { try {
requestSocket.close(); serverSocket.close();
} catch (IOException e) {} } catch (IOException e) {}
} }
if (clientEventSocket != null) {
try {
clientEventSocket.close();
} catch (IOException e) {}
}
if (eventSocket != null){
try {
eventSocket.close();
} catch (IOException e) {}
}
} }
protected void loopForRequests() { public Object getClientConnection() {
synchronized (clientRequestSocket) { return clientSocket;
super.loopForRequests();
}
} }
/** /**
@@ -113,8 +91,8 @@ public class DebugSupportImpl extends DebugSupport {
* @param event * @param event
*/ */
protected void sendEvent(DebugEvent event) { protected void sendEvent(DebugEvent event) {
synchronized (eventSocket) { synchronized (clientSocket) {
super.sendEvent(event); super.sendEvent(event);
} }
} }
} }

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved. * Copyright (c) 2007 LuaJ. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* 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 org.luaj.debug.j2se; package org.luaj.debug.j2se;
import java.io.File; import java.io.File;
@@ -39,7 +39,6 @@ import org.luaj.vm.LValue;
import org.luaj.vm.LoadState; import org.luaj.vm.LoadState;
import org.luaj.vm.LuaState; import org.luaj.vm.LuaState;
/** /**
* StandardLuaJVM executes a lua program in normal run mode or debug mode. * StandardLuaJVM executes a lua program in normal run mode or debug mode.
* *
@@ -47,106 +46,123 @@ import org.luaj.vm.LuaState;
* @version: 1.0 * @version: 1.0
*/ */
public class StandardLuaJVM { public class StandardLuaJVM {
private static final String CMD_LINE_DEBUG_OPTION_SUSPEND_ON_START = "suspendOnStart=";
private static final String CMD_LINE_DEBUG_OPTION_PORT = "port=";
protected boolean isDebugMode = false; protected boolean isDebugMode = false;
protected int requestPort; protected int debugPort = -1;;
protected int eventPort; protected boolean bSuspendOnStart = false;
protected String script; protected String script;
protected String[] scriptArgs; protected String[] scriptArgs;
protected LuaState state; protected LuaState state;
protected boolean isReady = false; protected boolean isReady = false;
protected boolean isTerminated = false; protected boolean isTerminated = false;
// command line parsing utilities // command line parsing utilities
class ParseException extends Exception { class ParseException extends Exception {
private static final long serialVersionUID = -3134938136698856577L; private static final long serialVersionUID = -3134938136698856577L;
public ParseException(String message) { public ParseException(String message) {
super(message); super(message);
} }
} }
protected void printUsage() { protected void printUsage() {
System.out.println("Usage:"); System.out.println("Usage:");
System.out.println("\t java StandardLuaJVM [-debug <requestPort> <eventPort>] <script> [<script arguments>]"); System.out.println("\t java StandardLuaJVM [-Dport=<port>[,suspendedOnStart=<true|false>]] <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.");
} }
void parse(String[] args) throws ParseException {
if (args == null || args.length < 1) {
throw new ParseException("Invalid command line arguments.");
}
if ("-debug".equals(args[0])) { void parse(String[] args) throws ParseException {
if (args.length < 4) { if (args == null || args.length < 1) {
throw new ParseException("Invalid command line arguments."); throw new ParseException("Invalid command line arguments.");
} }
this.isDebugMode = true; if (args[0] != null && args[0].startsWith("-D")) {
try { if (args.length < 2) {
this.requestPort = Integer.parseInt(args[1]); throw new ParseException("Invalid command line arguments.");
if (this.requestPort <= 0) { }
throw new ParseException("Invalid request port: it must be greater than zero.");
} this.isDebugMode = true;
String debugOptions = args[0];
this.eventPort = Integer.parseInt(args[2]); debugOptions = debugOptions.substring(2); // remove '-D'
if (this.eventPort <= 0) { String[] options = debugOptions.split(",");
throw new ParseException("Invalid event port: it must be greater than zero."); for (int i = 0; options != null && i < options.length; i++) {
} if (options[i].startsWith(CMD_LINE_DEBUG_OPTION_PORT)) {
} catch(NumberFormatException e) { String portString = options[i].substring(CMD_LINE_DEBUG_OPTION_PORT.length());
throw new ParseException("Invalid port number: " + e.getMessage()); try {
} this.debugPort = Integer.parseInt(portString);
if (this.debugPort <= 0) {
if (this.requestPort == this.eventPort) { throw new ParseException(
throw new ParseException("Invalid ports: request port and event port must be different"); "Invalid debug port: it must be greater than zero.");
}
} catch (NumberFormatException e) {
throw new ParseException("Invalid debug port: " + e.getMessage());
}
} else if (options[i].startsWith(CMD_LINE_DEBUG_OPTION_SUSPEND_ON_START)) {
String suspendOnStartStr
= options[i].substring(CMD_LINE_DEBUG_OPTION_SUSPEND_ON_START.length());
if (!suspendOnStartStr.equalsIgnoreCase("true") &&
!suspendOnStartStr.equalsIgnoreCase("false")) {
throw new ParseException("invalid debug flag: suspendOnStart");
}
this.bSuspendOnStart = Boolean.valueOf(suspendOnStartStr).booleanValue();
} else {
throw new ParseException("Invalid command line argument: " + debugOptions);
}
}
if (this.debugPort == -1) {
throw new ParseException("Invalid command line: debug port is missing");
} }
int tempArgsCount = args.length - 3; int tempArgsCount = args.length - 1;
String[] tempArgs = new String[tempArgsCount]; String[] tempArgs = new String[tempArgsCount];
System.arraycopy(args, 3, tempArgs, 0, tempArgsCount); System.arraycopy(args, 1, tempArgs, 0, tempArgsCount);
parseScriptArgs(tempArgs); parseScriptArgs(tempArgs);
} else { } else {
parseScriptArgs(args); parseScriptArgs(args);
} }
}
private void parseScriptArgs(String[] args)
throws org.luaj.debug.j2se.StandardLuaJVM.ParseException {
if (args == null || args.length < 1) {
throw new ParseException("script is missing.");
}
this.script = args[0];
DebugUtils.println("Lua script to run: " + this.script);
int scriptArgsLength = args.length - 1;
if (scriptArgsLength > 0) {
this.scriptArgs = new String[scriptArgsLength];
for (int i = 1; i < args.length; i++) {
this.scriptArgs[i - 1] = args[i];
}
}
} }
private void parseScriptArgs(String[] args)
throws org.luaj.debug.j2se.StandardLuaJVM.ParseException {
if (args == null || args.length < 1) {
throw new ParseException("script is missing.");
}
this.script = args[0];
DebugUtils.println("Lua script to run: " + this.script);
int scriptArgsLength = args.length - 1;
if (scriptArgsLength > 0) {
this.scriptArgs = new String[scriptArgsLength];
for (int i = 1; i < args.length; i++) {
this.scriptArgs[i - 1] = args[i];
}
}
}
// end of command line parsing utilities // end of command line parsing utilities
boolean isDebug() { boolean isDebug() {
return this.isDebugMode; return this.isDebugMode;
} }
int getRequestPort() { int getDebugPort() {
return this.requestPort; return this.debugPort;
} }
int getEventPort() { boolean getSuspendOnStart() {
return this.eventPort; return this.bSuspendOnStart;
} }
String getScript() { String getScript() {
return this.script; return this.script;
} }
boolean hasScriptArgs() { boolean hasScriptArgs() {
return (this.scriptArgs != null && this.scriptArgs.length > 0); return (this.scriptArgs != null && this.scriptArgs.length > 0);
} }
String[] getScriptArgs() { String[] getScriptArgs() {
return this.scriptArgs; return this.scriptArgs;
} }
@@ -158,22 +174,21 @@ public class StandardLuaJVM {
doRun(); doRun();
} }
} }
protected void init(LuaState state) { protected void init(LuaState state) {
// add standard bindings // add standard bindings
state.installStandardLibs(); state.installStandardLibs();
// add LuaJava bindings // add LuaJava bindings
LuajavaLib.install(state._G); LuajavaLib.install(state._G);
// add the compiler // add the compiler
LuaC.install(); LuaC.install();
} }
protected void doRun() throws IOException { protected void doRun() throws IOException {
// new lua state // new lua state
state = new LuaState(); state = new LuaState();
init(state); init(state);
@@ -182,22 +197,22 @@ public class StandardLuaJVM {
String[] scriptArgs = getScriptArgs(); String[] scriptArgs = getScriptArgs();
int numOfScriptArgs = (scriptArgs == null) ? 0 : scriptArgs.length; int numOfScriptArgs = (scriptArgs == null) ? 0 : scriptArgs.length;
LValue[] vargs = new LValue[numOfScriptArgs]; LValue[] vargs = new LValue[numOfScriptArgs];
for (int i = 0; i < numOfScriptArgs; i++) { for (int i = 0; i < numOfScriptArgs; i++) {
vargs[i] = new LString(getScriptArgs()[i]); vargs[i] = new LString(getScriptArgs()[i]);
} }
// load the Lua file // load the Lua file
DebugUtils.println("loading Lua script '" + getScript() + "'"); DebugUtils.println("loading Lua script '" + getScript() + "'");
InputStream is = new FileInputStream(new File(getScript())); InputStream is = new FileInputStream(new File(getScript()));
LPrototype p = LoadState.undump(state, is, getScript()); LPrototype p = LoadState.undump(state, is, getScript());
// create closure and execute // create closure and execute
LClosure c = new LClosure(state, p); LClosure c = new LClosure(state, p);
state.doCall(c, vargs); state.doCall(c, vargs);
} }
protected void doDebug() throws IOException { protected void doDebug() throws IOException {
DebugUtils.println("setting up LuaJava and debug stack state..."); DebugUtils.println("setting up LuaJava and debug stack state...");
// new lua debug state // new lua debug state
state = new DebugLuaState(); state = new DebugLuaState();
@@ -210,29 +225,28 @@ public class StandardLuaJVM {
// set up debug support if the file is successfully loaded // set up debug support if the file is successfully loaded
DebugUtils.println("start debugging..."); DebugUtils.println("start debugging...");
DebugSupport debugSupport DebugSupport debugSupport = new DebugSupportImpl(getDebugPort());
= new DebugSupportImpl(getRequestPort(), getEventPort()); getDebugState().setSuspendAtStart(getSuspendOnStart());
getDebugState().setDebugSupport(debugSupport); getDebugState().setDebugSupport(debugSupport);
getDebugState().setSuspendAtStart(true);
// create closure and execute // create closure and execute
final LClosure c = new LClosure(p, state._G); final LClosure c = new LClosure(p, state._G);
String[] args = getScriptArgs(); String[] args = getScriptArgs();
int numOfScriptArgs = (args != null ? args.length : 0); int numOfScriptArgs = (args != null ? args.length : 0);
LValue[] vargs = new LValue[numOfScriptArgs]; LValue[] vargs = new LValue[numOfScriptArgs];
for (int i = 0; i < numOfScriptArgs; i++) { for (int i = 0; i < numOfScriptArgs; i++) {
vargs[i] = new LString(args[i]); vargs[i] = new LString(args[i]);
} }
try { try {
getDebugState().doCall(c, vargs); getDebugState().doCall(c, vargs);
} catch (VMException e) { } catch (VMException e) {
System.err.println("VMException: " + e.getMessage()); System.err.println("VMException: " + e.getMessage());
} }
getDebugState().stop(); getDebugState().stop();
} }
private DebugLuaState getDebugState() { private DebugLuaState getDebugState() {
return (DebugLuaState)state; return (DebugLuaState) state;
} }
/** /**
@@ -248,11 +262,11 @@ public class StandardLuaJVM {
vm.run(); vm.run();
} catch (ParseException e) { } catch (ParseException e) {
System.out.println("Error: " + e.getMessage()); System.out.println("Error: " + e.getMessage());
vm.printUsage(); vm.printUsage();
return; return;
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error: " + e.getMessage()); System.out.println("Error: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@@ -21,7 +21,6 @@
******************************************************************************/ ******************************************************************************/
package org.luaj.debug.request; package org.luaj.debug.request;
import org.luaj.debug.response.DebugResponse;
public interface DebugRequestListener { public interface DebugRequestListener {
@@ -38,5 +37,5 @@ public interface DebugRequestListener {
* listing the (variable, value) pairs * listing the (variable, value) pairs
* step -- single step forward (go to next statement) * step -- single step forward (go to next statement)
*/ */
public DebugResponse handleRequest(DebugRequest request); public void handleRequest(DebugRequest request);
} }

View File

@@ -1,26 +0,0 @@
/*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.debug.response;
import org.luaj.debug.Serializable;
public interface DebugResponse extends Serializable {}

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved. * Copyright (c) 2007 LuaJ. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* 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 org.luaj.debug.response; package org.luaj.debug.response;
import java.io.DataInputStream; import java.io.DataInputStream;
@@ -26,19 +26,21 @@ import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import org.luaj.debug.StackFrame; import org.luaj.debug.StackFrame;
import org.luaj.debug.event.DebugEvent;
import org.luaj.debug.event.DebugEventType;
public class DebugResponseCallgraph extends DebugEvent {
public class DebugResponseCallgraph implements DebugResponse {
protected StackFrame[] stackFrames; protected StackFrame[] stackFrames;
public DebugResponseCallgraph(StackFrame[] callgraph) { public DebugResponseCallgraph(StackFrame[] callgraph) {
if (callgraph == null) { super(DebugEventType.clientRequestCallgraphReply);
this.stackFrames = new StackFrame[0]; if (callgraph == null) {
} else { this.stackFrames = new StackFrame[0];
this.stackFrames = callgraph; } else {
} this.stackFrames = callgraph;
}
} }
public StackFrame[] getCallgraph() { public StackFrame[] getCallgraph() {
return this.stackFrames; return this.stackFrames;
} }
@@ -46,29 +48,29 @@ public class DebugResponseCallgraph implements DebugResponse {
public String toString() { public String toString() {
StringBuffer buffer = new StringBuffer(); 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());
buffer.append("\n"); buffer.append("\n");
} }
return buffer.toString(); return buffer.toString();
} }
public static void serialize(DataOutputStream out, DebugResponseCallgraph response) public static void serialize(DataOutputStream out,
throws IOException { DebugResponseCallgraph response) throws IOException {
StackFrame[] stackFrames = response.getCallgraph(); StackFrame[] stackFrames = response.getCallgraph();
out.writeInt(stackFrames == null ? 0 : stackFrames.length); out.writeInt(stackFrames == null ? 0 : stackFrames.length);
for (int i = 0; stackFrames != null && i < stackFrames.length; i++) { for (int i = 0; stackFrames != null && i < stackFrames.length; i++) {
StackFrame.serialize(out, stackFrames[i]); StackFrame.serialize(out, stackFrames[i]);
} }
} }
public static DebugResponseCallgraph deserialize(DataInputStream in) throws IOException { public static DebugEvent deserialize(DataInputStream in) throws IOException {
int count = in.readInt(); int count = in.readInt();
StackFrame[] stackFrames = new StackFrame[count]; StackFrame[] stackFrames = new StackFrame[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
stackFrames[i] = StackFrame.deserialize(in); stackFrames[i] = StackFrame.deserialize(in);
} }
return new DebugResponseCallgraph(stackFrames); return new DebugResponseCallgraph(stackFrames);
} }
} }

View File

@@ -1,59 +0,0 @@
/*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
package org.luaj.debug.response;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DebugResponseSimple implements DebugResponse {
protected boolean isSuccessful;
public static final DebugResponseSimple SUCCESS = new DebugResponseSimple(true);
public static final DebugResponseSimple FAILURE = new DebugResponseSimple(false);
public DebugResponseSimple(boolean isSuccessful) {
this.isSuccessful = isSuccessful;
}
public boolean isSuccessful() {
return this.isSuccessful;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
return String.valueOf(isSuccessful);
}
public static void serialize(DataOutputStream out, DebugResponseSimple response)
throws IOException {
out.writeBoolean(response.isSuccessful());
}
public static DebugResponseSimple deserialize(DataInputStream in)
throws IOException {
boolean value = in.readBoolean();
return value ? SUCCESS : FAILURE;
}
}

View File

@@ -0,0 +1,47 @@
package org.luaj.debug.response;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.luaj.debug.SerializationHelper;
import org.luaj.debug.Variable;
import org.luaj.debug.event.DebugEvent;
import org.luaj.debug.event.DebugEventType;
public class DebugResponseStack extends DebugResponseVariables {
protected int stackFrameIndex;
public DebugResponseStack(int index, Variable[] variables) {
super(variables, DebugEventType.clientRequestStackReply);
this.stackFrameIndex = index;
}
public int getIndex() {
return this.stackFrameIndex;
}
public static void serialize(DataOutputStream out,
DebugResponseStack response)
throws IOException {
out.writeInt(response.getIndex());
Variable[] variables = response.getVariables();
out.writeInt(variables == null ? 0 : variables.length);
for (int i = 0; i < variables.length; i++) {
SerializationHelper.serialize(variables[i], out);
}
}
public static DebugEvent deserialize(DataInputStream in) throws IOException {
int index = in.readInt();
int count = in.readInt();
Variable[] variables = new Variable[count];
for (int i = 0; i < count; i++) {
variables[i] = (Variable) SerializationHelper.deserialize(in);
}
return new DebugResponseStack(index, variables);
}
}

View File

@@ -27,11 +27,14 @@ import java.io.IOException;
import org.luaj.debug.SerializationHelper; import org.luaj.debug.SerializationHelper;
import org.luaj.debug.Variable; import org.luaj.debug.Variable;
import org.luaj.debug.event.DebugEvent;
import org.luaj.debug.event.DebugEventType;
public class DebugResponseVariables implements DebugResponse { public class DebugResponseVariables extends DebugEvent {
protected Variable[] variables; protected Variable[] variables;
public DebugResponseVariables(Variable[] variables) { public DebugResponseVariables(Variable[] variables, DebugEventType type) {
super(type);
if (variables == null) { if (variables == null) {
this.variables = new Variable[0]; this.variables = new Variable[0];
} else { } else {
@@ -57,6 +60,7 @@ public class DebugResponseVariables implements DebugResponse {
public static void serialize(DataOutputStream out, public static void serialize(DataOutputStream out,
DebugResponseVariables response) DebugResponseVariables response)
throws IOException { throws IOException {
DebugEventType.serialize(out, response.getType());
Variable[] variables = response.getVariables(); Variable[] variables = response.getVariables();
out.writeInt(variables == null ? 0 : variables.length); out.writeInt(variables == null ? 0 : variables.length);
for (int i = 0; i < variables.length; i++) { for (int i = 0; i < variables.length; i++) {
@@ -64,13 +68,14 @@ public class DebugResponseVariables implements DebugResponse {
} }
} }
public static DebugResponseVariables deserialize(DataInputStream in) public static DebugEvent deserialize(DataInputStream in)
throws IOException { throws IOException {
DebugEventType type = DebugEventType.deserialize(in);
int count = in.readInt(); int count = in.readInt();
Variable[] variables = new Variable[count]; Variable[] variables = new Variable[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
variables[i] = (Variable) SerializationHelper.deserialize(in); variables[i] = (Variable) SerializationHelper.deserialize(in);
} }
return new DebugResponseVariables(variables); return new DebugResponseVariables(variables, type);
} }
} }

View File

@@ -2,92 +2,86 @@ package org.luaj.debug;
import java.io.IOException; import java.io.IOException;
import junit.framework.TestCase;
import org.luaj.debug.event.DebugEventType;
import org.luaj.debug.response.DebugResponseCallgraph; import org.luaj.debug.response.DebugResponseCallgraph;
import org.luaj.debug.response.DebugResponseSimple; import org.luaj.debug.response.DebugResponseStack;
import org.luaj.debug.response.DebugResponseVariables; import org.luaj.debug.response.DebugResponseVariables;
import org.luaj.vm.Lua; import org.luaj.vm.Lua;
import junit.framework.TestCase;
public class DebugResponseTest extends TestCase { public class DebugResponseTest extends TestCase {
public void testDebugResponseSimpleSerialization() {
try {
DebugResponseSimple responseIn = DebugResponseSimple.SUCCESS;
byte[] data = SerializationHelper.serialize(responseIn);
DebugResponseSimple responseOut
= (DebugResponseSimple)SerializationHelper.deserialize(data);
assertEquals(responseIn, responseOut);
} catch (IOException e) {
fail(e.getMessage());
}
}
public void testDebugResponseStackSerialization() {
try {
doTestDebugResponseStackSerialization(null);
doTestDebugResponseStackSerialization(new Variable[0]);
Variable[] variables = new Variable[5];
variables[0] = new Variable(0, "variable1", Lua.LUA_TSTRING, "value1");
variables[1] = new Variable(1, "variable2", Lua.LUA_TNIL, "nil");
variables[2] = new Variable(2, "variable3", Lua.LUA_TBOOLEAN, "false");
TableVariable childTable = new TableVariable(0, "child", Lua.LUA_TTABLE, new String[0], new Object[0]); public void testDebugResponseStackSerialization() {
String[] keys = new String[] {"key1", "key2"}; try {
Object[] values = new Object[] {"value1", childTable}; doTestDebugResponseStackSerialization(0, null);
variables[3] = new TableVariable(2, "variable4", Lua.LUA_TTABLE, keys, values);
variables[4] = new Variable(2, "variable3", Lua.LUA_TNUMBER, "10");
doTestDebugResponseStackSerialization(variables);
} catch (IOException e) {
fail(e.getMessage());
}
}
private void doTestDebugResponseStackSerialization(Variable[] variables) doTestDebugResponseStackSerialization(1, new Variable[0]);
throws IOException {
DebugResponseVariables stackIn = new DebugResponseVariables(variables); Variable[] variables = new Variable[5];
byte[] data = SerializationHelper.serialize(stackIn); variables[0] = new Variable(0, "variable1", Lua.LUA_TSTRING,
DebugResponseVariables stackOut "value1");
= (DebugResponseVariables) SerializationHelper.deserialize(data); variables[1] = new Variable(1, "variable2", Lua.LUA_TNIL, "nil");
Variable[] variablesIn = stackIn.getVariables(); variables[2] = new Variable(2, "variable3", Lua.LUA_TBOOLEAN,
Variable[] variablesOut = stackOut.getVariables(); "false");
assertNotNull(variablesIn);
assertNotNull(variablesOut); TableVariable childTable = new TableVariable(0, "child",
assertEquals(variablesIn.length, variablesOut.length); Lua.LUA_TTABLE, new String[0], new Object[0]);
for (int i = 0; i < variablesIn.length; i++) { String[] keys = new String[] { "key1", "key2" };
assertEquals(variablesIn[i], variablesOut[i]); Object[] values = new Object[] { "value1", childTable };
} variables[3] = new TableVariable(2, "variable4", Lua.LUA_TTABLE,
} keys, values);
public void testDebugResponseCallgraphSerialization() { variables[4] = new Variable(2, "variable3", Lua.LUA_TNUMBER, "10");
try { doTestDebugResponseStackSerialization(2, variables);
doTestDebugResponseCallgraphSerialization(null); } catch (IOException e) {
doTestDebugResponseCallgraphSerialization(new StackFrame[0]); fail(e.getMessage());
}
StackFrame[] frames = new StackFrame[1]; }
frames[0] = new StackFrame(100, "test.lua");
doTestDebugResponseCallgraphSerialization(frames); private void doTestDebugResponseStackSerialization(int index, Variable[] variables)
} catch (IOException e) { throws IOException {
fail(e.getMessage()); DebugResponseStack stackIn = new DebugResponseStack(index, variables);
} byte[] data = SerializationHelper.serialize(stackIn);
} DebugResponseStack stackOut = (DebugResponseStack) SerializationHelper
.deserialize(data);
private void doTestDebugResponseCallgraphSerialization(StackFrame[] frames) Variable[] variablesIn = stackIn.getVariables();
throws IOException { Variable[] variablesOut = stackOut.getVariables();
DebugResponseCallgraph responseIn = new DebugResponseCallgraph(frames); assertNotNull(variablesIn);
byte[] data = SerializationHelper.serialize(responseIn); assertNotNull(variablesOut);
DebugResponseCallgraph responseOut = assertEquals(stackIn.getIndex(), stackOut.getIndex());
(DebugResponseCallgraph) SerializationHelper.deserialize(data); assertEquals(variablesIn.length, variablesOut.length);
assertNotNull(responseOut); for (int i = 0; i < variablesIn.length; i++) {
StackFrame[] inFrames = responseIn.getCallgraph(); assertEquals(variablesIn[i], variablesOut[i]);
StackFrame[] outFrames = responseOut.getCallgraph(); }
assertNotNull(outFrames); }
assertEquals(inFrames.length, outFrames.length);
for (int i = 0; i < inFrames.length; i++) { public void testDebugResponseCallgraphSerialization() {
assertEquals(inFrames[i], outFrames[i]); try {
} doTestDebugResponseCallgraphSerialization(null);
} doTestDebugResponseCallgraphSerialization(new StackFrame[0]);
StackFrame[] frames = new StackFrame[1];
frames[0] = new StackFrame(100, "test.lua");
doTestDebugResponseCallgraphSerialization(frames);
} catch (IOException e) {
fail(e.getMessage());
}
}
private void doTestDebugResponseCallgraphSerialization(StackFrame[] frames)
throws IOException {
DebugResponseCallgraph responseIn = new DebugResponseCallgraph(frames);
byte[] data = SerializationHelper.serialize(responseIn);
DebugResponseCallgraph responseOut = (DebugResponseCallgraph) SerializationHelper
.deserialize(data);
assertNotNull(responseOut);
StackFrame[] inFrames = responseIn.getCallgraph();
StackFrame[] outFrames = responseOut.getCallgraph();
assertNotNull(outFrames);
assertEquals(inFrames.length, outFrames.length);
for (int i = 0; i < inFrames.length; i++) {
assertEquals(inFrames[i], outFrames[i]);
}
}
} }

View File

@@ -1,24 +1,24 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2007 LuaJ. All rights reserved. * Copyright (c) 2007 LuaJ. All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* 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 org.luaj.debug.j2se; package org.luaj.debug.j2se;
import java.io.IOException; import java.io.IOException;
@@ -33,135 +33,172 @@ import junit.framework.TestCase;
* Sanity test for StandardLuaJVM. * Sanity test for StandardLuaJVM.
*/ */
public class LuaJVMTest extends TestCase { public class LuaJVMTest extends TestCase {
public void testCommandLineParse() { public void testCommandLineParse() {
// null arguments // null arguments
String[] args = null; String[] args = null;
StandardLuaJVM vm = new StandardLuaJVM(); StandardLuaJVM vm = new StandardLuaJVM();
try { try {
vm.parse(args); vm.parse(args);
fail("Bad parsing program. Should never reach this line."); fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {} } catch (ParseException e) {}
// empty arguments
args = new String[] {};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// incomplete arguments // empty arguments
args = new String[] { "-debug" }; args = new String[] {};
try { vm = new StandardLuaJVM();
vm.parse(args); try {
fail("Bad parsing program. Should never reach this line."); vm.parse(args);
} catch (ParseException e) {} fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// incomplete arguments
args = new String[] { "-debug", "1046" };
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// missing script name
args = new String[] { "-debug", "1046", "1047"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// malformed request port // incomplete arguments
args = new String[] { "-debug", "104x", "1046", "dummy.lua"}; args = new String[] { "-D" };
try { vm = new StandardLuaJVM();
vm.parse(args); try {
fail("Bad parsing program. Should never reach this line."); vm.parse(args);
} catch (ParseException e) {} fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// malformed event port args = new String[] { "-D1046" };
args = new String[] { "-debug", "1046", "104x", "dummy.lua"}; vm = new StandardLuaJVM();
try { try {
vm.parse(args); vm.parse(args);
fail("Bad parsing program. Should never reach this line."); fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {} } catch (ParseException e) {}
// event port == request port
args = new String[] { "-debug", "1046", "1046", "dummy.lua"};
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// lua script cannot be found args = new String[] { "-DsuspendOnStart=true" };
args = new String[] { "-debug", "1046", "1047", "dummy.lua"}; vm = new StandardLuaJVM();
try { try {
vm.parse(args); vm.parse(args);
vm.run(); fail("Bad parsing program. Should never reach this line.");
fail("Should never reach this line."); } catch (ParseException e) {}
} catch (ParseException e) {
fail("Should never reach this line."); // invalid debug option format
} catch (IOException e) { args = new String[] { "-Dport=1044:suspendOnStart=true", "dummy.lua" };
//expected vm = new StandardLuaJVM();
} try {
vm.parse(args);
// lua script cannot be found assertFalse(1044 == vm.getDebugPort());
args = new String[] {"dummy.lua"}; assertFalse(true == vm.getSuspendOnStart());
try { assertEquals("dummy.lua", vm.getScript());
vm.parse(args); } catch (ParseException e) {
vm.run(); //expected
fail("Bad parsing program. Should never reach this line."); }
} catch (ParseException e) {
fail("Should never reach this line."); args = new String[] { "-Dport=1044,suspendOnStart=xyz", "dummy.lua" };
} catch (IOException e) { vm = new StandardLuaJVM();
//expected try {
} vm.parse(args);
} assertFalse(1044 == vm.getDebugPort());
assertFalse(true == vm.getSuspendOnStart());
public void testRun() { assertEquals("dummy.lua", vm.getScript());
String[] tests = new String[] { } catch (ParseException e) {
"autoload", //expected
"boolean", }
"calls",
"coercions",
"compare",
"math",
"mathlib",
"metatables",
"select",
"setlist",
"swingapp",
"test1",
"test2",
"test3",
"test4",
"test5",
"test6",
"test7",
"type",
"upvalues",
//"strlib"
};
// missing script name
args = new String[] { "-Dport=1047,suspendOnStart=true"};
vm = new StandardLuaJVM();
try {
vm.parse(args);
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {}
// lua script cannot be found
args = new String[] { "-Dport=1046,suspendOnStart", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
vm.run();
fail("Should never reach this line.");
} catch (ParseException e) {}
catch (IOException e) {}
// lua script cannot be found
args = new String[] { "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
vm.run();
fail("Bad parsing program. Should never reach this line.");
} catch (ParseException e) {
fail("Should never reach this line.");
} catch (IOException e) {}
// valid command line
args = new String[] { "-Dport=1044,suspendOnStart=true", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals(1044, vm.getDebugPort());
assertEquals(true, vm.getSuspendOnStart());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
args = new String[] { "-DsuspendOnStart=true,port=1044", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals(1044, vm.getDebugPort());
assertEquals(true, vm.getSuspendOnStart());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
args = new String[] { "-DsuspendOnStart=TRUE,port=1044", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals(1044, vm.getDebugPort());
assertEquals(true, vm.getSuspendOnStart());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
args = new String[] { "-Dport=1044", "dummy.lua" };
vm = new StandardLuaJVM();
try {
vm.parse(args);
assertEquals(1044, vm.getDebugPort());
assertEquals(false, vm.getSuspendOnStart());
assertEquals("dummy.lua", vm.getScript());
} catch (ParseException e) {
fail("Should never reach this line.");
}
}
public void testRun() {
String[] tests = new String[] { "autoload", "boolean", "calls",
"coercions", "compare", "math", "mathlib", "metatables",
"select", "setlist", "swingapp", "test1", "test2", "test3",
"test4", "test5", "test6", "test7", "type", "upvalues",
// "strlib"
};
for (int i = 0; i < tests.length; i++) { for (int i = 0; i < tests.length; i++) {
String test = tests[i]; String test = tests[i];
System.out.println("==> running test: " + test + ".lua"); System.out.println("==> running test: " + test + ".lua");
doTestRun(test + ".lua"); doTestRun(test + ".lua");
System.out.println("==> running test: " + test + ".luac"); System.out.println("==> running test: " + test + ".luac");
doTestRun(test + ".luac"); doTestRun(test + ".luac");
System.out.println(); System.out.println();
} }
} }
protected void doTestRun(String testName) { protected void doTestRun(String testName) {
String[] args = new String[1]; String[] args = new String[1];
URL filePath = getClass().getResource("/"+ testName); URL filePath = getClass().getResource("/" + testName);
if (filePath != null) { if (filePath != null) {
args[0] = filePath.getPath(); args[0] = filePath.getPath();
try { try {
StandardLuaJVM.main(args); StandardLuaJVM.main(args);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail("Test " + testName + " failed due to " + e.getMessage()); fail("Test " + testName + " failed due to " + e.getMessage());
} }
} }
} }
} }