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_events.h"
17
18namespace panda::ecmascript::tooling {
19std::unique_ptr<PtJson> BreakpointResolved::ToJson() const
20{
21    std::unique_ptr<PtJson> result = PtJson::CreateObject();
22    result->Add("breakpointId", breakpointId_.c_str());
23    ASSERT(location_ != nullptr);
24    result->Add("location", location_->ToJson());
25
26    std::unique_ptr<PtJson> object = PtJson::CreateObject();
27    object->Add("method", GetName().c_str());
28    object->Add("params", result);
29
30    return object;
31}
32
33std::unique_ptr<PtJson> Paused::ToJson() const
34{
35    std::unique_ptr<PtJson> result = PtJson::CreateObject();
36
37    std::unique_ptr<PtJson> array = PtJson::CreateArray();
38    size_t len = callFrames_.size();
39    for (size_t i = 0; i < len; i++) {
40        ASSERT(callFrames_[i] != nullptr);
41        array->Push(callFrames_[i]->ToJson());
42    }
43    result->Add("callFrames", array);
44    result->Add("reason", reason_.c_str());
45    if (data_) {
46        ASSERT(data_.value() != nullptr);
47        result->Add("data", data_.value()->ToJson());
48    }
49    if (hitBreakpoints_) {
50        std::unique_ptr<PtJson> breakpoints = PtJson::CreateArray();
51        len = hitBreakpoints_->size();
52        for (size_t i = 0; i < len; i++) {
53            breakpoints->Push(hitBreakpoints_.value()[i].c_str());
54        }
55        result->Add("hitBreakpoints", breakpoints);
56    }
57
58    std::unique_ptr<PtJson> object = PtJson::CreateObject();
59    object->Add("method", GetName().c_str());
60    object->Add("params", result);
61
62    return object;
63}
64
65std::unique_ptr<PtJson> Resumed::ToJson() const
66{
67    std::unique_ptr<PtJson> result = PtJson::CreateObject();
68
69    std::unique_ptr<PtJson> object = PtJson::CreateObject();
70    object->Add("method", GetName().c_str());
71    object->Add("params", result);
72
73    return object;
74}
75
76std::unique_ptr<PtJson> NativeCalling::ToJson() const
77{
78    std::unique_ptr<PtJson> result = PtJson::CreateObject();
79
80    result->Add("nativeAddress", reinterpret_cast<int64_t>(GetNativeAddress()));
81    result->Add("isStepInto", GetIntoStatus());
82
83    std::unique_ptr<PtJson> object = PtJson::CreateObject();
84    object->Add("method", GetName().c_str());
85    object->Add("params", result);
86
87    return object;
88}
89
90std::unique_ptr<PtJson> MixedStack::ToJson() const
91{
92    std::unique_ptr<PtJson> result = PtJson::CreateObject();
93
94    std::unique_ptr<PtJson> nativePointerArray = PtJson::CreateArray();
95    size_t nativePointerLength = nativePointer_.size();
96    for (size_t i = 0; i < nativePointerLength; ++i) {
97        nativePointerArray->Push(reinterpret_cast<int64_t>(nativePointer_[i]));
98    }
99    result->Add("nativePointer", nativePointerArray);
100
101    std::unique_ptr<PtJson> callFrameArray = PtJson::CreateArray();
102    size_t callFrameLength = callFrames_.size();
103    for (size_t i = 0; i < callFrameLength; ++i) {
104        ASSERT(callFrames_[i] != nullptr);
105        callFrameArray->Push(callFrames_[i]->ToJson());
106    }
107    result->Add("callFrames", callFrameArray);
108
109    std::unique_ptr<PtJson> object = PtJson::CreateObject();
110    object->Add("method", GetName().c_str());
111    object->Add("params", result);
112
113    return object;
114}
115
116std::unique_ptr<PtJson> ScriptFailedToParse::ToJson() const
117{
118    std::unique_ptr<PtJson> result = PtJson::CreateObject();
119
120    result->Add("scriptId", std::to_string(scriptId_).c_str());
121    result->Add("url", url_.c_str());
122    result->Add("startLine", startLine_);
123    result->Add("startColumn", startColumn_);
124    result->Add("endLine", endLine_);
125    result->Add("endColumn", endColumn_);
126    result->Add("executionContextId", executionContextId_);
127    result->Add("hash", hash_.c_str());
128    if (sourceMapUrl_) {
129        result->Add("sourceMapURL", sourceMapUrl_->c_str());
130    }
131    if (hasSourceUrl_) {
132        result->Add("hasSourceURL", hasSourceUrl_.value());
133    }
134    if (isModule_) {
135        result->Add("isModule", isModule_.value());
136    }
137    if (length_) {
138        result->Add("length", length_.value());
139    }
140    if (codeOffset_) {
141        result->Add("codeOffset", codeOffset_.value());
142    }
143    if (scriptLanguage_) {
144        result->Add("scriptLanguage", scriptLanguage_->c_str());
145    }
146    if (embedderName_) {
147        result->Add("embedderName", embedderName_->c_str());
148    }
149
150    std::unique_ptr<PtJson> object = PtJson::CreateObject();
151    object->Add("method", GetName().c_str());
152    object->Add("params", result);
153
154    return object;
155}
156
157std::unique_ptr<PtJson> ScriptParsed::ToJson() const
158{
159    std::unique_ptr<PtJson> result = PtJson::CreateObject();
160
161    result->Add("scriptId", std::to_string(scriptId_).c_str());
162    result->Add("url", url_.c_str());
163    result->Add("startLine", startLine_);
164    result->Add("startColumn", startColumn_);
165    result->Add("endLine", endLine_);
166    result->Add("endColumn", endColumn_);
167    result->Add("executionContextId", executionContextId_);
168    result->Add("hash", hash_.c_str());
169    if (isLiveEdit_) {
170        result->Add("isLiveEdit", isLiveEdit_.value());
171    }
172    if (sourceMapUrl_) {
173        result->Add("sourceMapURL", sourceMapUrl_->c_str());
174    }
175    if (hasSourceUrl_) {
176        result->Add("hasSourceURL", hasSourceUrl_.value());
177    }
178    if (isModule_) {
179        result->Add("isModule", isModule_.value());
180    }
181    if (length_) {
182        result->Add("length", length_.value());
183    }
184    if (codeOffset_) {
185        result->Add("codeOffset", codeOffset_.value());
186    }
187    if (scriptLanguage_) {
188        result->Add("scriptLanguage", scriptLanguage_->c_str());
189    }
190    if (embedderName_) {
191        result->Add("embedderName", embedderName_->c_str());
192    }
193
194    std::unique_ptr<PtJson> object = PtJson::CreateObject();
195    object->Add("method", GetName().c_str());
196    object->Add("params", result);
197
198    return object;
199}
200
201std::unique_ptr<PtJson> AddHeapSnapshotChunk::ToJson() const
202{
203    std::unique_ptr<PtJson> result = PtJson::CreateObject();
204
205    result->Add("chunk", chunk_.c_str());
206
207    std::unique_ptr<PtJson> object = PtJson::CreateObject();
208    object->Add("method", GetName().c_str());
209    object->Add("params", result);
210
211    return object;
212}
213
214std::unique_ptr<PtJson> ConsoleProfileFinished::ToJson() const
215{
216    std::unique_ptr<PtJson> result = PtJson::CreateObject();
217
218    result->Add("id", id_.c_str());
219    ASSERT(location_ != nullptr);
220    result->Add("location", location_->ToJson());
221    ASSERT(profile_ != nullptr);
222    result->Add("profile", profile_->ToJson());
223    if (title_) {
224        result->Add("title", title_->c_str());
225    }
226
227    std::unique_ptr<PtJson> object = PtJson::CreateObject();
228    object->Add("method", GetName().c_str());
229    object->Add("params", result);
230
231    return object;
232}
233
234std::unique_ptr<PtJson> ConsoleProfileStarted::ToJson() const
235{
236    std::unique_ptr<PtJson> result = PtJson::CreateObject();
237
238    result->Add("id", id_.c_str());
239    ASSERT(location_ != nullptr);
240    result->Add("location", location_->ToJson());
241    if (title_) {
242        result->Add("title", title_->c_str());
243    }
244
245    std::unique_ptr<PtJson> object = PtJson::CreateObject();
246    object->Add("method", GetName().c_str());
247    object->Add("params", result);
248
249    return object;
250}
251
252std::unique_ptr<PtJson> PreciseCoverageDeltaUpdate::ToJson() const
253{
254    std::unique_ptr<PtJson> result = PtJson::CreateObject();
255
256    result->Add("timestamp", timestamp_);
257    result->Add("occasion", occasion_.c_str());
258    std::unique_ptr<PtJson> array = PtJson::CreateArray();
259    size_t len = result_.size();
260    for (size_t i = 0; i < len; i++) {
261        ASSERT(result_[i] != nullptr);
262        std::unique_ptr<PtJson> res = result_[i]->ToJson();
263        array->Push(res);
264    }
265    result->Add("result", array);
266
267    std::unique_ptr<PtJson> object = PtJson::CreateObject();
268    object->Add("method", GetName().c_str());
269    object->Add("params", result);
270
271    return object;
272}
273
274std::unique_ptr<PtJson> HeapStatsUpdate::ToJson() const
275{
276    std::unique_ptr<PtJson> result = PtJson::CreateObject();
277
278    std::unique_ptr<PtJson> array = PtJson::CreateArray();
279    size_t len = statsUpdate_.size();
280    for (size_t i = 0; i < len; i++) {
281        array->Push(statsUpdate_[i]);
282    }
283    result->Add("statsUpdate", array);
284
285    std::unique_ptr<PtJson> object = PtJson::CreateObject();
286    object->Add("method", GetName().c_str());
287    object->Add("params", result);
288
289    return object;
290}
291
292std::unique_ptr<PtJson> LastSeenObjectId::ToJson() const
293{
294    std::unique_ptr<PtJson> result = PtJson::CreateObject();
295
296    result->Add("lastSeenObjectId", lastSeenObjectId_);
297    result->Add("timestamp", timestamp_);
298
299    std::unique_ptr<PtJson> object = PtJson::CreateObject();
300    object->Add("method", GetName().c_str());
301    object->Add("params", result);
302
303    return object;
304}
305
306std::unique_ptr<PtJson> ReportHeapSnapshotProgress::ToJson() const
307{
308    std::unique_ptr<PtJson> result = PtJson::CreateObject();
309
310    result->Add("done", done_);
311    result->Add("total", total_);
312    if (finished_) {
313        result->Add("finished", finished_.value());
314    }
315
316    std::unique_ptr<PtJson> object = PtJson::CreateObject();
317    object->Add("method", GetName().c_str());
318    object->Add("params", result);
319
320    return object;
321}
322
323std::unique_ptr<PtJson> BufferUsage::ToJson() const
324{
325    std::unique_ptr<PtJson> result = PtJson::CreateObject();
326
327    if (percentFull_) {
328        result->Add("percentFull", percentFull_.value());
329    }
330    if (eventCount_) {
331        result->Add("eventCount", eventCount_.value());
332    }
333    if (value_) {
334        result->Add("value", value_.value());
335    }
336
337    std::unique_ptr<PtJson> object = PtJson::CreateObject();
338    object->Add("method", GetName().c_str());
339    object->Add("params", result);
340
341    return object;
342}
343
344std::unique_ptr<PtJson> DataCollected::TraceEventToJson(TraceEvent &traceEvent) const
345{
346    std::unique_ptr<PtJson> event = PtJson::CreateObject();
347    std::unique_ptr<PtJson> args = PtJson::Parse(traceEvent.args_);
348    event->Add("args", args);
349    event->Add("cat", traceEvent.cat_.c_str());
350
351    if (traceEvent.dur_ != 0) {
352        event->Add("dur", traceEvent.dur_);
353    }
354
355    if (!traceEvent.id_.empty()) {
356        event->Add("id", traceEvent.id_.c_str());
357    }
358
359    event->Add("name", traceEvent.name_.c_str());
360    event->Add("ph", traceEvent.ph_.c_str());
361    event->Add("pid", traceEvent.pid_);
362
363    if (!traceEvent.s_.empty()) {
364        event->Add("s", traceEvent.s_.c_str());
365    }
366
367    if (traceEvent.tdur_ != 0) {
368        event->Add("tdur", traceEvent.tdur_);
369    }
370
371    event->Add("tid", traceEvent.tid_);
372    event->Add("ts", traceEvent.ts_);
373
374    if (traceEvent.tts_ != 0) {
375        event->Add("tts", traceEvent.tts_);
376    }
377
378    return event;
379}
380
381std::unique_ptr<PtJson> DataCollected::ToJson() const
382{
383    std::unique_ptr<PtJson> traceEvents = PtJson::CreateArray();
384    for (auto &traceEvent : *traceEvents_) {
385        traceEvents->Push(TraceEventToJson(traceEvent));
386    }
387
388    std::unique_ptr<PtJson> value = PtJson::CreateObject();
389    value->Add("value", traceEvents);
390
391    std::unique_ptr<PtJson> object = PtJson::CreateObject();
392    object->Add("method", GetName().c_str());
393    object->Add("params", value);
394
395    return object;
396}
397
398std::unique_ptr<PtJson> TracingComplete::ToJson() const
399{
400    std::unique_ptr<PtJson> result = PtJson::CreateObject();
401
402    result->Add("dataLossOccurred", dataLossOccurred_);
403    if (traceFormat_) {
404        result->Add("traceFormat", traceFormat_.value()->c_str());
405    }
406    if (streamCompression_) {
407        result->Add("streamCompression", streamCompression_.value()->c_str());
408    }
409
410    std::unique_ptr<PtJson> object = PtJson::CreateObject();
411    object->Add("method", GetName().c_str());
412    object->Add("params", result);
413
414    return object;
415}
416}  // namespace panda::ecmascript::tooling
417