re-apply my last two check-ins before the code structure refactoring:

(1) added getGlobals to returns the globals for Globals view in Eclipse
(2) fixed the scoping issue with stack state
(3) renamed DebugResponseStack to DebugResponseVariables
This commit is contained in:
Shu Lei
2007-11-08 01:55:27 +00:00
parent ff66779330
commit 25c008dab6
5 changed files with 244 additions and 202 deletions

View File

@@ -40,7 +40,7 @@ import org.luaj.debug.request.DebugRequestType;
import org.luaj.debug.response.DebugResponse; 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.DebugResponseSimple;
import org.luaj.debug.response.DebugResponseStack; 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;
import org.luaj.vm.LTable; import org.luaj.vm.LTable;
@@ -338,7 +338,9 @@ public class DebugStackState extends LuaState implements DebugRequestListener {
} 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 DebugResponseStack(getStack(index)); return new DebugResponseVariables(getStack(index));
} else if (DebugRequestType.global == requestType) {
return new DebugResponseVariables(getGlobals());
} else if (DebugRequestType.stepInto == requestType) { } else if (DebugRequestType.stepInto == requestType) {
DebugEvent event = new DebugEvent( DebugEvent event = new DebugEvent(
DebugEventType.resumedOnSteppingInto); DebugEventType.resumedOnSteppingInto);
@@ -485,7 +487,44 @@ public class DebugStackState extends LuaState implements DebugRequestListener {
Vector variables = new Vector(); Vector variables = new Vector();
Hashtable variablesSeen = new Hashtable(); Hashtable variablesSeen = new Hashtable();
addVariables(variables, variablesSeen, index); LPrototype p = calls[index].closure.p;
for (int i = index; i >= 0; i--) {
if (i == index || isInScope(p, calls[i])) {
addVariables(variables, variablesSeen, i);
}
}
Variable[] result = new Variable[variables.size()];
for (int i = 0; i < variables.size(); i++) {
result[i] = (Variable) variables.elementAt(i);
}
return result;
}
protected boolean isInScope(LPrototype p, CallInfo ci) {
LPrototype[] enclosingProtos = ci.closure.p.p;
boolean bFound = false;
for (int i = 0; enclosingProtos!= null && i < enclosingProtos.length; i++) {
if (enclosingProtos[i] == p) {
bFound = true;
break;
}
}
return bFound;
}
/**
* Returns the visible globals to the current VM.
* @return the visible globals.
*/
public Variable[] getGlobals() {
Vector variables = new Vector();
variables.addElement(
new TableVariable(0,
"*Globals*",
Lua.LUA_TTABLE,
(LTable) _G));
Variable[] result = new Variable[variables.size()]; Variable[] result = new Variable[variables.size()];
for (int i = 0; i < variables.size(); i++) { for (int i = 0; i < variables.size(); i++) {

View File

@@ -16,161 +16,164 @@ 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.DebugResponseSimple;
import org.luaj.debug.response.DebugResponseStack; import org.luaj.debug.response.DebugResponseVariables;
public class SerializationHelper { public class SerializationHelper {
public static byte[] serialize(Serializable object) throws IOException { public static byte[] serialize(Serializable object) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout); DataOutputStream dout = new DataOutputStream(bout);
serialize(object, dout); serialize(object, dout);
byte[] data = bout.toByteArray(); byte[] data = bout.toByteArray();
bout.close(); bout.close();
dout.close(); dout.close();
return data; return data;
} }
public static Serializable deserialize(byte[] data) throws IOException { public static Serializable deserialize(byte[] data) throws IOException {
ByteArrayInputStream bin = new ByteArrayInputStream(data); ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin); DataInputStream din = new DataInputStream(bin);
Serializable object = deserialize(din); Serializable object = deserialize(din);
bin.close(); bin.close();
din.close(); din.close();
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_DebugResponseStack = 4; static final int SERIAL_TYPE_DebugResponseVariables = 4;
static final int SERIAL_TYPE_DebugResponseSimple = 5; static final int SERIAL_TYPE_DebugResponseSimple = 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 {
if (object instanceof NullableString) { if (object instanceof NullableString) {
dout.writeInt(SERIAL_TYPE_NullableString); dout.writeInt(SERIAL_TYPE_NullableString);
NullableString.serialize(dout, (NullableString)object); NullableString.serialize(dout, (NullableString) object);
} else if (object instanceof TableVariable) { } else if (object instanceof TableVariable) {
dout.writeInt(SERIAL_TYPE_TableVariable); dout.writeInt(SERIAL_TYPE_TableVariable);
TableVariable.serialize(dout, (TableVariable)object); TableVariable.serialize(dout, (TableVariable) object);
} else if (object instanceof Variable) { } else if (object instanceof Variable) {
dout.writeInt(SERIAL_TYPE_Variable); dout.writeInt(SERIAL_TYPE_Variable);
Variable.serialize(dout, (Variable)object); Variable.serialize(dout, (Variable) object);
} 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 DebugResponseSimple) {
dout.writeInt(SERIAL_TYPE_DebugResponseSimple); dout.writeInt(SERIAL_TYPE_DebugResponseSimple);
DebugResponseSimple.serialize(dout, (DebugResponseSimple)object); DebugResponseSimple.serialize(dout, (DebugResponseSimple) object);
} else if (object instanceof DebugResponseStack) { } else if (object instanceof DebugResponseVariables) {
dout.writeInt(SERIAL_TYPE_DebugResponseStack); dout.writeInt(SERIAL_TYPE_DebugResponseVariables);
DebugResponseStack.serialize(dout, (DebugResponseStack)object); DebugResponseVariables.serialize(dout, (DebugResponseVariables) object);
} 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,
} else if (object instanceof DebugRequestType) { (DebugResponseCallgraph) object);
dout.writeInt(SERIAL_TYPE_DebugRequestType); } else if (object instanceof DebugRequestType) {
DebugRequestType.serialize(dout, (DebugRequestType)object); dout.writeInt(SERIAL_TYPE_DebugRequestType);
} else if (object instanceof DebugRequestStack) { DebugRequestType.serialize(dout, (DebugRequestType) object);
dout.writeInt(SERIAL_TYPE_DebugRequestStack); } else if (object instanceof DebugRequestStack) {
DebugRequestStack.serialize(dout, (DebugRequestStack)object); dout.writeInt(SERIAL_TYPE_DebugRequestStack);
} else if (object instanceof DebugRequestLineBreakpointToggle) { DebugRequestStack.serialize(dout, (DebugRequestStack) object);
dout.writeInt(SERIAL_TYPE_DebugRequestLineBreakpointToggle); } else if (object instanceof DebugRequestLineBreakpointToggle) {
DebugRequestLineBreakpointToggle.serialize(dout, (DebugRequestLineBreakpointToggle)object); dout.writeInt(SERIAL_TYPE_DebugRequestLineBreakpointToggle);
} else if (object instanceof DebugRequest) { DebugRequestLineBreakpointToggle.serialize(dout,
dout.writeInt(SERIAL_TYPE_DebugRequest); (DebugRequestLineBreakpointToggle) object);
DebugRequest.serialize(dout, (DebugRequest)object); } else if (object instanceof DebugRequest) {
} else if (object instanceof DebugEventType) { dout.writeInt(SERIAL_TYPE_DebugRequest);
dout.writeInt(SERIAL_TYPE_DebugEventType); DebugRequest.serialize(dout, (DebugRequest) object);
DebugEventType.serialize(dout, (DebugEventType)object); } else if (object instanceof DebugEventType) {
} else if (object instanceof DebugEventBreakpoint) { dout.writeInt(SERIAL_TYPE_DebugEventType);
dout.writeInt(SERIAL_TYPE_DebugEventBreakpoint); DebugEventType.serialize(dout, (DebugEventType) object);
DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint)object); } else if (object instanceof DebugEventBreakpoint) {
} else if (object instanceof DebugEventError) { dout.writeInt(SERIAL_TYPE_DebugEventBreakpoint);
dout.writeInt(SERIAL_TYPE_DebugEventError); DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint) object);
DebugEventError.serialize(dout, (DebugEventError)object); } else if (object instanceof DebugEventError) {
} else if (object instanceof DebugEvent) { dout.writeInt(SERIAL_TYPE_DebugEventError);
dout.writeInt(SERIAL_TYPE_DebugEvent); DebugEventError.serialize(dout, (DebugEventError) object);
DebugEvent.serialize(dout, (DebugEvent)object); } else if (object instanceof DebugEvent) {
} else { dout.writeInt(SERIAL_TYPE_DebugEvent);
// catch the errors: forgot to implement serialization/deserialization DebugEvent.serialize(dout, (DebugEvent) object);
throw new RuntimeException("serialization operation is not supported"); } else {
} // catch the errors: forgot to implement
} // serialization/deserialization
throw new RuntimeException(
"serialization operation is not supported");
}
}
public static Serializable deserialize(DataInputStream din) public static Serializable deserialize(DataInputStream din)
throws IOException { throws IOException {
Serializable object = null; Serializable object = null;
int type = din.readInt(); int type = din.readInt();
switch (type) { switch (type) {
case SERIAL_TYPE_NullableString: case SERIAL_TYPE_NullableString:
object = NullableString.deserialize(din); object = NullableString.deserialize(din);
break; break;
case SERIAL_TYPE_TableVariable: case SERIAL_TYPE_TableVariable:
object = TableVariable.deserialize(din); object = TableVariable.deserialize(din);
break; break;
case SERIAL_TYPE_Variable: case SERIAL_TYPE_Variable:
object = Variable.deserialize(din); object = Variable.deserialize(din);
break; break;
case SERIAL_TYPE_StackFrame: case SERIAL_TYPE_StackFrame:
object = StackFrame.deserialize(din); object = StackFrame.deserialize(din);
break; break;
case SERIAL_TYPE_DebugResponseSimple: case SERIAL_TYPE_DebugResponseSimple:
object = DebugResponseSimple.deserialize(din); object = DebugResponseSimple.deserialize(din);
break; break;
case SERIAL_TYPE_DebugResponseCallgraph: case SERIAL_TYPE_DebugResponseCallgraph:
object = DebugResponseCallgraph.deserialize(din); object = DebugResponseCallgraph.deserialize(din);
break; break;
case SERIAL_TYPE_DebugResponseStack: case SERIAL_TYPE_DebugResponseVariables:
object = DebugResponseStack.deserialize(din); object = DebugResponseVariables.deserialize(din);
break; break;
case SERIAL_TYPE_DebugRequestType: case SERIAL_TYPE_DebugRequestType:
object = DebugRequestType.deserialize(din); object = DebugRequestType.deserialize(din);
break; break;
case SERIAL_TYPE_DebugRequestStack: case SERIAL_TYPE_DebugRequestStack:
object = DebugRequestStack.deserialize(din); object = DebugRequestStack.deserialize(din);
break; break;
case SERIAL_TYPE_DebugRequestLineBreakpointToggle: case SERIAL_TYPE_DebugRequestLineBreakpointToggle:
object = DebugRequestLineBreakpointToggle.deserialize(din); object = DebugRequestLineBreakpointToggle.deserialize(din);
break; break;
case SERIAL_TYPE_DebugRequest: case SERIAL_TYPE_DebugRequest:
object = DebugRequest.deserialize(din); object = DebugRequest.deserialize(din);
break; break;
case SERIAL_TYPE_DebugEventType: case SERIAL_TYPE_DebugEventType:
object = DebugEventType.deserialize(din); object = DebugEventType.deserialize(din);
break; break;
case SERIAL_TYPE_DebugEventBreakpoint: case SERIAL_TYPE_DebugEventBreakpoint:
object = DebugEventBreakpoint.deserialize(din); object = DebugEventBreakpoint.deserialize(din);
break; break;
case SERIAL_TYPE_DebugEventError: case SERIAL_TYPE_DebugEventError:
object = DebugEventError.deserialize(din); object = DebugEventError.deserialize(din);
break; break;
case SERIAL_TYPE_DebugEvent: case SERIAL_TYPE_DebugEvent:
object = DebugEvent.deserialize(din); object = DebugEvent.deserialize(din);
break; break;
default: default:
throw new RuntimeException("deserialization operation is not supported"); throw new RuntimeException(
} "deserialization operation is not supported");
}
return object; return object;
} }
} }

