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 "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> EvaluateOnCallFrameReturns::ToJson() const
46 {
47 std::unique_ptr<PtJson> result = PtJson::CreateObject();
48
49 ASSERT(result_ != nullptr);
50 result->Add("result", result_->ToJson());
51 if (exceptionDetails_) {
52 ASSERT(exceptionDetails_.value() != nullptr);
53 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
54 }
55
56 return result;
57 }
58
ToJson() const59 std::unique_ptr<PtJson> GetPossibleBreakpointsReturns::ToJson() const
60 {
61 std::unique_ptr<PtJson> result = PtJson::CreateObject();
62
63 std::unique_ptr<PtJson> array = PtJson::CreateArray();
64 size_t len = locations_.size();
65 for (size_t i = 0; i < len; i++) {
66 ASSERT(locations_[i] != nullptr);
67 std::unique_ptr<PtJson> location = locations_[i]->ToJson();
68 array->Push(location);
69 }
70 result->Add("locations", array);
71
72 return result;
73 }
74
ToJson() const75 std::unique_ptr<PtJson> GetScriptSourceReturns::ToJson() const
76 {
77 std::unique_ptr<PtJson> result = PtJson::CreateObject();
78
79 result->Add("scriptSource", scriptSource_.c_str());
80 if (bytecode_) {
81 result->Add("bytecode", bytecode_->c_str());
82 }
83
84 return result;
85 }
86
ToJson() const87 std::unique_ptr<PtJson> RestartFrameReturns::ToJson() const
88 {
89 std::unique_ptr<PtJson> result = PtJson::CreateObject();
90
91 std::unique_ptr<PtJson> array = PtJson::CreateArray();
92 size_t len = callFrames_.size();
93 for (size_t i = 0; i < len; i++) {
94 ASSERT(callFrames_[i] != nullptr);
95 std::unique_ptr<PtJson> location = callFrames_[i]->ToJson();
96 array->Push(location);
97 }
98 result->Add("callFrames", array);
99
100 return result;
101 }
102
ToJson() const103 std::unique_ptr<PtJson> SearchInContentReturns::ToJson() const
104 {
105 std::unique_ptr<PtJson> result = PtJson::CreateObject();
106
107 std::unique_ptr<PtJson> array = PtJson::CreateArray();
108 size_t len = result_.size();
109 for (size_t i = 0; i < len; i++) {
110 ASSERT(result_[i] != nullptr);
111 std::unique_ptr<PtJson> res = result_[i]->ToJson();
112 array->Push(res);
113 }
114 result->Add("result", array);
115
116 return result;
117 }
118
ToJson() const119 std::unique_ptr<PtJson> SetBreakpointReturns::ToJson() const
120 {
121 std::unique_ptr<PtJson> result = PtJson::CreateObject();
122
123 result->Add("breakpointId", breakpointId_.c_str());
124 ASSERT(location_ != nullptr);
125 result->Add("actualLocation", location_->ToJson());
126
127 return result;
128 }
129
ToJson() const130 std::unique_ptr<PtJson> SetInstrumentationBreakpointReturns::ToJson() const
131 {
132 std::unique_ptr<PtJson> result = PtJson::CreateObject();
133
134 result->Add("breakpointId", breakpointId_.c_str());
135
136 return result;
137 }
138
ToJson() const139 std::unique_ptr<PtJson> SetScriptSourceReturns::ToJson() const
140 {
141 std::unique_ptr<PtJson> result = PtJson::CreateObject();
142
143 if (callFrames_) {
144 std::unique_ptr<PtJson> array = PtJson::CreateArray();
145 size_t len = callFrames_->size();
146 for (size_t i = 0; i < len; i++) {
147 ASSERT(callFrames_.value()[i] != nullptr);
148 std::unique_ptr<PtJson> location = callFrames_.value()[i]->ToJson();
149 array->Push(location);
150 }
151 result->Add("callFrames", array);
152 }
153 if (stackChanged_) {
154 result->Add("stackChanged", stackChanged_.value());
155 }
156 if (exceptionDetails_) {
157 ASSERT(exceptionDetails_.value() != nullptr);
158 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
159 }
160
161 return result;
162 }
163
ToJson() const164 std::unique_ptr<PtJson> GetPropertiesReturns::ToJson() const
165 {
166 std::unique_ptr<PtJson> result = PtJson::CreateObject();
167
168 std::unique_ptr<PtJson> array = PtJson::CreateArray();
169 size_t len = result_.size();
170 for (size_t i = 0; i < len; i++) {
171 ASSERT(result_[i] != nullptr);
172 std::unique_ptr<PtJson> location = result_[i]->ToJson();
173 array->Push(location);
174 }
175 result->Add("result", array);
176 if (internalPropertyDescripties_) {
177 array = PtJson::CreateArray();
178 len = internalPropertyDescripties_->size();
179 for (size_t i = 0; i < len; i++) {
180 ASSERT(internalPropertyDescripties_.value()[i] != nullptr);
181 std::unique_ptr<PtJson> location = internalPropertyDescripties_.value()[i]->ToJson();
182 array->Push(location);
183 }
184 result->Add("internalProperties", array);
185 }
186 if (privateProperties_) {
187 array = PtJson::CreateArray();
188 len = privateProperties_->size();
189 for (size_t i = 0; i < len; i++) {
190 ASSERT(privateProperties_.value()[i] != nullptr);
191 std::unique_ptr<PtJson> location = privateProperties_.value()[i]->ToJson();
192 array->Push(location);
193 }
194 result->Add("privateProperties", array);
195 }
196 if (exceptionDetails_) {
197 ASSERT(exceptionDetails_.value() != nullptr);
198 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
199 }
200
201 return result;
202 }
203
ToJson() const204 std::unique_ptr<PtJson> CallFunctionOnReturns::ToJson() const
205 {
206 std::unique_ptr<PtJson> result = PtJson::CreateObject();
207
208 ASSERT(result_ != nullptr);
209 result->Add("result", result_->ToJson());
210 if (exceptionDetails_) {
211 ASSERT(exceptionDetails_.value() != nullptr);
212 result->Add("exceptionDetails", exceptionDetails_.value()->ToJson());
213 }
214
215 return result;
216 }
217
ToJson() const218 std::unique_ptr<PtJson> StopSamplingReturns::ToJson() const
219 {
220 std::unique_ptr<PtJson> result = PtJson::CreateObject();
221
222 ASSERT(profile_ != nullptr);
223 result->Add("profile", profile_->ToJson());
224
225 return result;
226 }
227
ToJson() const228 std::unique_ptr<PtJson> GetHeapObjectIdReturns::ToJson() const
229 {
230 std::unique_ptr<PtJson> result = PtJson::CreateObject();
231
232 result->Add("heapSnapshotObjectId", std::to_string(heapSnapshotObjectId_).c_str());
233
234 return result;
235 }
236
ToJson() const237 std::unique_ptr<PtJson> GetObjectByHeapObjectIdReturns::ToJson() const
238 {
239 std::unique_ptr<PtJson> result = PtJson::CreateObject();
240
241 ASSERT(remoteObjectResult_ != nullptr);
242 result->Add("result", remoteObjectResult_->ToJson());
243
244 return result;
245 }
246
ToJson() const247 std::unique_ptr<PtJson> StopReturns::ToJson() const
248 {
249 std::unique_ptr<PtJson> result = PtJson::CreateObject();
250
251 ASSERT(profile_ != nullptr);
252 result->Add("profile", profile_->ToJson());
253
254 return result;
255 }
256
ToJson() const257 std::unique_ptr<PtJson> GetHeapUsageReturns::ToJson() const
258 {
259 std::unique_ptr<PtJson> result = PtJson::CreateObject();
260
261 result->Add("usedSize", usedSize_);
262 result->Add("totalSize", totalSize_);
263
264 return result;
265 }
266
ToJson() const267 std::unique_ptr<PtJson> GetBestEffortCoverageReturns::ToJson() const
268 {
269 std::unique_ptr<PtJson> result = PtJson::CreateObject();
270
271 std::unique_ptr<PtJson> array = PtJson::CreateArray();
272 size_t len = result_.size();
273 for (size_t i = 0; i < len; i++) {
274 ASSERT(result_[i] != nullptr);
275 std::unique_ptr<PtJson> scriptCoverage = result_[i]->ToJson();
276 array->Push(scriptCoverage);
277 }
278 result->Add("result", array);
279
280 return result;
281 }
282
ToJson() const283 std::unique_ptr<PtJson> StartPreciseCoverageReturns::ToJson() const
284 {
285 std::unique_ptr<PtJson> result = PtJson::CreateObject();
286
287 result->Add("timestamp", timestamp_);
288
289 return result;
290 }
291
ToJson() const292 std::unique_ptr<PtJson> TakePreciseCoverageReturns::ToJson() const
293 {
294 std::unique_ptr<PtJson> result = PtJson::CreateObject();
295
296 std::unique_ptr<PtJson> array = PtJson::CreateArray();
297 size_t len = result_.size();
298 for (size_t i = 0; i < len; i++) {
299 ASSERT(result_[i] != nullptr);
300 std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
301 array->Push(scriptTypeProfile);
302 }
303 result->Add("result", array);
304 result->Add("timestamp", timestamp_);
305
306 return result;
307 }
308
ToJson() const309 std::unique_ptr<PtJson> TakeTypeProfileReturns::ToJson() const
310 {
311 std::unique_ptr<PtJson> result = PtJson::CreateObject();
312
313 std::unique_ptr<PtJson> array = PtJson::CreateArray();
314 size_t len = result_.size();
315 for (size_t i = 0; i < len; i++) {
316 ASSERT(result_[i] != nullptr);
317 std::unique_ptr<PtJson> scriptTypeProfile = result_[i]->ToJson();
318 array->Push(scriptTypeProfile);
319 }
320 result->Add("result", array);
321
322 return result;
323 }
324
ToJson() const325 std::unique_ptr<PtJson> GetCategoriesReturns::ToJson() const
326 {
327 std::unique_ptr<PtJson> result = PtJson::CreateObject();
328
329 std::unique_ptr<PtJson> categories = PtJson::CreateArray();
330 size_t len = categories_.size();
331 for (size_t i = 0; i < len; i++) {
332 categories->Push(categories_[i].c_str());
333 }
334 result->Add("categories", categories);
335
336 return result;
337 }
338
ToJson() const339 std::unique_ptr<PtJson> RequestMemoryDumpReturns::ToJson() const
340 {
341 std::unique_ptr<PtJson> result = PtJson::CreateObject();
342
343 result->Add("dumpGuid", dumpGuid_.c_str());
344 result->Add("success", success_);
345
346 return result;
347 }
348 } // namespace panda::ecmascript::tooling