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