• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ECMASCRIPT_TOOLING_BASE_PT_TYPES_H
17 #define ECMASCRIPT_TOOLING_BASE_PT_TYPES_H
18 
19 #include <memory>
20 #include <optional>
21 
22 #include "base/pt_json.h"
23 
24 #include "ecmascript/debugger/debugger_api.h"
25 #include "ecmascript/dfx/cpu_profiler/samples_record.h"
26 #include "ecmascript/dfx/hprof/heap_sampling.h"
27 #include "libpandabase/macros.h"
28 
29 namespace panda::ecmascript::tooling {
30 // ========== Base types begin
31 class PtBaseTypes {
32 public:
33     PtBaseTypes() = default;
34     virtual ~PtBaseTypes() = default;
35     virtual std::unique_ptr<PtJson> ToJson() const = 0;
36 
37 private:
38     NO_COPY_SEMANTIC(PtBaseTypes);
39     NO_MOVE_SEMANTIC(PtBaseTypes);
40 
41     friend class ProtocolHandler;
42     friend class DebuggerImpl;
43 };
44 
45 // ========== Debugger types begin
46 // Debugger.BreakpointId
47 using BreakpointId = std::string;
48 struct BreakpointDetails {
ToStringBreakpointDetails49     static BreakpointId ToString(const BreakpointDetails &metaData)
50     {
51         return "id:" + std::to_string(metaData.line_) + ":" + std::to_string(metaData.column_) + ":" +
52             metaData.url_;
53     }
54 
ParseBreakpointIdBreakpointDetails55     static bool ParseBreakpointId(const BreakpointId &id, BreakpointDetails *metaData)
56     {
57         auto lineStart = id.find(':');
58         if (lineStart == std::string::npos) {
59             return false;
60         }
61         auto columnStart = id.find(':', lineStart + 1);
62         if (columnStart == std::string::npos) {
63             return false;
64         }
65         auto urlStart = id.find(':', columnStart + 1);
66         if (urlStart == std::string::npos) {
67             return false;
68         }
69         std::string lineStr = id.substr(lineStart + 1, columnStart - lineStart - 1);
70         std::string columnStr = id.substr(columnStart + 1, urlStart - columnStart - 1);
71         std::string url = id.substr(urlStart + 1);
72         metaData->line_ = std::stoi(lineStr);
73         metaData->column_ = std::stoi(columnStr);
74         metaData->url_ = url;
75 
76         return true;
77     }
78 
79     int32_t line_ {0};
80     int32_t column_ {0};
81     std::string url_ {};
82 };
83 
84 // Debugger.CallFrameId
85 using CallFrameId = int32_t;
86 
87 // Debugger.BreakpointInfo
88 class BreakpointInfo : public PtBaseTypes {
89 public:
90     BreakpointInfo() = default;
91     ~BreakpointInfo() override = default;
92 
93     static std::unique_ptr<BreakpointInfo> Create(const PtJson &params);
94     std::unique_ptr<PtJson> ToJson() const override;
95 
GetLineNumber()96     int32_t GetLineNumber() const
97     {
98         return lineNumber_;
99     }
100 
GetColumnNumber()101     int32_t GetColumnNumber() const
102     {
103         return columnNumber_;
104     }
105 
GetUrl()106     const std::string &GetUrl() const
107     {
108         return url_;
109     }
110 
GetCondition()111     const std::string &GetCondition() const
112     {
113         ASSERT(HasCondition());
114         return condition_.value();
115     }
116 
HasCondition()117     bool HasCondition() const
118     {
119         return condition_.has_value();
120     }
121 
GetUrlRegex()122     const std::string &GetUrlRegex() const
123     {
124         ASSERT(HasUrlRegex());
125         return urlRegex_.value();
126     }
127 
HasUrlRegex()128     bool HasUrlRegex() const
129     {
130         return urlRegex_.has_value();
131     }
132 
GetScriptHash()133     const std::string &GetScriptHash() const
134     {
135         ASSERT(HasScriptHash());
136         return scriptHash_.value();
137     }
138 
HasScriptHash()139     bool HasScriptHash() const
140     {
141         return scriptHash_.has_value();
142     }
143 
GetRestrictToFunction()144     bool GetRestrictToFunction() const
145     {
146         return restrictToFunction_.value_or(false);
147     }
148 
HasRestrictToFunction()149     bool HasRestrictToFunction() const
150     {
151         return restrictToFunction_.has_value();
152     }
153 
154 private:
155     NO_COPY_SEMANTIC(BreakpointInfo);
156     NO_MOVE_SEMANTIC(BreakpointInfo);
157 
158     int32_t lineNumber_ {0};
159     int32_t columnNumber_ {0};
160     std::string url_ {};
161     std::optional<std::string> condition_ {};
162     std::optional<std::string> urlRegex_ {};
163     std::optional<std::string> scriptHash_ {};
164     std::optional<bool> restrictToFunction_ {};
165 };
166 
167 // Runtime.ScriptId
168 using ScriptId = int32_t;
169 
170 // Debugger.BreakpointReturnInfo
171 class BreakpointReturnInfo : public PtBaseTypes {
172 public:
173     BreakpointReturnInfo() = default;
174     ~BreakpointReturnInfo() override = default;
175 
176     static std::unique_ptr<BreakpointReturnInfo> Create(const PtJson &params);
177     std::unique_ptr<PtJson> ToJson() const override;
178 
GetLineNumber()179     int32_t GetLineNumber()
180     {
181         return lineNumber_;
182     }
183 
GetColumnNumber()184     int32_t GetColumnNumber()
185     {
186         return columnNumber_;
187     }
188 
SetColumnNumber(int32_t column)189     BreakpointReturnInfo &SetColumnNumber(int32_t column)
190     {
191         columnNumber_ = column;
192         return *this;
193     }
194 
SetLineNumber(int32_t line)195     BreakpointReturnInfo &SetLineNumber(int32_t line)
196     {
197         lineNumber_ = line;
198         return *this;
199     }
200 
GetId()201     std::string GetId()
202     {
203         return id_;
204     }
205 
SetId(std::string & id)206     BreakpointReturnInfo &SetId(std::string &id)
207     {
208         id_ = id;
209         return *this;
210     }
211 
GetScriptId()212     ScriptId GetScriptId() const
213     {
214         return scriptId_;
215     }
216 
SetScriptId(ScriptId scriptId)217     BreakpointReturnInfo &SetScriptId(ScriptId scriptId)
218     {
219         scriptId_ = scriptId;
220         return *this;
221     }
222 
223 private:
224     NO_COPY_SEMANTIC(BreakpointReturnInfo);
225     NO_MOVE_SEMANTIC(BreakpointReturnInfo);
226 
227     std::string id_;
228     int32_t lineNumber_ {0};
229     int32_t columnNumber_ {0};
230     ScriptId scriptId_ {0};
231 };
232 
233 // ========== Runtime types begin
234 // Runtime.RemoteObjectId
235 using RemoteObjectId = int32_t;
236 
237 // Runtime.ExecutionContextId
238 using ExecutionContextId = int32_t;
239 
240 // Runtime.UnserializableValue
241 using UnserializableValue = std::string;
242 
243 // Runtime.UniqueDebuggerId
244 using UniqueDebuggerId = int32_t;
245 
246 // Runtime.RemoteObject
247 class RemoteObject : public PtBaseTypes {
248 public:
249     RemoteObject() = default;
250     ~RemoteObject() override = default;
251 
252     static std::unique_ptr<RemoteObject> FromTagged(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
253     static std::unique_ptr<RemoteObject> Create(const PtJson &params);
254     std::unique_ptr<PtJson> ToJson() const override;
255 
256     /*
257      * @see {#ObjectType}
258      */
GetType()259     const std::string &GetType() const
260     {
261         return type_;
262     }
263 
SetType(const std::string & type)264     RemoteObject &SetType(const std::string &type)
265     {
266         type_ = type;
267         return *this;
268     }
269     /*
270      * @see {#ObjectSubType}
271      */
GetSubType()272     const std::string &GetSubType() const
273     {
274         ASSERT(HasSubType());
275         return subType_.value();
276     }
277 
SetSubType(const std::string & type)278     RemoteObject &SetSubType(const std::string &type)
279     {
280         subType_ = type;
281         return *this;
282     }
283 
HasSubType()284     bool HasSubType() const
285     {
286         return subType_.has_value();
287     }
288 
GetClassName()289     const std::string &GetClassName() const
290     {
291         ASSERT(HasClassName());
292         return className_.value();
293     }
294 
SetClassName(const std::string & className)295     RemoteObject &SetClassName(const std::string &className)
296     {
297         className_ = className;
298         return *this;
299     }
300 
HasClassName()301     bool HasClassName() const
302     {
303         return className_.has_value();
304     }
305 
GetValue()306     Local<JSValueRef> GetValue() const
307     {
308         return value_.value_or(Local<JSValueRef>());
309     }
310 
SetValue(Local<JSValueRef> value)311     RemoteObject &SetValue(Local<JSValueRef> value)
312     {
313         value_ = value;
314         return *this;
315     }
316 
HasValue()317     bool HasValue() const
318     {
319         return value_.has_value();
320     }
321 
GetUnserializableValue()322     const UnserializableValue &GetUnserializableValue() const
323     {
324         ASSERT(HasUnserializableValue());
325         return unserializableValue_.value();
326     }
327 
SetUnserializableValue(const UnserializableValue & unserializableValue)328     RemoteObject &SetUnserializableValue(const UnserializableValue &unserializableValue)
329     {
330         unserializableValue_ = unserializableValue;
331         return *this;
332     }
333 
HasUnserializableValue()334     bool HasUnserializableValue() const
335     {
336         return unserializableValue_.has_value();
337     }
338 
GetDescription()339     const std::string &GetDescription() const
340     {
341         ASSERT(HasDescription());
342         return description_.value();
343     }
344 
SetDescription(const std::string & description)345     RemoteObject &SetDescription(const std::string &description)
346     {
347         description_ = description;
348         return *this;
349     }
350 
HasDescription()351     bool HasDescription() const
352     {
353         return description_.has_value();
354     }
355 
GetObjectId()356     RemoteObjectId GetObjectId() const
357     {
358         return objectId_.value_or(0);
359     }
360 
SetObjectId(RemoteObjectId objectId)361     RemoteObject &SetObjectId(RemoteObjectId objectId)
362     {
363         objectId_ = objectId;
364         return *this;
365     }
366 
HasObjectId()367     bool HasObjectId() const
368     {
369         return objectId_.has_value();
370     }
371 
372     struct TypeName {
373         static const std::string Object;     // NOLINT (readability-identifier-naming)
374         static const std::string Function;   // NOLINT (readability-identifier-naming)
375         static const std::string Undefined;  // NOLINT (readability-identifier-naming)
376         static const std::string String;     // NOLINT (readability-identifier-naming)
377         static const std::string Number;     // NOLINT (readability-identifier-naming)
378         static const std::string Boolean;    // NOLINT (readability-identifier-naming)
379         static const std::string Symbol;     // NOLINT (readability-identifier-naming)
380         static const std::string Bigint;     // NOLINT (readability-identifier-naming)
381         static const std::string Wasm;       // NOLINT (readability-identifier-naming)
ValidTypeName382         static bool Valid(const std::string &type)
383         {
384             return type == Object || type == Function || type == Undefined || type == String || type == Number ||
385                    type == Boolean || type == Symbol || type == Bigint || type == Wasm;
386         }
387     };
388 
389     struct SubTypeName {
390         static const std::string Array;        // NOLINT (readability-identifier-naming)
391         static const std::string Null;         // NOLINT (readability-identifier-naming)
392         static const std::string Node;         // NOLINT (readability-identifier-naming)
393         static const std::string Regexp;       // NOLINT (readability-identifier-naming)
394         static const std::string Date;         // NOLINT (readability-identifier-naming)
395         static const std::string Map;          // NOLINT (readability-identifier-naming)
396         static const std::string Set;          // NOLINT (readability-identifier-naming)
397         static const std::string Weakmap;      // NOLINT (readability-identifier-naming)
398         static const std::string Weakset;      // NOLINT (readability-identifier-naming)
399         static const std::string Iterator;     // NOLINT (readability-identifier-naming)
400         static const std::string Generator;    // NOLINT (readability-identifier-naming)
401         static const std::string Error;        // NOLINT (readability-identifier-naming)
402         static const std::string Proxy;        // NOLINT (readability-identifier-naming)
403         static const std::string Promise;      // NOLINT (readability-identifier-naming)
404         static const std::string Typedarray;   // NOLINT (readability-identifier-naming)
405         static const std::string Arraybuffer;  // NOLINT (readability-identifier-naming)
406         static const std::string Dataview;     // NOLINT (readability-identifier-naming)
407         static const std::string I32;          // NOLINT (readability-identifier-naming)
408         static const std::string I64;          // NOLINT (readability-identifier-naming)
409         static const std::string F32;          // NOLINT (readability-identifier-naming)
410         static const std::string F64;          // NOLINT (readability-identifier-naming)
411         static const std::string V128;         // NOLINT (readability-identifier-naming)
412         static const std::string Externref;    // NOLINT (readability-identifier-naming)
ValidSubTypeName413         static bool Valid(const std::string &type)
414         {
415             return type == Array || type == Null || type == Node || type == Regexp || type == Map || type == Set ||
416                    type == Weakmap || type == Iterator || type == Generator || type == Error || type == Proxy ||
417                    type == Promise || type == Typedarray || type == Arraybuffer || type == Dataview || type == I32 ||
418                    type == I64 || type == F32 || type == F64 || type == V128 || type == Externref;
419         }
420     };
421     struct ClassName {
422         static const std::string Object;          // NOLINT (readability-identifier-naming)
423         static const std::string Function;        // NOLINT (readability-identifier-naming)
424         static const std::string Array;           // NOLINT (readability-identifier-naming)
425         static const std::string Regexp;          // NOLINT (readability-identifier-naming)
426         static const std::string Date;            // NOLINT (readability-identifier-naming)
427         static const std::string Map;             // NOLINT (readability-identifier-naming)
428         static const std::string Set;             // NOLINT (readability-identifier-naming)
429         static const std::string Weakmap;         // NOLINT (readability-identifier-naming)
430         static const std::string Weakset;         // NOLINT (readability-identifier-naming)
431         static const std::string Dataview;         // NOLINT (readability-identifier-naming)
432         static const std::string ArrayIterator;   // NOLINT (readability-identifier-naming)
433         static const std::string StringIterator;  // NOLINT (readability-identifier-naming)
434         static const std::string SetIterator;     // NOLINT (readability-identifier-naming)
435         static const std::string MapIterator;     // NOLINT (readability-identifier-naming)
436         static const std::string Iterator;        // NOLINT (readability-identifier-naming)
437         static const std::string Error;           // NOLINT (readability-identifier-naming)
438         static const std::string Proxy;           // NOLINT (readability-identifier-naming)
439         static const std::string Promise;         // NOLINT (readability-identifier-naming)
440         static const std::string Typedarray;      // NOLINT (readability-identifier-naming)
441         static const std::string Arraybuffer;     // NOLINT (readability-identifier-naming)
442         static const std::string Global;          // NOLINT (readability-identifier-naming)
443         static const std::string Generator;       // NOLINT (readability-identifier-naming)
ValidClassName444         static bool Valid(const std::string &type)
445         {
446             return type == Object || type == Array || type == Regexp || type == Date || type == Map || type == Set ||
447                    type == Weakmap || type == Weakset || type == ArrayIterator || type == StringIterator ||
448                    type == Error || type == SetIterator || type == MapIterator || type == Iterator || type == Proxy ||
449                    type == Promise || type == Typedarray || type == Arraybuffer || type == Function;
450         }
451     };
452     static const std::string ObjectDescription;          // NOLINT (readability-identifier-naming)
453     static const std::string GlobalDescription;          // NOLINT (readability-identifier-naming)
454     static const std::string ProxyDescription;           // NOLINT (readability-identifier-naming)
455     static const std::string PromiseDescription;         // NOLINT (readability-identifier-naming)
456     static const std::string ArrayIteratorDescription;   // NOLINT (readability-identifier-naming)
457     static const std::string StringIteratorDescription;  // NOLINT (readability-identifier-naming)
458     static const std::string SetIteratorDescription;     // NOLINT (readability-identifier-naming)
459     static const std::string MapIteratorDescription;     // NOLINT (readability-identifier-naming)
460     static const std::string WeakRefDescription;         // NOLINT (readability-identifier-naming)
461     static const std::string WeakMapDescription;         // NOLINT (readability-identifier-naming)
462     static const std::string WeakSetDescription;         // NOLINT (readability-identifier-naming)
463     static const std::string DataViewDescription;         // NOLINT (readability-identifier-naming)
464     static const std::string JSPrimitiveRefDescription;     // NOLINT (readability-identifier-naming)
465     static const std::string JSPrimitiveNumberDescription;  // NOLINT (readability-identifier-naming)
466     static const std::string JSPrimitiveBooleanDescription; // NOLINT (readability-identifier-naming)
467     static const std::string JSPrimitiveStringDescription;  // NOLINT (readability-identifier-naming)
468     static const std::string JSPrimitiveSymbolDescription;  // NOLINT (readability-identifier-naming)
469     static const std::string JSIntlDescription;             // NOLINT (readability-identifier-naming)
470     static const std::string DateTimeFormatDescription;     // NOLINT (readability-identifier-naming)
471     static const std::string NumberFormatDescription;       // NOLINT (readability-identifier-naming)
472     static const std::string CollatorDescription;           // NOLINT (readability-identifier-naming)
473     static const std::string PluralRulesDescription;        // NOLINT (readability-identifier-naming)
474     static const std::string JSLocaleDescription;              // NOLINT (readability-identifier-naming)
475     static const std::string JSListFormatDescription;          // NOLINT (readability-identifier-naming)
476     static const std::string JSRelativeTimeFormatDescription;  // NOLINT (readability-identifier-naming)
477 
478 private:
479     NO_COPY_SEMANTIC(RemoteObject);
480     NO_MOVE_SEMANTIC(RemoteObject);
481 
482     std::string type_ {};
483     std::optional<std::string> subType_ {};
484     std::optional<std::string> className_ {};
485     std::optional<Local<JSValueRef>> value_ {};
486     std::optional<UnserializableValue> unserializableValue_ {};
487     std::optional<std::string> description_ {};
488     std::optional<RemoteObjectId> objectId_ {};
489 };
490 
491 class PrimitiveRemoteObject final : public RemoteObject {
492 public:
493     PrimitiveRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
494     ~PrimitiveRemoteObject() override = default;
495 };
496 
497 class StringRemoteObject final : public RemoteObject {
498 public:
499     StringRemoteObject(const EcmaVM *ecmaVm, Local<StringRef> tagged);
500     ~StringRemoteObject() override = default;
501 };
502 
503 class SymbolRemoteObject final : public RemoteObject {
504 public:
505     SymbolRemoteObject(const EcmaVM *ecmaVm, Local<SymbolRef> tagged);
506     ~SymbolRemoteObject() override = default;
507 
508 private:
509     std::string DescriptionForSymbol(const EcmaVM *ecmaVm, Local<SymbolRef> tagged) const;
510 };
511 
512 class FunctionRemoteObject final : public RemoteObject {
513 public:
514     FunctionRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
515     ~FunctionRemoteObject() override = default;
516 
517 private:
518     std::string DescriptionForFunction(const EcmaVM *ecmaVm, Local<FunctionRef> tagged) const;
519 };
520 
521 class GeneratorFunctionRemoteObject final : public RemoteObject {
522 public:
523     GeneratorFunctionRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
524     ~GeneratorFunctionRemoteObject() override = default;
525 
526 private:
527     std::string DescriptionForGeneratorFunction(const EcmaVM *ecmaVm, Local<FunctionRef> tagged) const;
528 };
529 
530 class ObjectRemoteObject final : public RemoteObject {
531 public:
532     ObjectRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged, const std::string &classname);
533     ObjectRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged, const std::string &classname,
534         const std::string &subtype);
535     ~ObjectRemoteObject() override = default;
536     static std::string DescriptionForObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
537 
538 private:
539     enum NumberSize : uint8_t { BYTES_OF_16BITS = 2, BYTES_OF_32BITS = 4, BYTES_OF_64BITS = 8 };
540     static std::string DescriptionForArray(const EcmaVM *ecmaVm, Local<ArrayRef> tagged);
541     static std::string DescriptionForRegexp(const EcmaVM *ecmaVm, Local<RegExpRef> tagged);
542     static std::string DescriptionForDate(const EcmaVM *ecmaVm, Local<DateRef> tagged);
543     static std::string DescriptionForMap(const EcmaVM *ecmaVm, Local<MapRef> tagged);
544     static std::string DescriptionForWeakMap(const EcmaVM *ecmaVm, Local<WeakMapRef> tagged);
545     static std::string DescriptionForSet(const EcmaVM *ecmaVm, Local<SetRef> tagged);
546     static std::string DescriptionForWeakSet(const EcmaVM *ecmaVm, Local<WeakSetRef> tagged);
547     static std::string DescriptionForDataView(Local<DataViewRef> tagged);
548     static std::string DescriptionForError(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
549     static std::string DescriptionForArrayIterator();
550     static std::string DescriptionForMapIterator();
551     static std::string DescriptionForSetIterator();
552     static std::string DescriptionForArrayBuffer(const EcmaVM *ecmaVm, Local<ArrayBufferRef> tagged);
553     static std::string DescriptionForSharedArrayBuffer(const EcmaVM *ecmaVm, Local<ArrayBufferRef> tagged);
554     static std::string DescriptionForUint8Array(const EcmaVM *ecmaVm, Local<TypedArrayRef> tagged);
555     static std::string DescriptionForInt8Array(const EcmaVM *ecmaVm, Local<TypedArrayRef> tagged);
556     static std::string DescriptionForInt16Array(const EcmaVM *ecmaVm, Local<TypedArrayRef> tagged);
557     static std::string DescriptionForInt32Array(const EcmaVM *ecmaVm, Local<TypedArrayRef> tagged);
558     static std::string DescriptionForPrimitiveNumber(const EcmaVM *ecmaVm, const Local<JSValueRef> &tagged);
559     static std::string DescriptionForPrimitiveString(const EcmaVM *ecmaVm, const Local<JSValueRef> &tagged);
560     static std::string DescriptionForPrimitiveBoolean(const EcmaVM *ecmaVm, const Local<JSValueRef> &tagged);
561     static std::string DescriptionForGeneratorObject(const EcmaVM *ecmaVm, const Local<JSValueRef> &tagged);
562     static std::string DescriptionForWeakRef();
563     static std::string DescriptionForDateTimeFormat();
564     static std::string DescriptionForNumberFormat();
565     static std::string DescriptionForCollator();
566     static std::string DescriptionForPluralRules();
567     static std::string DescriptionForJSLocale();
568     static std::string DescriptionForJSRelativeTimeFormat();
569     static std::string DescriptionForJSListFormat();
570     // container
571     static std::string DescriptionForArrayList();
572     static std::string DescriptionForDeque();
573     static std::string DescriptionForHashMap();
574     static std::string DescriptionForHashSet();
575     static std::string DescriptionForLightWeightMap();
576     static std::string DescriptionForLightWeightSet();
577     static std::string DescriptionForLinkedList();
578     static std::string DescriptionForList();
579     static std::string DescriptionForPlainArray();
580     static std::string DescriptionForQueue();
581     static std::string DescriptionForStack();
582     static std::string DescriptionForTreeMap();
583     static std::string DescriptionForTreeSet();
584     static std::string DescriptionForVector();
585 };
586 
587 // Runtime.ExceptionDetails
588 class ExceptionDetails final : public PtBaseTypes {
589 public:
590     ExceptionDetails() = default;
591     ~ExceptionDetails() override = default;
592     static std::unique_ptr<ExceptionDetails> Create(const PtJson &params);
593     std::unique_ptr<PtJson> ToJson() const override;
594 
GetExceptionId()595     int32_t GetExceptionId() const
596     {
597         return exceptionId_;
598     }
599 
SetExceptionId(int32_t exceptionId)600     ExceptionDetails &SetExceptionId(int32_t exceptionId)
601     {
602         exceptionId_ = exceptionId;
603         return *this;
604     }
605 
GetText()606     const std::string &GetText() const
607     {
608         return text_;
609     }
610 
SetText(const std::string & text)611     ExceptionDetails &SetText(const std::string &text)
612     {
613         text_ = text;
614         return *this;
615     }
616 
GetLine()617     int32_t GetLine() const
618     {
619         return lineNumber_;
620     }
621 
SetLine(int32_t lineNumber)622     ExceptionDetails &SetLine(int32_t lineNumber)
623     {
624         lineNumber_ = lineNumber;
625         return *this;
626     }
627 
GetColumn()628     int32_t GetColumn() const
629     {
630         return columnNumber_;
631     }
632 
SetColumn(int32_t columnNumber)633     ExceptionDetails &SetColumn(int32_t columnNumber)
634     {
635         columnNumber_ = columnNumber;
636         return *this;
637     }
638 
GetScriptId()639     ScriptId GetScriptId() const
640     {
641         return scriptId_.value_or(0);
642     }
643 
SetScriptId(ScriptId scriptId)644     ExceptionDetails &SetScriptId(ScriptId scriptId)
645     {
646         scriptId_ = scriptId;
647         return *this;
648     }
649 
HasScriptId()650     bool HasScriptId() const
651     {
652         return scriptId_.has_value();
653     }
654 
GetUrl()655     const std::string &GetUrl() const
656     {
657         ASSERT(HasUrl());
658         return url_.value();
659     }
660 
SetUrl(const std::string & url)661     ExceptionDetails &SetUrl(const std::string &url)
662     {
663         url_ = url;
664         return *this;
665     }
666 
HasUrl()667     bool HasUrl() const
668     {
669         return url_.has_value();
670     }
671 
GetException()672     RemoteObject *GetException() const
673     {
674         if (exception_) {
675             return exception_->get();
676         }
677         return nullptr;
678     }
679 
SetException(std::unique_ptr<RemoteObject> exception)680     ExceptionDetails &SetException(std::unique_ptr<RemoteObject> exception)
681     {
682         exception_ = std::move(exception);
683         return *this;
684     }
685 
HasException()686     bool HasException() const
687     {
688         return exception_.has_value();
689     }
690 
GetExecutionContextId()691     ExecutionContextId GetExecutionContextId() const
692     {
693         return executionContextId_.value_or(-1);
694     }
695 
SetExecutionContextId(ExecutionContextId executionContextId)696     ExceptionDetails &SetExecutionContextId(ExecutionContextId executionContextId)
697     {
698         executionContextId_ = executionContextId;
699         return *this;
700     }
701 
HasExecutionContextId()702     bool HasExecutionContextId() const
703     {
704         return executionContextId_.has_value();
705     }
706 
707 private:
708     NO_COPY_SEMANTIC(ExceptionDetails);
709     NO_MOVE_SEMANTIC(ExceptionDetails);
710 
711     int32_t exceptionId_ {0};
712     std::string text_ {};
713     int32_t lineNumber_ {0};
714     int32_t columnNumber_ {0};
715     std::optional<ScriptId> scriptId_ {};
716     std::optional<std::string> url_ {};
717     std::optional<std::unique_ptr<RemoteObject>> exception_ {};
718     std::optional<ExecutionContextId> executionContextId_ {0};
719 };
720 
721 // Runtime.InternalPropertyDescriptor
722 class InternalPropertyDescriptor final : public PtBaseTypes {
723 public:
724     InternalPropertyDescriptor() = default;
725     ~InternalPropertyDescriptor() override = default;
726 
727     static std::unique_ptr<InternalPropertyDescriptor> Create(const PtJson &params);
728     std::unique_ptr<PtJson> ToJson() const override;
729 
GetName()730     std::string GetName() const
731     {
732         return name_;
733     }
734 
SetName(const std::string & name)735     InternalPropertyDescriptor &SetName(const std::string &name)
736     {
737         name_ = name;
738         return *this;
739     }
740 
GetValue()741     RemoteObject *GetValue() const
742     {
743         if (value_) {
744             return value_->get();
745         }
746         return nullptr;
747     }
748 
SetValue(std::unique_ptr<RemoteObject> value)749     InternalPropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
750     {
751         value_ = std::move(value);
752         return *this;
753     }
754 
HasValue()755     bool HasValue() const
756     {
757         return value_.has_value();
758     }
759 
760 private:
761     NO_COPY_SEMANTIC(InternalPropertyDescriptor);
762     NO_MOVE_SEMANTIC(InternalPropertyDescriptor);
763 
764     std::string name_ {};
765     std::optional<std::unique_ptr<RemoteObject>> value_ {};
766 };
767 
768 // Runtime.PrivatePropertyDescriptor
769 class PrivatePropertyDescriptor final : public PtBaseTypes {
770 public:
771     PrivatePropertyDescriptor() = default;
772     ~PrivatePropertyDescriptor() override = default;
773 
774     static std::unique_ptr<PrivatePropertyDescriptor> Create(const PtJson &params);
775     std::unique_ptr<PtJson> ToJson() const override;
776 
GetName()777     std::string GetName() const
778     {
779         return name_;
780     }
781 
SetName(const std::string & name)782     PrivatePropertyDescriptor &SetName(const std::string &name)
783     {
784         name_ = name;
785         return *this;
786     }
787 
GetValue()788     RemoteObject *GetValue() const
789     {
790         if (value_) {
791             return value_->get();
792         }
793         return nullptr;
794     }
795 
SetValue(std::unique_ptr<RemoteObject> value)796     PrivatePropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
797     {
798         value_ = std::move(value);
799         return *this;
800     }
801 
HasValue()802     bool HasValue() const
803     {
804         return value_.has_value();
805     }
806 
GetGet()807     RemoteObject *GetGet() const
808     {
809         if (get_) {
810             return get_->get();
811         }
812         return nullptr;
813     }
814 
SetGet(std::unique_ptr<RemoteObject> get)815     PrivatePropertyDescriptor &SetGet(std::unique_ptr<RemoteObject> get)
816     {
817         get_ = std::move(get);
818         return *this;
819     }
820 
HasGet()821     bool HasGet() const
822     {
823         return get_.has_value();
824     }
825 
GetSet()826     RemoteObject *GetSet() const
827     {
828         if (set_) {
829             return set_->get();
830         }
831         return nullptr;
832     }
833 
SetSet(std::unique_ptr<RemoteObject> set)834     PrivatePropertyDescriptor &SetSet(std::unique_ptr<RemoteObject> set)
835     {
836         set_ = std::move(set);
837         return *this;
838     }
839 
HasSet()840     bool HasSet() const
841     {
842         return set_.has_value();
843     }
844 
845 private:
846     NO_COPY_SEMANTIC(PrivatePropertyDescriptor);
847     NO_MOVE_SEMANTIC(PrivatePropertyDescriptor);
848 
849     std::string name_ {};
850     std::optional<std::unique_ptr<RemoteObject>> value_ {};
851     std::optional<std::unique_ptr<RemoteObject>> get_ {};
852     std::optional<std::unique_ptr<RemoteObject>> set_ {};
853 };
854 
855 // Runtime.PropertyDescriptor
856 class PropertyDescriptor final : public PtBaseTypes {
857 public:
858     PropertyDescriptor() = default;
859     ~PropertyDescriptor() override = default;
860 
861     static std::unique_ptr<PropertyDescriptor> FromProperty(const EcmaVM *ecmaVm, Local<JSValueRef> name,
862         const PropertyAttribute &property);
863     static std::unique_ptr<PropertyDescriptor> Create(const PtJson &params);
864     std::unique_ptr<PtJson> ToJson() const override;
865 
GetName()866     std::string GetName() const
867     {
868         return name_;
869     }
870 
SetName(const std::string & name)871     PropertyDescriptor &SetName(const std::string &name)
872     {
873         name_ = name;
874         return *this;
875     }
876 
GetValue()877     RemoteObject *GetValue() const
878     {
879         if (value_) {
880             return value_->get();
881         }
882         return nullptr;
883     }
884 
SetValue(std::unique_ptr<RemoteObject> value)885     PropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
886     {
887         value_ = std::move(value);
888         return *this;
889     }
890 
HasValue()891     bool HasValue() const
892     {
893         return value_.has_value();
894     }
895 
GetWritable()896     bool GetWritable() const
897     {
898         return writable_.value_or(false);
899     }
900 
SetWritable(bool writable)901     PropertyDescriptor &SetWritable(bool writable)
902     {
903         writable_ = writable;
904         return *this;
905     }
906 
HasWritable()907     bool HasWritable() const
908     {
909         return writable_.has_value();
910     }
911 
GetGet()912     RemoteObject *GetGet() const
913     {
914         if (get_) {
915             return get_->get();
916         }
917         return nullptr;
918     }
919 
SetGet(std::unique_ptr<RemoteObject> get)920     PropertyDescriptor &SetGet(std::unique_ptr<RemoteObject> get)
921     {
922         get_ = std::move(get);
923         return *this;
924     }
925 
HasGet()926     bool HasGet() const
927     {
928         return get_.has_value();
929     }
930 
GetSet()931     RemoteObject *GetSet() const
932     {
933         if (set_) {
934             return set_->get();
935         }
936         return nullptr;
937     }
938 
SetSet(std::unique_ptr<RemoteObject> set)939     PropertyDescriptor &SetSet(std::unique_ptr<RemoteObject> set)
940     {
941         set_ = std::move(set);
942         return *this;
943     }
944 
HasSet()945     bool HasSet() const
946     {
947         return set_.has_value();
948     }
949 
GetConfigurable()950     bool GetConfigurable() const
951     {
952         return configurable_;
953     }
954 
SetConfigurable(bool configurable)955     PropertyDescriptor &SetConfigurable(bool configurable)
956     {
957         configurable_ = configurable;
958         return *this;
959     }
960 
GetEnumerable()961     bool GetEnumerable() const
962     {
963         return enumerable_;
964     }
965 
SetEnumerable(bool enumerable)966     PropertyDescriptor &SetEnumerable(bool enumerable)
967     {
968         enumerable_ = enumerable;
969         return *this;
970     }
971 
GetWasThrown()972     bool GetWasThrown() const
973     {
974         return wasThrown_.value_or(false);
975     }
976 
SetWasThrown(bool wasThrown)977     PropertyDescriptor &SetWasThrown(bool wasThrown)
978     {
979         wasThrown_ = wasThrown;
980         return *this;
981     }
982 
HasWasThrown()983     bool HasWasThrown() const
984     {
985         return wasThrown_.has_value();
986     }
987 
GetIsOwn()988     bool GetIsOwn() const
989     {
990         return isOwn_.value_or(false);
991     }
992 
SetIsOwn(bool isOwn)993     PropertyDescriptor &SetIsOwn(bool isOwn)
994     {
995         isOwn_ = isOwn;
996         return *this;
997     }
998 
HasIsOwn()999     bool HasIsOwn() const
1000     {
1001         return isOwn_.has_value();
1002     }
1003 
GetSymbol()1004     RemoteObject *GetSymbol() const
1005     {
1006         if (symbol_) {
1007             return symbol_->get();
1008         }
1009         return nullptr;
1010     }
1011 
SetSymbol(std::unique_ptr<RemoteObject> symbol)1012     PropertyDescriptor &SetSymbol(std::unique_ptr<RemoteObject> symbol)
1013     {
1014         symbol_ = std::move(symbol);
1015         return *this;
1016     }
1017 
HasSymbol()1018     bool HasSymbol() const
1019     {
1020         return symbol_.has_value();
1021     }
1022 
1023 private:
1024     NO_COPY_SEMANTIC(PropertyDescriptor);
1025     NO_MOVE_SEMANTIC(PropertyDescriptor);
1026 
1027     std::string name_ {};
1028     std::optional<std::unique_ptr<RemoteObject>> value_ {};
1029     std::optional<bool> writable_ {};
1030     std::optional<std::unique_ptr<RemoteObject>> get_ {};
1031     std::optional<std::unique_ptr<RemoteObject>> set_ {};
1032     bool configurable_ {false};
1033     bool enumerable_ {false};
1034     std::optional<bool> wasThrown_ {};
1035     std::optional<bool> isOwn_ {};
1036     std::optional<std::unique_ptr<RemoteObject>> symbol_ {};
1037 };
1038 
1039 // Runtime.CallArgument
1040 class CallArgument final : public PtBaseTypes {
1041 public:
1042     CallArgument() = default;
1043     ~CallArgument() override = default;
1044 
1045     static std::unique_ptr<CallArgument> Create(const PtJson &params);
1046     std::unique_ptr<PtJson> ToJson() const override;
1047 
GetValue()1048     Local<JSValueRef> GetValue() const
1049     {
1050         return value_.value_or(Local<JSValueRef>());
1051     }
1052 
SetValue(Local<JSValueRef> value)1053     CallArgument &SetValue(Local<JSValueRef> value)
1054     {
1055         value_ = value;
1056         return *this;
1057     }
1058 
HasValue()1059     bool HasValue() const
1060     {
1061         return value_.has_value();
1062     }
1063 
GetUnserializableValue()1064     const UnserializableValue &GetUnserializableValue() const
1065     {
1066         ASSERT(HasUnserializableValue());
1067         return unserializableValue_.value();
1068     }
1069 
SetUnserializableValue(const UnserializableValue & unserializableValue)1070     CallArgument &SetUnserializableValue(const UnserializableValue &unserializableValue)
1071     {
1072         unserializableValue_ = unserializableValue;
1073         return *this;
1074     }
1075 
HasUnserializableValue()1076     bool HasUnserializableValue() const
1077     {
1078         return unserializableValue_.has_value();
1079     }
1080 
GetObjectId()1081     RemoteObjectId GetObjectId() const
1082     {
1083         return objectId_.value_or(0);
1084     }
1085 
SetObjectId(RemoteObjectId objectId)1086     CallArgument &SetObjectId(RemoteObjectId objectId)
1087     {
1088         objectId_ = objectId;
1089         return *this;
1090     }
1091 
HasObjectId()1092     bool HasObjectId() const
1093     {
1094         return objectId_.has_value();
1095     }
1096 
1097 private:
1098     NO_COPY_SEMANTIC(CallArgument);
1099     NO_MOVE_SEMANTIC(CallArgument);
1100 
1101     std::optional<Local<JSValueRef>> value_ {};
1102     std::optional<UnserializableValue> unserializableValue_ {};
1103     std::optional<RemoteObjectId> objectId_ {};
1104 };
1105 
1106 // ========== Debugger types begin
1107 // Debugger.ScriptLanguage
1108 struct ScriptLanguage {
ValidScriptLanguage1109     static bool Valid(const std::string &language)
1110     {
1111         return language == JavaScript() || language == WebAssembly();
1112     }
JavaScriptScriptLanguage1113     static std::string JavaScript()
1114     {
1115         return "JavaScript";
1116     }
WebAssemblyScriptLanguage1117     static std::string WebAssembly()
1118     {
1119         return "WebAssembly";
1120     }
1121 };
1122 
1123 // Debugger.Location
1124 class Location : public PtBaseTypes {
1125 public:
1126     Location() = default;
1127     ~Location() override = default;
1128 
1129     static std::unique_ptr<Location> Create(const PtJson &params);
1130     std::unique_ptr<PtJson> ToJson() const override;
1131 
GetScriptId()1132     ScriptId GetScriptId() const
1133     {
1134         return scriptId_;
1135     }
1136 
SetScriptId(ScriptId scriptId)1137     Location &SetScriptId(ScriptId scriptId)
1138     {
1139         scriptId_ = scriptId;
1140         return *this;
1141     }
1142 
GetLine()1143     int32_t GetLine() const
1144     {
1145         return lineNumber_;
1146     }
1147 
SetLine(int32_t line)1148     Location &SetLine(int32_t line)
1149     {
1150         lineNumber_ = line;
1151         return *this;
1152     }
1153 
GetColumn()1154     int32_t GetColumn() const
1155     {
1156         return columnNumber_.value_or(-1);
1157     }
1158 
SetColumn(int32_t column)1159     Location &SetColumn(int32_t column)
1160     {
1161         columnNumber_ = column;
1162         return *this;
1163     }
1164 
HasColumn()1165     bool HasColumn() const
1166     {
1167         return columnNumber_.has_value();
1168     }
1169 
1170 private:
1171     NO_COPY_SEMANTIC(Location);
1172     NO_MOVE_SEMANTIC(Location);
1173 
1174     ScriptId scriptId_ {0};
1175     int32_t lineNumber_ {0};
1176     std::optional<int32_t> columnNumber_ {};
1177 };
1178 
1179 // Debugger.ScriptPosition
1180 class ScriptPosition : public PtBaseTypes {
1181 public:
1182     ScriptPosition() = default;
1183     ~ScriptPosition() override = default;
1184 
1185     static std::unique_ptr<ScriptPosition> Create(const PtJson &params);
1186     std::unique_ptr<PtJson> ToJson() const override;
1187 
GetLine()1188     int32_t GetLine() const
1189     {
1190         return lineNumber_;
1191     }
1192 
SetLine(int32_t line)1193     ScriptPosition &SetLine(int32_t line)
1194     {
1195         lineNumber_ = line;
1196         return *this;
1197     }
1198 
GetColumn()1199     int32_t GetColumn() const
1200     {
1201         return columnNumber_;
1202     }
1203 
SetColumn(int32_t column)1204     ScriptPosition &SetColumn(int32_t column)
1205     {
1206         columnNumber_ = column;
1207         return *this;
1208     }
1209 
1210 private:
1211     NO_COPY_SEMANTIC(ScriptPosition);
1212     NO_MOVE_SEMANTIC(ScriptPosition);
1213 
1214     int32_t lineNumber_ {0};
1215     int32_t columnNumber_ {0};
1216 };
1217 
1218 // Debugger.SearchMatch
1219 class SearchMatch : public PtBaseTypes {
1220 public:
1221     SearchMatch() = default;
1222     ~SearchMatch() override = default;
1223     static std::unique_ptr<SearchMatch> Create(const PtJson &params);
1224     std::unique_ptr<PtJson> ToJson() const override;
1225 
GetLine()1226     int32_t GetLine() const
1227     {
1228         return lineNumber_;
1229     }
1230 
SetLine(int32_t line)1231     SearchMatch &SetLine(int32_t line)
1232     {
1233         lineNumber_ = line;
1234         return *this;
1235     }
1236 
GetLineContent()1237     std::string GetLineContent() const
1238     {
1239         return lineContent_;
1240     }
1241 
SetLineContent(const std::string lineContent)1242     SearchMatch &SetLineContent(const std::string lineContent)
1243     {
1244         lineContent_ = lineContent;
1245         return *this;
1246     }
1247 
1248 private:
1249     NO_COPY_SEMANTIC(SearchMatch);
1250     NO_MOVE_SEMANTIC(SearchMatch);
1251 
1252     int32_t lineNumber_ {0};
1253     std::string lineContent_ {};
1254 };
1255 
1256 // Debugger.LocationRange
1257 class LocationRange : public PtBaseTypes {
1258 public:
1259     LocationRange() = default;
1260     ~LocationRange() override = default;
1261 
1262     static std::unique_ptr<LocationRange> Create(const PtJson &params);
1263     std::unique_ptr<PtJson> ToJson() const override;
1264 
GetScriptId()1265     ScriptId GetScriptId() const
1266     {
1267         return scriptId_;
1268     }
1269 
SetScriptId(ScriptId scriptId)1270     LocationRange &SetScriptId(ScriptId scriptId)
1271     {
1272         scriptId_ = scriptId;
1273         return *this;
1274     }
1275 
GetStart()1276     ScriptPosition *GetStart() const
1277     {
1278         return start_.get();
1279     }
1280 
SetStart(std::unique_ptr<ScriptPosition> start)1281     LocationRange &SetStart(std::unique_ptr<ScriptPosition> start)
1282     {
1283         start_ = std::move(start);
1284         return *this;
1285     }
1286 
GetEnd()1287     ScriptPosition *GetEnd() const
1288     {
1289         return end_.get();
1290     }
1291 
SetEnd(std::unique_ptr<ScriptPosition> end)1292     LocationRange &SetEnd(std::unique_ptr<ScriptPosition> end)
1293     {
1294         end_ = std::move(end);
1295         return *this;
1296     }
1297 
1298 private:
1299     NO_COPY_SEMANTIC(LocationRange);
1300     NO_MOVE_SEMANTIC(LocationRange);
1301 
1302     ScriptId scriptId_ {0};
1303     std::unique_ptr<ScriptPosition> start_ {nullptr};
1304     std::unique_ptr<ScriptPosition> end_ {nullptr};
1305 };
1306 
1307 // Debugger.BreakLocation
1308 class BreakLocation final : public PtBaseTypes {
1309 public:
1310     BreakLocation() = default;
1311     ~BreakLocation() override = default;
1312 
1313     static std::unique_ptr<BreakLocation> Create(const PtJson &params);
1314     std::unique_ptr<PtJson> ToJson() const override;
1315 
GetScriptId()1316     ScriptId GetScriptId() const
1317     {
1318         return scriptId_;
1319     }
1320 
SetScriptId(ScriptId scriptId)1321     BreakLocation &SetScriptId(ScriptId scriptId)
1322     {
1323         scriptId_ = scriptId;
1324         return *this;
1325     }
1326 
GetLine()1327     int32_t GetLine() const
1328     {
1329         return lineNumber_;
1330     }
1331 
SetLine(int32_t lineNumber)1332     BreakLocation &SetLine(int32_t lineNumber)
1333     {
1334         lineNumber_ = lineNumber;
1335         return *this;
1336     }
1337 
GetColumn()1338     int32_t GetColumn() const
1339     {
1340         return columnNumber_.value_or(-1);
1341     }
1342 
SetColumn(int32_t columnNumber)1343     BreakLocation &SetColumn(int32_t columnNumber)
1344     {
1345         columnNumber_ = columnNumber;
1346         return *this;
1347     }
1348 
HasColumn()1349     bool HasColumn() const
1350     {
1351         return columnNumber_.has_value();
1352     }
1353 
1354     /*
1355      * @see {#BreakType}
1356      */
GetType()1357     const std::string &GetType() const
1358     {
1359         ASSERT(HasType());
1360         return type_.value();
1361     }
1362 
SetType(const std::string & type)1363     BreakLocation &SetType(const std::string &type)
1364     {
1365         type_ = type;
1366         return *this;
1367     }
1368 
HasType()1369     bool HasType() const
1370     {
1371         return type_.has_value();
1372     }
1373 
1374     struct Type {
ValidType1375         static bool Valid(const std::string &type)
1376         {
1377             return type == DebuggerStatement() || type == Call() || type == Return();
1378         }
DebuggerStatementType1379         static std::string DebuggerStatement()
1380         {
1381             return "debuggerStatement";
1382         }
CallType1383         static std::string Call()
1384         {
1385             return "call";
1386         }
ReturnType1387         static std::string Return()
1388         {
1389             return "return";
1390         }
1391     };
1392 
1393 private:
1394     NO_COPY_SEMANTIC(BreakLocation);
1395     NO_MOVE_SEMANTIC(BreakLocation);
1396 
1397     ScriptId scriptId_ {0};
1398     int32_t lineNumber_ {0};
1399     std::optional<int32_t> columnNumber_ {};
1400     std::optional<std::string> type_ {};
1401 };
1402 using BreakType = BreakLocation::Type;
1403 
1404 enum class ScopeType : uint8_t {
1405     GLOBAL,
1406     LOCAL,
1407     WITH,
1408     CLOSURE,
1409     CATCH,
1410     BLOCK,
1411     SCRIPT,
1412     EVAL,
1413     MODULE,
1414     WASM_EXPRESSION_STACK
1415 };
1416 
1417 // Debugger.Scope
1418 class Scope final : public PtBaseTypes {
1419 public:
1420     Scope() = default;
1421     ~Scope() override = default;
1422 
1423     static std::unique_ptr<Scope> Create(const PtJson &params);
1424     std::unique_ptr<PtJson> ToJson() const override;
1425 
1426     /*
1427      * @see {#Scope::Type}
1428      */
GetType()1429     const std::string &GetType() const
1430     {
1431         return type_;
1432     }
1433 
SetType(const std::string & type)1434     Scope &SetType(const std::string &type)
1435     {
1436         type_ = type;
1437         return *this;
1438     }
1439 
GetObject()1440     RemoteObject *GetObject() const
1441     {
1442         return object_.get();
1443     }
1444 
SetObject(std::unique_ptr<RemoteObject> params)1445     Scope &SetObject(std::unique_ptr<RemoteObject> params)
1446     {
1447         object_ = std::move(params);
1448         return *this;
1449     }
1450 
GetName()1451     const std::string &GetName() const
1452     {
1453         ASSERT(HasName());
1454         return name_.value();
1455     }
1456 
SetName(const std::string & name)1457     Scope &SetName(const std::string &name)
1458     {
1459         name_ = name;
1460         return *this;
1461     }
1462 
HasName()1463     bool HasName() const
1464     {
1465         return name_.has_value();
1466     }
1467 
GetStartLocation()1468     Location *GetStartLocation() const
1469     {
1470         if (startLocation_) {
1471             return startLocation_->get();
1472         }
1473         return nullptr;
1474     }
1475 
SetStartLocation(std::unique_ptr<Location> location)1476     Scope &SetStartLocation(std::unique_ptr<Location> location)
1477     {
1478         startLocation_ = std::move(location);
1479         return *this;
1480     }
1481 
HasStartLocation()1482     bool HasStartLocation() const
1483     {
1484         return startLocation_.has_value();
1485     }
1486 
GetEndLocation()1487     Location *GetEndLocation() const
1488     {
1489         if (endLocation_) {
1490             return endLocation_->get();
1491         }
1492         return nullptr;
1493     }
1494 
SetEndLocation(std::unique_ptr<Location> location)1495     Scope &SetEndLocation(std::unique_ptr<Location> location)
1496     {
1497         endLocation_ = std::move(location);
1498         return *this;
1499     }
1500 
HasEndLocation()1501     bool HasEndLocation() const
1502     {
1503         return endLocation_.has_value();
1504     }
1505 
1506     struct Type {
ValidType1507         static bool Valid(const std::string &type)
1508         {
1509             return type == Global() || type == Local() || type == With() || type == Closure() || type == Catch() ||
1510                    type == Block() || type == Script() || type == Eval() || type == Module() ||
1511                    type == WasmExpressionStack();
1512         }
GlobalType1513         static std::string Global()
1514         {
1515             return "global";
1516         }
LocalType1517         static std::string Local()
1518         {
1519             return "local";
1520         }
WithType1521         static std::string With()
1522         {
1523             return "with";
1524         }
ClosureType1525         static std::string Closure()
1526         {
1527             return "closure";
1528         }
CatchType1529         static std::string Catch()
1530         {
1531             return "catch";
1532         }
BlockType1533         static std::string Block()
1534         {
1535             return "block";
1536         }
ScriptType1537         static std::string Script()
1538         {
1539             return "script";
1540         }
EvalType1541         static std::string Eval()
1542         {
1543             return "eval";
1544         }
ModuleType1545         static std::string Module()
1546         {
1547             return "module";
1548         }
WasmExpressionStackType1549         static std::string WasmExpressionStack()
1550         {
1551             return "wasm-expression-stack";
1552         }
1553     };
1554 
1555 private:
1556     NO_COPY_SEMANTIC(Scope);
1557     NO_MOVE_SEMANTIC(Scope);
1558 
1559     std::string type_ {};
1560     std::unique_ptr<RemoteObject> object_ {nullptr};
1561     std::optional<std::string> name_ {};
1562     std::optional<std::unique_ptr<Location>> startLocation_ {};
1563     std::optional<std::unique_ptr<Location>> endLocation_ {};
1564 };
1565 
1566 // Debugger.CallFrame
1567 class CallFrame final : public PtBaseTypes {
1568 public:
1569     CallFrame() = default;
1570     ~CallFrame() override = default;
1571 
1572     static std::unique_ptr<CallFrame> Create(const PtJson &params);
1573     std::unique_ptr<PtJson> ToJson() const override;
1574 
GetCallFrameId()1575     CallFrameId GetCallFrameId() const
1576     {
1577         return callFrameId_;
1578     }
1579 
SetCallFrameId(CallFrameId callFrameId)1580     CallFrame &SetCallFrameId(CallFrameId callFrameId)
1581     {
1582         callFrameId_ = callFrameId;
1583         return *this;
1584     }
1585 
GetFunctionName()1586     const std::string &GetFunctionName() const
1587     {
1588         return functionName_;
1589     }
1590 
SetFunctionName(const std::string & functionName)1591     CallFrame &SetFunctionName(const std::string &functionName)
1592     {
1593         functionName_ = functionName;
1594         return *this;
1595     }
1596 
GetFunctionLocation()1597     Location *GetFunctionLocation() const
1598     {
1599         if (functionLocation_) {
1600             return functionLocation_->get();
1601         }
1602         return nullptr;
1603     }
1604 
SetFunctionLocation(std::unique_ptr<Location> location)1605     CallFrame &SetFunctionLocation(std::unique_ptr<Location> location)
1606     {
1607         functionLocation_ = std::move(location);
1608         return *this;
1609     }
1610 
HasFunctionLocation()1611     bool HasFunctionLocation() const
1612     {
1613         return functionLocation_.has_value();
1614     }
1615 
GetLocation()1616     Location *GetLocation() const
1617     {
1618         return location_.get();
1619     }
1620 
SetLocation(std::unique_ptr<Location> location)1621     CallFrame &SetLocation(std::unique_ptr<Location> location)
1622     {
1623         location_ = std::move(location);
1624         return *this;
1625     }
1626 
GetUrl()1627     const std::string &GetUrl() const
1628     {
1629         return url_;
1630     }
1631 
SetUrl(const std::string & url)1632     CallFrame &SetUrl(const std::string &url)
1633     {
1634         url_ = url;
1635         return *this;
1636     }
1637 
GetScopeChain()1638     const std::vector<std::unique_ptr<Scope>> *GetScopeChain() const
1639     {
1640         return &scopeChain_;
1641     }
1642 
SetScopeChain(std::vector<std::unique_ptr<Scope>> scopeChain)1643     CallFrame &SetScopeChain(std::vector<std::unique_ptr<Scope>> scopeChain)
1644     {
1645         scopeChain_ = std::move(scopeChain);
1646         return *this;
1647     }
GetThis()1648     RemoteObject *GetThis() const
1649     {
1650         return this_.get();
1651     }
1652 
SetThis(std::unique_ptr<RemoteObject> thisObj)1653     CallFrame &SetThis(std::unique_ptr<RemoteObject> thisObj)
1654     {
1655         this_ = std::move(thisObj);
1656         return *this;
1657     }
1658 
GetReturnValue()1659     RemoteObject *GetReturnValue() const
1660     {
1661         if (returnValue_) {
1662             return returnValue_->get();
1663         }
1664         return nullptr;
1665     }
1666 
SetReturnValue(std::unique_ptr<RemoteObject> returnValue)1667     CallFrame &SetReturnValue(std::unique_ptr<RemoteObject> returnValue)
1668     {
1669         returnValue_ = std::move(returnValue);
1670         return *this;
1671     }
1672 
HasReturnValue()1673     bool HasReturnValue() const
1674     {
1675         return returnValue_.has_value();
1676     }
1677 
1678 private:
1679     NO_COPY_SEMANTIC(CallFrame);
1680     NO_MOVE_SEMANTIC(CallFrame);
1681 
1682     CallFrameId callFrameId_ {};
1683     std::string functionName_ {};
1684     std::optional<std::unique_ptr<Location>> functionLocation_ {};
1685     std::unique_ptr<Location> location_ {nullptr};
1686     std::string url_ {};
1687     std::vector<std::unique_ptr<Scope>> scopeChain_ {};
1688     std::unique_ptr<RemoteObject> this_ {nullptr};
1689     std::optional<std::unique_ptr<RemoteObject>> returnValue_ {};
1690 };
1691 
1692 // ========== Heapprofiler types begin
1693 
1694 using HeapSnapshotObjectId = int32_t;
1695 
1696 class SamplingHeapProfileSample  final :  public PtBaseTypes {
1697 public:
1698     SamplingHeapProfileSample() = default;
1699     ~SamplingHeapProfileSample() override = default;
1700     static std::unique_ptr<SamplingHeapProfileSample> Create(const PtJson &params);
1701     std::unique_ptr<PtJson> ToJson() const override;
1702 
SetSize(int32_t size)1703     SamplingHeapProfileSample &SetSize(int32_t size)
1704     {
1705         size_ = size;
1706         return *this;
1707     }
1708 
GetSize()1709     int32_t GetSize() const
1710     {
1711         return size_;
1712     }
1713 
SetNodeId(int32_t nodeId)1714     SamplingHeapProfileSample &SetNodeId(int32_t nodeId)
1715     {
1716         nodeId_ = nodeId;
1717         return *this;
1718     }
1719 
GetNodeId()1720     int32_t GetNodeId() const
1721     {
1722         return nodeId_;
1723     }
1724 
SetOrdinal(int64_t ordinal)1725     SamplingHeapProfileSample &SetOrdinal(int64_t ordinal)
1726     {
1727         ordinal_ = ordinal;
1728         return *this;
1729     }
1730 
GetOrdinal()1731     int64_t GetOrdinal() const
1732     {
1733         return ordinal_;
1734     }
1735 
1736 private:
1737     NO_COPY_SEMANTIC(SamplingHeapProfileSample);
1738     NO_MOVE_SEMANTIC(SamplingHeapProfileSample);
1739 
1740     int32_t size_ {0};
1741     int32_t nodeId_ {0};
1742     int64_t ordinal_ {0};
1743 };
1744 
1745 class RuntimeCallFrame  final :  public PtBaseTypes {
1746 public:
1747     RuntimeCallFrame() = default;
1748     ~RuntimeCallFrame() override = default;
1749     static std::unique_ptr<RuntimeCallFrame> Create(const PtJson &params);
1750     static std::unique_ptr<RuntimeCallFrame> FromFrameInfo(const FrameInfo &cpuFrameInfo);
1751     std::unique_ptr<PtJson> ToJson() const override;
1752 
SetFunctionName(const std::string & functionName)1753     RuntimeCallFrame &SetFunctionName(const std::string &functionName)
1754     {
1755         functionName_ = functionName;
1756         return *this;
1757     }
1758 
GetFunctionName()1759     const std::string &GetFunctionName() const
1760     {
1761         return functionName_;
1762     }
1763 
SetModuleName(const std::string & moduleName)1764     RuntimeCallFrame &SetModuleName(const std::string &moduleName)
1765     {
1766         moduleName_ = moduleName;
1767         return *this;
1768     }
1769 
GetModuleName()1770     const std::string &GetModuleName() const
1771     {
1772         return moduleName_;
1773     }
1774 
SetScriptId(const std::string & scriptId)1775     RuntimeCallFrame &SetScriptId(const std::string &scriptId)
1776     {
1777         scriptId_ = scriptId;
1778         return *this;
1779     }
1780 
GetScriptId()1781     const std::string &GetScriptId() const
1782     {
1783         return scriptId_;
1784     }
1785 
SetUrl(const std::string & url)1786     RuntimeCallFrame &SetUrl(const std::string &url)
1787     {
1788         url_ = url;
1789         return *this;
1790     }
1791 
GetUrl()1792     const std::string &GetUrl() const
1793     {
1794         return url_;
1795     }
1796 
SetLineNumber(int32_t lineNumber)1797     RuntimeCallFrame &SetLineNumber(int32_t lineNumber)
1798     {
1799         lineNumber_ = lineNumber;
1800         return *this;
1801     }
1802 
GetLineNumber()1803     int32_t GetLineNumber() const
1804     {
1805         return lineNumber_;
1806     }
1807 
SetColumnNumber(int32_t columnNumber)1808     RuntimeCallFrame &SetColumnNumber(int32_t columnNumber)
1809     {
1810         columnNumber_ = columnNumber;
1811         return *this;
1812     }
1813 
GetColumnNumber()1814     int32_t GetColumnNumber() const
1815     {
1816         return columnNumber_;
1817     }
1818 
1819 private:
1820     NO_COPY_SEMANTIC(RuntimeCallFrame);
1821     NO_MOVE_SEMANTIC(RuntimeCallFrame);
1822 
1823     std::string functionName_ {};
1824     std::string moduleName_ {};
1825     std::string scriptId_ {};
1826     std::string url_ {};
1827     int32_t lineNumber_ {0};
1828     int32_t columnNumber_ {0};
1829 };
1830 
1831 class SamplingHeapProfileNode  final :  public PtBaseTypes {
1832 public:
1833     SamplingHeapProfileNode() = default;
1834     ~SamplingHeapProfileNode() override = default;
1835     static std::unique_ptr<SamplingHeapProfileNode> Create(const PtJson &params);
1836     std::unique_ptr<PtJson> ToJson() const override;
1837 
SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)1838     SamplingHeapProfileNode &SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)
1839     {
1840         callFrame_ = std::move(callFrame);
1841         return *this;
1842     }
1843 
GetCallFrame()1844     RuntimeCallFrame *GetCallFrame() const
1845     {
1846         return callFrame_.get();
1847     }
1848 
SetSelfSize(int32_t selfSize)1849     SamplingHeapProfileNode &SetSelfSize(int32_t selfSize)
1850     {
1851         selfSize_ = selfSize;
1852         return *this;
1853     }
1854 
GetSelfSize()1855     int32_t GetSelfSize() const
1856     {
1857         return selfSize_;
1858     }
1859 
SetId(int32_t id)1860     SamplingHeapProfileNode &SetId(int32_t id)
1861     {
1862         id_ = id;
1863         return *this;
1864     }
1865 
GetId()1866     int32_t GetId() const
1867     {
1868         return id_;
1869     }
1870 
SetChildren(std::vector<std::unique_ptr<SamplingHeapProfileNode>> children)1871     SamplingHeapProfileNode &SetChildren(std::vector<std::unique_ptr<SamplingHeapProfileNode>> children)
1872     {
1873         children_ = std::move(children);
1874         return *this;
1875     }
1876 
GetChildren()1877     const std::vector<std::unique_ptr<SamplingHeapProfileNode>> *GetChildren() const
1878     {
1879         return &children_;
1880     }
1881 
1882 private:
1883     NO_COPY_SEMANTIC(SamplingHeapProfileNode);
1884     NO_MOVE_SEMANTIC(SamplingHeapProfileNode);
1885 
1886     std::unique_ptr<RuntimeCallFrame> callFrame_ {nullptr};
1887     int32_t selfSize_ {0};
1888     int32_t id_ {0};
1889     std::vector<std::unique_ptr<SamplingHeapProfileNode>> children_ {};
1890 };
1891 
1892 class SamplingHeapProfile final : public PtBaseTypes {
1893 public:
1894     SamplingHeapProfile() = default;
1895     ~SamplingHeapProfile() override = default;
1896     static std::unique_ptr<SamplingHeapProfile> Create(const PtJson &params);
1897     static std::unique_ptr<SamplingHeapProfile> FromSamplingInfo(const SamplingInfo *samplingInfo);
1898     static std::unique_ptr<SamplingHeapProfileNode> TransferHead(const SamplingNode *allocationNode);
1899     std::unique_ptr<PtJson> ToJson() const override;
1900 
SetHead(std::unique_ptr<SamplingHeapProfileNode> head)1901     SamplingHeapProfile &SetHead(std::unique_ptr<SamplingHeapProfileNode> head)
1902     {
1903         head_ = std::move(head);
1904         return *this;
1905     }
1906 
GetHead()1907     SamplingHeapProfileNode *GetHead() const
1908     {
1909         return head_.get();
1910     }
1911 
SetSamples(std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples)1912     SamplingHeapProfile &SetSamples(std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples)
1913     {
1914         samples_ = std::move(samples);
1915         return *this;
1916     }
1917 
GetSamples()1918     const std::vector<std::unique_ptr<SamplingHeapProfileSample>> *GetSamples() const
1919     {
1920         return &samples_;
1921     }
1922 
1923 private:
1924     NO_COPY_SEMANTIC(SamplingHeapProfile);
1925     NO_MOVE_SEMANTIC(SamplingHeapProfile);
1926 
1927     std::unique_ptr<SamplingHeapProfileNode> head_ {nullptr};
1928     std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples_ {};
1929 };
1930 
1931 // ========== Profiler types begin
1932 // Profiler.PositionTickInfo
1933 class PositionTickInfo final : public PtBaseTypes {
1934 public:
1935     PositionTickInfo() = default;
1936     ~PositionTickInfo() override = default;
1937 
1938     static std::unique_ptr<PositionTickInfo> Create(const PtJson &params);
1939     std::unique_ptr<PtJson> ToJson() const override;
1940 
GetLine()1941     int32_t GetLine() const
1942     {
1943         return line_;
1944     }
1945 
SetLine(int32_t line)1946     PositionTickInfo &SetLine(int32_t line)
1947     {
1948         line_ = line;
1949         return *this;
1950     }
1951 
GetTicks()1952     int32_t GetTicks() const
1953     {
1954         return ticks_;
1955     }
1956 
SetTicks(int32_t ticks)1957     PositionTickInfo &SetTicks(int32_t ticks)
1958     {
1959         ticks_ = ticks;
1960         return *this;
1961     }
1962 
1963 private:
1964     NO_COPY_SEMANTIC(PositionTickInfo);
1965     NO_MOVE_SEMANTIC(PositionTickInfo);
1966     int32_t line_ {0};
1967     int32_t ticks_ {0};
1968 };
1969 
1970 // Profiler.ProfileNode
1971 class ProfileNode final : public PtBaseTypes {
1972 public:
1973     ProfileNode() = default;
1974     ~ProfileNode() override = default;
1975 
1976     static std::unique_ptr<ProfileNode> Create(const PtJson &params);
1977     static std::unique_ptr<ProfileNode> FromCpuProfileNode(const CpuProfileNode &cpuProfileNode);
1978     std::unique_ptr<PtJson> ToJson() const override;
1979 
GetId()1980     int32_t GetId() const
1981     {
1982         return id_;
1983     }
1984 
SetId(int32_t id)1985     ProfileNode &SetId(int32_t id)
1986     {
1987         id_ = id;
1988         return *this;
1989     }
1990 
GetCallFrame()1991     RuntimeCallFrame *GetCallFrame() const
1992     {
1993         return callFrame_.get();
1994     }
1995 
SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)1996     ProfileNode &SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)
1997     {
1998         callFrame_ = std::move(callFrame);
1999         return *this;
2000     }
2001 
GetHitCount()2002     int32_t GetHitCount() const
2003     {
2004         ASSERT(HasHitCount());
2005         return hitCount_.value();
2006     }
2007 
SetHitCount(int32_t hitCount)2008     ProfileNode &SetHitCount(int32_t hitCount)
2009     {
2010         hitCount_ = hitCount;
2011         return *this;
2012     }
2013 
HasHitCount()2014     bool HasHitCount() const
2015     {
2016         return hitCount_.has_value();
2017     }
2018 
GetChildren()2019     const std::vector<int32_t> *GetChildren() const
2020     {
2021         if (children_) {
2022             return &children_.value();
2023         }
2024         return nullptr;
2025     }
2026 
SetChildren(std::vector<int32_t> children)2027     ProfileNode &SetChildren(std::vector<int32_t> children)
2028     {
2029         children_ = std::move(children);
2030         return *this;
2031     }
2032 
HasChildren()2033     bool HasChildren() const
2034     {
2035         return children_.has_value();
2036     }
2037 
GetPositionTicks()2038     const std::vector<std::unique_ptr<PositionTickInfo>> *GetPositionTicks() const
2039     {
2040         if (positionTicks_) {
2041             return &positionTicks_.value();
2042         }
2043         return nullptr;
2044     }
2045 
SetPositionTicks(std::vector<std::unique_ptr<PositionTickInfo>> positionTicks)2046     ProfileNode &SetPositionTicks(std::vector<std::unique_ptr<PositionTickInfo>> positionTicks)
2047     {
2048         positionTicks_ = std::move(positionTicks);
2049         return *this;
2050     }
2051 
HasPositionTicks()2052     bool HasPositionTicks() const
2053     {
2054         return positionTicks_.has_value();
2055     }
2056 
GetDeoptReason()2057     const std::string &GetDeoptReason() const
2058     {
2059         ASSERT(HasDeoptReason());
2060         return deoptReason_.value();
2061     }
2062 
SetDeoptReason(const std::string & deoptReason)2063     ProfileNode &SetDeoptReason(const std::string &deoptReason)
2064     {
2065         deoptReason_ = deoptReason;
2066         return *this;
2067     }
2068 
HasDeoptReason()2069     bool HasDeoptReason() const
2070     {
2071         return deoptReason_.has_value();
2072     }
2073 
2074 private:
2075     NO_COPY_SEMANTIC(ProfileNode);
2076     NO_MOVE_SEMANTIC(ProfileNode);
2077     int32_t id_ {0};
2078     std::unique_ptr<RuntimeCallFrame> callFrame_ {nullptr};
2079     std::optional<int32_t> hitCount_ {0};
2080     std::optional<std::vector<int32_t>> children_ {};
2081     std::optional<std::vector<std::unique_ptr<PositionTickInfo>>> positionTicks_ {};
2082     std::optional<std::string> deoptReason_ {};
2083 };
2084 
2085 // Profiler.Profile
2086 class Profile final : public PtBaseTypes {
2087 public:
2088     Profile() = default;
2089     ~Profile() override = default;
2090 
2091     static std::unique_ptr<Profile> Create(const PtJson &params);
2092     static std::unique_ptr<Profile> FromProfileInfo(const ProfileInfo &profileInfo);
2093     std::unique_ptr<PtJson> ToJson() const override;
2094 
GetTid()2095     int64_t GetTid() const
2096     {
2097         return tid_;
2098     }
2099 
SetTid(int64_t tid)2100     Profile &SetTid(int64_t tid)
2101     {
2102         tid_ = tid;
2103         return *this;
2104     }
2105 
GetStartTime()2106     int64_t GetStartTime() const
2107     {
2108         return startTime_;
2109     }
2110 
SetStartTime(int64_t startTime)2111     Profile &SetStartTime(int64_t startTime)
2112     {
2113         startTime_ = startTime;
2114         return *this;
2115     }
2116 
GetEndTime()2117     int64_t GetEndTime() const
2118     {
2119         return endTime_;
2120     }
2121 
SetEndTime(int64_t endTime)2122     Profile &SetEndTime(int64_t endTime)
2123     {
2124         endTime_ = endTime;
2125         return *this;
2126     }
2127 
GetGcTime()2128     int64_t GetGcTime() const
2129     {
2130         return gcTime_;
2131     }
2132 
SetGcTime(int64_t gcTime)2133     Profile &SetGcTime(int64_t gcTime)
2134     {
2135         gcTime_ = gcTime;
2136         return *this;
2137     }
2138 
GetCInterpreterTime()2139     int64_t GetCInterpreterTime() const
2140     {
2141         return cInterpreterTime_;
2142     }
2143 
SetCInterpreterTime(int64_t cInterpreterTime)2144     Profile &SetCInterpreterTime(int64_t cInterpreterTime)
2145     {
2146         cInterpreterTime_ = cInterpreterTime;
2147         return *this;
2148     }
2149 
GetAsmInterpreterTime()2150     int64_t GetAsmInterpreterTime() const
2151     {
2152         return asmInterpreterTime_;
2153     }
2154 
SetAsmInterpreterTime(int64_t asmInterpreterTime)2155     Profile &SetAsmInterpreterTime(int64_t asmInterpreterTime)
2156     {
2157         asmInterpreterTime_ = asmInterpreterTime;
2158         return *this;
2159     }
2160 
GetAotTime()2161     int64_t GetAotTime() const
2162     {
2163         return aotTime_;
2164     }
2165 
SetAotTime(int64_t aotTime)2166     Profile &SetAotTime(int64_t aotTime)
2167     {
2168         aotTime_ = aotTime;
2169         return *this;
2170     }
2171 
GetBuiltinTime()2172     int64_t GetBuiltinTime() const
2173     {
2174         return builtinTime_;
2175     }
2176 
SetBuiltinTime(int64_t builtinTime)2177     Profile &SetBuiltinTime(int64_t builtinTime)
2178     {
2179         builtinTime_ = builtinTime;
2180         return *this;
2181     }
2182 
GetNapiTime()2183     int64_t GetNapiTime() const
2184     {
2185         return napiTime_;
2186     }
2187 
SetNapiTime(int64_t napiTime)2188     Profile &SetNapiTime(int64_t napiTime)
2189     {
2190         napiTime_ = napiTime;
2191         return *this;
2192     }
2193 
GetArkuiEngineTime()2194     int64_t GetArkuiEngineTime() const
2195     {
2196         return arkuiEngineTime_;
2197     }
2198 
SetArkuiEngineTime(int64_t arkuiEngineTime)2199     Profile &SetArkuiEngineTime(int64_t arkuiEngineTime)
2200     {
2201         arkuiEngineTime_ = arkuiEngineTime;
2202         return *this;
2203     }
2204 
GetRuntimeTime()2205     int64_t GetRuntimeTime() const
2206     {
2207         return runtimeTime_;
2208     }
2209 
SetRuntimeTime(int64_t runtimeTime)2210     Profile &SetRuntimeTime(int64_t runtimeTime)
2211     {
2212         runtimeTime_ = runtimeTime;
2213         return *this;
2214     }
2215 
GetOtherTime()2216     int64_t GetOtherTime() const
2217     {
2218         return otherTime_;
2219     }
2220 
SetOtherTime(int64_t otherTime)2221     Profile &SetOtherTime(int64_t otherTime)
2222     {
2223         otherTime_ = otherTime;
2224         return *this;
2225     }
2226 
GetNodes()2227     const std::vector<std::unique_ptr<ProfileNode>> *GetNodes() const
2228     {
2229         return &nodes_;
2230     }
2231 
SetNodes(std::vector<std::unique_ptr<ProfileNode>> nodes)2232     Profile &SetNodes(std::vector<std::unique_ptr<ProfileNode>> nodes)
2233     {
2234         nodes_ = std::move(nodes);
2235         return *this;
2236     }
2237 
GetSamples()2238     const std::vector<int32_t> *GetSamples() const
2239     {
2240         if (samples_) {
2241             return &samples_.value();
2242         }
2243         return nullptr;
2244     }
2245 
SetSamples(std::vector<int32_t> samples)2246     Profile &SetSamples(std::vector<int32_t> samples)
2247     {
2248         samples_ = std::move(samples);
2249         return *this;
2250     }
2251 
HasSamples()2252     bool HasSamples() const
2253     {
2254         return samples_.has_value();
2255     }
2256 
GetTimeDeltas()2257     const std::vector<int32_t> *GetTimeDeltas() const
2258     {
2259         if (timeDeltas_) {
2260             return &timeDeltas_.value();
2261         }
2262         return nullptr;
2263     }
2264 
SetTimeDeltas(std::vector<int32_t> timeDeltas)2265     Profile &SetTimeDeltas(std::vector<int32_t> timeDeltas)
2266     {
2267         timeDeltas_ = std::move(timeDeltas);
2268         return *this;
2269     }
2270 
HasTimeDeltas()2271     bool HasTimeDeltas() const
2272     {
2273         return timeDeltas_.has_value();
2274     }
2275 
2276 private:
2277     NO_COPY_SEMANTIC(Profile);
2278     NO_MOVE_SEMANTIC(Profile);
2279 
2280     int64_t tid_ {0};
2281     int64_t startTime_ {0};
2282     int64_t endTime_ {0};
2283     int64_t gcTime_ {0};
2284     int64_t cInterpreterTime_ {0};
2285     int64_t asmInterpreterTime_ {0};
2286     int64_t aotTime_ {0};
2287     int64_t builtinTime_ {0};
2288     int64_t napiTime_ {0};
2289     int64_t arkuiEngineTime_ {0};
2290     int64_t runtimeTime_ {0};
2291     int64_t otherTime_ {0};
2292     std::vector<std::unique_ptr<ProfileNode>> nodes_ {};
2293     std::optional<std::vector<int32_t>> samples_ {};
2294     std::optional<std::vector<int32_t>> timeDeltas_ {};
2295 };
2296 
2297 // Profiler.Coverage
2298 class Coverage final : public PtBaseTypes {
2299 public:
2300     Coverage() = default;
2301     ~Coverage() override = default;
2302 
2303     static std::unique_ptr<Coverage> Create(const PtJson &params);
2304     std::unique_ptr<PtJson> ToJson() const override;
2305 
GetStartOffset()2306     int32_t GetStartOffset() const
2307     {
2308         return startOffset_;
2309     }
2310 
SetStartOffset(int32_t startOffset)2311     Coverage &SetStartOffset(int32_t startOffset)
2312     {
2313         startOffset_ = startOffset;
2314         return *this;
2315     }
2316 
GetEndOffset()2317     int32_t GetEndOffset() const
2318     {
2319         return endOffset_;
2320     }
2321 
SetEndOffset(int32_t endOffset)2322     Coverage &SetEndOffset(int32_t endOffset)
2323     {
2324         endOffset_ = endOffset;
2325         return *this;
2326     }
2327 
GetCount()2328     int32_t GetCount() const
2329     {
2330         return count_;
2331     }
2332 
SetCount(int32_t count)2333     Coverage &SetCount(int32_t count)
2334     {
2335         count_ = count;
2336         return *this;
2337     }
2338 
2339 private:
2340     NO_COPY_SEMANTIC(Coverage);
2341     NO_MOVE_SEMANTIC(Coverage);
2342 
2343     int32_t startOffset_ {0};
2344     int32_t endOffset_ {0};
2345     int32_t count_ {0};
2346 };
2347 
2348 // Profiler.FunctionCoverage
2349 class FunctionCoverage final : public PtBaseTypes {
2350 public:
2351     FunctionCoverage() = default;
2352     ~FunctionCoverage() override = default;
2353 
2354     static std::unique_ptr<FunctionCoverage> Create(const PtJson &params);
2355     std::unique_ptr<PtJson> ToJson() const override;
2356 
GetFunctionName()2357     const std::string &GetFunctionName() const
2358     {
2359         return functionName_;
2360     }
2361 
SetFunctionName(const std::string & functionName)2362     FunctionCoverage &SetFunctionName(const std::string &functionName)
2363     {
2364         functionName_ = functionName;
2365         return *this;
2366     }
2367 
GetRanges()2368     const std::vector<std::unique_ptr<Coverage>> *GetRanges() const
2369     {
2370         return &ranges_;
2371     }
2372 
SetFunctions(std::vector<std::unique_ptr<Coverage>> ranges)2373     FunctionCoverage &SetFunctions(std::vector<std::unique_ptr<Coverage>> ranges)
2374     {
2375         ranges_ = std::move(ranges);
2376         return *this;
2377     }
2378 
GetIsBlockCoverage()2379     bool GetIsBlockCoverage() const
2380     {
2381         return isBlockCoverage_;
2382     }
2383 
SetisBlockCoverage(bool isBlockCoverage)2384     FunctionCoverage &SetisBlockCoverage(bool isBlockCoverage)
2385     {
2386         isBlockCoverage_ = isBlockCoverage;
2387         return *this;
2388     }
2389 
2390 private:
2391     NO_COPY_SEMANTIC(FunctionCoverage);
2392     NO_MOVE_SEMANTIC(FunctionCoverage);
2393 
2394     std::string functionName_ {};
2395     std::vector<std::unique_ptr<Coverage>> ranges_ {};
2396     bool isBlockCoverage_ {};
2397 };
2398 
2399 // Profiler.ScriptCoverage
2400 // Profiler.GetBestEffortCoverage and Profiler.TakePreciseCoverage share this return value type
2401 class ScriptCoverage final : public PtBaseTypes {
2402 public:
2403     ScriptCoverage() = default;
2404     ~ScriptCoverage() override = default;
2405 
2406     static std::unique_ptr<ScriptCoverage> Create(const PtJson &params);
2407     std::unique_ptr<PtJson> ToJson() const override;
2408 
GetScriptId()2409     const std::string &GetScriptId() const
2410     {
2411         return scriptId_;
2412     }
2413 
SetScriptId(const std::string & scriptId)2414     ScriptCoverage &SetScriptId(const std::string &scriptId)
2415     {
2416         scriptId_ = scriptId;
2417         return *this;
2418     }
2419 
GetUrl()2420     const std::string &GetUrl() const
2421     {
2422         return url_;
2423     }
2424 
SetUrl(const std::string & url)2425     ScriptCoverage &SetUrl(const std::string &url)
2426     {
2427         url_ = url;
2428         return *this;
2429     }
2430 
GetFunctions()2431     const std::vector<std::unique_ptr<FunctionCoverage>> *GetFunctions() const
2432     {
2433         return &functions_;
2434     }
2435 
SetFunctions(std::vector<std::unique_ptr<FunctionCoverage>> functions)2436     ScriptCoverage &SetFunctions(std::vector<std::unique_ptr<FunctionCoverage>> functions)
2437     {
2438         functions_ = std::move(functions);
2439         return *this;
2440     }
2441 
2442 private:
2443     NO_COPY_SEMANTIC(ScriptCoverage);
2444     NO_MOVE_SEMANTIC(ScriptCoverage);
2445 
2446     std::string scriptId_ {};
2447     std::string url_ {};
2448     std::vector<std::unique_ptr<FunctionCoverage>> functions_ {};
2449 };
2450 
2451 // Profiler.TypeObject
2452 class TypeObject final : public PtBaseTypes {
2453 public:
2454     TypeObject() = default;
2455     ~TypeObject() override = default;
2456 
2457     static std::unique_ptr<TypeObject> Create(const PtJson &params);
2458     std::unique_ptr<PtJson> ToJson() const override;
2459 
GetName()2460     const std::string &GetName() const
2461     {
2462         return name_;
2463     }
2464 
SetName(const std::string & name)2465     TypeObject &SetName(const std::string &name)
2466     {
2467         name_ = name;
2468         return *this;
2469     }
2470 
2471 private:
2472     NO_COPY_SEMANTIC(TypeObject);
2473     NO_MOVE_SEMANTIC(TypeObject);
2474 
2475     std::string name_ {};
2476 };
2477 
2478 // Profiler.TypeProfileEntry
2479 class TypeProfileEntry final : public PtBaseTypes {
2480 public:
2481     TypeProfileEntry() = default;
2482     ~TypeProfileEntry() override = default;
2483 
2484     static std::unique_ptr<TypeProfileEntry> Create(const PtJson &params);
2485     std::unique_ptr<PtJson> ToJson() const override;
2486 
GetOffset()2487     int32_t GetOffset() const
2488     {
2489         return offset_;
2490     }
2491 
SetOffset(int32_t offset)2492     TypeProfileEntry &SetOffset(int32_t offset)
2493     {
2494         offset_ = offset;
2495         return *this;
2496     }
2497 
GetTypes()2498     const std::vector<std::unique_ptr<TypeObject>> *GetTypes() const
2499     {
2500         return &types_;
2501     }
2502 
SetTypes(std::vector<std::unique_ptr<TypeObject>> types)2503     TypeProfileEntry &SetTypes(std::vector<std::unique_ptr<TypeObject>> types)
2504     {
2505         types_ = std::move(types);
2506         return *this;
2507     }
2508 
2509 private:
2510     NO_COPY_SEMANTIC(TypeProfileEntry);
2511     NO_MOVE_SEMANTIC(TypeProfileEntry);
2512 
2513     int32_t offset_ {0};
2514     std::vector<std::unique_ptr<TypeObject>> types_ {};
2515 };
2516 
2517 // Profiler.ScriptTypeProfile
2518 class ScriptTypeProfile final : public PtBaseTypes {
2519 public:
2520     ScriptTypeProfile() = default;
2521     ~ScriptTypeProfile() override = default;
2522 
2523     static std::unique_ptr<ScriptTypeProfile> Create(const PtJson &params);
2524     std::unique_ptr<PtJson> ToJson() const override;
2525 
GetScriptId()2526     const std::string &GetScriptId() const
2527     {
2528         return scriptId_;
2529     }
2530 
SetScriptId(const std::string & scriptId)2531     ScriptTypeProfile &SetScriptId(const std::string &scriptId)
2532     {
2533         scriptId_ = scriptId;
2534         return *this;
2535     }
2536 
GetUrl()2537     const std::string &GetUrl() const
2538     {
2539         return url_;
2540     }
2541 
SetUrl(const std::string & url)2542     ScriptTypeProfile &SetUrl(const std::string &url)
2543     {
2544         url_ = url;
2545         return *this;
2546     }
2547 
GetEntries()2548     const std::vector<std::unique_ptr<TypeProfileEntry>> *GetEntries() const
2549     {
2550         return &entries_;
2551     }
2552 
SetEntries(std::vector<std::unique_ptr<TypeProfileEntry>> entries)2553     ScriptTypeProfile &SetEntries(std::vector<std::unique_ptr<TypeProfileEntry>> entries)
2554     {
2555         entries_ = std::move(entries);
2556         return *this;
2557     }
2558 
2559 private:
2560     NO_COPY_SEMANTIC(ScriptTypeProfile);
2561     NO_MOVE_SEMANTIC(ScriptTypeProfile);
2562 
2563     std::string scriptId_ {};
2564     std::string url_ {};
2565     std::vector<std::unique_ptr<TypeProfileEntry>> entries_ {};
2566 };
2567 
2568 // ========== Tracing types begin
2569 // Tracing.MemoryDumpConfig
2570 using MemoryDumpConfig = PtJson;
2571 
2572 // Tracing.MemoryDumpLevelOfDetail
2573 using MemoryDumpLevelOfDetail = std::string;
2574 struct MemoryDumpLevelOfDetailValues {
ValidMemoryDumpLevelOfDetailValues2575     static bool Valid(const std::string &values)
2576     {
2577         return values == Background() || values == Light() || values == Detailed();
2578     }
BackgroundMemoryDumpLevelOfDetailValues2579     static std::string Background()
2580     {
2581         return "background";
2582     }
LightMemoryDumpLevelOfDetailValues2583     static std::string Light()
2584     {
2585         return "light";
2586     }
DetailedMemoryDumpLevelOfDetailValues2587     static std::string Detailed()
2588     {
2589         return "detailed";
2590     }
2591 };
2592 
2593 // Tracing.StreamCompression
2594 using StreamCompression = std::string;
2595 struct StreamCompressionValues {
ValidStreamCompressionValues2596     static bool Valid(const std::string &values)
2597     {
2598         return values == None() || values == Gzip();
2599     }
NoneStreamCompressionValues2600     static std::string None()
2601     {
2602         return "none";
2603     }
GzipStreamCompressionValues2604     static std::string Gzip()
2605     {
2606         return "gzip";
2607     }
2608 };
2609 
2610 // Tracing.StreamFormat
2611 using StreamFormat = std::string;
2612 struct StreamFormatValues {
ValidStreamFormatValues2613     static bool Valid(const std::string &values)
2614     {
2615         return values == Json() || values == Proto();
2616     }
JsonStreamFormatValues2617     static std::string Json()
2618     {
2619         return "json";
2620     }
ProtoStreamFormatValues2621     static std::string Proto()
2622     {
2623         return "proto";
2624     }
2625 };
2626 
2627 // Tracing.TraceConfig
2628 class TraceConfig final : public PtBaseTypes {
2629 public:
2630     TraceConfig() = default;
2631     ~TraceConfig() override = default;
2632 
2633     static std::unique_ptr<TraceConfig> Create(const PtJson &params);
2634     std::unique_ptr<PtJson> ToJson() const override;
2635 
GetRecordMode()2636     std::string GetRecordMode() const
2637     {
2638         return recordMode_.value();
2639     }
2640 
SetRecordMode(std::string recordMode)2641     TraceConfig &SetRecordMode(std::string recordMode)
2642     {
2643         recordMode_ = recordMode;
2644         return *this;
2645     }
2646 
HasRecordMode()2647     bool HasRecordMode() const
2648     {
2649         return recordMode_.has_value();
2650     }
2651 
2652     struct RecordModeValues {
ValidRecordModeValues2653         static bool Valid(const std::string &values)
2654         {
2655             return values == RecordUntilFull() || values == RecordContinuously() ||
2656                    values == RecordAsMuchAsPossible() || values == EchoToConsole();
2657         }
RecordUntilFullRecordModeValues2658         static std::string RecordUntilFull()
2659         {
2660             return "recordUntilFull";
2661         }
RecordContinuouslyRecordModeValues2662         static std::string RecordContinuously()
2663         {
2664             return "recordContinuously";
2665         }
RecordAsMuchAsPossibleRecordModeValues2666         static std::string RecordAsMuchAsPossible()
2667         {
2668             return "recordAsMuchAsPossible";
2669         }
EchoToConsoleRecordModeValues2670         static std::string EchoToConsole()
2671         {
2672             return "echoToConsole";
2673         }
2674     };
2675 
GetEnableSampling()2676     bool GetEnableSampling() const
2677     {
2678         return enableSampling_.value();
2679     }
2680 
SetEnableSampling(bool enableSampling)2681     TraceConfig &SetEnableSampling(bool enableSampling)
2682     {
2683         enableSampling_ = enableSampling;
2684         return *this;
2685     }
2686 
HasEnableSampling()2687     bool HasEnableSampling() const
2688     {
2689         return enableSampling_.has_value();
2690     }
2691 
GetEnableSystrace()2692     bool GetEnableSystrace() const
2693     {
2694         return enableSystrace_.value();
2695     }
2696 
SetEnableSystrace(bool enableSystrace)2697     TraceConfig &SetEnableSystrace(bool enableSystrace)
2698     {
2699         enableSystrace_ = enableSystrace;
2700         return *this;
2701     }
2702 
HasEnableSystrace()2703     bool HasEnableSystrace() const
2704     {
2705         return enableSystrace_.has_value();
2706     }
2707 
GetEnableArgumentFilter()2708     bool GetEnableArgumentFilter() const
2709     {
2710         return enableArgumentFilter_.value();
2711     }
2712 
SetEnableArgumentFilter(bool enableArgumentFilter)2713     TraceConfig &SetEnableArgumentFilter(bool enableArgumentFilter)
2714     {
2715         enableArgumentFilter_ = enableArgumentFilter;
2716         return *this;
2717     }
2718 
HasEnableArgumentFilter()2719     bool HasEnableArgumentFilter() const
2720     {
2721         return enableArgumentFilter_.has_value();
2722     }
2723 
GetIncludedCategories()2724     const std::vector<std::string> *GetIncludedCategories() const
2725     {
2726         if (includedCategories_) {
2727             return &includedCategories_.value();
2728         }
2729         return nullptr;
2730     }
2731 
SetIncludedCategories(std::vector<std::string> includedCategories)2732     TraceConfig &SetIncludedCategories(std::vector<std::string> includedCategories)
2733     {
2734         includedCategories_ = includedCategories;
2735         return *this;
2736     }
2737 
HasIncludedCategories()2738     bool HasIncludedCategories() const
2739     {
2740         return includedCategories_.has_value();
2741     }
2742 
GetExcludedCategories()2743     const std::vector<std::string> *GetExcludedCategories() const
2744     {
2745         if (excludedCategories_) {
2746             return &excludedCategories_.value();
2747         }
2748         return nullptr;
2749     }
2750 
SetExcludedCategories(std::vector<std::string> excludedCategories)2751     TraceConfig &SetExcludedCategories(std::vector<std::string> excludedCategories)
2752     {
2753         excludedCategories_ = excludedCategories;
2754         return *this;
2755     }
2756 
HasExcludedCategories()2757     bool HasExcludedCategories() const
2758     {
2759         return excludedCategories_.has_value();
2760     }
2761 
GetSyntheticDelays()2762     const std::vector<std::string> *GetSyntheticDelays() const
2763     {
2764         if (syntheticDelays_) {
2765             return &syntheticDelays_.value();
2766         }
2767         return nullptr;
2768     }
2769 
SetSyntheticDelays(std::vector<std::string> syntheticDelays)2770     TraceConfig &SetSyntheticDelays(std::vector<std::string> syntheticDelays)
2771     {
2772         syntheticDelays_ = syntheticDelays;
2773         return *this;
2774     }
2775 
HasSyntheticDelays()2776     bool HasSyntheticDelays() const
2777     {
2778         return syntheticDelays_.has_value();
2779     }
2780 
2781 private:
2782     NO_COPY_SEMANTIC(TraceConfig);
2783     NO_MOVE_SEMANTIC(TraceConfig);
2784 
2785     std::optional<std::string> recordMode_ {};
2786     std::optional<bool> enableSampling_ {};
2787     std::optional<bool> enableSystrace_ {};
2788     std::optional<bool> enableArgumentFilter_ {};
2789     std::optional<std::vector<std::string>> includedCategories_ {};
2790     std::optional<std::vector<std::string>> excludedCategories_ {};
2791     std::optional<std::vector<std::string>> syntheticDelays_ {};
2792     std::optional<std::unique_ptr<MemoryDumpConfig>> memoryDumpConfig_ {};
2793 };
2794 
2795 // Tracing.TracingBackend
2796 using TracingBackend = std::string;
2797 struct TracingBackendValues {
ValidTracingBackendValues2798     static bool Valid(const std::string &values)
2799     {
2800         return values == Auto() || values == Chrome() || values == System();
2801     }
AutoTracingBackendValues2802     static std::string Auto()
2803     {
2804         return "auto";
2805     }
ChromeTracingBackendValues2806     static std::string Chrome()
2807     {
2808         return "chrome";
2809     }
SystemTracingBackendValues2810     static std::string System()
2811     {
2812         return "system";
2813     }
2814 };
2815 }  // namespace panda::ecmascript::tooling
2816 #endif
2817