1 /* 2 * Copyright (c) 2021-2022 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H 17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H 18 19 #include <unordered_map> 20 21 #include "ark_headers.h" 22 #include "ecmascript/napi/include/jsnapi.h" 23 #include "ecmascript/napi/include/dfx_jsnapi.h" 24 #include "native_engine/native_engine.h" 25 26 namespace panda::ecmascript { 27 struct JsFrameInfo { 28 std::string functionName; 29 std::string fileName; 30 std::string pos; 31 uintptr_t* nativePointer = nullptr; 32 }; 33 } 34 using ArkJsFrameInfo = panda::ecmascript::JsFrameInfo; 35 36 using panda::ecmascript::EcmaVM; 37 using panda::Local; 38 using panda::LocalScope; 39 using panda::JSValueRef; 40 using panda::JSNApi; 41 using panda::DFXJSNApi; 42 class ArkNativeObject; 43 class SerializationData { 44 public: SerializationData()45 SerializationData() : data_(nullptr), size_(0) {} 46 ~SerializationData() = default; 47 GetData()48 uint8_t* GetData() const 49 { 50 return data_.get(); 51 } GetSize()52 size_t GetSize() const 53 { 54 return size_; 55 } 56 57 private: 58 struct DataDeleter { operatorDataDeleter59 void operator()(uint8_t* p) const 60 { 61 free(p); 62 } 63 }; 64 65 std::unique_ptr<uint8_t, DataDeleter> data_; 66 size_t size_; 67 }; 68 69 class ArkNativeEngine : public NativeEngine { 70 friend struct MoudleNameLocker; 71 public: 72 // ArkNativeEngine constructor 73 ArkNativeEngine(EcmaVM* vm, void* jsEngine); 74 // ArkNativeEngine destructor 75 ~ArkNativeEngine() override; 76 77 const EcmaVM* GetEcmaVm() const; 78 79 void Loop(LoopMode mode, bool needSync = false) override; 80 81 // Get global native object value 82 NativeValue* GetGlobal() override; 83 // Create native null value 84 NativeValue* CreateNull() override; 85 // Create native undefined value 86 NativeValue* CreateUndefined() override; 87 // Create native boolean value 88 NativeValue* CreateBoolean(bool value) override; 89 // Create number value by int32_t 90 NativeValue* CreateNumber(int32_t value) override; 91 // Create number value by uint32_t 92 NativeValue* CreateNumber(uint32_t value) override; 93 // Create native number value by int64_t 94 NativeValue* CreateNumber(int64_t value) override; 95 // Create native number value by double 96 NativeValue* CreateNumber(double value) override; 97 // Create native bigint value by int64_t 98 NativeValue* CreateBigInt(int64_t value) override; 99 // Create native bigint value by uint64_t 100 NativeValue* CreateBigInt(uint64_t value) override; 101 // Create native string value by const char pointer 102 NativeValue* CreateString(const char* value, size_t length) override; 103 // Create native string value by const char16_t pointer 104 NativeValue* CreateString16(const char16_t* value, size_t length) override; 105 // Create native symbol value 106 NativeValue* CreateSymbol(NativeValue* value) override; 107 // Create native value of external pointer 108 NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint, 109 size_t nativeBindingSize = 0) override; 110 // Create native object value 111 NativeValue* CreateObject() override; 112 // Create native function value 113 NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override; 114 // Create native array value 115 NativeValue* CreateArray(size_t length) override; 116 // Create native array buffer value 117 NativeValue* CreateArrayBuffer(void** value, size_t length) override; 118 // Create native array buffer value of external 119 NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 120 NativeValue* CreateBuffer(void** value, size_t length) override; 121 NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override; 122 NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 123 // Create native typed array value 124 NativeValue* CreateTypedArray(NativeTypedArrayType type, 125 NativeValue* value, 126 size_t length, 127 size_t offset) override; 128 // Create native data view value 129 NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override; 130 // Create native promise value 131 NativeValue* CreatePromise(NativeDeferred** deferred) override; 132 void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override; 133 // Create native error value 134 NativeValue* CreateError(NativeValue* code, NativeValue* message) override; 135 // For concurrent 136 bool InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback) override; 137 bool InitTaskPoolFunc(NativeEngine* engine, NativeValue* func, void* taskInfo) override; 138 void* GetCurrentTaskInfo() const override; 139 // Call function 140 NativeValue* CallFunction(NativeValue* thisVar, 141 NativeValue* function, 142 NativeValue* const* argv, 143 size_t argc) override; 144 // Run script 145 NativeValue* RunScript(NativeValue* script) override; 146 NativeValue* RunScriptPath(const char* path) override; 147 148 NativeValue* RunScriptBuffer(const char* path, std::vector<uint8_t>& buffer, bool isBundle) override; 149 bool RunScriptBuffer(const std::string& path, uint8_t* buffer, size_t size, bool isBundle) override; 150 151 // Run buffer script 152 NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override; 153 NativeValue* RunActor(std::vector<uint8_t>& buffer, const char* descriptor) override; 154 // Set lib path 155 void SetPackagePath(const std::string appLinPathKey, const std::vector<std::string>& packagePath); 156 // Define native class 157 NativeValue* DefineClass(const char* name, 158 NativeCallback callback, 159 void* data, 160 const NativePropertyDescriptor* properties, 161 size_t length) override; 162 // Create instance by defined class 163 NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override; 164 165 // Create native reference 166 NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, 167 NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override; 168 bool IsExceptionPending() const override; 169 NativeValue* GetAndClearLastException() override; 170 // Throw exception 171 bool Throw(NativeValue* error) override; 172 // Throw exception 173 bool Throw(NativeErrorType type, const char* code, const char* message) override; 174 175 void* CreateRuntime() override; 176 NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override; 177 NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override; 178 void DeleteSerializationData(NativeValue* value) const override; 179 NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override; 180 NativeValue* LoadArkModule(const char* str, int32_t len, const std::string& fileName); 181 182 static NativeValue* ArkValueToNativeValue(ArkNativeEngine* engine, Local<JSValueRef> value); 183 184 NativeValue* ValueToNativeValue(JSValueWrapper& value) override; 185 186 bool ExecuteJsBin(const std::string& fileName); 187 panda::Local<panda::ObjectRef> LoadModuleByName(const std::string& moduleName, bool isAppModule, 188 const std::string& param, const std::string& instanceName, void* instance, const std::string& path = ""); 189 190 bool TriggerFatalException(NativeValue* error) override; 191 NativeValue* CreateDate(double value) override; 192 NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override; 193 bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override; 194 195 // Detect performance to obtain cpuprofiler file 196 void StartCpuProfiler(const std::string& fileName = "") override; 197 void StopCpuProfiler() override; 198 199 void ResumeVM() override; 200 bool SuspendVM() override; 201 bool IsSuspended() override; 202 bool CheckSafepoint() override; 203 bool SuspendVMById(uint32_t tid) override; 204 void ResumeVMById(uint32_t tid) override; 205 206 // isVmMode means the internal class in vm is visible. 207 // isPrivate means the number and string is not visible. 208 void DumpHeapSnapshot(const std::string& path, bool isVmMode = true, 209 DumpFormat dumpFormat = DumpFormat::JSON) override; 210 // Dump the file into faultlog for heap leak. 211 void DumpHeapSnapshot(bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON, 212 bool isPrivate = false) override; 213 bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override; 214 bool BuildJsStackTrace(std::string& stackTraceStr) override; 215 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames) override; 216 217 bool DeleteWorker(NativeEngine* workerEngine) override; 218 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override; 219 bool StopHeapTracking(const std::string& filePath) override; 220 221 void PrintStatisticResult() override; 222 void StartRuntimeStat() override; 223 void StopRuntimeStat() override; 224 size_t GetArrayBufferSize() override; 225 size_t GetHeapTotalSize() override; 226 size_t GetHeapUsedSize() override; 227 void NotifyApplicationState(bool inBackground) override; 228 void NotifyIdleStatusControl(std::function<void(bool)> callback) override; 229 void NotifyIdleTime(int idleMicroSec) override; 230 void NotifyMemoryPressure(bool inHighMemoryPressure = false) override; 231 232 void AllowCrossThreadExecution() const override; 233 static void PromiseRejectCallback(void* values); 234 235 // debugger 236 bool IsMixedDebugEnabled(); 237 void NotifyNativeCalling(const void *nativeAddress); 238 239 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; 240 void HandleUncaughtException() override; 241 bool HasPendingException() override; 242 void RegisterPermissionCheck(PermissionCheckCallback callback) override; 243 bool ExecutePermissionCheck() override; 244 void RegisterTranslateBySourceMap(SourceMapCallback callback) override; 245 std::string ExecuteTranslateBySourceMap(const std::string& rawStack) override; 246 panda::Local<panda::ObjectRef> GetModuleFromName( 247 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param, 248 const std::string& instanceName, void** instance); 249 250 NativeChunk& GetNativeChunk(); GetPromiseRejectCallBackRef()251 NativeReference* GetPromiseRejectCallBackRef() 252 { 253 return promiseRejectCallbackRef_; 254 } GetConcurrentCallbackFunc()255 NapiConcurrentCallback GetConcurrentCallbackFunc() 256 { 257 return concurrentCallbackFunc_; 258 } 259 GetCheckCallbackRef()260 NativeReference* GetCheckCallbackRef() 261 { 262 return checkCallbackRef_; 263 } 264 265 static bool napiProfilerEnabled; 266 267 private: 268 static NativeEngine* CreateRuntimeFunc(NativeEngine* engine, void* jsEngine); 269 270 EcmaVM* vm_ = nullptr; 271 panda::LocalScope topScope_; 272 NapiConcurrentCallback concurrentCallbackFunc_ { nullptr }; 273 NativeReference* promiseRejectCallbackRef_ { nullptr }; 274 NativeReference* checkCallbackRef_ { nullptr }; 275 std::unordered_map<NativeModule*, panda::Global<panda::JSValueRef>> loadedModules_; 276 static PermissionCheckCallback permissionCheckCallback_; 277 UncaughtExceptionCallback uncaughtExceptionCallback_ { nullptr }; 278 SourceMapCallback SourceMapCallback_ { nullptr }; 279 inline void SetModuleName(ArkNativeObject *nativeObj, std::string moduleName); 280 static bool napiProfilerParamReaded; 281 static std::string tempModuleName_; 282 }; 283 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H */ 284