1 /*
2 * Copyright (c) 2010-2011 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 #include "src/inspector/v8-inspector-impl.h"
32
33 #include <vector>
34
35 #include "include/v8-context.h"
36 #include "include/v8-local-handle.h"
37 #include "include/v8-microtask-queue.h"
38 #include "include/v8-platform.h"
39 #include "src/base/platform/mutex.h"
40 #include "src/debug/debug-interface.h"
41 #include "src/inspector/inspected-context.h"
42 #include "src/inspector/string-util.h"
43 #include "src/inspector/v8-console-agent-impl.h"
44 #include "src/inspector/v8-console-message.h"
45 #include "src/inspector/v8-console.h"
46 #include "src/inspector/v8-debugger-agent-impl.h"
47 #include "src/inspector/v8-debugger.h"
48 #include "src/inspector/v8-inspector-session-impl.h"
49 #include "src/inspector/v8-profiler-agent-impl.h"
50 #include "src/inspector/v8-runtime-agent-impl.h"
51 #include "src/inspector/v8-stack-trace-impl.h"
52
53 namespace v8_inspector {
54
create(v8::Isolate * isolate,V8InspectorClient * client)55 std::unique_ptr<V8Inspector> V8Inspector::create(v8::Isolate* isolate,
56 V8InspectorClient* client) {
57 return std::unique_ptr<V8Inspector>(new V8InspectorImpl(isolate, client));
58 }
59
V8InspectorImpl(v8::Isolate * isolate,V8InspectorClient * client)60 V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate,
61 V8InspectorClient* client)
62 : m_isolate(isolate),
63 m_client(client),
64 m_debugger(new V8Debugger(isolate, this)),
65 m_capturingStackTracesCount(0),
66 m_lastExceptionId(0),
67 m_lastContextId(0),
68 m_isolateId(generateUniqueId()) {
69 v8::debug::SetInspector(m_isolate, this);
70 v8::debug::SetConsoleDelegate(m_isolate, console());
71 }
72
~V8InspectorImpl()73 V8InspectorImpl::~V8InspectorImpl() {
74 v8::debug::SetInspector(m_isolate, nullptr);
75 v8::debug::SetConsoleDelegate(m_isolate, nullptr);
76 }
77
contextGroupId(v8::Local<v8::Context> context) const78 int V8InspectorImpl::contextGroupId(v8::Local<v8::Context> context) const {
79 return contextGroupId(InspectedContext::contextId(context));
80 }
81
contextGroupId(int contextId) const82 int V8InspectorImpl::contextGroupId(int contextId) const {
83 auto it = m_contextIdToGroupIdMap.find(contextId);
84 return it != m_contextIdToGroupIdMap.end() ? it->second : 0;
85 }
86
resolveUniqueContextId(V8DebuggerId uniqueId) const87 int V8InspectorImpl::resolveUniqueContextId(V8DebuggerId uniqueId) const {
88 auto it = m_uniqueIdToContextId.find(uniqueId.pair());
89 return it == m_uniqueIdToContextId.end() ? 0 : it->second;
90 }
91
compileAndRunInternalScript(v8::Local<v8::Context> context,v8::Local<v8::String> source)92 v8::MaybeLocal<v8::Value> V8InspectorImpl::compileAndRunInternalScript(
93 v8::Local<v8::Context> context, v8::Local<v8::String> source) {
94 v8::Local<v8::UnboundScript> unboundScript;
95 if (!v8::debug::CompileInspectorScript(m_isolate, source)
96 .ToLocal(&unboundScript))
97 return v8::MaybeLocal<v8::Value>();
98 v8::MicrotasksScope microtasksScope(m_isolate,
99 v8::MicrotasksScope::kDoNotRunMicrotasks);
100 v8::Context::Scope contextScope(context);
101 v8::Isolate::SafeForTerminationScope allowTermination(m_isolate);
102 return unboundScript->BindToCurrentContext()->Run(context);
103 }
104
compileScript(v8::Local<v8::Context> context,const String16 & code,const String16 & fileName)105 v8::MaybeLocal<v8::Script> V8InspectorImpl::compileScript(
106 v8::Local<v8::Context> context, const String16& code,
107 const String16& fileName) {
108 v8::ScriptOrigin origin(m_isolate, toV8String(m_isolate, fileName), 0, 0,
109 false);
110 v8::ScriptCompiler::Source source(toV8String(m_isolate, code), origin);
111 return v8::ScriptCompiler::Compile(context, &source,
112 v8::ScriptCompiler::kNoCompileOptions);
113 }
114
enableStackCapturingIfNeeded()115 void V8InspectorImpl::enableStackCapturingIfNeeded() {
116 if (!m_capturingStackTracesCount)
117 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate,
118 true);
119 ++m_capturingStackTracesCount;
120 }
121
disableStackCapturingIfNeeded()122 void V8InspectorImpl::disableStackCapturingIfNeeded() {
123 if (!(--m_capturingStackTracesCount))
124 V8StackTraceImpl::setCaptureStackTraceForUncaughtExceptions(m_isolate,
125 false);
126 }
127
muteExceptions(int contextGroupId)128 void V8InspectorImpl::muteExceptions(int contextGroupId) {
129 m_muteExceptionsMap[contextGroupId]++;
130 }
131
unmuteExceptions(int contextGroupId)132 void V8InspectorImpl::unmuteExceptions(int contextGroupId) {
133 m_muteExceptionsMap[contextGroupId]--;
134 }
135
ensureConsoleMessageStorage(int contextGroupId)136 V8ConsoleMessageStorage* V8InspectorImpl::ensureConsoleMessageStorage(
137 int contextGroupId) {
138 auto storageIt = m_consoleStorageMap.find(contextGroupId);
139 if (storageIt == m_consoleStorageMap.end())
140 storageIt = m_consoleStorageMap
141 .insert(std::make_pair(
142 contextGroupId,
143 std::unique_ptr<V8ConsoleMessageStorage>(
144 new V8ConsoleMessageStorage(this, contextGroupId))))
145 .first;
146 return storageIt->second.get();
147 }
148
hasConsoleMessageStorage(int contextGroupId)149 bool V8InspectorImpl::hasConsoleMessageStorage(int contextGroupId) {
150 auto storageIt = m_consoleStorageMap.find(contextGroupId);
151 return storageIt != m_consoleStorageMap.end();
152 }
153
createStackTrace(v8::Local<v8::StackTrace> stackTrace)154 std::unique_ptr<V8StackTrace> V8InspectorImpl::createStackTrace(
155 v8::Local<v8::StackTrace> stackTrace) {
156 return m_debugger->createStackTrace(stackTrace);
157 }
158
connect(int contextGroupId,V8Inspector::Channel * channel,StringView state)159 std::unique_ptr<V8InspectorSession> V8InspectorImpl::connect(
160 int contextGroupId, V8Inspector::Channel* channel, StringView state) {
161 int sessionId = ++m_lastSessionId;
162 std::unique_ptr<V8InspectorSessionImpl> session =
163 V8InspectorSessionImpl::create(this, contextGroupId, sessionId, channel,
164 state);
165 m_sessions[contextGroupId][sessionId] = session.get();
166 return std::move(session);
167 }
168
disconnect(V8InspectorSessionImpl * session)169 void V8InspectorImpl::disconnect(V8InspectorSessionImpl* session) {
170 auto& map = m_sessions[session->contextGroupId()];
171 map.erase(session->sessionId());
172 if (map.empty()) m_sessions.erase(session->contextGroupId());
173 }
174
getContext(int groupId,int contextId) const175 InspectedContext* V8InspectorImpl::getContext(int groupId,
176 int contextId) const {
177 if (!groupId || !contextId) return nullptr;
178
179 auto contextGroupIt = m_contexts.find(groupId);
180 if (contextGroupIt == m_contexts.end()) return nullptr;
181
182 auto contextIt = contextGroupIt->second->find(contextId);
183 if (contextIt == contextGroupIt->second->end()) return nullptr;
184
185 return contextIt->second.get();
186 }
187
getContext(int contextId) const188 InspectedContext* V8InspectorImpl::getContext(int contextId) const {
189 return getContext(contextGroupId(contextId), contextId);
190 }
191
contextById(int contextId)192 v8::MaybeLocal<v8::Context> V8InspectorImpl::contextById(int contextId) {
193 InspectedContext* context = getContext(contextId);
194 return context ? context->context() : v8::MaybeLocal<v8::Context>();
195 }
196
contextCreated(const V8ContextInfo & info)197 void V8InspectorImpl::contextCreated(const V8ContextInfo& info) {
198 int contextId = ++m_lastContextId;
199 auto* context = new InspectedContext(this, info, contextId);
200 m_contextIdToGroupIdMap[contextId] = info.contextGroupId;
201
202 DCHECK(m_uniqueIdToContextId.find(context->uniqueId().pair()) ==
203 m_uniqueIdToContextId.end());
204 m_uniqueIdToContextId.insert(
205 std::make_pair(context->uniqueId().pair(), contextId));
206
207 auto contextIt = m_contexts.find(info.contextGroupId);
208 if (contextIt == m_contexts.end())
209 contextIt = m_contexts
210 .insert(std::make_pair(
211 info.contextGroupId,
212 std::unique_ptr<ContextByIdMap>(new ContextByIdMap())))
213 .first;
214 const auto& contextById = contextIt->second;
215
216 DCHECK(contextById->find(contextId) == contextById->cend());
217 (*contextById)[contextId].reset(context);
218 forEachSession(
219 info.contextGroupId, [&context](V8InspectorSessionImpl* session) {
220 session->runtimeAgent()->addBindings(context);
221 session->runtimeAgent()->reportExecutionContextCreated(context);
222 });
223 }
224
contextDestroyed(v8::Local<v8::Context> context)225 void V8InspectorImpl::contextDestroyed(v8::Local<v8::Context> context) {
226 int contextId = InspectedContext::contextId(context);
227 int groupId = contextGroupId(context);
228 contextCollected(groupId, contextId);
229 }
230
contextCollected(int groupId,int contextId)231 void V8InspectorImpl::contextCollected(int groupId, int contextId) {
232 m_contextIdToGroupIdMap.erase(contextId);
233
234 auto storageIt = m_consoleStorageMap.find(groupId);
235 if (storageIt != m_consoleStorageMap.end())
236 storageIt->second->contextDestroyed(contextId);
237
238 InspectedContext* inspectedContext = getContext(groupId, contextId);
239 if (!inspectedContext) return;
240
241 forEachSession(groupId, [&inspectedContext](V8InspectorSessionImpl* session) {
242 session->runtimeAgent()->reportExecutionContextDestroyed(inspectedContext);
243 });
244 discardInspectedContext(groupId, contextId);
245 }
246
resetContextGroup(int contextGroupId)247 void V8InspectorImpl::resetContextGroup(int contextGroupId) {
248 m_consoleStorageMap.erase(contextGroupId);
249 m_muteExceptionsMap.erase(contextGroupId);
250 auto contextsIt = m_contexts.find(contextGroupId);
251 // Context might have been removed already by discardContextScript()
252 if (contextsIt != m_contexts.end()) {
253 for (const auto& map_entry : *contextsIt->second)
254 m_uniqueIdToContextId.erase(map_entry.second->uniqueId().pair());
255 m_contexts.erase(contextsIt);
256 }
257 forEachSession(contextGroupId,
258 [](V8InspectorSessionImpl* session) { session->reset(); });
259 }
260
idleStarted()261 void V8InspectorImpl::idleStarted() { m_isolate->SetIdle(true); }
262
idleFinished()263 void V8InspectorImpl::idleFinished() { m_isolate->SetIdle(false); }
264
exceptionThrown(v8::Local<v8::Context> context,StringView message,v8::Local<v8::Value> exception,StringView detailedMessage,StringView url,unsigned lineNumber,unsigned columnNumber,std::unique_ptr<V8StackTrace> stackTrace,int scriptId)265 unsigned V8InspectorImpl::exceptionThrown(
266 v8::Local<v8::Context> context, StringView message,
267 v8::Local<v8::Value> exception, StringView detailedMessage, StringView url,
268 unsigned lineNumber, unsigned columnNumber,
269 std::unique_ptr<V8StackTrace> stackTrace, int scriptId) {
270 int groupId = contextGroupId(context);
271 if (!groupId || m_muteExceptionsMap[groupId]) return 0;
272 std::unique_ptr<V8StackTraceImpl> stackTraceImpl(
273 static_cast<V8StackTraceImpl*>(stackTrace.release()));
274 unsigned exceptionId = nextExceptionId();
275 std::unique_ptr<V8ConsoleMessage> consoleMessage =
276 V8ConsoleMessage::createForException(
277 m_client->currentTimeMS(), toString16(detailedMessage),
278 toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl),
279 scriptId, m_isolate, toString16(message),
280 InspectedContext::contextId(context), exception, exceptionId);
281 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
282 return exceptionId;
283 }
284
exceptionRevoked(v8::Local<v8::Context> context,unsigned exceptionId,StringView message)285 void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context,
286 unsigned exceptionId,
287 StringView message) {
288 int groupId = contextGroupId(context);
289 if (!groupId) return;
290
291 std::unique_ptr<V8ConsoleMessage> consoleMessage =
292 V8ConsoleMessage::createForRevokedException(
293 m_client->currentTimeMS(), toString16(message), exceptionId);
294 ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
295 }
296
captureStackTrace(bool fullStack)297 std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace(
298 bool fullStack) {
299 return m_debugger->captureStackTrace(fullStack);
300 }
301
storeCurrentStackTrace(StringView description)302 V8StackTraceId V8InspectorImpl::storeCurrentStackTrace(StringView description) {
303 return m_debugger->storeCurrentStackTrace(description);
304 }
305
externalAsyncTaskStarted(const V8StackTraceId & parent)306 void V8InspectorImpl::externalAsyncTaskStarted(const V8StackTraceId& parent) {
307 m_debugger->externalAsyncTaskStarted(parent);
308 }
309
externalAsyncTaskFinished(const V8StackTraceId & parent)310 void V8InspectorImpl::externalAsyncTaskFinished(const V8StackTraceId& parent) {
311 m_debugger->externalAsyncTaskFinished(parent);
312 }
313
asyncTaskScheduled(StringView taskName,void * task,bool recurring)314 void V8InspectorImpl::asyncTaskScheduled(StringView taskName, void* task,
315 bool recurring) {
316 if (!task) return;
317 m_debugger->asyncTaskScheduled(taskName, task, recurring);
318 }
319
asyncTaskCanceled(void * task)320 void V8InspectorImpl::asyncTaskCanceled(void* task) {
321 if (!task) return;
322 m_debugger->asyncTaskCanceled(task);
323 }
324
asyncTaskStarted(void * task)325 void V8InspectorImpl::asyncTaskStarted(void* task) {
326 if (!task) return;
327 m_debugger->asyncTaskStarted(task);
328 }
329
asyncTaskFinished(void * task)330 void V8InspectorImpl::asyncTaskFinished(void* task) {
331 if (!task) return;
332 m_debugger->asyncTaskFinished(task);
333 }
334
allAsyncTasksCanceled()335 void V8InspectorImpl::allAsyncTasksCanceled() {
336 m_debugger->allAsyncTasksCanceled();
337 }
338
regexContext()339 v8::MaybeLocal<v8::Context> V8InspectorImpl::regexContext() {
340 if (m_regexContext.IsEmpty()) {
341 m_regexContext.Reset(m_isolate, v8::Context::New(m_isolate));
342 if (m_regexContext.IsEmpty()) {
343 DCHECK(m_isolate->IsExecutionTerminating());
344 return {};
345 }
346 }
347 return m_regexContext.Get(m_isolate);
348 }
349
exceptionMetaDataContext()350 v8::MaybeLocal<v8::Context> V8InspectorImpl::exceptionMetaDataContext() {
351 if (m_exceptionMetaDataContext.IsEmpty()) {
352 m_exceptionMetaDataContext.Reset(m_isolate, v8::Context::New(m_isolate));
353 if (m_exceptionMetaDataContext.IsEmpty()) {
354 DCHECK(m_isolate->IsExecutionTerminating());
355 return {};
356 }
357 }
358 return m_exceptionMetaDataContext.Get(m_isolate);
359 }
360
discardInspectedContext(int contextGroupId,int contextId)361 void V8InspectorImpl::discardInspectedContext(int contextGroupId,
362 int contextId) {
363 auto* context = getContext(contextGroupId, contextId);
364 if (!context) return;
365 m_uniqueIdToContextId.erase(context->uniqueId().pair());
366 m_contexts[contextGroupId]->erase(contextId);
367 if (m_contexts[contextGroupId]->empty()) m_contexts.erase(contextGroupId);
368 }
369
sessionById(int contextGroupId,int sessionId)370 V8InspectorSessionImpl* V8InspectorImpl::sessionById(int contextGroupId,
371 int sessionId) {
372 auto it = m_sessions.find(contextGroupId);
373 if (it == m_sessions.end()) return nullptr;
374 auto it2 = it->second.find(sessionId);
375 return it2 == it->second.end() ? nullptr : it2->second;
376 }
377
console()378 V8Console* V8InspectorImpl::console() {
379 if (!m_console) m_console.reset(new V8Console(this));
380 return m_console.get();
381 }
382
forEachContext(int contextGroupId,const std::function<void (InspectedContext *)> & callback)383 void V8InspectorImpl::forEachContext(
384 int contextGroupId,
385 const std::function<void(InspectedContext*)>& callback) {
386 auto it = m_contexts.find(contextGroupId);
387 if (it == m_contexts.end()) return;
388 std::vector<int> ids;
389 ids.reserve(it->second->size());
390 for (auto& contextIt : *(it->second)) ids.push_back(contextIt.first);
391
392 // Retrieve by ids each time since |callback| may destroy some contexts.
393 for (auto& contextId : ids) {
394 it = m_contexts.find(contextGroupId);
395 if (it == m_contexts.end()) continue;
396 auto contextIt = it->second->find(contextId);
397 if (contextIt != it->second->end()) callback(contextIt->second.get());
398 }
399 }
400
forEachSession(int contextGroupId,const std::function<void (V8InspectorSessionImpl *)> & callback)401 void V8InspectorImpl::forEachSession(
402 int contextGroupId,
403 const std::function<void(V8InspectorSessionImpl*)>& callback) {
404 auto it = m_sessions.find(contextGroupId);
405 if (it == m_sessions.end()) return;
406 std::vector<int> ids;
407 ids.reserve(it->second.size());
408 for (auto& sessionIt : it->second) ids.push_back(sessionIt.first);
409
410 // Retrieve by ids each time since |callback| may destroy some contexts.
411 for (auto& sessionId : ids) {
412 it = m_sessions.find(contextGroupId);
413 if (it == m_sessions.end()) continue;
414 auto sessionIt = it->second.find(sessionId);
415 if (sessionIt != it->second.end()) callback(sessionIt->second);
416 }
417 }
418
generateUniqueId()419 int64_t V8InspectorImpl::generateUniqueId() {
420 int64_t id = m_client->generateUniqueId();
421 if (!id) id = v8::debug::GetNextRandomInt64(m_isolate);
422 if (!id) id = 1;
423 return id;
424 }
425
EvaluateScope(const InjectedScript::Scope & scope)426 V8InspectorImpl::EvaluateScope::EvaluateScope(
427 const InjectedScript::Scope& scope)
428 : m_scope(scope),
429 m_isolate(scope.inspector()->isolate()),
430 m_safeForTerminationScope(m_isolate) {}
431
432 struct V8InspectorImpl::EvaluateScope::CancelToken {
433 v8::base::Mutex m_mutex;
434 bool m_canceled = false;
435 };
436
~EvaluateScope()437 V8InspectorImpl::EvaluateScope::~EvaluateScope() {
438 if (m_scope.tryCatch().HasTerminated()) {
439 m_scope.inspector()->debugger()->reportTermination();
440 }
441 if (m_cancelToken) {
442 v8::base::MutexGuard lock(&m_cancelToken->m_mutex);
443 m_cancelToken->m_canceled = true;
444 m_isolate->CancelTerminateExecution();
445 }
446 }
447
448 class V8InspectorImpl::EvaluateScope::TerminateTask : public v8::Task {
449 public:
TerminateTask(v8::Isolate * isolate,std::shared_ptr<CancelToken> token)450 TerminateTask(v8::Isolate* isolate, std::shared_ptr<CancelToken> token)
451 : m_isolate(isolate), m_token(std::move(token)) {}
452
Run()453 void Run() override {
454 // CancelToken contains m_canceled bool which may be changed from main
455 // thread, so lock mutex first.
456 v8::base::MutexGuard lock(&m_token->m_mutex);
457 if (m_token->m_canceled) return;
458 m_isolate->TerminateExecution();
459 }
460
461 private:
462 v8::Isolate* m_isolate;
463 std::shared_ptr<CancelToken> m_token;
464 };
465
setTimeout(double timeout)466 protocol::Response V8InspectorImpl::EvaluateScope::setTimeout(double timeout) {
467 if (m_isolate->IsExecutionTerminating()) {
468 return protocol::Response::ServerError("Execution was terminated");
469 }
470 m_cancelToken.reset(new CancelToken());
471 v8::debug::GetCurrentPlatform()->CallDelayedOnWorkerThread(
472 std::make_unique<TerminateTask>(m_isolate, m_cancelToken), timeout);
473 return protocol::Response::Success();
474 }
475
associateExceptionData(v8::Local<v8::Context>,v8::Local<v8::Value> exception,v8::Local<v8::Name> key,v8::Local<v8::Value> value)476 bool V8InspectorImpl::associateExceptionData(v8::Local<v8::Context>,
477 v8::Local<v8::Value> exception,
478 v8::Local<v8::Name> key,
479 v8::Local<v8::Value> value) {
480 if (!exception->IsObject()) {
481 return false;
482 }
483 v8::Local<v8::Context> context;
484 if (!exceptionMetaDataContext().ToLocal(&context)) return false;
485 v8::TryCatch tryCatch(m_isolate);
486 v8::Context::Scope contextScope(context);
487 v8::HandleScope handles(m_isolate);
488 if (m_exceptionMetaData.IsEmpty())
489 m_exceptionMetaData.Reset(m_isolate,
490 v8::debug::EphemeronTable::New(m_isolate));
491
492 v8::Local<v8::debug::EphemeronTable> map = m_exceptionMetaData.Get(m_isolate);
493 v8::MaybeLocal<v8::Value> entry = map->Get(m_isolate, exception);
494 v8::Local<v8::Object> object;
495 if (entry.IsEmpty() || !entry.ToLocalChecked()->IsObject()) {
496 object =
497 v8::Object::New(m_isolate, v8::Null(m_isolate), nullptr, nullptr, 0);
498 m_exceptionMetaData.Reset(m_isolate,
499 map->Set(m_isolate, exception, object));
500 } else {
501 object = entry.ToLocalChecked().As<v8::Object>();
502 }
503 CHECK(object->IsObject());
504 v8::Maybe<bool> result = object->CreateDataProperty(context, key, value);
505 return result.FromMaybe(false);
506 }
507
getAssociatedExceptionData(v8::Local<v8::Value> exception)508 v8::MaybeLocal<v8::Object> V8InspectorImpl::getAssociatedExceptionData(
509 v8::Local<v8::Value> exception) {
510 if (!exception->IsObject()) {
511 return v8::MaybeLocal<v8::Object>();
512 }
513 v8::EscapableHandleScope scope(m_isolate);
514 v8::Local<v8::Context> context;
515 if (m_exceptionMetaData.IsEmpty() ||
516 !exceptionMetaDataContext().ToLocal(&context)) {
517 return v8::MaybeLocal<v8::Object>();
518 }
519 v8::Local<v8::debug::EphemeronTable> map = m_exceptionMetaData.Get(m_isolate);
520 auto entry = map->Get(m_isolate, exception);
521 v8::Local<v8::Value> object;
522 if (!entry.ToLocal(&object) || !object->IsObject())
523 return v8::MaybeLocal<v8::Object>();
524 return scope.Escape(object.As<v8::Object>());
525 }
526 } // namespace v8_inspector
527