| OLD | NEW |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "test/inspector/inspector-impl.h" | 5 #include "test/inspector/inspector-impl.h" |
| 6 | 6 |
| 7 #include "include/v8.h" | 7 #include "include/v8.h" |
| 8 #include "src/inspector/string-16.h" | 8 |
| 9 #include "src/vector.h" |
| 9 | 10 |
| 10 namespace { | 11 namespace { |
| 11 | 12 |
| 12 const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1; | 13 const int kInspectorClientIndex = v8::Context::kDebugIdIndex + 1; |
| 13 | 14 |
| 14 class ChannelImpl final : public v8_inspector::V8Inspector::Channel { | 15 class ChannelImpl final : public v8_inspector::V8Inspector::Channel { |
| 15 public: | 16 public: |
| 16 explicit ChannelImpl(InspectorClientImpl::FrontendChannel* frontend_channel) | 17 explicit ChannelImpl(InspectorClientImpl::FrontendChannel* frontend_channel) |
| 17 : frontend_channel_(frontend_channel) {} | 18 : frontend_channel_(frontend_channel) {} |
| 18 virtual ~ChannelImpl() = default; | 19 virtual ~ChannelImpl() = default; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 33 }; | 34 }; |
| 34 | 35 |
| 35 InspectorClientImpl* InspectorClientFromContext( | 36 InspectorClientImpl* InspectorClientFromContext( |
| 36 v8::Local<v8::Context> context) { | 37 v8::Local<v8::Context> context) { |
| 37 InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>( | 38 InspectorClientImpl* inspector_client = static_cast<InspectorClientImpl*>( |
| 38 context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); | 39 context->GetAlignedPointerFromEmbedderData(kInspectorClientIndex)); |
| 39 CHECK(inspector_client); | 40 CHECK(inspector_client); |
| 40 return inspector_client; | 41 return inspector_client; |
| 41 } | 42 } |
| 42 | 43 |
| 43 v8_inspector::String16 ToString16(v8::Local<v8::String> str) { | 44 v8::internal::Vector<uint16_t> ToVector(v8::Local<v8::String> str) { |
| 44 std::unique_ptr<uint16_t[]> buffer(new uint16_t[str->Length()]); | 45 v8::internal::Vector<uint16_t> buffer = |
| 45 str->Write(reinterpret_cast<uint16_t*>(buffer.get()), 0, str->Length()); | 46 v8::internal::Vector<uint16_t>::New(str->Length()); |
| 46 return v8_inspector::String16(buffer.get(), str->Length()); | 47 str->Write(buffer.start(), 0, str->Length()); |
| 48 return buffer; |
| 47 } | 49 } |
| 48 | 50 |
| 49 void MessageHandler(v8::Local<v8::Message> message, | 51 void MessageHandler(v8::Local<v8::Message> message, |
| 50 v8::Local<v8::Value> exception) { | 52 v8::Local<v8::Value> exception) { |
| 51 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 53 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 52 v8::Local<v8::Context> context = isolate->GetEnteredContext(); | 54 v8::Local<v8::Context> context = isolate->GetEnteredContext(); |
| 53 if (context.IsEmpty()) return; | 55 if (context.IsEmpty()) return; |
| 54 v8_inspector::V8Inspector* inspector = | 56 v8_inspector::V8Inspector* inspector = |
| 55 InspectorClientImpl::InspectorFromContext(context); | 57 InspectorClientImpl::InspectorFromContext(context); |
| 56 | 58 |
| 57 v8::Local<v8::StackTrace> stack = message->GetStackTrace(); | 59 v8::Local<v8::StackTrace> stack = message->GetStackTrace(); |
| 58 int script_id = | 60 int script_id = |
| 59 static_cast<int>(message->GetScriptOrigin().ScriptID()->Value()); | 61 static_cast<int>(message->GetScriptOrigin().ScriptID()->Value()); |
| 60 if (!stack.IsEmpty() && stack->GetFrameCount() > 0) { | 62 if (!stack.IsEmpty() && stack->GetFrameCount() > 0) { |
| 61 int top_script_id = stack->GetFrame(0)->GetScriptId(); | 63 int top_script_id = stack->GetFrame(0)->GetScriptId(); |
| 62 if (top_script_id == script_id) script_id = 0; | 64 if (top_script_id == script_id) script_id = 0; |
| 63 } | 65 } |
| 64 int line_number = message->GetLineNumber(context).FromMaybe(0); | 66 int line_number = message->GetLineNumber(context).FromMaybe(0); |
| 65 int column_number = 0; | 67 int column_number = 0; |
| 66 if (message->GetStartColumn(context).IsJust()) | 68 if (message->GetStartColumn(context).IsJust()) |
| 67 column_number = message->GetStartColumn(context).FromJust() + 1; | 69 column_number = message->GetStartColumn(context).FromJust() + 1; |
| 68 | 70 |
| 69 v8_inspector::StringView detailed_message; | 71 v8_inspector::StringView detailed_message; |
| 70 v8_inspector::String16 message_text_string = ToString16(message->Get()); | 72 v8::internal::Vector<uint16_t> message_text_string = ToVector(message->Get()); |
| 71 v8_inspector::StringView message_text(message_text_string.characters16(), | 73 v8_inspector::StringView message_text(message_text_string.start(), |
| 72 message_text_string.length()); | 74 message_text_string.length()); |
| 73 v8_inspector::String16 url_string; | 75 v8::internal::Vector<uint16_t> url_string; |
| 74 if (message->GetScriptOrigin().ResourceName()->IsString()) { | 76 if (message->GetScriptOrigin().ResourceName()->IsString()) { |
| 75 url_string = | 77 url_string = |
| 76 ToString16(message->GetScriptOrigin().ResourceName().As<v8::String>()); | 78 ToVector(message->GetScriptOrigin().ResourceName().As<v8::String>()); |
| 77 } | 79 } |
| 78 v8_inspector::StringView url(url_string.characters16(), url_string.length()); | 80 v8_inspector::StringView url(url_string.start(), url_string.length()); |
| 79 | 81 |
| 80 inspector->exceptionThrown(context, message_text, exception, detailed_message, | 82 inspector->exceptionThrown(context, message_text, exception, detailed_message, |
| 81 url, line_number, column_number, | 83 url, line_number, column_number, |
| 82 inspector->createStackTrace(stack), script_id); | 84 inspector->createStackTrace(stack), script_id); |
| 83 } | 85 } |
| 84 | 86 |
| 85 } // namespace | 87 } // namespace |
| 86 | 88 |
| 87 class ConnectTask : public TaskRunner::Task { | 89 class ConnectTask : public TaskRunner::Task { |
| 88 public: | 90 public: |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 152 return InspectorClientFromContext(context)->inspector_.get(); | 154 return InspectorClientFromContext(context)->inspector_.get(); |
| 153 } | 155 } |
| 154 | 156 |
| 155 v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( | 157 v8_inspector::V8InspectorSession* InspectorClientImpl::SessionFromContext( |
| 156 v8::Local<v8::Context> context) { | 158 v8::Local<v8::Context> context) { |
| 157 return InspectorClientFromContext(context)->session_.get(); | 159 return InspectorClientFromContext(context)->session_.get(); |
| 158 } | 160 } |
| 159 | 161 |
| 160 class SendMessageToBackendTask : public TaskRunner::Task { | 162 class SendMessageToBackendTask : public TaskRunner::Task { |
| 161 public: | 163 public: |
| 162 explicit SendMessageToBackendTask(const v8_inspector::String16& message) | 164 explicit SendMessageToBackendTask( |
| 165 const v8::internal::Vector<uint16_t>& message) |
| 163 : message_(message) {} | 166 : message_(message) {} |
| 164 | 167 |
| 165 bool is_inspector_task() final { return true; } | 168 bool is_inspector_task() final { return true; } |
| 166 | 169 |
| 167 void Run(v8::Isolate* isolate, | 170 void Run(v8::Isolate* isolate, |
| 168 const v8::Global<v8::Context>& global_context) override { | 171 const v8::Global<v8::Context>& global_context) override { |
| 169 v8_inspector::V8InspectorSession* session = nullptr; | 172 v8_inspector::V8InspectorSession* session = nullptr; |
| 170 { | 173 { |
| 171 v8::HandleScope handle_scope(isolate); | 174 v8::HandleScope handle_scope(isolate); |
| 172 v8::Local<v8::Context> context = global_context.Get(isolate); | 175 v8::Local<v8::Context> context = global_context.Get(isolate); |
| 173 session = InspectorClientImpl::SessionFromContext(context); | 176 session = InspectorClientImpl::SessionFromContext(context); |
| 174 CHECK(session); | 177 CHECK(session); |
| 175 } | 178 } |
| 176 v8_inspector::StringView message_view( | 179 v8_inspector::StringView message_view(message_.start(), message_.length()); |
| 177 reinterpret_cast<const uint16_t*>(message_.characters16()), | |
| 178 message_.length()); | |
| 179 session->dispatchProtocolMessage(message_view); | 180 session->dispatchProtocolMessage(message_view); |
| 180 } | 181 } |
| 181 | 182 |
| 182 private: | 183 private: |
| 183 v8_inspector::String16 message_; | 184 v8::internal::Vector<uint16_t> message_; |
| 184 }; | 185 }; |
| 185 | 186 |
| 186 TaskRunner* SendMessageToBackendExtension::backend_task_runner_ = nullptr; | 187 TaskRunner* SendMessageToBackendExtension::backend_task_runner_ = nullptr; |
| 187 | 188 |
| 188 v8::Local<v8::FunctionTemplate> | 189 v8::Local<v8::FunctionTemplate> |
| 189 SendMessageToBackendExtension::GetNativeFunctionTemplate( | 190 SendMessageToBackendExtension::GetNativeFunctionTemplate( |
| 190 v8::Isolate* isolate, v8::Local<v8::String> name) { | 191 v8::Isolate* isolate, v8::Local<v8::String> name) { |
| 191 return v8::FunctionTemplate::New( | 192 return v8::FunctionTemplate::New( |
| 192 isolate, SendMessageToBackendExtension::SendMessageToBackend); | 193 isolate, SendMessageToBackendExtension::SendMessageToBackend); |
| 193 } | 194 } |
| 194 | 195 |
| 195 void SendMessageToBackendExtension::SendMessageToBackend( | 196 void SendMessageToBackendExtension::SendMessageToBackend( |
| 196 const v8::FunctionCallbackInfo<v8::Value>& args) { | 197 const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 197 CHECK(backend_task_runner_); | 198 CHECK(backend_task_runner_); |
| 198 CHECK(args.Length() == 1 && args[0]->IsString()); | 199 CHECK(args.Length() == 1 && args[0]->IsString()); |
| 199 v8::Local<v8::String> message = args[0].As<v8::String>(); | 200 v8::Local<v8::String> message = args[0].As<v8::String>(); |
| 200 backend_task_runner_->Append( | 201 backend_task_runner_->Append(new SendMessageToBackendTask(ToVector(message))); |
| 201 new SendMessageToBackendTask(ToString16(message))); | |
| 202 } | 202 } |
| OLD | NEW |