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 #include "src/inspector/java-script-call-frame.h"
32 
33 #include "src/debug/debug-interface.h"
34 #include "src/inspector/string-util.h"
35 
36 namespace v8_inspector {
37 
JavaScriptCallFrame(v8::Local<v8::Context> debuggerContext,v8::Local<v8::Object> callFrame)38 JavaScriptCallFrame::JavaScriptCallFrame(v8::Local<v8::Context> debuggerContext,
39                                          v8::Local<v8::Object> callFrame)
40     : m_isolate(debuggerContext->GetIsolate()),
41       m_debuggerContext(m_isolate, debuggerContext),
42       m_callFrame(m_isolate, callFrame) {}
43 
~JavaScriptCallFrame()44 JavaScriptCallFrame::~JavaScriptCallFrame() {}
45 
callV8FunctionReturnInt(const char * name) const46 int JavaScriptCallFrame::callV8FunctionReturnInt(const char* name) const {
47   v8::HandleScope handleScope(m_isolate);
48   v8::MicrotasksScope microtasks(m_isolate,
49                                  v8::MicrotasksScope::kDoNotRunMicrotasks);
50   v8::Local<v8::Context> context =
51       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
52   v8::Local<v8::Object> callFrame =
53       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
54   v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(
55       callFrame->Get(context, toV8StringInternalized(m_isolate, name))
56           .ToLocalChecked());
57   v8::Local<v8::Value> result;
58   if (!func->Call(context, callFrame, 0, nullptr).ToLocal(&result) ||
59       !result->IsInt32())
60     return 0;
61   return result.As<v8::Int32>()->Value();
62 }
63 
sourceID() const64 int JavaScriptCallFrame::sourceID() const {
65   return callV8FunctionReturnInt("sourceID");
66 }
67 
line() const68 int JavaScriptCallFrame::line() const {
69   return callV8FunctionReturnInt("line");
70 }
71 
column() const72 int JavaScriptCallFrame::column() const {
73   return callV8FunctionReturnInt("column");
74 }
75 
contextId() const76 int JavaScriptCallFrame::contextId() const {
77   return callV8FunctionReturnInt("contextId");
78 }
79 
isAtReturn() const80 bool JavaScriptCallFrame::isAtReturn() const {
81   v8::HandleScope handleScope(m_isolate);
82   v8::Local<v8::Context> context =
83       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
84   v8::Local<v8::Object> callFrame =
85       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
86   v8::Local<v8::Value> result;
87   if (!callFrame->Get(context, toV8StringInternalized(m_isolate, "isAtReturn"))
88            .ToLocal(&result) ||
89       !result->IsBoolean())
90     return false;
91   return result.As<v8::Boolean>()->BooleanValue(context).FromMaybe(false);
92 }
93 
details() const94 v8::Local<v8::Object> JavaScriptCallFrame::details() const {
95   v8::MicrotasksScope microtasks(m_isolate,
96                                  v8::MicrotasksScope::kDoNotRunMicrotasks);
97   v8::Local<v8::Context> context =
98       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
99   v8::Local<v8::Object> callFrame =
100       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
101   v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(
102       callFrame->Get(context, toV8StringInternalized(m_isolate, "details"))
103           .ToLocalChecked());
104   return v8::Local<v8::Object>::Cast(
105       func->Call(context, callFrame, 0, nullptr).ToLocalChecked());
106 }
107 
evaluate(v8::Local<v8::Value> expression)108 v8::MaybeLocal<v8::Value> JavaScriptCallFrame::evaluate(
109     v8::Local<v8::Value> expression) {
110   v8::MicrotasksScope microtasks(m_isolate,
111                                  v8::MicrotasksScope::kRunMicrotasks);
112   v8::Local<v8::Context> context =
113       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
114   v8::Local<v8::Object> callFrame =
115       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
116   v8::Local<v8::Function> evalFunction = v8::Local<v8::Function>::Cast(
117       callFrame->Get(context, toV8StringInternalized(m_isolate, "evaluate"))
118           .ToLocalChecked());
119   return evalFunction->Call(context, callFrame, 1, &expression);
120 }
121 
restart()122 v8::MaybeLocal<v8::Value> JavaScriptCallFrame::restart() {
123   v8::MicrotasksScope microtasks(m_isolate,
124                                  v8::MicrotasksScope::kDoNotRunMicrotasks);
125   v8::Local<v8::Context> context =
126       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
127   v8::Local<v8::Object> callFrame =
128       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
129   v8::Local<v8::Function> restartFunction = v8::Local<v8::Function>::Cast(
130       callFrame->Get(context, toV8StringInternalized(m_isolate, "restart"))
131           .ToLocalChecked());
132   v8::DebugInterface::SetLiveEditEnabled(m_isolate, true);
133   v8::MaybeLocal<v8::Value> result = restartFunction->Call(
134       m_debuggerContext.Get(m_isolate), callFrame, 0, nullptr);
135   v8::DebugInterface::SetLiveEditEnabled(m_isolate, false);
136   return result;
137 }
138 
setVariableValue(int scopeNumber,v8::Local<v8::Value> variableName,v8::Local<v8::Value> newValue)139 v8::MaybeLocal<v8::Value> JavaScriptCallFrame::setVariableValue(
140     int scopeNumber, v8::Local<v8::Value> variableName,
141     v8::Local<v8::Value> newValue) {
142   v8::MicrotasksScope microtasks(m_isolate,
143                                  v8::MicrotasksScope::kDoNotRunMicrotasks);
144   v8::Local<v8::Context> context =
145       v8::Local<v8::Context>::New(m_isolate, m_debuggerContext);
146   v8::Local<v8::Object> callFrame =
147       v8::Local<v8::Object>::New(m_isolate, m_callFrame);
148   v8::Local<v8::Function> setVariableValueFunction =
149       v8::Local<v8::Function>::Cast(
150           callFrame
151               ->Get(context,
152                     toV8StringInternalized(m_isolate, "setVariableValue"))
153               .ToLocalChecked());
154   v8::Local<v8::Value> argv[] = {
155       v8::Local<v8::Value>(v8::Integer::New(m_isolate, scopeNumber)),
156       variableName, newValue};
157   return setVariableValueFunction->Call(context, callFrame, arraysize(argv),
158                                         argv);
159 }
160 
161 }  // namespace v8_inspector
162