Home | History | Annotate | Download | only in v8
      1 /*
      2  * Copyright (c) 2010, Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef ScriptDebugServer_h
     32 #define ScriptDebugServer_h
     33 
     34 
     35 #include "InspectorBackendDispatcher.h"
     36 #include "bindings/v8/ScopedPersistent.h"
     37 #include "core/inspector/ScriptBreakpoint.h"
     38 #include "core/inspector/ScriptDebugListener.h"
     39 #include <v8-debug.h>
     40 #include "wtf/HashMap.h"
     41 #include "wtf/Noncopyable.h"
     42 #include "wtf/PassOwnPtr.h"
     43 #include "wtf/Vector.h"
     44 #include "wtf/text/StringHash.h"
     45 #include "wtf/text/WTFString.h"
     46 
     47 namespace WebCore {
     48 
     49 class ScriptDebugListener;
     50 class ScriptObject;
     51 class ScriptState;
     52 class ScriptValue;
     53 class JavaScriptCallFrame;
     54 
     55 class ScriptDebugServer {
     56     WTF_MAKE_NONCOPYABLE(ScriptDebugServer);
     57 public:
     58     String setBreakpoint(const String& sourceID, const ScriptBreakpoint&, int* actualLineNumber, int* actualColumnNumber, bool interstatementLocation);
     59     void removeBreakpoint(const String& breakpointId);
     60     void clearBreakpoints();
     61     void setBreakpointsActivated(bool activated);
     62 
     63     enum PauseOnExceptionsState {
     64         DontPauseOnExceptions,
     65         PauseOnAllExceptions,
     66         PauseOnUncaughtExceptions
     67     };
     68     PauseOnExceptionsState pauseOnExceptionsState();
     69     void setPauseOnExceptionsState(PauseOnExceptionsState pauseOnExceptionsState);
     70 
     71     void setPauseOnNextStatement(bool pause);
     72     bool canBreakProgram();
     73     void breakProgram();
     74     void continueProgram();
     75     void stepIntoStatement();
     76     void stepOverStatement();
     77     void stepOutOfFunction();
     78 
     79     bool setScriptSource(const String& sourceID, const String& newContent, bool preview, String* error, RefPtr<TypeBuilder::Debugger::SetScriptSourceError>&, ScriptValue* newCallFrames, ScriptObject* result);
     80     void updateCallStack(ScriptValue* callFrame);
     81 
     82     void setScriptPreprocessor(const String& preprocessorBody);
     83 
     84     class Task {
     85     public:
     86         virtual ~Task() { }
     87         virtual void run() = 0;
     88     };
     89     static void interruptAndRun(PassOwnPtr<Task>, v8::Isolate*);
     90     void runPendingTasks();
     91 
     92     bool isPaused();
     93     bool runningNestedMessageLoop() { return m_runningNestedMessageLoop; }
     94 
     95     v8::Local<v8::Value> functionScopes(v8::Handle<v8::Function>);
     96     v8::Local<v8::Value> getInternalProperties(v8::Handle<v8::Object>&);
     97     v8::Handle<v8::Value> setFunctionVariableValue(v8::Handle<v8::Value> functionValue, int scopeNumber, const String& variableName, v8::Handle<v8::Value> newValue);
     98 
     99 
    100     virtual void compileScript(ScriptState*, const String& expression, const String& sourceURL, String* scriptId, String* exceptionMessage);
    101     virtual void clearCompiledScripts();
    102     virtual void runScript(ScriptState*, const String& scriptId, ScriptValue* result, bool* wasThrown, String* exceptionMessage);
    103 
    104 protected:
    105     explicit ScriptDebugServer(v8::Isolate*);
    106     virtual ~ScriptDebugServer();
    107 
    108     ScriptValue currentCallFrame();
    109 
    110     virtual ScriptDebugListener* getDebugListenerForContext(v8::Handle<v8::Context>) = 0;
    111     virtual void runMessageLoopOnPause(v8::Handle<v8::Context>) = 0;
    112     virtual void quitMessageLoopOnPause() = 0;
    113 
    114     static void breakProgramCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
    115     void handleProgramBreak(v8::Handle<v8::Object> executionState, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpoints);
    116     void handleProgramBreak(const v8::Debug::EventDetails&, v8::Handle<v8::Value> exception, v8::Handle<v8::Array> hitBreakpointNumbers);
    117 
    118     static void v8DebugEventCallback(const v8::Debug::EventDetails& eventDetails);
    119     void handleV8DebugEvent(const v8::Debug::EventDetails& eventDetails);
    120 
    121     void dispatchDidParseSource(ScriptDebugListener* listener, v8::Handle<v8::Object> sourceObject);
    122 
    123     void ensureDebuggerScriptCompiled();
    124 
    125     v8::Local<v8::Value> callDebuggerMethod(const char* functionName, int argc, v8::Handle<v8::Value> argv[]);
    126 
    127     String preprocessSourceCode(const String& sourceCode);
    128 
    129     PauseOnExceptionsState m_pauseOnExceptionsState;
    130     ScopedPersistent<v8::Object> m_debuggerScript;
    131     ScopedPersistent<v8::Object> m_executionState;
    132     v8::Handle<v8::Context> m_pausedContext;
    133     bool m_breakpointsActivated;
    134     ScopedPersistent<v8::FunctionTemplate> m_breakProgramCallbackTemplate;
    135     HashMap<String, OwnPtr<ScopedPersistent<v8::Script> > > m_compiledScripts;
    136     v8::Isolate* m_isolate;
    137 
    138 private:
    139     PassRefPtr<JavaScriptCallFrame> wrapCallFrames(v8::Handle<v8::Object> executionState, int maximumLimit);
    140     bool executeSkipPauseRequest(ScriptDebugListener::SkipPauseRequest, v8::Handle<v8::Object> executionState);
    141 
    142     class ScriptPreprocessor;
    143     OwnPtr<ScriptPreprocessor> m_scriptPreprocessor;
    144     bool m_runningNestedMessageLoop;
    145 };
    146 
    147 } // namespace WebCore
    148 
    149 
    150 #endif // ScriptDebugServer_h
    151