isolate-data.h 6.67 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_
#define V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_

#include <map>
9
#include <memory>
10 11 12 13

#include "include/v8-inspector.h"
#include "include/v8-platform.h"
#include "include/v8.h"
14 15
#include "src/base/macros.h"
#include "src/base/platform/platform.h"
16
#include "src/utils/vector.h"
17 18 19

class TaskRunner;

20
class IsolateData : public v8_inspector::V8InspectorClient {
21 22 23 24 25 26 27 28 29 30
 public:
  class SetupGlobalTask {
   public:
    virtual ~SetupGlobalTask() = default;
    virtual void Run(v8::Isolate* isolate,
                     v8::Local<v8::ObjectTemplate> global) = 0;
  };
  using SetupGlobalTasks = std::vector<std::unique_ptr<SetupGlobalTask>>;

  IsolateData(TaskRunner* task_runner, SetupGlobalTasks setup_global_tasks,
31
              v8::StartupData* startup_data, bool with_inspector);
32 33
  static IsolateData* FromContext(v8::Local<v8::Context> context);

34
  v8::Isolate* isolate() const { return isolate_.get(); }
35
  TaskRunner* task_runner() const { return task_runner_; }
36 37

  // Setting things up.
38
  int CreateContextGroup();
39
  void ResetContextGroup(int context_group_id);
40
  v8::Local<v8::Context> GetContext(int context_group_id);
41
  int GetContextGroupId(v8::Local<v8::Context> context);
42 43 44 45
  void RegisterModule(v8::Local<v8::Context> context,
                      v8::internal::Vector<uint16_t> name,
                      v8::ScriptCompiler::Source* source);

46 47
  // Working with V8Inspector api.
  int ConnectSession(int context_group_id,
48 49
                     const v8_inspector::StringView& state,
                     v8_inspector::V8Inspector::Channel* channel);
50
  std::vector<uint8_t> DisconnectSession(int session_id);
51 52 53 54 55 56 57 58
  void SendMessage(int session_id, const v8_inspector::StringView& message);
  void BreakProgram(int context_group_id,
                    const v8_inspector::StringView& reason,
                    const v8_inspector::StringView& details);
  void SchedulePauseOnNextStatement(int context_group_id,
                                    const v8_inspector::StringView& reason,
                                    const v8_inspector::StringView& details);
  void CancelPauseOnNextStatement(int context_group_id);
59 60 61 62
  void AsyncTaskScheduled(const v8_inspector::StringView& name, void* task,
                          bool recurring);
  void AsyncTaskStarted(void* task);
  void AsyncTaskFinished(void* task);
63 64 65 66 67 68

  v8_inspector::V8StackTraceId StoreCurrentStackTrace(
      const v8_inspector::StringView& description);
  void ExternalAsyncTaskStarted(const v8_inspector::V8StackTraceId& parent);
  void ExternalAsyncTaskFinished(const v8_inspector::V8StackTraceId& parent);

69
  void AddInspectedObject(int session_id, v8::Local<v8::Value> object);
70 71 72 73 74

