1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "tooling/base/pt_returns.h"
17
18 namespace panda::ecmascript::tooling {
ToJson() const19 std::unique_ptr<PtJson> EnableReturns::ToJson() const
20 {
21 std::unique_ptr<PtJson> result = PtJson::CreateObject();
22
23 result->Add("debuggerId", std::to_string(debuggerId_).c_str());
24
25 return result;
26 }
27
ToJson() const28 std::unique_ptr<PtJson> SetBreakpointByUrlReturns::ToJson() const
29 {
30 std::unique_ptr<PtJson> result = PtJson::CreateObject();
31
32 result->Add("breakpointId", id_.c_str());
33 std::unique_ptr<PtJson> array = PtJson::CreateArray();
34 size_t len = locations_.size();
35 for (size_t i = 0; i < len; i++) {
36 ASSERT(locations_[i] != nullptr);
37 std::unique_ptr<PtJson> location = locations_[i]->ToJson();
38 array->Push(location);
39 }
40 result->Add("locations", array);
41
42 return result;
43 }
44
ToJson() const45 std::unique_ptr<PtJson> GetPossibleAndSetBreakpointByUrlReturns::ToJson() const
46 {
47 std::unique_ptr<PtJson> result = PtJson::CreateObject();
48
49 std::unique_ptr<PtJson> array = PtJson::CreateArray();
50 size_t len = locations_.size();
51 for (size_t i = 0; i < len; i++) {
52 ASSERT(locations_[i] != nullptr);
53 std::unique_ptr<PtJson> location = locations_[i]->ToJson();
54 array->Push(location);
55 }
56 result->Add("locations", array);
57
58 return result;
59 }
60
ToJson() const61 std::unique_ptr<PtJson> EvaluateOnCallFrameReturns::ToJson() const
62 {
63 std::unique_ptr<PtJson> result = PtJson::CreateObject();
64
65 ASSERT(result_ != nullptr);
66 result->Add("result", result_->ToJson());
67 if (exceptionDetails_) {
68 ASSERT(exceptionDetails_.value() != nullptr);
69 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
70 }
71
72 return result;
73 }
74
ToJson() const75 std::unique_ptr<PtJson> GetPossibleBreakpointsReturns::ToJson() const
76 {
77 std::unique_ptr<PtJson> result = PtJson::CreateObject();
78
79 std::unique_ptr<PtJson> array = PtJson::CreateArray();
80 size_t len = locations_.size();
81 for (size_t i = 0; i < len; i++) {
82 ASSERT(locations_[i] != nullptr);
83 std::unique_ptr<PtJson> location = locations_[i]->ToJson();
84 array->Push(location);
85 }
86 result->Add("locations", array);
87
88 return result;
89 }
90
ToJson() const91 std::unique_ptr<PtJson> GetScriptSourceReturns::ToJson() const
92 {
93 std::unique_ptr<PtJson> result = PtJson::CreateObject();
94
95 result->Add("scriptSource", scriptSource_.c_str());
96 if (bytecode_) {
97 result->Add("bytecode", bytecode_->c_str());
98 }
99
100 return result;
101 }
102
ToJson() const103 std::unique_ptr<PtJson> RestartFrameReturns::ToJson() const
104 {
105 std::unique_ptr<PtJson> result = PtJson::CreateObject();
106
107 std::unique_ptr<PtJson> array = PtJson::CreateArray();
108 size_t len = callFrames_.size();
109 for (size_t i = 0; i < len; i++) {
110 ASSERT(callFrames_[i] != nullptr);
111 std::unique_ptr<PtJson> location = callFrames_[i]->ToJson();
112 array->Push(location);
113 }
114 result->Add("callFrames", array);
115
116 return result;
117 }
118
ToJson() const119 std::unique_ptr<PtJson> SearchInContentReturns::ToJson() const
120 {
121 std::unique_ptr<PtJson> result = PtJson::CreateObject();
122
123 std::unique_ptr<PtJson> array = PtJson::CreateArray();
124 size_t len = result_.size();
125 for (size_t i = 0; i < len; i++) {
126 ASSERT(result_[i] != nullptr);
127 std::unique_ptr<PtJson> res = result_[i]->ToJson();
128 array->Push(res);
129 }
130 result->Add("result", array);
131
132 return result;
133 }
134
ToJson() const135 std::unique_ptr<PtJson> SetBreakpointReturns::ToJson() const
136 {
137 std::unique_ptr<PtJson> result = PtJson::CreateObject();
138
139 result->Add("breakpointId", breakpointId_.c_str());
140 ASSERT(location_ != nullptr);
141 result->Add("actualLocation", location_->ToJson());
142
143 return result;
144 }
145
ToJson() const146 std::unique_ptr<PtJson> SetInstrumentationBreakpointReturns::ToJson() const
147 {
148 std::unique_ptr<PtJson> result = PtJson::CreateObject();
149
150 result->Add("breakpointId", breakpointId_.c_str());
151
152 return result;
153 }
154
ToJson() const155 std::unique_ptr<PtJson> SetScriptSourceReturns::ToJson() const
156 {
157 std::unique_ptr<PtJson> result = PtJson::CreateObject();
158
159 if (callFrames_) {
160 std::unique_ptr<PtJson> array = PtJson::CreateArray();
161 size_t len = callFrames_->size();
162 for (size_t i = 0; i < len; i++) {
163 ASSERT(callFrames_.value()[i] != nullptr);
164 std::unique_ptr<PtJson> location = callFrames_.value()[i]->ToJson();
165 array->Push(location);
166 }
167 result->Add("callFrames", array);
168 }
169 if (stackChanged_) {
170 result->Add("stackChanged", stackChanged_.value());
171 }
172 if (exceptionDetails_) {
173 ASSERT(exceptionDetails_.value() != nullptr);
174 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
175 }
176
177 return result;
178 }
179
ToJson() const180 std::unique_ptr<PtJson> GetPropertiesReturns::ToJson() const
181 {
182 std::unique_ptr<PtJson> result = PtJson::CreateObject();
183
184 std::unique_ptr<PtJson> array = PtJson::CreateArray();
185 size_t len = result_.size();
186 for (size_t i = 0; i < len; i++) {
187 ASSERT(result_[i] != nullptr);
188 std::unique_ptr<PtJson> location = result_[i]->ToJson();
189 array->Push(location);
190 }
191 result->Add("result", array);
192 if (internalPropertyDescripties_) {
193 array = PtJson::CreateArray();
194 len = internalPropertyDescripties_->size();
195 for (size_t i = 0; i < len; i++) {
196 ASSERT(internalPropertyDescripties_.value()[i] != nullptr);
197 std::unique_ptr<PtJson> location = internalPropertyDescripties_.value()[i]->ToJson();
198 array->Push(location);
199 }
200 result->Add("internalProperties", array);
201 }
202 if (privateProperties_) {
203 array = PtJson::CreateArray();
204 len = privateProperties_->size();
205 for (size_t i = 0; i < len; i++) {
206 ASSERT(privateProperties_.value()[i] != nullptr);
207 std::unique_ptr<PtJson> location = privateProperties_.value()[i]->ToJson();
208 array->Push(location);
209 }
210 result->Add("privateProperties", array);
211 }
212 if (exceptionDetails_) {
213 ASSERT(exceptionDetails_.value() != nullptr);
214 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
215 }
216
217 return result;
218 }
219
ToJson() const220 std::unique_ptr<PtJson> CallFunctionOnReturns::ToJson() const
221 {
222 std::unique_ptr<PtJson> result = PtJson::CreateObject();
223
224 ASSERT(result_ != nullptr);
225 result->Add("result", result_->ToJson());
226 if (exceptionDetails_) {
227 ASSERT(exceptionDetails_.value() != nullptr);
228 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
229 }
230
231 return result;
232 }
233
ToJson() const234 std::unique_ptr<PtJson> StopSamplingReturns::ToJson() const
235 {
236 std::unique_ptr<PtJson> result = PtJson::CreateObject();
237
238 ASSERT(profile_ != nullptr);
239 result->Add("profile", profile_->ToJson());
240
241 return result;
242 }
243
ToJson() const244 std::unique_ptr<PtJson> GetHeapObjectIdReturns::ToJson() const
245 {
246 std::unique_ptr<PtJson> result = PtJson::CreateObject();
247
248 result->Add("heapSnapshotObjectId", std::to_string(heapSnapshotObjectId_).c_str());
249
250 return result;
251 }
252
ToJson() const253 std::unique_ptr<PtJson> GetObjectByHeapObjectIdReturns::ToJson() const
254 {
255 std::unique_ptr<PtJson> result = PtJson::CreateObject();
256
257 ASSERT(remoteObjectResult_ != nullptr);
258 result->Add("result", remoteObjectResult_->ToJson());
259
260 return result;
261 }
262
ToJson() const263 std::unique_ptr<PtJson> StopReturns::ToJson() const
264 {
265 std::unique_ptr<PtJson> result = PtJson::CreateObject();
266
267 ASSERT(profile_ != nullptr);
268 result->Add("profile", profile_->ToJson());
269
270 return result;
271 }
272
ToJson() const273 std::unique_ptr<PtJson> GetHeapUsageReturns::ToJson() const
274 {
275 std::unique_ptr<PtJson> result = PtJson::CreateObject();
276
277 result->Add("usedSize", usedSize_);
278 result->Add("totalSize", totalSize_);
279
280 return result;
281 }
282
Create(const PtJson & params)283 std::unique_ptr<GetHeapUsageReturns> GetHeapUsageReturns::Create(const PtJson ¶ms)
284 {
285 auto heapUsageReturns = std::make_unique<GetHeapUsageReturns>();
286 std::string error;
287 Result ret;
288
289 double usedSize;
290 ret = params.GetDouble("usedSize", &usedSize);
291 if (ret == Result::SUCCESS) {
292 heapUsageReturns->usedSize_ = usedSize;
293 } else {
294 error += "Unknown 'usedSize';";
295 }
296
297 double totalSize;
298 ret = params.GetDouble("totalSize", &totalSize);
299 if (ret == Result::SUCCESS) {
300 heapUsageReturns->totalSize_ = totalSize;
301 } else {
302 error += "Unknown 'totalSize';";
303 }
304
305 if (!error.empty()) {
306 LOG_DEBUGGER(ERROR) << "HeapUsageReturns::Create " << error;
307 return nullptr;
308 }
309
310 return heapUsageReturns;
311 }
312
ToJson() const313 std::unique_ptr<PtJson> GetBestEffortCoverageReturns::ToJson() const
314 {
315 std::unique_ptr<PtJson> result = PtJson::CreateObject();
316
317 std::unique_ptr<PtJson> array = PtJson::CreateArray();
318 size_t len = result_.size();
319 for (size_t i = 0; i < len; i++) {
320 ASSERT(result_[i] != nullptr);
321 std::unique_ptr<PtJson> scriptCoverage = result_[i]->ToJson();
322 array->Push(scriptCoverage);
323 }
324 result->Add("result", array);
325
326 return result;
327 }
328
ToJson() const329 std::unique_ptr<PtJson> StartPreciseCoverageReturns::ToJson() const
330 {
331 std::unique_ptr<PtJson> result = PtJson::CreateObject();
332
333 result->Add("timestamp", timestamp_);
334
335 return result;
336 }
337
ToJson() const338 std::unique_ptr<PtJson> TakePreciseCoverageReturns::ToJson() const
339 {
340 std::unique_ptr<PtJson> result = PtJson::CreateObject();
341
342 std::unique_ptr<PtJson> array = PtJson::CreateArray();
343 size_t len = result_.size();
344 for (size_t i = 0; i < len; i++) {
345 ASSERT(result_[i] != nullptr);
346 std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
347 array->Push(scriptTypeProfile);
348 }
349 result->Add("result", array);
350 result->Add("timestamp", timestamp_);
351
352 return result;
353 }
354
ToJson() const355 std::unique_ptr<PtJson> TakeTypeProfileReturns::ToJson() const
356 {
357 std::unique_ptr<PtJson> result = PtJson::CreateObject();
358
359 std::unique_ptr<PtJson> array = PtJson::CreateArray();
360 size_t len = result_.size();
361 for (size_t i = 0; i < len; i++) {
362 ASSERT(result_[i] != nullptr);
363 std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
364 array->Push(scriptTypeProfile);
365 }
366 result->Add("result", array);
367
368 return result;
369 }
370
ToJson() const371 std::unique_ptr<PtJson> GetCategoriesReturns::ToJson() const
372 {
373 std::unique_ptr<PtJson> result = PtJson::CreateObject();
374
375 std::unique_ptr<PtJson> categories = PtJson::CreateArray();
376 size_t len = categories_.size();
377 for (size_t i = 0; i < len; i++) {
378 categories->Push(categories_[i].c_str());
379 }
380 result->Add("categories", categories);
381
382 return result;
383 }
384
ToJson() const385 std::unique_ptr<PtJson> RequestMemoryDumpReturns::ToJson() const
386 {
387 std::unique_ptr<PtJson> result = PtJson::CreateObject();
388
389 result->Add("dumpGuid", dumpGuid_.c_str());
390 result->Add("success", success_);
391
392 return result;
393 }
394
ToJson() const395 std::unique_ptr<PtJson> GetNavigationHistoryReturns::ToJson() const
396 {
397 std::unique_ptr<PtJson> result = PtJson::CreateObject();
398 std::unique_ptr<PtJson> array = PtJson::CreateArray();
399
400 result->Add("currentIndex", 0);
401 array->Push(PtJson::CreateObject());
402 result->Add("entries", array);
403
404 return result;
405 }
406 } // namespace panda::ecmascript::tooling