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