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_AGENT_RUNTIME_IMPL_H
17 #define ECMASCRIPT_TOOLING_AGENT_RUNTIME_IMPL_H
18 
19 
20 #include "tooling/base/pt_params.h"
21 #include "dispatcher.h"
22 
23 #include "libpandabase/macros.h"
24 
25 namespace panda::ecmascript::tooling {
26 class RuntimeImpl final {
27 public:
RuntimeImpl(const EcmaVM *vm, ProtocolChannel *channel)28     RuntimeImpl(const EcmaVM *vm, ProtocolChannel *channel)
29         : vm_(vm), frontend_(channel) {}
30     ~RuntimeImpl() = default;
31 
32     DispatchResponse Enable();
33     DispatchResponse Disable();
34     DispatchResponse RunIfWaitingForDebugger();
35     DispatchResponse GetHeapUsage(double *usedSize, double *totalSize);
36     DispatchResponse GetProperties(
37         const GetPropertiesParams &params,
38         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc,
39         std::optional<std::vector<std::unique_ptr<InternalPropertyDescriptor>>> *outInternalDescs,
40         std::optional<std::vector<std::unique_ptr<PrivatePropertyDescriptor>>> *outPrivateProps,
41         std::optional<std::unique_ptr<ExceptionDetails>> *outExceptionDetails);
42 
43     class DispatcherImpl final : public DispatcherBase {
44     public:
DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<RuntimeImpl> runtime)45         DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<RuntimeImpl> runtime)
46             : DispatcherBase(channel), runtime_(std::move(runtime)) {}
47         ~DispatcherImpl() override = default;
48 
49         void Dispatch(const DispatchRequest &request) override;
50         void Disable(const DispatchRequest &request);
51         void Enable(const DispatchRequest &request);
52         void RunIfWaitingForDebugger(const DispatchRequest &request);
53         void GetProperties(const DispatchRequest &request);
54         void GetHeapUsage(const DispatchRequest &request);
55 
56         enum class Method {
57             ENABLE,
58             DISABLE,
59             GET_PROPERTIES,
60             RUN_IF_WAITING_FOR_DEBUGGER,
61             GET_HEAP_USAGE,
62             UNKNOWN
63         };
64         Method GetMethodEnum(const std::string& method);
65 
66     private:
67         std::unique_ptr<RuntimeImpl> runtime_ {};
68 
69         NO_COPY_SEMANTIC(DispatcherImpl);
70         NO_MOVE_SEMANTIC(DispatcherImpl);
71     };
72 
73 private:
74     NO_COPY_SEMANTIC(RuntimeImpl);
75     NO_MOVE_SEMANTIC(RuntimeImpl);
76     enum NumberSize : uint8_t { BYTES_OF_16BITS = 2, BYTES_OF_32BITS = 4, BYTES_OF_64BITS = 8 };
77 
78     void CacheObjectIfNeeded(Local<JSValueRef> valRef, RemoteObject *remoteObj);
79 
80     template <typename TypedArrayRef>
81     void AddTypedArrayRef(Local<ArrayBufferRef> arrayBufferRef, int32_t length,
82         const char* name, std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
83     void AddTypedArrayRefs(Local<ArrayBufferRef> arrayBufferRef,
84         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
85     void AddSharedArrayBufferRefs(Local<ArrayBufferRef> arrayBufferRef,
86         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
87     void GetProtoOrProtoType(Local<JSValueRef> value, bool isOwn, bool isAccessorOnly,
88                              std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
89     void GetAdditionalProperties(Local<JSValueRef> value,
90         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
91     void SetKeyValue(Local<JSValueRef> &jsValueRef,
92         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc, const std::string &cstrProName);
93     void GetPrimitiveNumberValue(Local<JSValueRef> value,
94         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
95     void GetPrimitiveStringValue(Local<JSValueRef> value,
96         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
97     void GetPrimitiveBooleanValue(Local<JSValueRef> value,
98         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
99     void GetMapIteratorValue(Local<JSValueRef> value,
100         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
101     void GetSetIteratorValue(Local<JSValueRef> value,
102         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
103     void GetGeneratorFunctionValue(Local<JSValueRef> value,
104         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
105     void GetGeneratorObjectValue(Local<JSValueRef> value,
106         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
107     void GetNumberFormatValue(Local<JSValueRef> value,
108         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
109     void GetCollatorValue(Local<JSValueRef> value,
110         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
111     void GetDateTimeFormatValue(Local<JSValueRef> value,
112         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
113     void GetSharedMapValue(Local<JSValueRef> value,
114         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
115     void GetMapValue(Local<JSValueRef> value,
116         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
117     void GetWeakMapValue(Local<JSValueRef> value,
118         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
119     void GetSendableSetValue(Local<JSValueRef> value,
120         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
121     void GetSetValue(Local<JSValueRef> value,
122         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
123     void GetWeakSetValue(Local<JSValueRef> value,
124         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
125     void GetDataViewValue(Local<JSValueRef> value,
126         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
127     void GetRegExpValue(Local<JSValueRef> value,
128         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
129     void GetArrayListValue(Local<JSValueRef> value,
130         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
131     void GetDequeValue(Local<JSValueRef> value,
132         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
133     void GetHashMapValue(Local<JSValueRef> value,
134         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
135     void GetHashSetValue(Local<JSValueRef> value,
136         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
137     void GetLightWeightMapValue(Local<JSValueRef> value,
138         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
139     void GetLightWeightSetValue(Local<JSValueRef> value,
140         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
141     void GetLinkedListValue(Local<JSValueRef> value,
142         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
143     void GetListValue(Local<JSValueRef> value,
144         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
145     void GetStackValue(Local<JSValueRef> value,
146         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
147     void GetPlainArrayValue(Local<JSValueRef> value,
148         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
149     void GetQueueValue(Local<JSValueRef> value,
150         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
151     void GetTreeMapValue(Local<JSValueRef> value,
152         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
153     void GetTreeSetValue(Local<JSValueRef> value,
154         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
155     void GetVectorValue(Local<JSValueRef> value,
156         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
157     void GetProxyValue(Local<JSValueRef> value,
158         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
159     void GetPromiseValue(Local<JSValueRef> value,
160         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
161     void InitializeExtendedProtocolsList();
162 
163     class Frontend {
164     public:
Frontend(ProtocolChannel *channel)165         explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
166         ~Frontend() = default;
167 
168         void RunIfWaitingForDebugger();
169 
170     private:
171         bool AllowNotify() const;
172 
173         ProtocolChannel *channel_ {nullptr};
174     };
175 
176     const EcmaVM *vm_ {nullptr};
177     Frontend frontend_;
178 
179     RemoteObjectId curObjectId_ {0};
180     std::unordered_map<RemoteObjectId, Global<JSValueRef>> properties_ {};
181     Global<MapRef> internalObjects_;
182     std::vector<std::string> runtimeExtendedProtocols_ {};
183 
184     friend class DebuggerImpl;
185 };
186 }  // namespace panda::ecmascript::tooling
187 #endif
188