• 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 "base/pt_params.h"
21 #include "dispatcher.h"
22 
23 #include "libpandabase/macros.h"
24 
25 namespace panda::ecmascript::tooling {
26 enum class ArkInternalValueType {None, Entry, Scope, ScopeList};
27 class RuntimeImpl final {
28 public:
RuntimeImpl(const EcmaVM * vm,ProtocolChannel * channel)29     RuntimeImpl(const EcmaVM *vm, ProtocolChannel *channel)
30         : vm_(vm), frontend_(channel) {}
31     ~RuntimeImpl() = default;
32 
33     DispatchResponse Enable();
34     DispatchResponse Disable();
35     DispatchResponse RunIfWaitingForDebugger();
36     DispatchResponse CallFunctionOn(
37         const CallFunctionOnParams &params,
38         std::unique_ptr<RemoteObject> *outRemoteObject,
39         std::optional<std::unique_ptr<ExceptionDetails>> *outExceptionDetails);
40     DispatchResponse GetHeapUsage(double *usedSize, double *totalSize);
41     DispatchResponse GetProperties(
42         const GetPropertiesParams &params,
43         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc,
44         std::optional<std::vector<std::unique_ptr<InternalPropertyDescriptor>>> *outInternalDescs,
45         std::optional<std::vector<std::unique_ptr<PrivatePropertyDescriptor>>> *outPrivateProps,
46         std::optional<std::unique_ptr<ExceptionDetails>> *outExceptionDetails);
47 
48     class DispatcherImpl final : public DispatcherBase {
49     public:
DispatcherImpl(ProtocolChannel * channel,std::unique_ptr<RuntimeImpl> runtime)50         DispatcherImpl(ProtocolChannel *channel, std::unique_ptr<RuntimeImpl> runtime)
51             : DispatcherBase(channel), runtime_(std::move(runtime)) {}
52         ~DispatcherImpl() override = default;
53 
54         void Dispatch(const DispatchRequest &request) override;
55         void Disable(const DispatchRequest &request);
56         void Enable(const DispatchRequest &request);
57         void RunIfWaitingForDebugger(const DispatchRequest &request);
58         void GetProperties(const DispatchRequest &request);
59         void CallFunctionOn(const DispatchRequest &request);
60         void GetHeapUsage(const DispatchRequest &request);
61 
62     private:
63         using AgentHandler = void (RuntimeImpl::DispatcherImpl::*)(const DispatchRequest &request);
64         std::unique_ptr<RuntimeImpl> runtime_ {};
65 
66         NO_COPY_SEMANTIC(DispatcherImpl);
67         NO_MOVE_SEMANTIC(DispatcherImpl);
68     };
69 
70 private:
71     NO_COPY_SEMANTIC(RuntimeImpl);
72     NO_MOVE_SEMANTIC(RuntimeImpl);
73     enum NumberSize : uint8_t { BYTES_OF_16BITS = 2, BYTES_OF_32BITS = 4, BYTES_OF_64BITS = 8 };
74 
75     void CacheObjectIfNeeded(Local<JSValueRef> valRef, RemoteObject *remoteObj);
76 
77     template <typename TypedArrayRef>
78     void AddTypedArrayRef(Local<ArrayBufferRef> arrayBufferRef, int32_t length,
79         const char* name, std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
80     void AddTypedArrayRefs(Local<ArrayBufferRef> arrayBufferRef,
81         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
82     void AddSharedArrayBufferRefs(Local<ArrayBufferRef> arrayBufferRef,
83         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
84     void GetProtoOrProtoType(Local<JSValueRef> value, bool isOwn, bool isAccessorOnly,
85                              std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
86     void GetAdditionalProperties(Local<JSValueRef> value,
87         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
88     void SetKeyValue(Local<JSValueRef> &jsValueRef,
89         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc, const std::string &cstrProName);
90     void GetPrimitiveNumberValue(Local<JSValueRef> value,
91         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
92     void GetPrimitiveStringValue(Local<JSValueRef> value,
93         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
94     void GetPrimitiveBooleanValue(Local<JSValueRef> value,
95         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
96     void GetMapIteratorValue(Local<JSValueRef> value,
97         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
98     void GetSetIteratorValue(Local<JSValueRef> value,
99         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
100     void GetGeneratorFunctionValue(Local<JSValueRef> value,
101         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
102     void GetGeneratorObjectValue(Local<JSValueRef> value,
103         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
104     void GetNumberFormatValue(Local<JSValueRef> value,
105         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
106     void GetCollatorValue(Local<JSValueRef> value,
107         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
108     void GetDateTimeFormatValue(Local<JSValueRef> value,
109         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
110     void AddInternalProperties(Local<ObjectRef> object, ArkInternalValueType type);
111     void GetMapValue(Local<JSValueRef> value,
112         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
113     void GetSetValue(Local<JSValueRef> value,
114         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
115     void GetRegExpValue(Local<JSValueRef> value,
116         std::vector<std::unique_ptr<PropertyDescriptor>> *outPropertyDesc);
117     class Frontend {
118     public:
Frontend(ProtocolChannel * channel)119         explicit Frontend(ProtocolChannel *channel) : channel_(channel) {}
120         ~Frontend() = default;
121 
122         void RunIfWaitingForDebugger();
123 
124     private:
125         bool AllowNotify() const;
126 
127         ProtocolChannel *channel_ {nullptr};
128     };
129 
130     const EcmaVM *vm_ {nullptr};
131     Frontend frontend_;
132 
133     RemoteObjectId curObjectId_ {0};
134     std::unordered_map<RemoteObjectId, Global<JSValueRef>> properties_ {};
135     Global<MapRef> internalObjects_;
136 
137     friend class DebuggerImpl;
138 };
139 }  // namespace panda::ecmascript::tooling
140 #endif