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> DebuggerEnableReturns::ToJson() const
20 {
21     std::unique_ptr<PtJson> result = PtJson::CreateObject();
22 
23     result->Add("debuggerId", std::to_string(debuggerId_).c_str());
24     std::unique_ptr<PtJson> array = PtJson::CreateArray();
25     size_t len = protocols_.size();
26     for (size_t i = 0; i < len; i++) {
27         array->Push(protocols_[i].c_str());
28     }
29     result->Add("protocols", array);
30 
31     return result;
32 }
33 
ToJson() const34 std::unique_ptr<PtJson> EnableReturns::ToJson() const
35 {
36     std::unique_ptr<PtJson> result = PtJson::CreateObject();
37     std::unique_ptr<PtJson> array = PtJson::CreateArray();
38     size_t len = protocols_.size();
39     for (size_t i = 0; i < len; i++) {
40         array->Push(protocols_[i].c_str());
41     }
42     result->Add("protocols", array);
43 
44     return result;
45 }
46 
ToJson() const47 std::unique_ptr<PtJson> SetBreakpointByUrlReturns::ToJson() const
48 {
49     std::unique_ptr<PtJson> result = PtJson::CreateObject();
50 
51     result->Add("breakpointId", id_.c_str());
52     std::unique_ptr<PtJson> array = PtJson::CreateArray();
53     size_t len = locations_.size();
54     for (size_t i = 0; i < len; i++) {
55         ASSERT(locations_[i] != nullptr);
56         std::unique_ptr<PtJson> location = locations_[i]->ToJson();
57         array->Push(location);
58     }
59     result->Add("locations", array);
60 
61     return result;
62 }
63 
ToJson() const64 std::unique_ptr<PtJson> GetPossibleAndSetBreakpointByUrlReturns::ToJson() const
65 {
66     std::unique_ptr<PtJson> result = PtJson::CreateObject();
67 
68     std::unique_ptr<PtJson> array = PtJson::CreateArray();
69     size_t len = locations_.size();
70     for (size_t i = 0; i < len; i++) {
71         ASSERT(locations_[i] != nullptr);
72         std::unique_ptr<PtJson> location = locations_[i]->ToJson();
73         array->Push(location);
74     }
75     result->Add("locations", array);
76 
77     return result;
78 }
79 
ToJson() const80 std::unique_ptr<PtJson> EvaluateOnCallFrameReturns::ToJson() const
81 {
82     std::unique_ptr<PtJson> result = PtJson::CreateObject();
83 
84     ASSERT(result_ != nullptr);
85     result->Add("result", result_->ToJson());
86     if (exceptionDetails_) {
87         ASSERT(exceptionDetails_.value() != nullptr);
88         result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
89     }
90 
91     return result;
92 }
93 
ToJson() const94 std::unique_ptr<PtJson> GetPossibleBreakpointsReturns::ToJson() const
95 {
96     std::unique_ptr<PtJson> result = PtJson::CreateObject();
97 
98     std::unique_ptr<PtJson> array = PtJson::CreateArray();
99     size_t len = locations_.size();
100     for (size_t i = 0; i < len; i++) {
101         ASSERT(locations_[i] != nullptr);
102         std::unique_ptr<PtJson> location = locations_[i]->ToJson();
103         array->Push(location);
104     }
105     result->Add("locations", array);
106 
107     return result;
108 }
109 
ToJson() const110 std::unique_ptr<PtJson> GetScriptSourceReturns::ToJson() const
111 {
112     std::unique_ptr<PtJson> result = PtJson::CreateObject();
113 
114     result->Add("scriptSource", scriptSource_.c_str());
115     if (bytecode_) {
116         result->Add("bytecode", bytecode_->c_str());
117     }
118 
119     return result;
120 }
121 
ToJson() const122 std::unique_ptr<PtJson> RestartFrameReturns::ToJson() const
123 {
124     std::unique_ptr<PtJson> result = PtJson::CreateObject();
125 
126     std::unique_ptr<PtJson> array = PtJson::CreateArray();
127     size_t len = callFrames_.size();
128     for (size_t i = 0; i < len; i++) {
129         ASSERT(callFrames_[i] != nullptr);
130         std::unique_ptr<PtJson> location = callFrames_[i]->ToJson();
131         array->Push(location);
132     }
133     result->Add("callFrames", array);
134 
135     return result;
136 }
137 
ToJson() const138 std::unique_ptr<PtJson> SearchInContentReturns::ToJson() const
139 {
140     std::unique_ptr<PtJson> result = PtJson::CreateObject();
141 
142     std::unique_ptr<PtJson> array = PtJson::CreateArray();
143     size_t len = result_.size();
144     for (size_t i = 0; i < len; i++) {
145         ASSERT(result_[i] != nullptr);
146         std::unique_ptr<PtJson> res = result_[i]->ToJson();
147         array->Push(res);
148     }
149     result->Add("result", array);
150 
151     return result;
152 }
153 
ToJson() const154 std::unique_ptr<PtJson> SetBreakpointReturns::ToJson() const
155 {
156     std::unique_ptr<PtJson> result = PtJson::CreateObject();
157 
158     result->Add("breakpointId", breakpointId_.c_str());
159     ASSERT(location_ != nullptr);
160     result->Add("actualLocation", location_->ToJson());
161 
162     return result;
163 }
164 
ToJson() const165 std::unique_ptr<PtJson> SetInstrumentationBreakpointReturns::ToJson() const
166 {
167     std::unique_ptr<PtJson> result = PtJson::CreateObject();
168 
169     result->Add("breakpointId", breakpointId_.c_str());
170 
171     return result;
172 }
173 
ToJson() const174 std::unique_ptr<PtJson> SetScriptSourceReturns::ToJson() const
175 {
176     std::unique_ptr<PtJson> result = PtJson::CreateObject();
177 
178     if (callFrames_) {
179         std::unique_ptr<PtJson> array = PtJson::CreateArray();
180         size_t len = callFrames_->size();
181         for (size_t i = 0; i < len; i++) {
182             ASSERT(callFrames_.value()[i] != nullptr);
183             std::unique_ptr<PtJson> location = callFrames_.value()[i]->ToJson();
184             array->Push(location);
185         }
186         result->Add("callFrames", array);
187     }
188     if (stackChanged_) {
189         result->Add("stackChanged", stackChanged_.value());
190     }
191     if (exceptionDetails_) {
192         ASSERT(exceptionDetails_.value() != nullptr);
193         result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
194     }
195 
196     return result;
197 }
198 
ToJson() const199 std::unique_ptr<PtJson> GetPropertiesReturns::ToJson() const
200 {
201     std::unique_ptr<PtJson> result = PtJson::CreateObject();
202 
203     std::unique_ptr<PtJson> array = PtJson::CreateArray();
204     size_t len = result_.size();
205     for (size_t i = 0; i < len; i++) {
206         ASSERT(result_[i] != nullptr);
207         std::unique_ptr<PtJson> location = result_[i]->ToJson();
208         array->Push(location);
209     }
210     result->Add("result", array);
211     if (internalPropertyDescripties_) {
212         array = PtJson::CreateArray();
213         len = internalPropertyDescripties_->size();
214         for (size_t i = 0; i < len; i++) {
215             ASSERT(internalPropertyDescripties_.value()[i] != nullptr);
216             std::unique_ptr<PtJson> location = internalPropertyDescripties_.value()[i]->ToJson();
217             array->Push(location);
218         }
219         result->Add("internalProperties", array);
220     }
221     if (privateProperties_) {
222         array = PtJson::CreateArray();
223         len = privateProperties_->size();
224         for (size_t i = 0; i < len; i++) {
225             ASSERT(privateProperties_.value()[i] != nullptr);
226             std::unique_ptr<PtJson> location = privateProperties_.value()[i]->ToJson();
227             array->Push(location);
228         }
229         result->Add("privateProperties", array);
230     }
231     if (exceptionDetails_) {
232         ASSERT(exceptionDetails_.value() != nullptr);
233         result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
234     }
235 
236     return result;
237 }
238 
ToJson() const239 std::unique_ptr<PtJson> CallFunctionOnReturns::ToJson() const
240 {
241     std::unique_ptr<PtJson> result = PtJson::CreateObject();
242 
243     ASSERT(result_ != nullptr);
244     result->Add("result", result_->ToJson());
245     if (exceptionDetails_) {
246         ASSERT(exceptionDetails_.value() != nullptr);
247         result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
248     }
249 
250     return result;
251 }
252 
ToJson() const253 std::unique_ptr<PtJson> StopSamplingReturns::ToJson() const
254 {
255     std::unique_ptr<PtJson> result = PtJson::CreateObject();
256 
257     ASSERT(profile_ != nullptr);
258     result->Add("profile", profile_->ToJson());
259 
260     return result;
261 }
262 
ToJson() const263 std::unique_ptr<PtJson> GetHeapObjectIdReturns::ToJson() const
264 {
265     std::unique_ptr<PtJson> result = PtJson::CreateObject();
266 
267     result->Add("heapSnapshotObjectId", std::to_string(heapSnapshotObjectId_).c_str());
268 
269     return result;
270 }
271 
ToJson() const272 std::unique_ptr<PtJson> GetObjectByHeapObjectIdReturns::ToJson() const
273 {
274     std::unique_ptr<PtJson> result = PtJson::CreateObject();
275 
276     ASSERT(remoteObjectResult_ != nullptr);
277     result->Add("result", remoteObjectResult_->ToJson());
278 
279     return result;
280 }
281 
ToJson() const282 std::unique_ptr<PtJson> StopReturns::ToJson() const
283 {
284     std::unique_ptr<PtJson> result = PtJson::CreateObject();
285 
286     ASSERT(profile_ != nullptr);
287     result->Add("profile", profile_->ToJson());
288 
289     return result;
290 }
291 
ToJson() const292 std::unique_ptr<PtJson> GetHeapUsageReturns::ToJson() const
293 {
294     std::unique_ptr<PtJson> result = PtJson::CreateObject();
295 
296     result->Add("usedSize", usedSize_);
297     result->Add("totalSize", totalSize_);
298 
299     return result;
300 }
301 
Create(const PtJson &params)302 std::unique_ptr<GetHeapUsageReturns> GetHeapUsageReturns::Create(const PtJson &params)
303 {
304     auto heapUsageReturns = std::make_unique<GetHeapUsageReturns>();
305     std::string error;
306     Result ret;
307 
308     double usedSize;
309     ret = params.GetDouble("usedSize", &usedSize);
310     if (ret == Result::SUCCESS) {
311         heapUsageReturns->usedSize_ = usedSize;
312     } else {
313         error += "Unknown 'usedSize';";
314     }
315 
316     double totalSize;
317     ret = params.GetDouble("totalSize", &totalSize);
318     if (ret == Result::SUCCESS) {
319         heapUsageReturns->totalSize_ = totalSize;
320     } else {
321         error += "Unknown 'totalSize';";
322     }
323 
324     if (!error.empty()) {
325         LOG_DEBUGGER(ERROR) << "HeapUsageReturns::Create " << error;
326         return nullptr;
327     }
328 
329     return heapUsageReturns;
330 }
331 
ToJson() const332 std::unique_ptr<PtJson> GetBestEffortCoverageReturns::ToJson() const
333 {
334     std::unique_ptr<PtJson> result = PtJson::CreateObject();
335 
336     std::unique_ptr<PtJson> array = PtJson::CreateArray();
337     size_t len = result_.size();
338     for (size_t i = 0; i < len; i++) {
339         ASSERT(result_[i] != nullptr);
340         std::unique_ptr<PtJson> scriptCoverage = result_[i]->ToJson();
341         array->Push(scriptCoverage);
342     }
343     result->Add("result", array);
344 
345     return result;
346 }
347 
ToJson() const348 std::unique_ptr<PtJson> StartPreciseCoverageReturns::ToJson() const
349 {
350     std::unique_ptr<PtJson> result = PtJson::CreateObject();
351 
352     result->Add("timestamp", timestamp_);
353 
354     return result;
355 }
356 
ToJson() const357 std::unique_ptr<PtJson> TakePreciseCoverageReturns::ToJson() const
358 {
359     std::unique_ptr<PtJson> result = PtJson::CreateObject();
360 
361     std::unique_ptr<PtJson> array = PtJson::CreateArray();
362     size_t len = result_.size();
363     for (size_t i = 0; i < len; i++) {
364         ASSERT(result_[i] != nullptr);
365         std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
366         array->Push(scriptTypeProfile);
367     }
368     result->Add("result", array);
369     result->Add("timestamp", timestamp_);
370 
371     return result;
372 }
373 
ToJson() const374 std::unique_ptr<PtJson> TakeTypeProfileReturns::ToJson() const
375 {
376     std::unique_ptr<PtJson> result = PtJson::CreateObject();
377 
378     std::unique_ptr<PtJson> array = PtJson::CreateArray();
379     size_t len = result_.size();
380     for (size_t i = 0; i < len; i++) {
381         ASSERT(result_[i] != nullptr);
382         std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
383         array->Push(scriptTypeProfile);
384     }
385     result->Add("result", array);
386 
387     return result;
388 }
389 
ToJson() const390 std::unique_ptr<PtJson> GetCategoriesReturns::ToJson() const
391 {
392     std::unique_ptr<PtJson> result = PtJson::CreateObject();
393 
394     std::unique_ptr<PtJson> categories = PtJson::CreateArray();
395     size_t len = categories_.size();
396     for (size_t i = 0; i < len; i++) {
397         categories->Push(categories_[i].c_str());
398     }
399     result->Add("categories", categories);
400 
401     return result;
402 }
403 
ToJson() const404 std::unique_ptr<PtJson> RequestMemoryDumpReturns::ToJson() const
405 {
406     std::unique_ptr<PtJson> result = PtJson::CreateObject();
407 
408     result->Add("dumpGuid", dumpGuid_.c_str());
409     result->Add("success", success_);
410 
411     return result;
412 }
413 
ToJson() const414 std::unique_ptr<PtJson> GetNavigationHistoryReturns::ToJson() const
415 {
416     std::unique_ptr<PtJson> result = PtJson::CreateObject();
417     std::unique_ptr<PtJson> array = PtJson::CreateArray();
418 
419     result->Add("currentIndex", 0);
420     array->Push(PtJson::CreateObject());
421     result->Add("entries", array);
422 
423     return result;
424 }
425 }  // namespace panda::ecmascript::tooling