| package org.eclipse.e4.languages.javascript.debug.rhino; |
| |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.LinkedList; |
| import java.util.List; |
| import java.util.Map; |
| |
| import org.mozilla.javascript.Context; |
| import org.mozilla.javascript.debug.DebugFrame; |
| import org.mozilla.javascript.debug.DebuggableScript; |
| |
| public class ThreadData { |
| |
| private final LinkedList contexts = new LinkedList(); |
| private final Long threadId; |
| private final RhinoDebugger debugger; |
| |
| private long currentContextId = 0L; |
| private long currentFrameId = 0L; |
| |
| public ThreadData(Long threadId, RhinoDebugger debugger) { |
| this.threadId = threadId; |
| this.debugger = debugger; |
| } |
| |
| public synchronized Object toJSON() { |
| Map result = new HashMap(); |
| result.put("threadId", threadId); |
| Context top = (Context) contexts.getFirst(); |
| result.put("state", top == null ? "running" : ((ContextData) top.getDebuggerContextData()).getState()); |
| ArrayList contextIds = new ArrayList(contexts.size()); |
| for (Iterator iterator = contexts.iterator(); iterator.hasNext();) { |
| Context context = (Context) iterator.next(); |
| ContextData data = (ContextData) context.getDebuggerContextData(); |
| contextIds.add(data.getId()); |
| } |
| result.put("contexts", contextIds); |
| return result; |
| } |
| |
| public synchronized void contextCreated(Context context) { |
| context.setGeneratingDebug(true); |
| context.setOptimizationLevel(-1); |
| ContextData contextData = new ContextData(threadId, new Long(currentContextId++), debugger); |
| context.setDebugger(debugger, contextData); |
| contexts.push(context); |
| } |
| |
| public synchronized void contextReleased(Context context) { |
| contexts.pop(); |
| } |
| |
| public synchronized boolean hasContext() { |
| return !contexts.isEmpty(); |
| } |
| |
| public synchronized List getFrameIds() { |
| ArrayList result = new ArrayList(contexts.size()); |
| for (Iterator iterator = contexts.iterator(); iterator.hasNext();) { |
| Context context = (Context) iterator.next(); |
| ContextData data = (ContextData) context.getDebuggerContextData(); |
| result.addAll(data.getFrameIds()); |
| } |
| return result; |
| } |
| |
| public synchronized void suspend() { |
| Context context = (Context) contexts.getFirst(); |
| if (context == null) |
| return; |
| ContextData data = (ContextData) context.getDebuggerContextData(); |
| data.suspend(); |
| } |
| |
| public synchronized void resume(String stepType) { |
| Context context = (Context) contexts.getFirst(); |
| if (context == null) |
| return; |
| ContextData data = (ContextData) context.getDebuggerContextData(); |
| data.resume(stepType); |
| } |
| |
| public DebugFrameImpl getFrame(Long frameId) { |
| for (Iterator iterator = contexts.iterator(); iterator.hasNext();) { |
| Context context = (Context) iterator.next(); |
| ContextData data = (ContextData) context.getDebuggerContextData(); |
| DebugFrameImpl frame = data.getFrame(frameId); |
| if (frame != null) |
| return frame; |
| } |
| return null; |
| } |
| |
| public synchronized DebugFrame getFrame(Context context, DebuggableScript debuggableScript, ScriptImpl script) { |
| return new DebugFrameImpl(new Long(currentFrameId++), context, debuggableScript, script); |
| } |
| } |