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 using panda::ecmascript::EcmaVM; 27 using panda::Local; 28 using panda::LocalScope; 29 using panda::JSValueRef; 30 31 class ArkNativeEngine : public NativeEngine { 32 public: 33 // ArkNativeEngine constructor 34 ArkNativeEngine(EcmaVM* vm, void* jsEngine); 35 ArkNativeEngine(NativeEngineInterface* engineImpl, void* jsEngine, bool isAppModule); 36 // ArkNativeEngine destructor 37 ~ArkNativeEngine() override; 38 39 const EcmaVM* GetEcmaVm() const; 40 41 void Loop(LoopMode mode, bool needSync = false) override; 42 43 // Get global native object value 44 NativeValue* GetGlobal() override; 45 // Create native null value 46 NativeValue* CreateNull() override; 47 // Create native undefined value 48 NativeValue* CreateUndefined() override; 49 // Create native boolean value 50 NativeValue* CreateBoolean(bool value) override; 51 // Create number value by int32_t 52 NativeValue* CreateNumber(int32_t value) override; 53 // Create number value by uint32_t 54 NativeValue* CreateNumber(uint32_t value) override; 55 // Create native number value by int64_t 56 NativeValue* CreateNumber(int64_t value) override; 57 // Create native number value by double 58 NativeValue* CreateNumber(double value) override; 59 // Create native bigint value by int64_t 60 NativeValue* CreateBigInt(int64_t value) override; 61 // Create native bigint value by uint64_t 62 NativeValue* CreateBigInt(uint64_t value) override; 63 // Create native string value by const char pointer 64 NativeValue* CreateString(const char* value, size_t length) override; 65 // Create native string value by const char16_t pointer 66 NativeValue* CreateString16(const char16_t* value, size_t length) override; 67 // Create native symbol value 68 NativeValue* CreateSymbol(NativeValue* value) override; 69 // Create native value of external pointer 70 NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint, 71 size_t nativeBindingSize = 0) override; 72 // Create native object value 73 NativeValue* CreateObject() override; 74 // Create special native object value 75 NativeValue* CreateNativeBindingObject(void* detach, void* attach) override; 76 NativeValue* CreateNBObject(DetachCallback detach, AttachCallback attach) override; 77 // Create native function value 78 NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override; 79 // Create native array value 80 NativeValue* CreateArray(size_t length) override; 81 // Create native array buffer value 82 NativeValue* CreateArrayBuffer(void** value, size_t length) override; 83 // Create native array buffer value of external 84 NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 85 NativeValue* CreateBuffer(void** value, size_t length) override; 86 NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override; 87 NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 88 // Create native typed array value 89 NativeValue* CreateTypedArray(NativeTypedArrayType type, 90 NativeValue* value, 91 size_t length, 92 size_t offset) override; 93 // Create native data view value 94 NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override; 95 // Create native promise value 96 NativeValue* CreatePromise(NativeDeferred** deferred) override; 97 void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override; 98 // Create native error value 99 NativeValue* CreateError(NativeValue* code, NativeValue* message) override; 100 bool InitTaskPoolThread(NativeEngine* engine, NapiConcurrentCallback callback) override; 101 bool InitTaskPoolFunc(NativeEngine* engine, NativeValue* func) override; 102 // Call function 103 NativeValue* CallFunction(NativeValue* thisVar, 104 NativeValue* function, 105 NativeValue* const* argv, 106 size_t argc) override; 107 // Run script 108 NativeValue* RunScript(NativeValue* script) override; 109 NativeValue* RunScriptPath(const char* path) override; 110 111 NativeValue* RunScriptBuffer(const char* path, std::vector<uint8_t>& buffer, bool isBundle) override; 112 113 // Run buffer script 114 NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override; 115 NativeValue* RunActor(std::vector<uint8_t>& buffer, const char* descriptor) override; 116 // Set lib path 117 void SetPackagePath(const std::string appLinPathKey, const std::vector<std::string>& packagePath); 118 // Define native class 119 NativeValue* DefineClass(const char* name, 120 NativeCallback callback, 121 void* data, 122 const NativePropertyDescriptor* properties, 123 size_t length) override; 124 // Create instance by defined class 125 NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override; 126 127 // Create native reference 128 NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, 129 NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override; 130 bool IsExceptionPending() const override; 131 NativeValue* GetAndClearLastException() override; 132 // Throw exception 133 bool Throw(NativeValue* error) override; 134 // Throw exception 135 bool Throw(NativeErrorType type, const char* code, const char* message) override; 136 137 void* CreateRuntime() override; 138 NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override; 139 NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override; 140 void DeleteSerializationData(NativeValue* value) const override; 141 NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override; 142 NativeValue* LoadArkModule(const char* str, int32_t len, const std::string& fileName); 143 144 static NativeValue* ArkValueToNativeValue(ArkNativeEngine* engine, Local<JSValueRef> value); 145 146 NativeValue* ValueToNativeValue(JSValueWrapper& value) override; 147 148 bool ExecuteJsBin(const std::string& fileName); 149 panda::Local<panda::ObjectRef> LoadModuleByName(const std::string& moduleName, bool isAppModule, 150 const std::string& param, const std::string& instanceName, void* instance, const std::string& path = ""); 151 152 bool TriggerFatalException(NativeValue* error) override; 153 NativeValue* CreateDate(double value) override; 154 NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override; 155 bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override; 156 157 // Detect performance to obtain cpuprofiler file 158 void StartCpuProfiler(const std::string& fileName = "") override; 159 void StopCpuProfiler() override; 160 161 void ResumeVM() override; 162 bool SuspendVM() override; 163 bool IsSuspended() override; 164 bool CheckSafepoint() override; 165 166 // isVmMode means the internal class in vm is visible. 167 // isPrivate means the number and string is not visible. 168 void DumpHeapSnapshot(const std::string& path, bool isVmMode = true, 169 DumpFormat dumpFormat = DumpFormat::JSON) override; 170 // Dump the file into faultlog for heap leak. 171 void DumpHeapSnapshot(bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON, 172 bool isPrivate = false) override; 173 bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override; 174 bool BuildJsStackTrace(std::string& stackTraceStr) override; 175 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames) override; 176 bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override; 177 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override; 178 bool StopHeapTracking(const std::string& filePath) override; 179 180 void PrintStatisticResult() override; 181 void StartRuntimeStat() override; 182 void StopRuntimeStat() override; 183 size_t GetArrayBufferSize() override; 184 size_t GetHeapTotalSize() override; 185 size_t GetHeapUsedSize() override; 186 void NotifyApplicationState(bool inBackground) override; 187 void NotifyIdleTime(int idleMicroSec) override; 188 void NotifyMemoryPressure(bool inHighMemoryPressure = false) override; 189 190 // debugger 191 bool IsMixedDebugEnabled(); 192 void NotifyNativeCalling(const void *nativeAddress); 193 194 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; 195 void HandleUncaughtException() override; 196 bool HasPendingException() override; 197 panda::Local<panda::ObjectRef> GetModuleFromName( 198 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param, 199 const std::string& instanceName, void** instance); 200 201 NativeChunk& GetNativeChunk(); 202 }; 203 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H */ 204