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