blob: 8b290faebc1865519b0e7c514f497585dfc0bbda [file] [log] [blame]
yurys@chromium.org3cd77102010-02-04 09:38:40 +00001/*
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +00002 * Copyright (C) 2013 Apple Inc. All rights reserved.
pfeldman@chromium.org52ba9e62012-06-19 23:21:03 +00003 * Copyright (C) 2012 Google Inc. All rights reserved.
yurys@chromium.org3cd77102010-02-04 09:38:40 +00004 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "config.h"
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +000033#include "InjectedScript.h"
yurys@chromium.org3cd77102010-02-04 09:38:40 +000034
ross.kirsling@sony.come257a3b2020-05-19 23:56:00 +000035#include "JSCInlines.h"
drousso@apple.com3a3286d2019-01-15 08:25:33 +000036#include "JSLock.h"
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +000037#include "ScriptFunctionCall.h"
38#include "ScriptObject.h"
bburg@apple.com61f04d62017-11-28 19:58:16 +000039#include <wtf/JSONValues.h>
drousso@apple.com3a3286d2019-01-15 08:25:33 +000040#include <wtf/Vector.h>
weinig@apple.com3f5ab022012-09-06 17:36:48 +000041#include <wtf/text/WTFString.h>
yurys@chromium.org3cd77102010-02-04 09:38:40 +000042
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +000043namespace Inspector {
yurys@chromium.org3cd77102010-02-04 09:38:40 +000044
yurys@chromium.org7256c3a2011-04-04 08:08:28 +000045InjectedScript::InjectedScript()
utatane.tea@gmail.com84077632018-06-23 08:39:34 +000046 : InjectedScriptBase("InjectedScript"_s)
yurys@chromium.org7256c3a2011-04-04 08:08:28 +000047{
48}
49
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +000050InjectedScript::InjectedScript(Deprecated::ScriptObject injectedScriptObject, InspectorEnvironment* environment)
utatane.tea@gmail.com84077632018-06-23 08:39:34 +000051 : InjectedScriptBase("InjectedScript"_s, injectedScriptObject, environment)
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +000052{
53}
54
55InjectedScript::~InjectedScript()
yurys@chromium.org3cd77102010-02-04 09:38:40 +000056{
57}
58
darin@apple.coma4ddc782021-05-30 16:11:40 +000059void InjectedScript::execute(Protocol::ErrorString& errorString, const String& functionString, ExecuteOptions&& options, RefPtr<Protocol::Runtime::RemoteObject>& result, std::optional<bool>& wasThrown, std::optional<int>& savedResultIndex)
drousso@apple.com3a3286d2019-01-15 08:25:33 +000060{
61 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "execute"_s, inspectorEnvironment()->functionCallHandler());
62 function.appendArgument(functionString);
63 function.appendArgument(options.objectGroup);
64 function.appendArgument(options.includeCommandLineAPI);
65 function.appendArgument(options.returnByValue);
66 function.appendArgument(options.generatePreview);
67 function.appendArgument(options.saveResult);
68 function.appendArgument(arrayFromVector(WTFMove(options.args)));
69 makeEvalCall(errorString, function, result, wasThrown, savedResultIndex);
70}
71
darin@apple.coma4ddc782021-05-30 16:11:40 +000072void InjectedScript::evaluate(Protocol::ErrorString& errorString, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, bool saveResult, RefPtr<Protocol::Runtime::RemoteObject>& result, std::optional<bool>& wasThrown, std::optional<int>& savedResultIndex)
yurys@chromium.org3cd77102010-02-04 09:38:40 +000073{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +000074 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "evaluate"_s, inspectorEnvironment()->functionCallHandler());
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +000075 function.appendArgument(expression);
76 function.appendArgument(objectGroup);
pfeldman@chromium.org9c856a32011-01-26 10:19:04 +000077 function.appendArgument(includeCommandLineAPI);
pfeldman@chromium.orgf40526f2011-08-04 16:09:00 +000078 function.appendArgument(returnByValue);
yurys@chromium.orgc3ce0942012-11-01 12:17:00 +000079 function.appendArgument(generatePreview);
joepeck@webkit.org029930dd2015-02-27 01:15:23 +000080 function.appendArgument(saveResult);
81 makeEvalCall(errorString, function, result, wasThrown, savedResultIndex);
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +000082}
yurys@chromium.org774d8db2010-03-31 15:24:19 +000083
drousso@apple.com8a0bc012018-12-04 09:08:42 +000084void InjectedScript::awaitPromise(const String& promiseObjectId, bool returnByValue, bool generatePreview, bool saveResult, AsyncCallCallback&& callback)
85{
86 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "awaitPromise"_s, inspectorEnvironment()->functionCallHandler());
87 function.appendArgument(promiseObjectId);
88 function.appendArgument(returnByValue);
89 function.appendArgument(generatePreview);
90 function.appendArgument(saveResult);
91 makeAsyncCall(function, WTFMove(callback));
92}
93
darin@apple.coma4ddc782021-05-30 16:11:40 +000094void InjectedScript::callFunctionOn(Protocol::ErrorString& errorString, const String& objectId, const String& expression, const String& arguments, bool returnByValue, bool generatePreview, RefPtr<Protocol::Runtime::RemoteObject>& result, std::optional<bool>& wasThrown)
pfeldman@chromium.orgad390c62011-03-02 17:39:48 +000095{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +000096 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "callFunctionOn"_s, inspectorEnvironment()->functionCallHandler());
pfeldman@chromium.org78278562011-03-21 07:42:31 +000097 function.appendArgument(objectId);
pfeldman@chromium.orgad390c62011-03-02 17:39:48 +000098 function.appendArgument(expression);
pfeldman@chromium.org19665e82011-07-26 10:09:52 +000099 function.appendArgument(arguments);
pfeldman@chromium.orgf40526f2011-08-04 16:09:00 +0000100 function.appendArgument(returnByValue);
yurys@chromium.orgc3ce0942012-11-01 12:17:00 +0000101 function.appendArgument(generatePreview);
drousso@apple.com8a0bc012018-12-04 09:08:42 +0000102
darin@apple.coma4ddc782021-05-30 16:11:40 +0000103 std::optional<int> savedResultIndex;
drousso@apple.com8a0bc012018-12-04 09:08:42 +0000104 makeEvalCall(errorString, function, result, wasThrown, savedResultIndex);
105 ASSERT(!savedResultIndex);
pfeldman@chromium.orgad390c62011-03-02 17:39:48 +0000106}
107
darin@apple.coma4ddc782021-05-30 16:11:40 +0000108void InjectedScript::evaluateOnCallFrame(Protocol::ErrorString& errorString, JSC::JSValue callFrames, const String& callFrameId, const String& expression, const String& objectGroup, bool includeCommandLineAPI, bool returnByValue, bool generatePreview, bool saveResult, RefPtr<Protocol::Runtime::RemoteObject>& result, std::optional<bool>& wasThrown, std::optional<int>& savedResultIndex)
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000109{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000110 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "evaluateOnCallFrame"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org83c5e072011-04-27 08:55:09 +0000111 function.appendArgument(callFrames);
pfeldman@chromium.org78278562011-03-21 07:42:31 +0000112 function.appendArgument(callFrameId);
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000113 function.appendArgument(expression);
114 function.appendArgument(objectGroup);
pfeldman@chromium.org9c856a32011-01-26 10:19:04 +0000115 function.appendArgument(includeCommandLineAPI);
pfeldman@chromium.orgf6458062011-08-10 14:37:23 +0000116 function.appendArgument(returnByValue);
yurys@chromium.orgc3ce0942012-11-01 12:17:00 +0000117 function.appendArgument(generatePreview);
joepeck@webkit.org029930dd2015-02-27 01:15:23 +0000118 function.appendArgument(saveResult);
119 makeEvalCall(errorString, function, result, wasThrown, savedResultIndex);
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000120}
121
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000122void InjectedScript::getFunctionDetails(Protocol::ErrorString& errorString, const String& functionId, RefPtr<Protocol::Debugger::FunctionDetails>& result)
yurys@chromium.org0114f052011-11-14 09:27:25 +0000123{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000124 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getFunctionDetails"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org0114f052011-11-14 09:27:25 +0000125 function.appendArgument(functionId);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000126
bburg@apple.com3d715712017-12-12 21:49:13 +0000127 RefPtr<JSON::Value> resultValue = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000128 if (!resultValue || resultValue->type() != JSON::Value::Type::Object) {
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000129 errorString = resultValue->asString();
130 if (!errorString)
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000131 errorString = "Internal error"_s;
yurys@chromium.org0114f052011-11-14 09:27:25 +0000132 return;
133 }
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000134
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000135 result = Protocol::BindingTraits<Protocol::Debugger::FunctionDetails>::runtimeCast(resultValue.releaseNonNull());
yurys@chromium.org0114f052011-11-14 09:27:25 +0000136}
137
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000138void InjectedScript::functionDetails(Protocol::ErrorString& errorString, JSC::JSValue value, RefPtr<Protocol::Debugger::FunctionDetails>& result)
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000139{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000140 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "functionDetails"_s, inspectorEnvironment()->functionCallHandler());
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000141 function.appendArgument(value);
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000142
bburg@apple.com3d715712017-12-12 21:49:13 +0000143 RefPtr<JSON::Value> resultValue = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000144 if (!resultValue || resultValue->type() != JSON::Value::Type::Object) {
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000145 errorString = resultValue->asString();
146 if (!errorString)
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000147 errorString = "Internal error"_s;
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000148 return;
149 }
150
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000151 result = Protocol::BindingTraits<Protocol::Debugger::FunctionDetails>::runtimeCast(resultValue.releaseNonNull());
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000152}
153
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000154void InjectedScript::getPreview(Protocol::ErrorString& errorString, const String& objectId, RefPtr<Protocol::Runtime::ObjectPreview>& result)
joepeck@webkit.org34919fc2017-06-22 21:12:01 +0000155{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000156 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getPreview"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.org34919fc2017-06-22 21:12:01 +0000157 function.appendArgument(objectId);
158
bburg@apple.com3d715712017-12-12 21:49:13 +0000159 RefPtr<JSON::Value> resultValue = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000160 if (!resultValue || resultValue->type() != JSON::Value::Type::Object) {
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000161 errorString = resultValue->asString();
162 if (!errorString)
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000163 errorString = "Internal error"_s;
joepeck@webkit.org34919fc2017-06-22 21:12:01 +0000164 return;
165 }
166
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000167 result = Protocol::BindingTraits<Protocol::Runtime::ObjectPreview>::runtimeCast(resultValue.releaseNonNull());
joepeck@webkit.org34919fc2017-06-22 21:12:01 +0000168}
169
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000170void InjectedScript::getProperties(Protocol::ErrorString& errorString, const String& objectId, bool ownProperties, int fetchStart, int fetchCount, bool generatePreview, RefPtr<JSON::ArrayOf<Protocol::Runtime::PropertyDescriptor>>& properties)
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000171{
drousso@apple.comeb748032019-09-19 05:58:27 +0000172 ASSERT(fetchStart >= 0);
173 ASSERT(fetchCount >= 0);
174
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000175 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getProperties"_s, inspectorEnvironment()->functionCallHandler());
pfeldman@chromium.org78278562011-03-21 07:42:31 +0000176 function.appendArgument(objectId);
pfeldman@chromium.org6da2ce12011-08-10 13:41:30 +0000177 function.appendArgument(ownProperties);
drousso@apple.comeb748032019-09-19 05:58:27 +0000178 function.appendArgument(fetchStart);
179 function.appendArgument(fetchCount);
joepeck@webkit.org1af2d8d2015-02-25 00:31:42 +0000180 function.appendArgument(generatePreview);
181
bburg@apple.com3d715712017-12-12 21:49:13 +0000182 RefPtr<JSON::Value> result = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000183 if (!result || result->type() != JSON::Value::Type::Array) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000184 errorString = "Internal error"_s;
joepeck@webkit.org1af2d8d2015-02-25 00:31:42 +0000185 return;
186 }
187
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000188 properties = Protocol::BindingTraits<JSON::ArrayOf<Protocol::Runtime::PropertyDescriptor>>::runtimeCast(result.releaseNonNull());
joepeck@webkit.org1af2d8d2015-02-25 00:31:42 +0000189}
190
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000191void InjectedScript::getDisplayableProperties(Protocol::ErrorString& errorString, const String& objectId, int fetchStart, int fetchCount, bool generatePreview, RefPtr<JSON::ArrayOf<Protocol::Runtime::PropertyDescriptor>>& properties)
joepeck@webkit.org1af2d8d2015-02-25 00:31:42 +0000192{
drousso@apple.comeb748032019-09-19 05:58:27 +0000193 ASSERT(fetchStart >= 0);
194 ASSERT(fetchCount >= 0);
195
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000196 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getDisplayableProperties"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.org1af2d8d2015-02-25 00:31:42 +0000197 function.appendArgument(objectId);
drousso@apple.comeb748032019-09-19 05:58:27 +0000198 function.appendArgument(fetchStart);
199 function.appendArgument(fetchCount);
commit-queue@webkit.org4b254d62015-02-22 03:50:34 +0000200 function.appendArgument(generatePreview);
pfeldman@chromium.orge573fa32011-03-17 18:10:15 +0000201
bburg@apple.com3d715712017-12-12 21:49:13 +0000202 RefPtr<JSON::Value> result = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000203 if (!result || result->type() != JSON::Value::Type::Array) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000204 errorString = "Internal error"_s;
pfeldman@chromium.orge573fa32011-03-17 18:10:15 +0000205 return;
206 }
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000207
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000208 properties = Protocol::BindingTraits<JSON::ArrayOf<Protocol::Runtime::PropertyDescriptor>>::runtimeCast(result.releaseNonNull());
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000209}
210
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000211void InjectedScript::getInternalProperties(Protocol::ErrorString& errorString, const String& objectId, bool generatePreview, RefPtr<JSON::ArrayOf<Protocol::Runtime::InternalPropertyDescriptor>>& properties)
commit-queue@webkit.orgc639f2b2012-10-04 16:12:42 +0000212{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000213 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getInternalProperties"_s, inspectorEnvironment()->functionCallHandler());
commit-queue@webkit.orgc639f2b2012-10-04 16:12:42 +0000214 function.appendArgument(objectId);
commit-queue@webkit.org4b254d62015-02-22 03:50:34 +0000215 function.appendArgument(generatePreview);
commit-queue@webkit.orgc639f2b2012-10-04 16:12:42 +0000216
bburg@apple.com3d715712017-12-12 21:49:13 +0000217 RefPtr<JSON::Value> result = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000218 if (!result || result->type() != JSON::Value::Type::Array) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000219 errorString = "Internal error"_s;
commit-queue@webkit.orgc639f2b2012-10-04 16:12:42 +0000220 return;
221 }
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000222
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000223 auto array = Protocol::BindingTraits<JSON::ArrayOf<Protocol::Runtime::InternalPropertyDescriptor>>::runtimeCast(result.releaseNonNull());
224 if (array->length())
225 properties = WTFMove(array);
joepeck@webkit.org763bbbd2015-01-29 19:25:16 +0000226}
227
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000228void InjectedScript::getCollectionEntries(Protocol::ErrorString& errorString, const String& objectId, const String& objectGroup, int fetchStart, int fetchCount, RefPtr<JSON::ArrayOf<Protocol::Runtime::CollectionEntry>>& entries)
joepeck@webkit.org763bbbd2015-01-29 19:25:16 +0000229{
drousso@apple.comeb748032019-09-19 05:58:27 +0000230 ASSERT(fetchStart >= 0);
231 ASSERT(fetchCount >= 0);
232
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000233 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "getCollectionEntries"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.org763bbbd2015-01-29 19:25:16 +0000234 function.appendArgument(objectId);
235 function.appendArgument(objectGroup);
drousso@apple.comeb748032019-09-19 05:58:27 +0000236 function.appendArgument(fetchStart);
237 function.appendArgument(fetchCount);
joepeck@webkit.org763bbbd2015-01-29 19:25:16 +0000238
bburg@apple.com3d715712017-12-12 21:49:13 +0000239 RefPtr<JSON::Value> result = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000240 if (!result || result->type() != JSON::Value::Type::Array) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000241 errorString = "Internal error"_s;
joepeck@webkit.org763bbbd2015-01-29 19:25:16 +0000242 return;
243 }
244
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000245 entries = Protocol::BindingTraits<JSON::ArrayOf<Protocol::Runtime::CollectionEntry>>::runtimeCast(result.releaseNonNull());
commit-queue@webkit.orgc639f2b2012-10-04 16:12:42 +0000246}
247
darin@apple.coma4ddc782021-05-30 16:11:40 +0000248void InjectedScript::saveResult(Protocol::ErrorString& errorString, const String& callArgumentJSON, std::optional<int>& savedResultIndex)
joepeck@webkit.orgcc05b502015-03-03 02:24:49 +0000249{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000250 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "saveResult"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.orgcc05b502015-03-03 02:24:49 +0000251 function.appendArgument(callArgumentJSON);
252
bburg@apple.com3d715712017-12-12 21:49:13 +0000253 RefPtr<JSON::Value> result = makeCall(function);
bburg@apple.com61f04d62017-11-28 19:58:16 +0000254 if (!result || result->type() != JSON::Value::Type::Integer) {
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000255 errorString = "Internal error"_s;
joepeck@webkit.orgcc05b502015-03-03 02:24:49 +0000256 return;
257 }
258
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000259 savedResultIndex = result->asInteger();
joepeck@webkit.orgcc05b502015-03-03 02:24:49 +0000260}
261
darin@apple.com9c3ff982018-02-17 19:10:15 +0000262Ref<JSON::ArrayOf<Protocol::Debugger::CallFrame>> InjectedScript::wrapCallFrames(JSC::JSValue callFrames) const
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000263{
264 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000265 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "wrapCallFrames"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org83c5e072011-04-27 08:55:09 +0000266 function.appendArgument(callFrames);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000267
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000268 auto callResult = callFunctionWithEvalEnabled(function);
269 ASSERT(callResult);
270 if (!callResult || !callResult.value())
darin@apple.com9c3ff982018-02-17 19:10:15 +0000271 return JSON::ArrayOf<Protocol::Debugger::CallFrame>::create();
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000272
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000273 RefPtr<JSON::Value> result = toInspectorValue(globalObject(), callResult.value());
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000274 if (result && result->type() == JSON::Value::Type::Array)
275 return Protocol::BindingTraits<JSON::ArrayOf<Protocol::Debugger::CallFrame>>::runtimeCast(result.releaseNonNull());
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000276
darin@apple.com9c3ff982018-02-17 19:10:15 +0000277 return JSON::ArrayOf<Protocol::Debugger::CallFrame>::create();
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000278}
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000279
darin@apple.com9c3ff982018-02-17 19:10:15 +0000280RefPtr<Protocol::Runtime::RemoteObject> InjectedScript::wrapObject(JSC::JSValue value, const String& groupName, bool generatePreview) const
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000281{
282 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000283 Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapObject"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000284 wrapFunction.appendArgument(value);
pfeldman@chromium.orgad390c62011-03-02 17:39:48 +0000285 wrapFunction.appendArgument(groupName);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000286 wrapFunction.appendArgument(hasAccessToInspectedScriptState());
pfeldman@chromium.org279884b2012-08-09 15:13:56 +0000287 wrapFunction.appendArgument(generatePreview);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000288
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000289 auto callResult = callFunctionWithEvalEnabled(wrapFunction);
290 if (!callResult)
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000291 return nullptr;
292
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000293 auto resultValue = toInspectorValue(globalObject(), callResult.value());
294 if (!resultValue)
295 return nullptr;
burg@cs.washington.eduba2d1032014-09-30 03:30:54 +0000296
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000297 auto resultObject = resultValue->asObject();
298 if (!resultObject)
299 return nullptr;
300
301 return Protocol::BindingTraits<Protocol::Runtime::RemoteObject>::runtimeCast(resultObject.releaseNonNull());
pfeldman@chromium.orgc2268702013-02-18 10:51:37 +0000302}
303
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000304RefPtr<Protocol::Runtime::RemoteObject> InjectedScript::wrapJSONString(const String& json, const String& groupName, bool generatePreview) const
305{
306 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000307 Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapJSONString"_s, inspectorEnvironment()->functionCallHandler());
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000308 wrapFunction.appendArgument(json);
309 wrapFunction.appendArgument(groupName);
310 wrapFunction.appendArgument(generatePreview);
311
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000312 auto callResult = callFunctionWithEvalEnabled(wrapFunction);
313 if (!callResult)
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000314 return nullptr;
315
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000316 if (callResult.value().isNull())
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000317 return nullptr;
318
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000319 auto resultValue = toInspectorValue(globalObject(), callResult.value());
320 if (!resultValue)
321 return nullptr;
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000322
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000323 auto resultObject = resultValue->asObject();
324 if (!resultObject)
325 return nullptr;
326
327 return Protocol::BindingTraits<Protocol::Runtime::RemoteObject>::runtimeCast(resultObject.releaseNonNull());
eric.carlson@apple.com5a5e76a2017-12-12 00:41:54 +0000328}
329
darin@apple.com9c3ff982018-02-17 19:10:15 +0000330RefPtr<Protocol::Runtime::RemoteObject> InjectedScript::wrapTable(JSC::JSValue table, JSC::JSValue columns) const
pfeldman@chromium.orgc2268702013-02-18 10:51:37 +0000331{
332 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000333 Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), "wrapTable"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000334 wrapFunction.appendArgument(hasAccessToInspectedScriptState());
pfeldman@chromium.orgc2268702013-02-18 10:51:37 +0000335 wrapFunction.appendArgument(table);
darin@apple.com6be924b2016-04-16 02:25:56 +0000336 if (!columns)
pfeldman@chromium.orgc2268702013-02-18 10:51:37 +0000337 wrapFunction.appendArgument(false);
338 else
339 wrapFunction.appendArgument(columns);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000340
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000341 auto callResult = callFunctionWithEvalEnabled(wrapFunction);
342 if (!callResult)
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000343 return nullptr;
344
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000345 auto resultValue = toInspectorValue(globalObject(), callResult.value());
346 if (!resultValue)
347 return nullptr;
burg@cs.washington.eduba2d1032014-09-30 03:30:54 +0000348
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000349 auto resultObject = resultValue->asObject();
350 if (!resultObject)
351 return nullptr;
352
353 return Protocol::BindingTraits<Protocol::Runtime::RemoteObject>::runtimeCast(resultObject.releaseNonNull());
pfeldman@chromium.org351b1a92011-02-24 14:43:25 +0000354}
355
darin@apple.com9c3ff982018-02-17 19:10:15 +0000356RefPtr<Protocol::Runtime::ObjectPreview> InjectedScript::previewValue(JSC::JSValue value) const
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000357{
358 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000359 Deprecated::ScriptFunctionCall wrapFunction(injectedScriptObject(), "previewValue"_s, inspectorEnvironment()->functionCallHandler());
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000360 wrapFunction.appendArgument(value);
361
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000362 auto callResult = callFunctionWithEvalEnabled(wrapFunction);
363 if (!callResult)
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000364 return nullptr;
365
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000366 auto resultValue = toInspectorValue(globalObject(), callResult.value());
367 if (!resultValue)
368 return nullptr;
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000369
drousso@apple.com7b9e7092020-09-10 19:23:17 +0000370 auto resultObject = resultValue->asObject();
371 if (!resultObject)
372 return nullptr;
373
374 return Protocol::BindingTraits<Protocol::Runtime::ObjectPreview>::runtimeCast(resultObject.releaseNonNull());
commit-queue@webkit.org46f46f22016-03-10 06:15:25 +0000375}
376
drousso@apple.com926dd972019-03-19 19:31:31 +0000377void InjectedScript::setEventValue(JSC::JSValue value)
378{
379 ASSERT(!hasNoValue());
380 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "setEventValue"_s, inspectorEnvironment()->functionCallHandler());
381 function.appendArgument(value);
382 makeCall(function);
383}
384
385void InjectedScript::clearEventValue()
386{
387 ASSERT(!hasNoValue());
388 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "clearEventValue"_s, inspectorEnvironment()->functionCallHandler());
389 makeCall(function);
390}
391
darin@apple.com6be924b2016-04-16 02:25:56 +0000392void InjectedScript::setExceptionValue(JSC::JSValue value)
joepeck@webkit.orgde4d1cf2014-11-19 23:49:36 +0000393{
394 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000395 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "setExceptionValue"_s, inspectorEnvironment()->functionCallHandler());
joepeck@webkit.orgde4d1cf2014-11-19 23:49:36 +0000396 function.appendArgument(value);
bburg@apple.com3d715712017-12-12 21:49:13 +0000397 makeCall(function);
joepeck@webkit.orgde4d1cf2014-11-19 23:49:36 +0000398}
399
400void InjectedScript::clearExceptionValue()
401{
402 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000403 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "clearExceptionValue"_s, inspectorEnvironment()->functionCallHandler());
bburg@apple.com3d715712017-12-12 21:49:13 +0000404 makeCall(function);
joepeck@webkit.orgde4d1cf2014-11-19 23:49:36 +0000405}
406
darin@apple.com6be924b2016-04-16 02:25:56 +0000407JSC::JSValue InjectedScript::findObjectById(const String& objectId) const
yurys@chromium.org503c0f82012-05-11 15:05:42 +0000408{
409 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000410 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "findObjectById"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org503c0f82012-05-11 15:05:42 +0000411 function.appendArgument(objectId);
412
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000413 auto callResult = callFunctionWithEvalEnabled(function);
414 ASSERT(callResult);
415 if (!callResult)
416 return { };
417 return callResult.value();
yurys@chromium.org503c0f82012-05-11 15:05:42 +0000418}
419
darin@apple.com6be924b2016-04-16 02:25:56 +0000420void InjectedScript::inspectObject(JSC::JSValue value)
pfeldman@chromium.org351b1a92011-02-24 14:43:25 +0000421{
422 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000423 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "inspectObject"_s, inspectorEnvironment()->functionCallHandler());
commit-queue@webkit.orgc15383f2013-12-21 02:47:10 +0000424 function.appendArgument(value);
bburg@apple.com3d715712017-12-12 21:49:13 +0000425 makeCall(function);
commit-queue@webkit.orgc15383f2013-12-21 02:47:10 +0000426}
427
428void InjectedScript::releaseObject(const String& objectId)
429{
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000430 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "releaseObject"_s, inspectorEnvironment()->functionCallHandler());
commit-queue@webkit.orgc15383f2013-12-21 02:47:10 +0000431 function.appendArgument(objectId);
bburg@apple.com3d715712017-12-12 21:49:13 +0000432 makeCall(function);
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000433}
434
loislo@chromium.orgd7d49c22011-03-04 15:05:59 +0000435void InjectedScript::releaseObjectGroup(const String& objectGroup)
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000436{
437 ASSERT(!hasNoValue());
utatane.tea@gmail.com84077632018-06-23 08:39:34 +0000438 Deprecated::ScriptFunctionCall releaseFunction(injectedScriptObject(), "releaseObjectGroup"_s, inspectorEnvironment()->functionCallHandler());
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000439 releaseFunction.appendArgument(objectGroup);
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000440
drousso@apple.comb6d9ffe2019-09-20 08:28:09 +0000441 auto callResult = callFunctionWithEvalEnabled(releaseFunction);
442 ASSERT_UNUSED(callResult, callResult);
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000443}
pfeldman@chromium.org654a83d2011-01-19 09:00:58 +0000444
drousso@apple.comddfe77b2020-10-27 06:39:08 +0000445JSC::JSObject* InjectedScript::createCommandLineAPIObject(JSC::JSValue callFrame) const
446{
447 ASSERT(!hasNoValue());
448 Deprecated::ScriptFunctionCall function(injectedScriptObject(), "createCommandLineAPIObject"_s, inspectorEnvironment()->functionCallHandler());
449 function.appendArgument(callFrame);
450
451 auto callResult = callFunctionWithEvalEnabled(function);
452 ASSERT(callResult);
453 return callResult ? asObject(callResult.value()) : nullptr;
454}
455
drousso@apple.com3a3286d2019-01-15 08:25:33 +0000456JSC::JSValue InjectedScript::arrayFromVector(Vector<JSC::JSValue>&& vector)
457{
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000458 JSC::JSGlobalObject* globalObject = this->globalObject();
459 if (!globalObject)
drousso@apple.com3a3286d2019-01-15 08:25:33 +0000460 return JSC::jsUndefined();
461
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000462 JSC::JSLockHolder lock(globalObject);
drousso@apple.com3a3286d2019-01-15 08:25:33 +0000463
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000464 JSC::JSArray* array = JSC::constructEmptyArray(globalObject, nullptr);
drousso@apple.com3a3286d2019-01-15 08:25:33 +0000465 if (!array)
466 return JSC::jsUndefined();
467
468 for (auto& item : vector)
ysuzuki@apple.com52e98bb2019-10-22 09:24:48 +0000469 array->putDirectIndex(globalObject, array->length(), item);
drousso@apple.com3a3286d2019-01-15 08:25:33 +0000470
471 return array;
472}
473
joepeck@webkit.org69a4eaf2014-01-09 19:00:48 +0000474} // namespace Inspector
yurys@chromium.org3cd77102010-02-04 09:38:40 +0000475