• 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 #ifdef SUPPORT_PROFILER_CDP
23 #include "ecmascript/dfx/cpu_profiler/samples_record.h"
24 #endif
25 #include "ecmascript/tooling/backend/debugger_api.h"
26 #include "ecmascript/tooling/base/pt_json.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 // ========== Runtime types begin
88 // Runtime.ScriptId
89 using ScriptId = int32_t;
90 
91 // Runtime.RemoteObjectId
92 
93 using RemoteObjectId = int32_t;
94 
95 // Runtime.ExecutionContextId
96 using ExecutionContextId = int32_t;
97 
98 // Runtime.UnserializableValue
99 using UnserializableValue = std::string;
100 
101 // Runtime.UniqueDebuggerId
102 using UniqueDebuggerId = int32_t;
103 
104 // Runtime.RemoteObject
105 class RemoteObject : public PtBaseTypes {
106 public:
107     RemoteObject() = default;
108     ~RemoteObject() override = default;
109 
110     static std::unique_ptr<RemoteObject> FromTagged(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
111     static std::unique_ptr<RemoteObject> Create(const PtJson &params);
112     std::unique_ptr<PtJson> ToJson() const override;
113 
114     /*
115      * @see {#ObjectType}
116      */
GetType()117     const std::string &GetType() const
118     {
119         return type_;
120     }
121 
SetType(const std::string & type)122     RemoteObject &SetType(const std::string &type)
123     {
124         type_ = type;
125         return *this;
126     }
127     /*
128      * @see {#ObjectSubType}
129      */
GetSubType()130     const std::string &GetSubType() const
131     {
132         ASSERT(HasSubType());
133         return subType_.value();
134     }
135 
SetSubType(const std::string & type)136     RemoteObject &SetSubType(const std::string &type)
137     {
138         subType_ = type;
139         return *this;
140     }
141 
HasSubType()142     bool HasSubType() const
143     {
144         return subType_.has_value();
145     }
146 
GetClassName()147     const std::string &GetClassName() const
148     {
149         ASSERT(HasClassName());
150         return className_.value();
151     }
152 
SetClassName(const std::string & className)153     RemoteObject &SetClassName(const std::string &className)
154     {
155         className_ = className;
156         return *this;
157     }
158 
HasClassName()159     bool HasClassName() const
160     {
161         return className_.has_value();
162     }
163 
GetValue()164     Local<JSValueRef> GetValue() const
165     {
166         return value_.value_or(Local<JSValueRef>());
167     }
168 
SetValue(Local<JSValueRef> value)169     RemoteObject &SetValue(Local<JSValueRef> value)
170     {
171         value_ = value;
172         return *this;
173     }
174 
HasValue()175     bool HasValue() const
176     {
177         return value_.has_value();
178     }
179 
GetUnserializableValue()180     const UnserializableValue &GetUnserializableValue() const
181     {
182         ASSERT(HasUnserializableValue());
183         return unserializableValue_.value();
184     }
185 
SetUnserializableValue(const UnserializableValue & unserializableValue)186     RemoteObject &SetUnserializableValue(const UnserializableValue &unserializableValue)
187     {
188         unserializableValue_ = unserializableValue;
189         return *this;
190     }
191 
HasUnserializableValue()192     bool HasUnserializableValue() const
193     {
194         return unserializableValue_.has_value();
195     }
196 
GetDescription()197     const std::string &GetDescription() const
198     {
199         ASSERT(HasDescription());
200         return description_.value();
201     }
202 
SetDescription(const std::string & description)203     RemoteObject &SetDescription(const std::string &description)
204     {
205         description_ = description;
206         return *this;
207     }
208 
HasDescription()209     bool HasDescription() const
210     {
211         return description_.has_value();
212     }
213 
GetObjectId()214     RemoteObjectId GetObjectId() const
215     {
216         return objectId_.value_or(0);
217     }
218 
SetObjectId(RemoteObjectId objectId)219     RemoteObject &SetObjectId(RemoteObjectId objectId)
220     {
221         objectId_ = objectId;
222         return *this;
223     }
224 
HasObjectId()225     bool HasObjectId() const
226     {
227         return objectId_.has_value();
228     }
229 
230     struct TypeName {
231         static const std::string Object;     // NOLINT (readability-identifier-naming)
232         static const std::string Function;   // NOLINT (readability-identifier-naming)
233         static const std::string Undefined;  // NOLINT (readability-identifier-naming)
234         static const std::string String;     // NOLINT (readability-identifier-naming)
235         static const std::string Number;     // NOLINT (readability-identifier-naming)
236         static const std::string Boolean;    // NOLINT (readability-identifier-naming)
237         static const std::string Symbol;     // NOLINT (readability-identifier-naming)
238         static const std::string Bigint;     // NOLINT (readability-identifier-naming)
239         static const std::string Wasm;       // NOLINT (readability-identifier-naming)
ValidTypeName240         static bool Valid(const std::string &type)
241         {
242             return type == Object || type == Function || type == Undefined || type == String || type == Number ||
243                    type == Boolean || type == Symbol || type == Bigint || type == Wasm;
244         }
245     };
246 
247     struct SubTypeName {
248         static const std::string Array;        // NOLINT (readability-identifier-naming)
249         static const std::string Null;         // NOLINT (readability-identifier-naming)
250         static const std::string Node;         // NOLINT (readability-identifier-naming)
251         static const std::string Regexp;       // NOLINT (readability-identifier-naming)
252         static const std::string Date;         // NOLINT (readability-identifier-naming)
253         static const std::string Map;          // NOLINT (readability-identifier-naming)
254         static const std::string Set;          // NOLINT (readability-identifier-naming)
255         static const std::string Weakmap;      // NOLINT (readability-identifier-naming)
256         static const std::string Weakset;      // NOLINT (readability-identifier-naming)
257         static const std::string Iterator;     // NOLINT (readability-identifier-naming)
258         static const std::string Generator;    // NOLINT (readability-identifier-naming)
259         static const std::string Error;        // NOLINT (readability-identifier-naming)
260         static const std::string Proxy;        // NOLINT (readability-identifier-naming)
261         static const std::string Promise;      // NOLINT (readability-identifier-naming)
262         static const std::string Typedarray;   // NOLINT (readability-identifier-naming)
263         static const std::string Arraybuffer;  // NOLINT (readability-identifier-naming)
264         static const std::string Dataview;     // NOLINT (readability-identifier-naming)
265         static const std::string I32;          // NOLINT (readability-identifier-naming)
266         static const std::string I64;          // NOLINT (readability-identifier-naming)
267         static const std::string F32;          // NOLINT (readability-identifier-naming)
268         static const std::string F64;          // NOLINT (readability-identifier-naming)
269         static const std::string V128;         // NOLINT (readability-identifier-naming)
270         static const std::string Externref;    // NOLINT (readability-identifier-naming)
ValidSubTypeName271         static bool Valid(const std::string &type)
272         {
273             return type == Array || type == Null || type == Node || type == Regexp || type == Map || type == Set ||
274                    type == Weakmap || type == Iterator || type == Generator || type == Error || type == Proxy ||
275                    type == Promise || type == Typedarray || type == Arraybuffer || type == Dataview || type == I32 ||
276                    type == I64 || type == F32 || type == F64 || type == V128 || type == Externref;
277         }
278     };
279     struct ClassName {
280         static const std::string Object;          // NOLINT (readability-identifier-naming)
281         static const std::string Function;        // NOLINT (readability-identifier-naming)
282         static const std::string Array;           // NOLINT (readability-identifier-naming)
283         static const std::string Regexp;          // NOLINT (readability-identifier-naming)
284         static const std::string Date;            // NOLINT (readability-identifier-naming)
285         static const std::string Map;             // NOLINT (readability-identifier-naming)
286         static const std::string Set;             // NOLINT (readability-identifier-naming)
287         static const std::string Weakmap;         // NOLINT (readability-identifier-naming)
288         static const std::string Weakset;         // NOLINT (readability-identifier-naming)
289         static const std::string ArrayIterator;   // NOLINT (readability-identifier-naming)
290         static const std::string StringIterator;  // NOLINT (readability-identifier-naming)
291         static const std::string SetIterator;     // NOLINT (readability-identifier-naming)
292         static const std::string MapIterator;     // NOLINT (readability-identifier-naming)
293         static const std::string Iterator;        // NOLINT (readability-identifier-naming)
294         static const std::string Error;           // NOLINT (readability-identifier-naming)
295         static const std::string Proxy;           // NOLINT (readability-identifier-naming)
296         static const std::string Promise;         // NOLINT (readability-identifier-naming)
297         static const std::string Typedarray;      // NOLINT (readability-identifier-naming)
298         static const std::string Arraybuffer;     // NOLINT (readability-identifier-naming)
299         static const std::string Global;          // NOLINT (readability-identifier-naming)
ValidClassName300         static bool Valid(const std::string &type)
301         {
302             return type == Object || type == Array || type == Regexp || type == Date || type == Map || type == Set ||
303                    type == Weakmap || type == Weakset || type == ArrayIterator || type == StringIterator ||
304                    type == Error || type == SetIterator || type == MapIterator || type == Iterator || type == Proxy ||
305                    type == Promise || type == Typedarray || type == Arraybuffer || type == Function;
306         }
307     };
308     static const std::string ObjectDescription;          // NOLINT (readability-identifier-naming)
309     static const std::string GlobalDescription;          // NOLINT (readability-identifier-naming)
310     static const std::string ProxyDescription;           // NOLINT (readability-identifier-naming)
311     static const std::string PromiseDescription;         // NOLINT (readability-identifier-naming)
312     static const std::string ArrayIteratorDescription;   // NOLINT (readability-identifier-naming)
313     static const std::string StringIteratorDescription;  // NOLINT (readability-identifier-naming)
314     static const std::string SetIteratorDescription;     // NOLINT (readability-identifier-naming)
315     static const std::string MapIteratorDescription;     // NOLINT (readability-identifier-naming)
316     static const std::string WeakMapDescription;         // NOLINT (readability-identifier-naming)
317     static const std::string WeakSetDescription;         // NOLINT (readability-identifier-naming)
318 
319 private:
320     NO_COPY_SEMANTIC(RemoteObject);
321     NO_MOVE_SEMANTIC(RemoteObject);
322 
323     std::string type_ {};
324     std::optional<std::string> subType_ {};
325     std::optional<std::string> className_ {};
326     std::optional<Local<JSValueRef>> value_ {};
327     std::optional<UnserializableValue> unserializableValue_ {};
328     std::optional<std::string> description_ {};
329     std::optional<RemoteObjectId> objectId_ {};
330 };
331 
332 class PrimitiveRemoteObject final : public RemoteObject {
333 public:
334     PrimitiveRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
335     ~PrimitiveRemoteObject() override = default;
336 };
337 
338 class StringRemoteObject final : public RemoteObject {
339 public:
340     StringRemoteObject(const EcmaVM *ecmaVm, Local<StringRef> tagged);
341     virtual ~StringRemoteObject() = default;
342 };
343 
344 class SymbolRemoteObject final : public RemoteObject {
345 public:
346     SymbolRemoteObject(const EcmaVM *ecmaVm, Local<SymbolRef> tagged);
347     ~SymbolRemoteObject() override = default;
348 
349 private:
350     std::string DescriptionForSymbol(const EcmaVM *ecmaVm, Local<SymbolRef> tagged) const;
351 };
352 
353 class FunctionRemoteObject final : public RemoteObject {
354 public:
355     FunctionRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
356     ~FunctionRemoteObject() override = default;
357 
358 private:
359     std::string DescriptionForFunction(const EcmaVM *ecmaVm, Local<FunctionRef> tagged) const;
360 };
361 
362 class ObjectRemoteObject final : public RemoteObject {
363 public:
364     ObjectRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged, const std::string &classname);
365     ObjectRemoteObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged, const std::string &classname,
366         const std::string &subtype);
367     ~ObjectRemoteObject() override = default;
368     static std::string DescriptionForObject(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
369 
370 private:
371     static std::string DescriptionForArray(const EcmaVM *ecmaVm, Local<ArrayRef> tagged);
372     static std::string DescriptionForRegexp(const EcmaVM *ecmaVm, Local<RegExpRef> tagged);
373     static std::string DescriptionForDate(const EcmaVM *ecmaVm, Local<DateRef> tagged);
374     static std::string DescriptionForMap(Local<MapRef> tagged);
375     static std::string DescriptionForSet(Local<SetRef> tagged);
376     static std::string DescriptionForError(const EcmaVM *ecmaVm, Local<JSValueRef> tagged);
377     static std::string DescriptionForArrayBuffer(const EcmaVM *ecmaVm, Local<ArrayBufferRef> tagged);
378 };
379 
380 // Runtime.ExceptionDetails
381 class ExceptionDetails final : public PtBaseTypes {
382 public:
383     ExceptionDetails() = default;
384     ~ExceptionDetails() override = default;
385     static std::unique_ptr<ExceptionDetails> Create(const PtJson &params);
386     std::unique_ptr<PtJson> ToJson() const override;
387 
GetExceptionId()388     int32_t GetExceptionId() const
389     {
390         return exceptionId_;
391     }
392 
SetExceptionId(int32_t exceptionId)393     ExceptionDetails &SetExceptionId(int32_t exceptionId)
394     {
395         exceptionId_ = exceptionId;
396         return *this;
397     }
398 
GetText()399     const std::string &GetText() const
400     {
401         return text_;
402     }
403 
SetText(const std::string & text)404     ExceptionDetails &SetText(const std::string &text)
405     {
406         text_ = text;
407         return *this;
408     }
409 
GetLine()410     int32_t GetLine() const
411     {
412         return lineNumber_;
413     }
414 
SetLine(int32_t lineNumber)415     ExceptionDetails &SetLine(int32_t lineNumber)
416     {
417         lineNumber_ = lineNumber;
418         return *this;
419     }
420 
GetColumn()421     int32_t GetColumn() const
422     {
423         return columnNumber_;
424     }
425 
SetColumn(int32_t columnNumber)426     ExceptionDetails &SetColumn(int32_t columnNumber)
427     {
428         columnNumber_ = columnNumber;
429         return *this;
430     }
431 
GetScriptId()432     ScriptId GetScriptId() const
433     {
434         return scriptId_.value_or(0);
435     }
436 
SetScriptId(ScriptId scriptId)437     ExceptionDetails &SetScriptId(ScriptId scriptId)
438     {
439         scriptId_ = scriptId;
440         return *this;
441     }
442 
HasScriptId()443     bool HasScriptId() const
444     {
445         return scriptId_.has_value();
446     }
447 
GetUrl()448     const std::string &GetUrl() const
449     {
450         ASSERT(HasUrl());
451         return url_.value();
452     }
453 
SetUrl(const std::string & url)454     ExceptionDetails &SetUrl(const std::string &url)
455     {
456         url_ = url;
457         return *this;
458     }
459 
HasUrl()460     bool HasUrl() const
461     {
462         return url_.has_value();
463     }
464 
GetException()465     RemoteObject *GetException() const
466     {
467         if (exception_) {
468             return exception_->get();
469         }
470         return nullptr;
471     }
472 
SetException(std::unique_ptr<RemoteObject> exception)473     ExceptionDetails &SetException(std::unique_ptr<RemoteObject> exception)
474     {
475         exception_ = std::move(exception);
476         return *this;
477     }
478 
HasException()479     bool HasException() const
480     {
481         return exception_.has_value();
482     }
483 
GetExecutionContextId()484     ExecutionContextId GetExecutionContextId() const
485     {
486         return executionContextId_.value_or(-1);
487     }
488 
SetExecutionContextId(ExecutionContextId executionContextId)489     ExceptionDetails &SetExecutionContextId(ExecutionContextId executionContextId)
490     {
491         executionContextId_ = executionContextId;
492         return *this;
493     }
494 
HasExecutionContextId()495     bool HasExecutionContextId() const
496     {
497         return executionContextId_.has_value();
498     }
499 
500 private:
501     NO_COPY_SEMANTIC(ExceptionDetails);
502     NO_MOVE_SEMANTIC(ExceptionDetails);
503 
504     int32_t exceptionId_ {0};
505     std::string text_ {};
506     int32_t lineNumber_ {0};
507     int32_t columnNumber_ {0};
508     std::optional<ScriptId> scriptId_ {};
509     std::optional<std::string> url_ {};
510     std::optional<std::unique_ptr<RemoteObject>> exception_ {};
511     std::optional<ExecutionContextId> executionContextId_ {0};
512 };
513 
514 // Runtime.InternalPropertyDescriptor
515 class InternalPropertyDescriptor final : public PtBaseTypes {
516 public:
517     InternalPropertyDescriptor() = default;
518     ~InternalPropertyDescriptor() override = default;
519 
520     static std::unique_ptr<InternalPropertyDescriptor> Create(const PtJson &params);
521     std::unique_ptr<PtJson> ToJson() const override;
522 
GetName()523     std::string GetName() const
524     {
525         return name_;
526     }
527 
SetName(const std::string & name)528     InternalPropertyDescriptor &SetName(const std::string &name)
529     {
530         name_ = name;
531         return *this;
532     }
533 
GetValue()534     RemoteObject *GetValue() const
535     {
536         if (value_) {
537             return value_->get();
538         }
539         return nullptr;
540     }
541 
SetValue(std::unique_ptr<RemoteObject> value)542     InternalPropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
543     {
544         value_ = std::move(value);
545         return *this;
546     }
547 
HasValue()548     bool HasValue() const
549     {
550         return value_.has_value();
551     }
552 
553 private:
554     NO_COPY_SEMANTIC(InternalPropertyDescriptor);
555     NO_MOVE_SEMANTIC(InternalPropertyDescriptor);
556 
557     std::string name_ {};
558     std::optional<std::unique_ptr<RemoteObject>> value_ {};
559 };
560 
561 // Runtime.PrivatePropertyDescriptor
562 class PrivatePropertyDescriptor final : public PtBaseTypes {
563 public:
564     PrivatePropertyDescriptor() = default;
565     ~PrivatePropertyDescriptor() override = default;
566 
567     static std::unique_ptr<PrivatePropertyDescriptor> Create(const PtJson &params);
568     std::unique_ptr<PtJson> ToJson() const override;
569 
GetName()570     std::string GetName() const
571     {
572         return name_;
573     }
574 
SetName(const std::string & name)575     PrivatePropertyDescriptor &SetName(const std::string &name)
576     {
577         name_ = name;
578         return *this;
579     }
580 
GetValue()581     RemoteObject *GetValue() const
582     {
583         if (value_) {
584             return value_->get();
585         }
586         return nullptr;
587     }
588 
SetValue(std::unique_ptr<RemoteObject> value)589     PrivatePropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
590     {
591         value_ = std::move(value);
592         return *this;
593     }
594 
HasValue()595     bool HasValue() const
596     {
597         return value_.has_value();
598     }
599 
GetGet()600     RemoteObject *GetGet() const
601     {
602         if (get_) {
603             return get_->get();
604         }
605         return nullptr;
606     }
607 
SetGet(std::unique_ptr<RemoteObject> get)608     PrivatePropertyDescriptor &SetGet(std::unique_ptr<RemoteObject> get)
609     {
610         get_ = std::move(get);
611         return *this;
612     }
613 
HasGet()614     bool HasGet() const
615     {
616         return get_.has_value();
617     }
618 
GetSet()619     RemoteObject *GetSet() const
620     {
621         if (set_) {
622             return set_->get();
623         }
624         return nullptr;
625     }
626 
SetSet(std::unique_ptr<RemoteObject> set)627     PrivatePropertyDescriptor &SetSet(std::unique_ptr<RemoteObject> set)
628     {
629         set_ = std::move(set);
630         return *this;
631     }
632 
HasSet()633     bool HasSet() const
634     {
635         return set_.has_value();
636     }
637 
638 private:
639     NO_COPY_SEMANTIC(PrivatePropertyDescriptor);
640     NO_MOVE_SEMANTIC(PrivatePropertyDescriptor);
641 
642     std::string name_ {};
643     std::optional<std::unique_ptr<RemoteObject>> value_ {};
644     std::optional<std::unique_ptr<RemoteObject>> get_ {};
645     std::optional<std::unique_ptr<RemoteObject>> set_ {};
646 };
647 
648 // Runtime.PropertyDescriptor
649 class PropertyDescriptor final : public PtBaseTypes {
650 public:
651     PropertyDescriptor() = default;
652     ~PropertyDescriptor() override = default;
653 
654     static std::unique_ptr<PropertyDescriptor> FromProperty(const EcmaVM *ecmaVm, Local<JSValueRef> name,
655         const PropertyAttribute &property);
656     static std::unique_ptr<PropertyDescriptor> Create(const PtJson &params);
657     std::unique_ptr<PtJson> ToJson() const override;
658 
GetName()659     std::string GetName() const
660     {
661         return name_;
662     }
663 
SetName(const std::string & name)664     PropertyDescriptor &SetName(const std::string &name)
665     {
666         name_ = name;
667         return *this;
668     }
669 
GetValue()670     RemoteObject *GetValue() const
671     {
672         if (value_) {
673             return value_->get();
674         }
675         return nullptr;
676     }
677 
SetValue(std::unique_ptr<RemoteObject> value)678     PropertyDescriptor &SetValue(std::unique_ptr<RemoteObject> value)
679     {
680         value_ = std::move(value);
681         return *this;
682     }
683 
HasValue()684     bool HasValue() const
685     {
686         return value_.has_value();
687     }
688 
GetWritable()689     bool GetWritable() const
690     {
691         return writable_.value_or(false);
692     }
693 
SetWritable(bool writable)694     PropertyDescriptor &SetWritable(bool writable)
695     {
696         writable_ = writable;
697         return *this;
698     }
699 
HasWritable()700     bool HasWritable() const
701     {
702         return writable_.has_value();
703     }
704 
GetGet()705     RemoteObject *GetGet() const
706     {
707         if (get_) {
708             return get_->get();
709         }
710         return nullptr;
711     }
712 
SetGet(std::unique_ptr<RemoteObject> get)713     PropertyDescriptor &SetGet(std::unique_ptr<RemoteObject> get)
714     {
715         get_ = std::move(get);
716         return *this;
717     }
718 
HasGet()719     bool HasGet() const
720     {
721         return get_.has_value();
722     }
723 
GetSet()724     RemoteObject *GetSet() const
725     {
726         if (set_) {
727             return set_->get();
728         }
729         return nullptr;
730     }
731 
SetSet(std::unique_ptr<RemoteObject> set)732     PropertyDescriptor &SetSet(std::unique_ptr<RemoteObject> set)
733     {
734         set_ = std::move(set);
735         return *this;
736     }
737 
HasSet()738     bool HasSet() const
739     {
740         return set_.has_value();
741     }
742 
GetConfigurable()743     bool GetConfigurable() const
744     {
745         return configurable_;
746     }
747 
SetConfigurable(bool configurable)748     PropertyDescriptor &SetConfigurable(bool configurable)
749     {
750         configurable_ = configurable;
751         return *this;
752     }
753 
GetEnumerable()754     bool GetEnumerable() const
755     {
756         return enumerable_;
757     }
758 
SetEnumerable(bool enumerable)759     PropertyDescriptor &SetEnumerable(bool enumerable)
760     {
761         enumerable_ = enumerable;
762         return *this;
763     }
764 
GetWasThrown()765     bool GetWasThrown() const
766     {
767         return wasThrown_.value_or(false);
768     }
769 
SetWasThrown(bool wasThrown)770     PropertyDescriptor &SetWasThrown(bool wasThrown)
771     {
772         wasThrown_ = wasThrown;
773         return *this;
774     }
775 
HasWasThrown()776     bool HasWasThrown() const
777     {
778         return wasThrown_.has_value();
779     }
780 
GetIsOwn()781     bool GetIsOwn() const
782     {
783         return isOwn_.value_or(false);
784     }
785 
SetIsOwn(bool isOwn)786     PropertyDescriptor &SetIsOwn(bool isOwn)
787     {
788         isOwn_ = isOwn;
789         return *this;
790     }
791 
HasIsOwn()792     bool HasIsOwn() const
793     {
794         return isOwn_.has_value();
795     }
796 
GetSymbol()797     RemoteObject *GetSymbol() const
798     {
799         if (symbol_) {
800             return symbol_->get();
801         }
802         return nullptr;
803     }
804 
SetSymbol(std::unique_ptr<RemoteObject> symbol)805     PropertyDescriptor &SetSymbol(std::unique_ptr<RemoteObject> symbol)
806     {
807         symbol_ = std::move(symbol);
808         return *this;
809     }
810 
HasSymbol()811     bool HasSymbol() const
812     {
813         return symbol_.has_value();
814     }
815 
816 private:
817     NO_COPY_SEMANTIC(PropertyDescriptor);
818     NO_MOVE_SEMANTIC(PropertyDescriptor);
819 
820     std::string name_ {};
821     std::optional<std::unique_ptr<RemoteObject>> value_ {};
822     std::optional<bool> writable_ {};
823     std::optional<std::unique_ptr<RemoteObject>> get_ {};
824     std::optional<std::unique_ptr<RemoteObject>> set_ {};
825     bool configurable_ {false};
826     bool enumerable_ {false};
827     std::optional<bool> wasThrown_ {};
828     std::optional<bool> isOwn_ {};
829     std::optional<std::unique_ptr<RemoteObject>> symbol_ {};
830 };
831 
832 // Runtime.CallArgument
833 class CallArgument final : public PtBaseTypes {
834 public:
835     CallArgument() = default;
836     ~CallArgument() override = default;
837 
838     static std::unique_ptr<CallArgument> Create(const PtJson &params);
839     std::unique_ptr<PtJson> ToJson() const override;
840 
GetValue()841     Local<JSValueRef> GetValue() const
842     {
843         return value_.value_or(Local<JSValueRef>());
844     }
845 
SetValue(Local<JSValueRef> value)846     CallArgument &SetValue(Local<JSValueRef> value)
847     {
848         value_ = value;
849         return *this;
850     }
851 
HasValue()852     bool HasValue() const
853     {
854         return value_.has_value();
855     }
856 
GetUnserializableValue()857     const UnserializableValue &GetUnserializableValue() const
858     {
859         ASSERT(HasUnserializableValue());
860         return unserializableValue_.value();
861     }
862 
SetUnserializableValue(const UnserializableValue & unserializableValue)863     CallArgument &SetUnserializableValue(const UnserializableValue &unserializableValue)
864     {
865         unserializableValue_ = unserializableValue;
866         return *this;
867     }
868 
HasUnserializableValue()869     bool HasUnserializableValue() const
870     {
871         return unserializableValue_.has_value();
872     }
873 
GetObjectId()874     RemoteObjectId GetObjectId() const
875     {
876         return objectId_.value_or(0);
877     }
878 
SetObjectId(RemoteObjectId objectId)879     CallArgument &SetObjectId(RemoteObjectId objectId)
880     {
881         objectId_ = objectId;
882         return *this;
883     }
884 
HasObjectId()885     bool HasObjectId() const
886     {
887         return objectId_.has_value();
888     }
889 
890 private:
891     NO_COPY_SEMANTIC(CallArgument);
892     NO_MOVE_SEMANTIC(CallArgument);
893 
894     std::optional<Local<JSValueRef>> value_ {};
895     std::optional<UnserializableValue> unserializableValue_ {};
896     std::optional<RemoteObjectId> objectId_ {};
897 };
898 
899 // ========== Debugger types begin
900 // Debugger.ScriptLanguage
901 struct ScriptLanguage {
ValidScriptLanguage902     static bool Valid(const std::string &language)
903     {
904         return language == JavaScript() || language == WebAssembly();
905     }
JavaScriptScriptLanguage906     static std::string JavaScript()
907     {
908         return "JavaScript";
909     }
WebAssemblyScriptLanguage910     static std::string WebAssembly()
911     {
912         return "WebAssembly";
913     }
914 };
915 
916 // Debugger.Location
917 class Location : public PtBaseTypes {
918 public:
919     Location() = default;
920     ~Location() override = default;
921 
922     static std::unique_ptr<Location> Create(const PtJson &params);
923     std::unique_ptr<PtJson> ToJson() const override;
924 
GetScriptId()925     ScriptId GetScriptId() const
926     {
927         return scriptId_;
928     }
929 
SetScriptId(ScriptId scriptId)930     Location &SetScriptId(ScriptId scriptId)
931     {
932         scriptId_ = scriptId;
933         return *this;
934     }
935 
GetLine()936     int32_t GetLine() const
937     {
938         return lineNumber_;
939     }
940 
SetLine(int32_t line)941     Location &SetLine(int32_t line)
942     {
943         lineNumber_ = line;
944         return *this;
945     }
946 
GetColumn()947     int32_t GetColumn() const
948     {
949         return columnNumber_.value_or(-1);
950     }
951 
SetColumn(int32_t column)952     Location &SetColumn(int32_t column)
953     {
954         columnNumber_ = column;
955         return *this;
956     }
957 
HasColumn()958     bool HasColumn() const
959     {
960         return columnNumber_.has_value();
961     }
962 
963 private:
964     NO_COPY_SEMANTIC(Location);
965     NO_MOVE_SEMANTIC(Location);
966 
967     ScriptId scriptId_ {0};
968     int32_t lineNumber_ {0};
969     std::optional<int32_t> columnNumber_ {};
970 };
971 
972 // Debugger.ScriptPosition
973 class ScriptPosition : public PtBaseTypes {
974 public:
975     ScriptPosition() = default;
976     ~ScriptPosition() override = default;
977 
978     static std::unique_ptr<ScriptPosition> Create(const PtJson &params);
979     std::unique_ptr<PtJson> ToJson() const override;
980 
GetLine()981     int32_t GetLine() const
982     {
983         return lineNumber_;
984     }
985 
SetLine(int32_t line)986     ScriptPosition &SetLine(int32_t line)
987     {
988         lineNumber_ = line;
989         return *this;
990     }
991 
GetColumn()992     int32_t GetColumn() const
993     {
994         return columnNumber_;
995     }
996 
SetColumn(int32_t column)997     ScriptPosition &SetColumn(int32_t column)
998     {
999         columnNumber_ = column;
1000         return *this;
1001     }
1002 
1003 private:
1004     NO_COPY_SEMANTIC(ScriptPosition);
1005     NO_MOVE_SEMANTIC(ScriptPosition);
1006 
1007     int32_t lineNumber_ {0};
1008     int32_t columnNumber_ {0};
1009 };
1010 
1011 // Debugger.SearchMatch
1012 class SearchMatch : public PtBaseTypes {
1013 public:
1014     SearchMatch() = default;
1015     ~SearchMatch() override = default;
1016     static std::unique_ptr<SearchMatch> Create(const PtJson &params);
1017     std::unique_ptr<PtJson> ToJson() const override;
1018 
1019 private:
1020     NO_COPY_SEMANTIC(SearchMatch);
1021     NO_MOVE_SEMANTIC(SearchMatch);
1022 
1023     int32_t lineNumber_ {0};
1024     std::string lineContent_ {};
1025 };
1026 
1027 // Debugger.LocationRange
1028 class LocationRange : public PtBaseTypes {
1029 public:
1030     LocationRange() = default;
1031     ~LocationRange() override = default;
1032 
1033     static std::unique_ptr<LocationRange> Create(const PtJson &params);
1034     std::unique_ptr<PtJson> ToJson() const override;
1035 
GetScriptId()1036     ScriptId GetScriptId() const
1037     {
1038         return scriptId_;
1039     }
1040 
SetScriptId(ScriptId scriptId)1041     LocationRange &SetScriptId(ScriptId scriptId)
1042     {
1043         scriptId_ = scriptId;
1044         return *this;
1045     }
1046 
GetStart()1047     ScriptPosition *GetStart() const
1048     {
1049         return start_.get();
1050     }
1051 
SetStart(std::unique_ptr<ScriptPosition> start)1052     LocationRange &SetStart(std::unique_ptr<ScriptPosition> start)
1053     {
1054         start_ = std::move(start);
1055         return *this;
1056     }
1057 
GetEnd()1058     ScriptPosition *GetEnd() const
1059     {
1060         return end_.get();
1061     }
1062 
SetEnd(std::unique_ptr<ScriptPosition> end)1063     LocationRange &SetEnd(std::unique_ptr<ScriptPosition> end)
1064     {
1065         end_ = std::move(end);
1066         return *this;
1067     }
1068 
1069 private:
1070     NO_COPY_SEMANTIC(LocationRange);
1071     NO_MOVE_SEMANTIC(LocationRange);
1072 
1073     ScriptId scriptId_ {0};
1074     std::unique_ptr<ScriptPosition> start_ {nullptr};
1075     std::unique_ptr<ScriptPosition> end_ {nullptr};
1076 };
1077 
1078 // Debugger.BreakLocation
1079 class BreakLocation final : public PtBaseTypes {
1080 public:
1081     BreakLocation() = default;
1082     ~BreakLocation() override = default;
1083 
1084     static std::unique_ptr<BreakLocation> Create(const PtJson &params);
1085     std::unique_ptr<PtJson> ToJson() const override;
1086 
GetScriptId()1087     ScriptId GetScriptId() const
1088     {
1089         return scriptId_;
1090     }
1091 
SetScriptId(ScriptId scriptId)1092     BreakLocation &SetScriptId(ScriptId scriptId)
1093     {
1094         scriptId_ = scriptId;
1095         return *this;
1096     }
1097 
GetLine()1098     int32_t GetLine() const
1099     {
1100         return lineNumber_;
1101     }
1102 
SetLine(int32_t lineNumber)1103     BreakLocation &SetLine(int32_t lineNumber)
1104     {
1105         lineNumber_ = lineNumber;
1106         return *this;
1107     }
1108 
GetColumn()1109     int32_t GetColumn() const
1110     {
1111         return columnNumber_.value_or(-1);
1112     }
1113 
SetColumn(int32_t columnNumber)1114     BreakLocation &SetColumn(int32_t columnNumber)
1115     {
1116         columnNumber_ = columnNumber;
1117         return *this;
1118     }
1119 
HasColumn()1120     bool HasColumn() const
1121     {
1122         return columnNumber_.has_value();
1123     }
1124 
1125     /*
1126      * @see {#BreakType}
1127      */
GetType()1128     const std::string &GetType() const
1129     {
1130         ASSERT(HasType());
1131         return type_.value();
1132     }
1133 
SetType(const std::string & type)1134     BreakLocation &SetType(const std::string &type)
1135     {
1136         type_ = type;
1137         return *this;
1138     }
1139 
HasType()1140     bool HasType() const
1141     {
1142         return type_.has_value();
1143     }
1144 
1145     struct Type {
ValidType1146         static bool Valid(const std::string &type)
1147         {
1148             return type == DebuggerStatement() || type == Call() || type == Return();
1149         }
DebuggerStatementType1150         static std::string DebuggerStatement()
1151         {
1152             return "debuggerStatement";
1153         }
CallType1154         static std::string Call()
1155         {
1156             return "call";
1157         }
ReturnType1158         static std::string Return()
1159         {
1160             return "return";
1161         }
1162     };
1163 
1164 private:
1165     NO_COPY_SEMANTIC(BreakLocation);
1166     NO_MOVE_SEMANTIC(BreakLocation);
1167 
1168     ScriptId scriptId_ {0};
1169     int32_t lineNumber_ {0};
1170     std::optional<int32_t> columnNumber_ {};
1171     std::optional<std::string> type_ {};
1172 };
1173 using BreakType = BreakLocation::Type;
1174 
1175 enum class ScopeType : uint8_t {
1176     GLOBAL,
1177     LOCAL,
1178     WITH,
1179     CLOSURE,
1180     CATCH,
1181     BLOCK,
1182     SCRIPT,
1183     EVAL,
1184     MODULE,
1185     WASM_EXPRESSION_STACK
1186 };
1187 
1188 // Debugger.Scope
1189 class Scope final : public PtBaseTypes {
1190 public:
1191     Scope() = default;
1192     ~Scope() override = default;
1193 
1194     static std::unique_ptr<Scope> Create(const PtJson &params);
1195     std::unique_ptr<PtJson> ToJson() const override;
1196 
1197     /*
1198      * @see {#Scope::Type}
1199      */
GetType()1200     const std::string &GetType() const
1201     {
1202         return type_;
1203     }
1204 
SetType(const std::string & type)1205     Scope &SetType(const std::string &type)
1206     {
1207         type_ = type;
1208         return *this;
1209     }
1210 
GetObject()1211     RemoteObject *GetObject() const
1212     {
1213         return object_.get();
1214     }
1215 
SetObject(std::unique_ptr<RemoteObject> params)1216     Scope &SetObject(std::unique_ptr<RemoteObject> params)
1217     {
1218         object_ = std::move(params);
1219         return *this;
1220     }
1221 
GetName()1222     const std::string &GetName() const
1223     {
1224         ASSERT(HasName());
1225         return name_.value();
1226     }
1227 
SetName(const std::string & name)1228     Scope &SetName(const std::string &name)
1229     {
1230         name_ = name;
1231         return *this;
1232     }
1233 
HasName()1234     bool HasName() const
1235     {
1236         return name_.has_value();
1237     }
1238 
GetStartLocation()1239     Location *GetStartLocation() const
1240     {
1241         if (startLocation_) {
1242             return startLocation_->get();
1243         }
1244         return nullptr;
1245     }
1246 
SetStartLocation(std::unique_ptr<Location> location)1247     Scope &SetStartLocation(std::unique_ptr<Location> location)
1248     {
1249         startLocation_ = std::move(location);
1250         return *this;
1251     }
1252 
HasStartLocation()1253     bool HasStartLocation() const
1254     {
1255         return startLocation_.has_value();
1256     }
1257 
GetEndLocation()1258     Location *GetEndLocation() const
1259     {
1260         if (endLocation_) {
1261             return endLocation_->get();
1262         }
1263         return nullptr;
1264     }
1265 
SetEndLocation(std::unique_ptr<Location> location)1266     Scope &SetEndLocation(std::unique_ptr<Location> location)
1267     {
1268         endLocation_ = std::move(location);
1269         return *this;
1270     }
1271 
HasEndLocation()1272     bool HasEndLocation() const
1273     {
1274         return endLocation_.has_value();
1275     }
1276 
1277     struct Type {
ValidType1278         static bool Valid(const std::string &type)
1279         {
1280             return type == Global() || type == Local() || type == With() || type == Closure() || type == Catch() ||
1281                    type == Block() || type == Script() || type == Eval() || type == Module() ||
1282                    type == WasmExpressionStack();
1283         }
GlobalType1284         static std::string Global()
1285         {
1286             return "global";
1287         }
LocalType1288         static std::string Local()
1289         {
1290             return "local";
1291         }
WithType1292         static std::string With()
1293         {
1294             return "with";
1295         }
ClosureType1296         static std::string Closure()
1297         {
1298             return "closure";
1299         }
CatchType1300         static std::string Catch()
1301         {
1302             return "catch";
1303         }
BlockType1304         static std::string Block()
1305         {
1306             return "block";
1307         }
ScriptType1308         static std::string Script()
1309         {
1310             return "script";
1311         }
EvalType1312         static std::string Eval()
1313         {
1314             return "eval";
1315         }
ModuleType1316         static std::string Module()
1317         {
1318             return "module";
1319         }
WasmExpressionStackType1320         static std::string WasmExpressionStack()
1321         {
1322             return "wasm-expression-stack";
1323         }
1324     };
1325 
1326 private:
1327     NO_COPY_SEMANTIC(Scope);
1328     NO_MOVE_SEMANTIC(Scope);
1329 
1330     std::string type_ {};
1331     std::unique_ptr<RemoteObject> object_ {nullptr};
1332     std::optional<std::string> name_ {};
1333     std::optional<std::unique_ptr<Location>> startLocation_ {};
1334     std::optional<std::unique_ptr<Location>> endLocation_ {};
1335 };
1336 
1337 // Debugger.CallFrame
1338 class CallFrame final : public PtBaseTypes {
1339 public:
1340     CallFrame() = default;
1341     ~CallFrame() override = default;
1342 
1343     static std::unique_ptr<CallFrame> Create(const PtJson &params);
1344     std::unique_ptr<PtJson> ToJson() const override;
1345 
GetCallFrameId()1346     CallFrameId GetCallFrameId() const
1347     {
1348         return callFrameId_;
1349     }
1350 
SetCallFrameId(CallFrameId callFrameId)1351     CallFrame &SetCallFrameId(CallFrameId callFrameId)
1352     {
1353         callFrameId_ = callFrameId;
1354         return *this;
1355     }
1356 
GetFunctionName()1357     const std::string &GetFunctionName() const
1358     {
1359         return functionName_;
1360     }
1361 
SetFunctionName(const std::string & functionName)1362     CallFrame &SetFunctionName(const std::string &functionName)
1363     {
1364         functionName_ = functionName;
1365         return *this;
1366     }
1367 
GetFunctionLocation()1368     Location *GetFunctionLocation() const
1369     {
1370         if (functionLocation_) {
1371             return functionLocation_->get();
1372         }
1373         return nullptr;
1374     }
1375 
SetFunctionLocation(std::unique_ptr<Location> location)1376     CallFrame &SetFunctionLocation(std::unique_ptr<Location> location)
1377     {
1378         functionLocation_ = std::move(location);
1379         return *this;
1380     }
1381 
HasFunctionLocation()1382     bool HasFunctionLocation() const
1383     {
1384         return functionLocation_.has_value();
1385     }
1386 
GetLocation()1387     Location *GetLocation() const
1388     {
1389         return location_.get();
1390     }
1391 
SetLocation(std::unique_ptr<Location> location)1392     CallFrame &SetLocation(std::unique_ptr<Location> location)
1393     {
1394         location_ = std::move(location);
1395         return *this;
1396     }
1397 
GetUrl()1398     const std::string &GetUrl() const
1399     {
1400         return url_;
1401     }
1402 
SetUrl(const std::string & url)1403     CallFrame &SetUrl(const std::string &url)
1404     {
1405         url_ = url;
1406         return *this;
1407     }
1408 
GetScopeChain()1409     const std::vector<std::unique_ptr<Scope>> *GetScopeChain() const
1410     {
1411         return &scopeChain_;
1412     }
1413 
SetScopeChain(std::vector<std::unique_ptr<Scope>> scopeChain)1414     CallFrame &SetScopeChain(std::vector<std::unique_ptr<Scope>> scopeChain)
1415     {
1416         scopeChain_ = std::move(scopeChain);
1417         return *this;
1418     }
GetThis()1419     RemoteObject *GetThis() const
1420     {
1421         return this_.get();
1422     }
1423 
SetThis(std::unique_ptr<RemoteObject> thisObj)1424     CallFrame &SetThis(std::unique_ptr<RemoteObject> thisObj)
1425     {
1426         this_ = std::move(thisObj);
1427         return *this;
1428     }
1429 
GetReturnValue()1430     RemoteObject *GetReturnValue() const
1431     {
1432         if (returnValue_) {
1433             return returnValue_->get();
1434         }
1435         return nullptr;
1436     }
1437 
SetReturnValue(std::unique_ptr<RemoteObject> returnValue)1438     CallFrame &SetReturnValue(std::unique_ptr<RemoteObject> returnValue)
1439     {
1440         returnValue_ = std::move(returnValue);
1441         return *this;
1442     }
1443 
HasReturnValue()1444     bool HasReturnValue() const
1445     {
1446         return returnValue_.has_value();
1447     }
1448 
1449 private:
1450     NO_COPY_SEMANTIC(CallFrame);
1451     NO_MOVE_SEMANTIC(CallFrame);
1452 
1453     CallFrameId callFrameId_ {};
1454     std::string functionName_ {};
1455     std::optional<std::unique_ptr<Location>> functionLocation_ {};
1456     std::unique_ptr<Location> location_ {nullptr};
1457     std::string url_ {};
1458     std::vector<std::unique_ptr<Scope>> scopeChain_ {};
1459     std::unique_ptr<RemoteObject> this_ {nullptr};
1460     std::optional<std::unique_ptr<RemoteObject>> returnValue_ {};
1461 };
1462 
1463 #ifdef SUPPORT_PROFILER_CDP
1464 // ========== Heapprofiler types begin
1465 
1466 using HeapSnapshotObjectId = int32_t;
1467 
1468 class SamplingHeapProfileSample  final :  public PtBaseTypes {
1469 public:
1470     SamplingHeapProfileSample() = default;
1471     ~SamplingHeapProfileSample() override = default;
1472     static std::unique_ptr<SamplingHeapProfileSample> Create(const PtJson &params);
1473     std::unique_ptr<PtJson> ToJson() const override;
1474 
SetSize(int32_t size)1475     SamplingHeapProfileSample &SetSize(int32_t size)
1476     {
1477         size_ = size;
1478         return *this;
1479     }
1480 
GetSize()1481     int32_t GetSize() const
1482     {
1483         return size_;
1484     }
1485 
SetNodeId(int32_t nodeId)1486     SamplingHeapProfileSample &SetNodeId(int32_t nodeId)
1487     {
1488         nodeId_ = nodeId;
1489         return *this;
1490     }
1491 
GetNodeId()1492     int32_t GetNodeId() const
1493     {
1494         return nodeId_;
1495     }
1496 
SetOrdinal(int32_t ordinal)1497     SamplingHeapProfileSample &SetOrdinal(int32_t ordinal)
1498     {
1499         ordinal_ = ordinal;
1500         return *this;
1501     }
1502 
GetOrdinal()1503     int32_t GetOrdinal() const
1504     {
1505         return ordinal_;
1506     }
1507 
1508 private:
1509     NO_COPY_SEMANTIC(SamplingHeapProfileSample);
1510     NO_MOVE_SEMANTIC(SamplingHeapProfileSample);
1511 
1512     int32_t size_ {0};
1513     int32_t nodeId_ {0};
1514     int32_t ordinal_ {0};
1515 };
1516 
1517 class RuntimeCallFrame  final :  public PtBaseTypes {
1518 public:
1519     RuntimeCallFrame() = default;
1520     ~RuntimeCallFrame() override = default;
1521     static std::unique_ptr<RuntimeCallFrame> Create(const PtJson &params);
1522     static std::unique_ptr<RuntimeCallFrame> FromFrameInfo(const FrameInfo &cpuFrameInfo);
1523     std::unique_ptr<PtJson> ToJson() const override;
1524 
SetFunctionName(const std::string & functionName)1525     RuntimeCallFrame &SetFunctionName(const std::string &functionName)
1526     {
1527         functionName_ = functionName;
1528         return *this;
1529     }
1530 
GetFunctionName()1531     const std::string &GetFunctionName() const
1532     {
1533         return functionName_;
1534     }
1535 
SetScriptId(const std::string & scriptId)1536     RuntimeCallFrame &SetScriptId(const std::string &scriptId)
1537     {
1538         scriptId_ = scriptId;
1539         return *this;
1540     }
1541 
GetScriptId()1542     const std::string &GetScriptId() const
1543     {
1544         return scriptId_;
1545     }
1546 
SetUrl(const std::string & url)1547     RuntimeCallFrame &SetUrl(const std::string &url)
1548     {
1549         url_ = url;
1550         return *this;
1551     }
1552 
GetUrl()1553     const std::string &GetUrl() const
1554     {
1555         return url_;
1556     }
1557 
SetLineNumber(int32_t lineNumber)1558     RuntimeCallFrame &SetLineNumber(int32_t lineNumber)
1559     {
1560         lineNumber_ = lineNumber;
1561         return *this;
1562     }
1563 
GetLineNumber()1564     int32_t GetLineNumber() const
1565     {
1566         return lineNumber_;
1567     }
1568 
SetColumnNumber(int32_t columnNumber)1569     RuntimeCallFrame &SetColumnNumber(int32_t columnNumber)
1570     {
1571         columnNumber_ = columnNumber;
1572         return *this;
1573     }
1574 
GetColumnNumber()1575     int32_t GetColumnNumber() const
1576     {
1577         return columnNumber_;
1578     }
1579 
1580 private:
1581     NO_COPY_SEMANTIC(RuntimeCallFrame);
1582     NO_MOVE_SEMANTIC(RuntimeCallFrame);
1583 
1584     std::string functionName_ {};
1585     std::string scriptId_ {};
1586     std::string url_ {};
1587     int32_t lineNumber_ {0};
1588     int32_t columnNumber_ {0};
1589 };
1590 
1591 class SamplingHeapProfileNode  final :  public PtBaseTypes {
1592 public:
1593     SamplingHeapProfileNode() = default;
1594     ~SamplingHeapProfileNode() override = default;
1595     static std::unique_ptr<SamplingHeapProfileNode> Create(const PtJson &params);
1596     std::unique_ptr<PtJson> ToJson() const override;
1597 
SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)1598     SamplingHeapProfileNode &SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)
1599     {
1600         callFrame_ = std::move(callFrame);
1601         return *this;
1602     }
1603 
GetCallFrame()1604     RuntimeCallFrame *GetCallFrame() const
1605     {
1606         return callFrame_.get();
1607     }
1608 
SetSelfSize(int32_t selfSize)1609     SamplingHeapProfileNode &SetSelfSize(int32_t selfSize)
1610     {
1611         selfSize_ = selfSize;
1612         return *this;
1613     }
1614 
GetSelfSize()1615     int32_t GetSelfSize() const
1616     {
1617         return selfSize_;
1618     }
1619 
SetId(int32_t id)1620     SamplingHeapProfileNode &SetId(int32_t id)
1621     {
1622         id_ = id;
1623         return *this;
1624     }
1625 
GetId()1626     int32_t GetId() const
1627     {
1628         return id_;
1629     }
1630 
SetChildren(std::vector<std::unique_ptr<SamplingHeapProfileNode>> children)1631     SamplingHeapProfileNode &SetChildren(std::vector<std::unique_ptr<SamplingHeapProfileNode>> children)
1632     {
1633         children_ = std::move(children);
1634         return *this;
1635     }
1636 
GetChildren()1637     const std::vector<std::unique_ptr<SamplingHeapProfileNode>> *GetChildren() const
1638     {
1639         return &children_;
1640     }
1641 
1642 private:
1643     NO_COPY_SEMANTIC(SamplingHeapProfileNode);
1644     NO_MOVE_SEMANTIC(SamplingHeapProfileNode);
1645 
1646     std::unique_ptr<RuntimeCallFrame> callFrame_ {nullptr};
1647     int32_t selfSize_ {0};
1648     int32_t id_ {0};
1649     std::vector<std::unique_ptr<SamplingHeapProfileNode>> children_ {};
1650 };
1651 
1652 class SamplingHeapProfile final : public PtBaseTypes {
1653 public:
1654     SamplingHeapProfile() = default;
1655     ~SamplingHeapProfile() override = default;
1656     static std::unique_ptr<SamplingHeapProfile> Create(const PtJson &params);
1657     std::unique_ptr<PtJson> ToJson() const override;
1658 
SetHead(std::unique_ptr<SamplingHeapProfileNode> head)1659     SamplingHeapProfile &SetHead(std::unique_ptr<SamplingHeapProfileNode> head)
1660     {
1661         head_ = std::move(head);
1662         return *this;
1663     }
1664 
GetHead()1665     SamplingHeapProfileNode *GetHead() const
1666     {
1667         return head_.get();
1668     }
1669 
SetSamples(std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples)1670     SamplingHeapProfile &SetSamples(std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples)
1671     {
1672         samples_ = std::move(samples);
1673         return *this;
1674     }
1675 
GetSamples()1676     const std::vector<std::unique_ptr<SamplingHeapProfileSample>> *GetSamples() const
1677     {
1678         return &samples_;
1679     }
1680 
1681 private:
1682     NO_COPY_SEMANTIC(SamplingHeapProfile);
1683     NO_MOVE_SEMANTIC(SamplingHeapProfile);
1684 
1685     std::unique_ptr<SamplingHeapProfileNode> head_ {nullptr};
1686     std::vector<std::unique_ptr<SamplingHeapProfileSample>> samples_ {};
1687 };
1688 
1689 // ========== Profiler types begin
1690 // Profiler.PositionTickInfo
1691 class PositionTickInfo final : public PtBaseTypes {
1692 public:
1693     PositionTickInfo() = default;
1694     ~PositionTickInfo() override = default;
1695 
1696     static std::unique_ptr<PositionTickInfo> Create(const PtJson &params);
1697     std::unique_ptr<PtJson> ToJson() const override;
1698 
GetLine()1699     int32_t GetLine() const
1700     {
1701         return line_;
1702     }
1703 
SetLine(int32_t line)1704     PositionTickInfo &SetLine(int32_t line)
1705     {
1706         line_ = line;
1707         return *this;
1708     }
1709 
GetTicks()1710     int32_t GetTicks() const
1711     {
1712         return ticks_;
1713     }
1714 
SetTicks(int32_t ticks)1715     PositionTickInfo &SetTicks(int32_t ticks)
1716     {
1717         ticks_ = ticks;
1718         return *this;
1719     }
1720 
1721 private:
1722     NO_COPY_SEMANTIC(PositionTickInfo);
1723     NO_MOVE_SEMANTIC(PositionTickInfo);
1724     int32_t line_ {0};
1725     int32_t ticks_ {0};
1726 };
1727 
1728 // Profiler.ProfileNode
1729 class ProfileNode final : public PtBaseTypes {
1730 public:
1731     ProfileNode() = default;
1732     ~ProfileNode() override = default;
1733 
1734     static std::unique_ptr<ProfileNode> Create(const PtJson &params);
1735     static std::unique_ptr<ProfileNode> FromCpuProfileNode(const CpuProfileNode &cpuProfileNode);
1736     std::unique_ptr<PtJson> ToJson() const override;
1737 
GetId()1738     int32_t GetId() const
1739     {
1740         return id_;
1741     }
1742 
SetId(int32_t id)1743     ProfileNode &SetId(int32_t id)
1744     {
1745         id_ = id;
1746         return *this;
1747     }
1748 
GetCallFrame()1749     RuntimeCallFrame *GetCallFrame() const
1750     {
1751         return callFrame_.get();
1752     }
1753 
SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)1754     ProfileNode &SetCallFrame(std::unique_ptr<RuntimeCallFrame> callFrame)
1755     {
1756         callFrame_ = std::move(callFrame);
1757         return *this;
1758     }
1759 
GetHitCount()1760     int32_t GetHitCount() const
1761     {
1762         ASSERT(HasHitCount());
1763         return hitCount_.value();
1764     }
1765 
SetHitCount(int32_t hitCount)1766     ProfileNode &SetHitCount(int32_t hitCount)
1767     {
1768         hitCount_ = hitCount;
1769         return *this;
1770     }
1771 
HasHitCount()1772     bool HasHitCount() const
1773     {
1774         return hitCount_.has_value();
1775     }
1776 
GetChildren()1777     const std::vector<int32_t> *GetChildren() const
1778     {
1779         if (children_) {
1780             return &children_.value();
1781         }
1782         return nullptr;
1783     }
1784 
SetChildren(std::vector<int32_t> children)1785     ProfileNode &SetChildren(std::vector<int32_t> children)
1786     {
1787         children_ = std::move(children);
1788         return *this;
1789     }
1790 
HasChildren()1791     bool HasChildren() const
1792     {
1793         return children_.has_value();
1794     }
1795 
GetPositionTicks()1796     const std::vector<std::unique_ptr<PositionTickInfo>> *GetPositionTicks() const
1797     {
1798         if (positionTicks_) {
1799             return &positionTicks_.value();
1800         }
1801         return nullptr;
1802     }
1803 
SetPositionTicks(std::vector<std::unique_ptr<PositionTickInfo>> positionTicks)1804     ProfileNode &SetPositionTicks(std::vector<std::unique_ptr<PositionTickInfo>> positionTicks)
1805     {
1806         positionTicks_ = std::move(positionTicks);
1807         return *this;
1808     }
1809 
HasPositionTicks()1810     bool HasPositionTicks() const
1811     {
1812         return positionTicks_.has_value();
1813     }
1814 
GetDeoptReason()1815     const std::string &GetDeoptReason() const
1816     {
1817         ASSERT(HasDeoptReason());
1818         return deoptReason_.value();
1819     }
1820 
SetDeoptReason(const std::string & deoptReason)1821     ProfileNode &SetDeoptReason(const std::string &deoptReason)
1822     {
1823         deoptReason_ = deoptReason;
1824         return *this;
1825     }
1826 
HasDeoptReason()1827     bool HasDeoptReason() const
1828     {
1829         return deoptReason_.has_value();
1830     }
1831 
1832 private:
1833     NO_COPY_SEMANTIC(ProfileNode);
1834     NO_MOVE_SEMANTIC(ProfileNode);
1835     int32_t id_ {0};
1836     std::unique_ptr<RuntimeCallFrame> callFrame_ {nullptr};
1837     std::optional<int32_t> hitCount_ {0};
1838     std::optional<std::vector<int32_t>> children_ {};
1839     std::optional<std::vector<std::unique_ptr<PositionTickInfo>>> positionTicks_ {};
1840     std::optional<std::string> deoptReason_ {};
1841 };
1842 
1843 // Profiler.Profile
1844 class Profile final : public PtBaseTypes {
1845 public:
1846     Profile() = default;
1847     ~Profile() override = default;
1848 
1849     static std::unique_ptr<Profile> Create(const PtJson &params);
1850     static std::unique_ptr<Profile> FromProfileInfo(const ProfileInfo &profileInfo);
1851     std::unique_ptr<PtJson> ToJson() const override;
1852 
GetStartTime()1853     int64_t GetStartTime() const
1854     {
1855         return startTime_;
1856     }
1857 
SetStartTime(int64_t startTime)1858     Profile &SetStartTime(int64_t startTime)
1859     {
1860         startTime_ = startTime;
1861         return *this;
1862     }
1863 
GetEndTime()1864     int64_t GetEndTime() const
1865     {
1866         return endTime_;
1867     }
1868 
SetEndTime(int64_t endTime)1869     Profile &SetEndTime(int64_t endTime)
1870     {
1871         endTime_ = endTime;
1872         return *this;
1873     }
1874 
GetNodes()1875     const std::vector<std::unique_ptr<ProfileNode>> *GetNodes() const
1876     {
1877         return &nodes_;
1878     }
1879 
SetNodes(std::vector<std::unique_ptr<ProfileNode>> nodes)1880     Profile &SetNodes(std::vector<std::unique_ptr<ProfileNode>> nodes)
1881     {
1882         nodes_ = std::move(nodes);
1883         return *this;
1884     }
1885 
GetSamples()1886     const std::vector<int32_t> *GetSamples() const
1887     {
1888         if (samples_) {
1889             return &samples_.value();
1890         }
1891         return nullptr;
1892     }
1893 
SetSamples(std::vector<int32_t> samples)1894     Profile &SetSamples(std::vector<int32_t> samples)
1895     {
1896         samples_ = std::move(samples);
1897         return *this;
1898     }
1899 
HasSamples()1900     bool HasSamples() const
1901     {
1902         return samples_.has_value();
1903     }
1904 
GetTimeDeltas()1905     const std::vector<int32_t> *GetTimeDeltas() const
1906     {
1907         if (timeDeltas_) {
1908             return &timeDeltas_.value();
1909         }
1910         return nullptr;
1911     }
1912 
SetTimeDeltas(std::vector<int32_t> timeDeltas)1913     Profile &SetTimeDeltas(std::vector<int32_t> timeDeltas)
1914     {
1915         timeDeltas_ = std::move(timeDeltas);
1916         return *this;
1917     }
1918 
HasTimeDeltas()1919     bool HasTimeDeltas() const
1920     {
1921         return timeDeltas_.has_value();
1922     }
1923 
1924 private:
1925     NO_COPY_SEMANTIC(Profile);
1926     NO_MOVE_SEMANTIC(Profile);
1927 
1928     int64_t startTime_ {0};
1929     int64_t endTime_ {0};
1930     std::vector<std::unique_ptr<ProfileNode>> nodes_ {};
1931     std::optional<std::vector<int32_t>> samples_ {};
1932     std::optional<std::vector<int32_t>> timeDeltas_ {};
1933 };
1934 
1935 // Profiler.Coverage
1936 class Coverage final : public PtBaseTypes {
1937 public:
1938     Coverage() = default;
1939     ~Coverage() override = default;
1940 
1941     static std::unique_ptr<Coverage> Create(const PtJson &params);
1942     std::unique_ptr<PtJson> ToJson() const override;
1943 
GetStartOffset()1944     int32_t GetStartOffset() const
1945     {
1946         return startOffset_;
1947     }
1948 
SetStartOffset(int32_t startOffset)1949     Coverage &SetStartOffset(int32_t startOffset)
1950     {
1951         startOffset_ = startOffset;
1952         return *this;
1953     }
1954 
GetEndOffset()1955     int32_t GetEndOffset() const
1956     {
1957         return endOffset_;
1958     }
1959 
SetEndOffset(int32_t endOffset)1960     Coverage &SetEndOffset(int32_t endOffset)
1961     {
1962         endOffset_ = endOffset;
1963         return *this;
1964     }
1965 
GetCount()1966     int32_t GetCount() const
1967     {
1968         return count_;
1969     }
1970 
SetCount(int32_t count)1971     Coverage &SetCount(int32_t count)
1972     {
1973         count_ = count;
1974         return *this;
1975     }
1976 
1977 private:
1978     NO_COPY_SEMANTIC(Coverage);
1979     NO_MOVE_SEMANTIC(Coverage);
1980 
1981     int32_t startOffset_ {0};
1982     int32_t endOffset_ {0};
1983     int32_t count_ {0};
1984 };
1985 
1986 // Profiler.FunctionCoverage
1987 class FunctionCoverage final : public PtBaseTypes {
1988 public:
1989     FunctionCoverage() = default;
1990     ~FunctionCoverage() override = default;
1991 
1992     static std::unique_ptr<FunctionCoverage> Create(const PtJson &params);
1993     std::unique_ptr<PtJson> ToJson() const override;
1994 
GetFunctionName()1995     const std::string &GetFunctionName() const
1996     {
1997         return functionName_;
1998     }
1999 
SetFunctionName(const std::string & functionName)2000     FunctionCoverage &SetFunctionName(const std::string &functionName)
2001     {
2002         functionName_ = functionName;
2003         return *this;
2004     }
2005 
GetRanges()2006     const std::vector<std::unique_ptr<Coverage>> *GetRanges() const
2007     {
2008         return &ranges_;
2009     }
2010 
SetFunctions(std::vector<std::unique_ptr<Coverage>> ranges)2011     FunctionCoverage &SetFunctions(std::vector<std::unique_ptr<Coverage>> ranges)
2012     {
2013         ranges_ = std::move(ranges);
2014         return *this;
2015     }
2016 
GetIsBlockCoverage()2017     bool GetIsBlockCoverage() const
2018     {
2019         return isBlockCoverage_;
2020     }
2021 
SetisBlockCoverage(bool isBlockCoverage)2022     FunctionCoverage &SetisBlockCoverage(bool isBlockCoverage)
2023     {
2024         isBlockCoverage_ = isBlockCoverage;
2025         return *this;
2026     }
2027 
2028 private:
2029     NO_COPY_SEMANTIC(FunctionCoverage);
2030     NO_MOVE_SEMANTIC(FunctionCoverage);
2031 
2032     std::string functionName_ {};
2033     std::vector<std::unique_ptr<Coverage>> ranges_ {};
2034     bool isBlockCoverage_ {};
2035 };
2036 
2037 // Profiler.ScriptCoverage
2038 // Profiler.GetBestEffortCoverage and Profiler.TakePreciseCoverage share this return value type
2039 class ScriptCoverage final : public PtBaseTypes {
2040 public:
2041     ScriptCoverage() = default;
2042     ~ScriptCoverage() override = default;
2043 
2044     static std::unique_ptr<ScriptCoverage> Create(const PtJson &params);
2045     std::unique_ptr<PtJson> ToJson() const override;
2046 
GetScriptId()2047     const std::string &GetScriptId() const
2048     {
2049         return scriptId_;
2050     }
2051 
SetScriptId(const std::string & scriptId)2052     ScriptCoverage &SetScriptId(const std::string &scriptId)
2053     {
2054         scriptId_ = scriptId;
2055         return *this;
2056     }
2057 
GetUrl()2058     const std::string &GetUrl() const
2059     {
2060         return url_;
2061     }
2062 
SetUrl(const std::string & url)2063     ScriptCoverage &SetUrl(const std::string &url)
2064     {
2065         url_ = url;
2066         return *this;
2067     }
2068 
GetFunctions()2069     const std::vector<std::unique_ptr<FunctionCoverage>> *GetFunctions() const
2070     {
2071         return &functions_;
2072     }
2073 
SetFunctions(std::vector<std::unique_ptr<FunctionCoverage>> functions)2074     ScriptCoverage &SetFunctions(std::vector<std::unique_ptr<FunctionCoverage>> functions)
2075     {
2076         functions_ = std::move(functions);
2077         return *this;
2078     }
2079 
2080 private:
2081     NO_COPY_SEMANTIC(ScriptCoverage);
2082     NO_MOVE_SEMANTIC(ScriptCoverage);
2083 
2084     std::string scriptId_ {};
2085     std::string url_ {};
2086     std::vector<std::unique_ptr<FunctionCoverage>> functions_ {};
2087 };
2088 
2089 // Profiler.TypeObject
2090 class TypeObject final : public PtBaseTypes {
2091 public:
2092     TypeObject() = default;
2093     ~TypeObject() override = default;
2094 
2095     static std::unique_ptr<TypeObject> Create(const PtJson &params);
2096     std::unique_ptr<PtJson> ToJson() const override;
2097 
GetName()2098     const std::string &GetName() const
2099     {
2100         return name_;
2101     }
2102 
SetName(const std::string & name)2103     TypeObject &SetName(const std::string &name)
2104     {
2105         name_ = name;
2106         return *this;
2107     }
2108 
2109 private:
2110     NO_COPY_SEMANTIC(TypeObject);
2111     NO_MOVE_SEMANTIC(TypeObject);
2112 
2113     std::string name_ {};
2114 };
2115 
2116 // Profiler.TypeProfileEntry
2117 class TypeProfileEntry final : public PtBaseTypes {
2118 public:
2119     TypeProfileEntry() = default;
2120     ~TypeProfileEntry() override = default;
2121 
2122     static std::unique_ptr<TypeProfileEntry> Create(const PtJson &params);
2123     std::unique_ptr<PtJson> ToJson() const override;
2124 
GetOffset()2125     int32_t GetOffset() const
2126     {
2127         return offset_;
2128     }
2129 
SetOffset(int32_t offset)2130     TypeProfileEntry &SetOffset(int32_t offset)
2131     {
2132         offset_ = offset;
2133         return *this;
2134     }
2135 
GetTypes()2136     const std::vector<std::unique_ptr<TypeObject>> *GetTypes() const
2137     {
2138         return &types_;
2139     }
2140 
SetTypes(std::vector<std::unique_ptr<TypeObject>> types)2141     TypeProfileEntry &SetTypes(std::vector<std::unique_ptr<TypeObject>> types)
2142     {
2143         types_ = std::move(types);
2144         return *this;
2145     }
2146 
2147 private:
2148     NO_COPY_SEMANTIC(TypeProfileEntry);
2149     NO_MOVE_SEMANTIC(TypeProfileEntry);
2150 
2151     int32_t offset_ {0};
2152     std::vector<std::unique_ptr<TypeObject>> types_ {};
2153 };
2154 
2155 // Profiler.ScriptTypeProfile
2156 class ScriptTypeProfile final : public PtBaseTypes {
2157 public:
2158     ScriptTypeProfile() = default;
2159     ~ScriptTypeProfile() override = default;
2160 
2161     static std::unique_ptr<ScriptTypeProfile> Create(const PtJson &params);
2162     std::unique_ptr<PtJson> ToJson() const override;
2163 
GetScriptId()2164     const std::string &GetScriptId() const
2165     {
2166         return scriptId_;
2167     }
2168 
SetScriptId(const std::string & scriptId)2169     ScriptTypeProfile &SetScriptId(const std::string &scriptId)
2170     {
2171         scriptId_ = scriptId;
2172         return *this;
2173     }
2174 
GetUrl()2175     const std::string &GetUrl() const
2176     {
2177         return url_;
2178     }
2179 
SetUrl(const std::string & url)2180     ScriptTypeProfile &SetUrl(const std::string &url)
2181     {
2182         url_ = url;
2183         return *this;
2184     }
2185 
GetEntries()2186     const std::vector<std::unique_ptr<TypeProfileEntry>> *GetEntries() const
2187     {
2188         return &entries_;
2189     }
2190 
SetEntries(std::vector<std::unique_ptr<TypeProfileEntry>> entries)2191     ScriptTypeProfile &SetEntries(std::vector<std::unique_ptr<TypeProfileEntry>> entries)
2192     {
2193         entries_ = std::move(entries);
2194         return *this;
2195     }
2196 
2197 private:
2198     NO_COPY_SEMANTIC(ScriptTypeProfile);
2199     NO_MOVE_SEMANTIC(ScriptTypeProfile);
2200 
2201     std::string scriptId_ {};
2202     std::string url_ {};
2203     std::vector<std::unique_ptr<TypeProfileEntry>> entries_ {};
2204 };
2205 #endif
2206 }  // namespace panda::ecmascript::tooling
2207 #endif
2208