View File

@@ -31,7 +31,7 @@ import org.luaj.debug.EnumType;
public class DebugRequestType extends EnumType { public class DebugRequestType extends EnumType {
public static final DebugRequestType start = new DebugRequestType("start", 0); public static final DebugRequestType start = new DebugRequestType("start", 0);
public static final DebugRequestType resume = new DebugRequestType("resume", 1); public static final DebugRequestType resume = new DebugRequestType("resume", 1);
public static final DebugRequestType suspend = new DebugRequestType("suspend", 2); public static final DebugRequestType suspend = new DebugRequestType("suspend", 2);
public static final DebugRequestType exit = new DebugRequestType("exit", 3); public static final DebugRequestType exit = new DebugRequestType("exit", 3);
public static final DebugRequestType lineBreakpointSet = new DebugRequestType("lineBreakpointSet", 4); public static final DebugRequestType lineBreakpointSet = new DebugRequestType("lineBreakpointSet", 4);
public static final DebugRequestType lineBreakpointClear = new DebugRequestType("lineBreakpointClear", 5); public static final DebugRequestType lineBreakpointClear = new DebugRequestType("lineBreakpointClear", 5);
@@ -42,32 +42,34 @@ public class DebugRequestType extends EnumType {
public static final DebugRequestType stepInto = new DebugRequestType("stepInto", 10); public static final DebugRequestType stepInto = new DebugRequestType("stepInto", 10);
public static final DebugRequestType stepOver = new DebugRequestType("stepOver", 11); public static final DebugRequestType stepOver = new DebugRequestType("stepOver", 11);
public static final DebugRequestType stepReturn = new DebugRequestType("stepReturn", 12); public static final DebugRequestType stepReturn = new DebugRequestType("stepReturn", 12);
public static final DebugRequestType global = new DebugRequestType("global", 13);
protected static final DebugRequestType[] ENUMS = new DebugRequestType[] { protected static final DebugRequestType[] ENUMS = new DebugRequestType[] {
start, start,
resume, resume,
suspend, suspend,
exit, exit,
lineBreakpointSet, lineBreakpointSet,
lineBreakpointClear, lineBreakpointClear,
watchpointSet, watchpointSet,
watchpointClear, watchpointClear,
callgraph, callgraph,
stack, stack,
stepInto, stepInto,
stepOver, stepOver,
stepReturn stepReturn,
global
}; };
public DebugRequestType(String name, int ordinal) { public DebugRequestType(String name, int ordinal) {
super(name, ordinal); super(name, ordinal);
} }
public static DebugRequestType deserialize(DataInputStream in) throws IOException { public static DebugRequestType deserialize(DataInputStream in) throws IOException {
int ordinal = in.readInt(); int ordinal = in.readInt();
if (ordinal < 0 || ordinal >= ENUMS.length) { if (ordinal < 0 || ordinal >= ENUMS.length) {
throw new RuntimeException("DebugRequestType: ordinal is out of the range."); throw new RuntimeException("DebugRequestType: ordinal is out of the range.");
} }
return ENUMS[ordinal]; return ENUMS[ordinal];
} }
} }

View File

@@ -28,16 +28,15 @@ import java.io.IOException;
import org.luaj.debug.SerializationHelper; import org.luaj.debug.SerializationHelper;
import org.luaj.debug.Variable; import org.luaj.debug.Variable;
public class DebugResponseVariables implements DebugResponse {
public class DebugResponseStack implements DebugResponse {
protected Variable[] variables; protected Variable[] variables;
public DebugResponseStack(Variable[] variables) { public DebugResponseVariables(Variable[] variables) {
if (variables == null) { if (variables == null) {
this.variables = new Variable[0]; this.variables = new Variable[0];
} else { } else {
this.variables = variables; this.variables = variables;
} }
} }
public Variable[] getVariables() { public Variable[] getVariables() {
@@ -55,21 +54,23 @@ public class DebugResponseStack implements DebugResponse {
return buffer.toString(); return buffer.toString();
} }
public static void serialize(DataOutputStream out, DebugResponseStack response) public static void serialize(DataOutputStream out,
DebugResponseVariables response)
throws IOException { throws IOException {
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++) {
SerializationHelper.serialize(variables[i], out); SerializationHelper.serialize(variables[i], out);
} }
} }
public static DebugResponseStack deserialize(DataInputStream in) throws IOException { public static DebugResponseVariables deserialize(DataInputStream in)
int count = in.readInt(); throws IOException {
Variable[] variables = new Variable[count]; int count = in.readInt();
for (int i = 0; i < count; i++) { Variable[] variables = new Variable[count];
variables[i] = (Variable) SerializationHelper.deserialize(in); for (int i = 0; i < count; i++) {
} variables[i] = (Variable) SerializationHelper.deserialize(in);
return new DebugResponseStack(variables); }
} return new DebugResponseVariables(variables);
}
} }