  // Test utilities.
  void SetCurrentTimeMS(double time);
  void SetMemoryInfo(v8::Local<v8::Value> memory_info);
  void SetLogConsoleApiMessageCalls(bool log);
75
  void SetLogMaxAsyncCallStackDepthChanged(bool log);
76 77
  void SetMaxAsyncTaskStacksForTest(int limit);
  void DumpAsyncTaskStacksStateForTest();
78 79
  void FireContextCreated(v8::Local<v8::Context> context, int context_group_id);
  void FireContextDestroyed(v8::Local<v8::Context> context);
80
  void FreeContext(v8::Local<v8::Context> context);
81
  void SetResourceNamePrefix(v8::Local<v8::String> prefix);
82

83 84 85 86 87 88 89 90 91 92 93 94 95
 private:
  struct VectorCompare {
    bool operator()(const v8::internal::Vector<uint16_t>& lhs,
                    const v8::internal::Vector<uint16_t>& rhs) const {
      for (int i = 0; i < lhs.length() && i < rhs.length(); ++i) {
        if (lhs[i] != rhs[i]) return lhs[i] < rhs[i];
      }
      return false;
    }
  };
  static v8::MaybeLocal<v8::Module> ModuleResolveCallback(
      v8::Local<v8::Context> context, v8::Local<v8::String> specifier,
      v8::Local<v8::Module> referrer);
96 97
  static void MessageHandler(v8::Local<v8::Message> message,
                             v8::Local<v8::Value> exception);
98 99 100
  static void PromiseRejectHandler(v8::PromiseRejectMessage data);
  static int HandleMessage(v8::Local<v8::Message> message,
                           v8::Local<v8::Value> exception);
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  std::vector<int> GetSessionIds(int context_group_id);

  // V8InspectorClient implementation.
  bool formatAccessorsAsProperties(v8::Local<v8::Value>) override;
  v8::Local<v8::Context> ensureDefaultContextInGroup(
      int context_group_id) override;
  double currentTimeMS() override;
  v8::MaybeLocal<v8::Value> memoryInfo(v8::Isolate* isolate,
                                       v8::Local<v8::Context>) override;
  void runMessageLoopOnPause(int context_group_id) override;
  void quitMessageLoopOnPause() override;
  void consoleAPIMessage(int contextGroupId,
                         v8::Isolate::MessageErrorLevel level,
                         const v8_inspector::StringView& message,
                         const v8_inspector::StringView& url,
                         unsigned lineNumber, unsigned columnNumber,
                         v8_inspector::V8StackTrace*) override;
118
  bool isInspectableHeapObject(v8::Local<v8::Object>) override;
119
  void maxAsyncCallStackDepthChanged(int depth) override;
120 121
  std::unique_ptr<v8_inspector::StringBuffer> resourceNameToUrl(
      const v8_inspector::StringView& resourceName) override;
122

123 124 125 126 127 128 129 130
  // The isolate gets deleted by its {Dispose} method, not by the default
  // deleter. Therefore we have to define a custom deleter for the unique_ptr to
  // call {Dispose}. We have to use the unique_ptr so that the isolate get
  // disposed in the right order, relative to other member variables.
  struct IsolateDeleter {
    void operator()(v8::Isolate* isolate) const { isolate->Dispose(); }
  };

131 132
  TaskRunner* task_runner_;
  SetupGlobalTasks setup_global_tasks_;
133
  std::unique_ptr<v8::ArrayBuffer::Allocator> array_buffer_allocator_;
134
  std::unique_ptr<v8::Isolate, IsolateDeleter> isolate_;
135
  std::unique_ptr<v8_inspector::V8Inspector> inspector_;
136 137 138 139 140
  int last_context_group_id_ = 0;
  std::map<int, v8::Global<v8::Context>> contexts_;
  std::map<v8::internal::Vector<uint16_t>, v8::Global<v8::Module>,
           VectorCompare>
      modules_;
141 142 143 144 145 146 147
  int last_session_id_ = 0;
  std::map<int, std::unique_ptr<v8_inspector::V8InspectorSession>> sessions_;
  std::map<v8_inspector::V8InspectorSession*, int> context_group_by_session_;
  v8::Global<v8::Value> memory_info_;
  bool current_time_set_ = false;
  double current_time_ = 0.0;
  bool log_console_api_message_calls_ = false;
148
  bool log_max_async_call_stack_depth_changed_ = false;
149
  v8::Global<v8::Private> not_inspectable_private_;
150
  v8::Global<v8::String> resource_name_prefix_;
151 152

  DISALLOW_COPY_AND_ASSIGN(IsolateData);
153
};
154

155
#endif  //  V8_TEST_INSPECTOR_PROTOCOL_ISOLATE_DATA_H_