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.DebugResponseCallgraph;
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.LClosure;
import org.luaj.vm.LTable;
@@ -338,7 +338,9 @@ public class DebugStackState extends LuaState implements DebugRequestListener {
} else if (DebugRequestType.stack == requestType) {
DebugRequestStack stackRequest = (DebugRequestStack) request;
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) {
DebugEvent event = new DebugEvent(
DebugEventType.resumedOnSteppingInto);
@@ -485,7 +487,44 @@ public class DebugStackState extends LuaState implements DebugRequestListener {
Vector variables = new Vector();
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()];
for (int i = 0; i < variables.size(); i++) {

View File

@@ -16,8 +16,7 @@ import org.luaj.debug.request.DebugRequestStack;
import org.luaj.debug.request.DebugRequestType;
import org.luaj.debug.response.DebugResponseCallgraph;
import org.luaj.debug.response.DebugResponseSimple;
import org.luaj.debug.response.DebugResponseStack;
import org.luaj.debug.response.DebugResponseVariables;
public class SerializationHelper {
@@ -47,12 +46,11 @@ public class SerializationHelper {
return object;
}
static final int SERIAL_TYPE_NullableString = 0;
static final int SERIAL_TYPE_TableVariable = 1;
static final int SERIAL_TYPE_Variable = 2;
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_StackFrame = 6;
static final int SERIAL_TYPE_DebugRequestType = 7;
@@ -68,52 +66,56 @@ public class SerializationHelper {
throws IOException {
if (object instanceof NullableString) {
dout.writeInt(SERIAL_TYPE_NullableString);
NullableString.serialize(dout, (NullableString)object);
NullableString.serialize(dout, (NullableString) object);
} else if (object instanceof TableVariable) {
dout.writeInt(SERIAL_TYPE_TableVariable);
TableVariable.serialize(dout, (TableVariable)object);
TableVariable.serialize(dout, (TableVariable) object);
} else if (object instanceof Variable) {
dout.writeInt(SERIAL_TYPE_Variable);
Variable.serialize(dout, (Variable)object);
Variable.serialize(dout, (Variable) object);
} else if (object instanceof StackFrame) {
dout.writeInt(SERIAL_TYPE_StackFrame);
StackFrame.serialize(dout, (StackFrame)object);
StackFrame.serialize(dout, (StackFrame) object);
} else if (object instanceof DebugResponseSimple) {
dout.writeInt(SERIAL_TYPE_DebugResponseSimple);
DebugResponseSimple.serialize(dout, (DebugResponseSimple)object);
} else if (object instanceof DebugResponseStack) {
dout.writeInt(SERIAL_TYPE_DebugResponseStack);
DebugResponseStack.serialize(dout, (DebugResponseStack)object);
DebugResponseSimple.serialize(dout, (DebugResponseSimple) object);
} else if (object instanceof DebugResponseVariables) {
dout.writeInt(SERIAL_TYPE_DebugResponseVariables);
DebugResponseVariables.serialize(dout, (DebugResponseVariables) object);
} else if (object instanceof DebugResponseCallgraph) {
dout.writeInt(SERIAL_TYPE_DebugResponseCallgraph);
DebugResponseCallgraph.serialize(dout, (DebugResponseCallgraph)object);
DebugResponseCallgraph.serialize(dout,
(DebugResponseCallgraph) object);
} else if (object instanceof DebugRequestType) {
dout.writeInt(SERIAL_TYPE_DebugRequestType);
DebugRequestType.serialize(dout, (DebugRequestType)object);
DebugRequestType.serialize(dout, (DebugRequestType) object);
} else if (object instanceof DebugRequestStack) {
dout.writeInt(SERIAL_TYPE_DebugRequestStack);
DebugRequestStack.serialize(dout, (DebugRequestStack)object);
DebugRequestStack.serialize(dout, (DebugRequestStack) object);
} else if (object instanceof DebugRequestLineBreakpointToggle) {
dout.writeInt(SERIAL_TYPE_DebugRequestLineBreakpointToggle);
DebugRequestLineBreakpointToggle.serialize(dout, (DebugRequestLineBreakpointToggle)object);
DebugRequestLineBreakpointToggle.serialize(dout,
(DebugRequestLineBreakpointToggle) object);
} else if (object instanceof DebugRequest) {
dout.writeInt(SERIAL_TYPE_DebugRequest);
DebugRequest.serialize(dout, (DebugRequest)object);
DebugRequest.serialize(dout, (DebugRequest) object);
} else if (object instanceof DebugEventType) {
dout.writeInt(SERIAL_TYPE_DebugEventType);
DebugEventType.serialize(dout, (DebugEventType)object);
DebugEventType.serialize(dout, (DebugEventType) object);
} else if (object instanceof DebugEventBreakpoint) {
dout.writeInt(SERIAL_TYPE_DebugEventBreakpoint);
DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint)object);
DebugEventBreakpoint.serialize(dout, (DebugEventBreakpoint) object);
} else if (object instanceof DebugEventError) {
dout.writeInt(SERIAL_TYPE_DebugEventError);
DebugEventError.serialize(dout, (DebugEventError)object);
DebugEventError.serialize(dout, (DebugEventError) object);
} else if (object instanceof DebugEvent) {
dout.writeInt(SERIAL_TYPE_DebugEvent);
DebugEvent.serialize(dout, (DebugEvent)object);
DebugEvent.serialize(dout, (DebugEvent) object);
} else {
// catch the errors: forgot to implement serialization/deserialization
throw new RuntimeException("serialization operation is not supported");
// catch the errors: forgot to implement
// serialization/deserialization
throw new RuntimeException(
"serialization operation is not supported");
}
}
@@ -140,8 +142,8 @@ public class SerializationHelper {
case SERIAL_TYPE_DebugResponseCallgraph:
object = DebugResponseCallgraph.deserialize(din);
break;
case SERIAL_TYPE_DebugResponseStack:
object = DebugResponseStack.deserialize(din);
case SERIAL_TYPE_DebugResponseVariables:
object = DebugResponseVariables.deserialize(din);
break;
case SERIAL_TYPE_DebugRequestType:
object = DebugRequestType.deserialize(din);
@@ -168,7 +170,8 @@ public class SerializationHelper {
object = DebugEvent.deserialize(din);
break;
default:
throw new RuntimeException("deserialization operation is not supported");
throw new RuntimeException(
"deserialization operation is not supported");
}
return object;

View File

@@ -42,6 +42,7 @@ public class DebugRequestType extends EnumType {
public static final DebugRequestType stepInto = new DebugRequestType("stepInto", 10);
public static final DebugRequestType stepOver = new DebugRequestType("stepOver", 11);
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[] {
start,
@@ -56,7 +57,8 @@ public class DebugRequestType extends EnumType {
stack,
stepInto,
stepOver,
stepReturn
stepReturn,
global
};
public DebugRequestType(String name, int ordinal) {

View File

@@ -28,11 +28,10 @@ import java.io.IOException;
import org.luaj.debug.SerializationHelper;
import org.luaj.debug.Variable;
public class DebugResponseStack implements DebugResponse {
public class DebugResponseVariables implements DebugResponse {
protected Variable[] variables;
public DebugResponseStack(Variable[] variables) {
public DebugResponseVariables(Variable[] variables) {
if (variables == null) {
this.variables = new Variable[0];
} else {
@@ -55,7 +54,8 @@ public class DebugResponseStack implements DebugResponse {
return buffer.toString();
}
public static void serialize(DataOutputStream out, DebugResponseStack response)
public static void serialize(DataOutputStream out,
DebugResponseVariables response)
throws IOException {
Variable[] variables = response.getVariables();
out.writeInt(variables == null ? 0 : variables.length);
@@ -64,12 +64,13 @@ public class DebugResponseStack implements DebugResponse {
}
}
public static DebugResponseStack deserialize(DataInputStream in) throws IOException {
public static DebugResponseVariables deserialize(DataInputStream in)
throws IOException {
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(variables);
return new DebugResponseVariables(variables);
}
}

View File

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