View File

@@ -2,13 +2,10 @@ package org.luaj.debug;
import java.io.IOException; import java.io.IOException;
import org.luaj.debug.SerializationHelper;
import org.luaj.debug.StackFrame;
import org.luaj.debug.TableVariable;
import org.luaj.debug.Variable;
import org.luaj.debug.response.DebugResponseCallgraph; import org.luaj.debug.response.DebugResponseCallgraph;
import org.luaj.debug.response.DebugResponseSimple; import org.luaj.debug.response.DebugResponseSimple;
import org.luaj.debug.response.DebugResponseStack; import org.luaj.debug.response.DebugResponseVariables;
import org.luaj.vm.Lua; import org.luaj.vm.Lua;
import junit.framework.TestCase; import junit.framework.TestCase;
@@ -51,10 +48,10 @@ public class DebugResponseTest extends TestCase {
private void doTestDebugResponseStackSerialization(Variable[] variables) private void doTestDebugResponseStackSerialization(Variable[] variables)
throws IOException { throws IOException {
DebugResponseStack stackIn = new DebugResponseStack(variables); DebugResponseVariables stackIn = new DebugResponseVariables(variables);
byte[] data = SerializationHelper.serialize(stackIn); byte[] data = SerializationHelper.serialize(stackIn);
DebugResponseStack stackOut DebugResponseVariables stackOut
= (DebugResponseStack) SerializationHelper.deserialize(data); = (DebugResponseVariables) SerializationHelper.deserialize(data);
Variable[] variablesIn = stackIn.getVariables(); Variable[] variablesIn = stackIn.getVariables();
Variable[] variablesOut = stackOut.getVariables(); Variable[] variablesOut = stackOut.getVariables();
assertNotNull(variablesIn); assertNotNull(variablesIn);