3333#include " src/inspector/inspected-context.h"
3434#include " src/inspector/protocol/Protocol.h"
3535#include " src/inspector/string-util.h"
36+ #include " src/inspector/v8-console.h"
3637#include " src/inspector/v8-console-message.h"
3738#include " src/inspector/v8-debugger-agent-impl.h"
3839#include " src/inspector/v8-debugger.h"
@@ -69,6 +70,27 @@ std::unique_ptr<protocol::DictionaryValue> ParseObjectId(
6970 return wrapUnique (protocol::DictionaryValue::cast (parsedValue.release ()));
7071}
7172
73+ bool ensureContext (ErrorString* errorString, V8InspectorImpl* inspector,
74+ int contextGroupId, Maybe<int > executionContextId,
75+ int * contextId) {
76+ if (executionContextId.isJust ()) {
77+ *contextId = executionContextId.fromJust ();
78+ } else {
79+ v8::HandleScope handles (inspector->isolate ());
80+ v8::Local<v8::Context> defaultContext =
81+ inspector->client ()->ensureDefaultContextInGroup (contextGroupId);
82+
83+ if (defaultContext.IsEmpty ()) {
84+ *errorString = " Cannot find default execution context" ;
85+ return false ;
86+ }
87+
88+ *contextId = V8Debugger::contextId (defaultContext);
89+ }
90+
91+ return true ;
92+ }
93+
7294} // namespace
7395
7496V8RuntimeAgentImpl::V8RuntimeAgentImpl (
@@ -91,6 +113,24 @@ void V8RuntimeAgentImpl::evaluate(
91113 std::unique_ptr<EvaluateCallback> callback) {
92114 ErrorString errorString;
93115
116+ int contextId = 0 ;
117+ if (!ensureContext (&errorString, m_inspector, m_session->contextGroupId (),
118+ std::move (executionContextId), &contextId)) {
119+ callback->sendFailure (errorString);
120+ return ;
121+ }
122+
123+ std::unique_ptr<V8Console::CommandLineAPIScope> claScope;
124+
125+ if (includeCommandLineAPI.fromMaybe (false )) {
126+ InspectedContext* inspectedContext =
127+ m_inspector->getContext (m_session->contextGroupId (), contextId);
128+ claScope.reset (new V8Console::CommandLineAPIScope (
129+ inspectedContext->context (),
130+ V8Console::createCommandLineAPI (inspectedContext),
131+ inspectedContext->context ()->Global ()));
132+ }
133+
94134 JsValueRef expStr;
95135 if (JsCreateStringUtf16 (expression.characters16 (), expression.length (),
96136 &expStr) != JsNoError) {
@@ -99,9 +139,10 @@ void V8RuntimeAgentImpl::evaluate(
99139 return ;
100140 }
101141
142+ bool isError = false ;
102143 v8::Local<v8::Value> evalResult =
103- jsrt::InspectorHelpers::EvaluateOnCallFrame (
104- /* ordinal */ 0 , expStr, returnByValue.fromMaybe (false ));
144+ jsrt::InspectorHelpers::EvaluateOnGlobalCallFrame (
145+ expStr, returnByValue.fromMaybe (false ), &isError );
105146
106147 if (evalResult.IsEmpty ()) {
107148 errorString = " Failed to evaluate expression" ;
@@ -114,7 +155,7 @@ void V8RuntimeAgentImpl::evaluate(
114155 std::unique_ptr<protocol::Value> protocolValue =
115156 toProtocolValue (&errorString, v8::Context::GetCurrent (), evalResult);
116157 if (!protocolValue) {
117- callback->sendSuccess ( nullptr , exceptionDetails );
158+ callback->sendFailure (errorString );
118159 return ;
119160 }
120161 std::unique_ptr<protocol::Runtime::RemoteObject> remoteObject =
@@ -125,6 +166,29 @@ void V8RuntimeAgentImpl::evaluate(
125166 return ;
126167 }
127168
169+ if (isError) {
170+ protocol::ErrorSupport errors;
171+ std::unique_ptr<protocol::Runtime::RemoteObject> exceptionObject =
172+ protocol::Runtime::RemoteObject::parse (protocolValue.get (), &errors);
173+ if (!exceptionObject) {
174+ errorString = errors.errors ();
175+ callback->sendFailure (errorString);
176+ return ;
177+ }
178+
179+ std::unique_ptr<protocol::Runtime::ExceptionDetails> exDetails =
180+ protocol::Runtime::ExceptionDetails::create ()
181+ .setExceptionId (m_session->inspector ()->nextExceptionId ())
182+ .setText (exceptionObject->getDescription (" Uncaught" ))
183+ .setLineNumber (0 )
184+ .setColumnNumber (0 )
185+ .build ();
186+
187+ exDetails->setException (std::move (exceptionObject));
188+
189+ exceptionDetails = std::move (exDetails);
190+ }
191+
128192 callback->sendSuccess (std::move (remoteObject), exceptionDetails);
129193}
130194
0 commit comments