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 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 using panda::JSNApi; 31 using panda::DFXJSNApi; 32 33 class SerializationData { 34 public: SerializationData()35 SerializationData() : data_(nullptr), size_(0) {} 36 ~SerializationData() = default; 37 GetData()38 uint8_t* GetData() const 39 { 40 return data_.get(); 41 } GetSize()42 size_t GetSize() const 43 { 44 return size_; 45 } 46 47 private: 48 struct DataDeleter { operatorDataDeleter49 void operator()(uint8_t* p) const 50 { 51 free(p); 52 } 53 }; 54 55 std::unique_ptr<uint8_t, DataDeleter> data_; 56 size_t size_; 57 }; 58 59 class ArkNativeEngine : public NativeEngine { 60 public: 61 // ArkNativeEngine constructor 62 ArkNativeEngine(EcmaVM* vm, void* jsEngine); 63 // ArkNativeEngine destructor 64 ~ArkNativeEngine() override; 65 GetEcmaVm()66 const EcmaVM* GetEcmaVm() const 67 { 68 return vm_; 69 } 70 71 void Loop(LoopMode mode, bool needSync = false) override; 72 73 // Get global native object value 74 NativeValue* GetGlobal() override; 75 // Create native null value 76 NativeValue* CreateNull() override; 77 // Create native undefined value 78 NativeValue* CreateUndefined() override; 79 // Create native boolean value 80 NativeValue* CreateBoolean(bool value) override; 81 // Create number value by int32_t 82 NativeValue* CreateNumber(int32_t value) override; 83 // Create number value by uint32_t 84 NativeValue* CreateNumber(uint32_t value) override; 85 // Create native number value by int64_t 86 NativeValue* CreateNumber(int64_t value) override; 87 // Create native number value by double 88 NativeValue* CreateNumber(double value) override; 89 // Create native bigint value by int64_t 90 NativeValue* CreateBigInt(int64_t value) override; 91 // Create native bigint value by uint64_t 92 NativeValue* CreateBigInt(uint64_t value) override; 93 // Create native string value by const char pointer 94 NativeValue* CreateString(const char* value, size_t length) override; 95 // Create native string value by const char16_t pointer 96 NativeValue* CreateString16(const char16_t* value, size_t length) override; 97 // Create native symbol value 98 NativeValue* CreateSymbol(NativeValue* value) override; 99 // Create native value of external pointer 100 NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint) override; 101 // Create native object value 102 NativeValue* CreateObject() override; 103 // Create native function value 104 NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override; 105 // Create native array value 106 NativeValue* CreateArray(size_t length) override; 107 // Create native array buffer value 108 NativeValue* CreateArrayBuffer(void** value, size_t length) override; 109 // Create native array buffer value of external 110 NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 111 NativeValue* CreateBuffer(void** value, size_t length) override; 112 NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override; 113 NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 114 // Create native typed array value 115 NativeValue* CreateTypedArray(NativeTypedArrayType type, 116 NativeValue* value, 117 size_t length, 118 size_t offset) override; 119 // Create native data view value 120 NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override; 121 // Create native promise value 122 NativeValue* CreatePromise(NativeDeferred** deferred) override; 123 void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, NativeReference* checkCallbackRef) override; 124 static void PromiseRejectCallback(void* values); 125 // Create native error value 126 NativeValue* CreateError(NativeValue* code, NativeValue* message) override; 127 // Call function 128 NativeValue* CallFunction(NativeValue* thisVar, 129 NativeValue* function, 130 NativeValue* const* argv, 131 size_t argc) override; 132 // Run script 133 NativeValue* RunScript(NativeValue* script) override; 134 // Run buffer script 135 NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override; 136 NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override; 137 // Set lib path 138 void SetPackagePath(const std::string& packagePath); 139 // Define native class 140 NativeValue* DefineClass(const char* name, 141 NativeCallback callback, 142 void* data, 143 const NativePropertyDescriptor* properties, 144 size_t length) override; 145 // Create instance by defined class 146 NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override; 147 148 // Create native reference 149 NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, 150 NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override; 151 // Throw exception 152 bool Throw(NativeValue* error) override; 153 // Throw exception 154 bool Throw(NativeErrorType type, const char* code, const char* message) override; 155 156 void* CreateRuntime() override; 157 NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override; 158 NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override; 159 void DeleteSerializationData(NativeValue* value) const override; 160 ExceptionInfo* GetExceptionForWorker() const override; 161 NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override; 162 NativeValue* LoadArkModule(const char* str, int32_t len, const std::string& fileName); 163 164 static NativeValue* ArkValueToNativeValue(ArkNativeEngine* engine, Local<JSValueRef> value); 165 166 NativeValue* ValueToNativeValue(JSValueWrapper& value) override; 167 168 bool ExecuteJsBin(const std::string& fileName); 169 panda::Global<panda::ObjectRef> LoadModuleByName( 170 const std::string& moduleName, bool isAppModule, const std::string& param, 171 const std::string& instanceName, void* instance); 172 173 virtual bool TriggerFatalException(NativeValue* error) override; 174 NativeValue* CreateDate(double value) override; 175 NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override; 176 bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override; 177 178 // Detect performance to obtain cpuprofiler file 179 void StartCpuProfiler(const std::string fileName = "") override; 180 void StopCpuProfiler() override; 181 182 void ResumeVM() override; 183 bool SuspendVM() override; 184 bool IsSuspended() override; 185 bool CheckSafepoint() override; 186 187 void DumpHeapSnapShot(const std::string &path, bool isVmMode = true, 188 DumpFormat dumpFormat = DumpFormat::JSON) override; 189 std::string BuildNativeAndJsBackStackTrace() override; 190 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override; 191 bool StopHeapTracking(const std::string &filePath, DumpFormat dumpFormat = DumpFormat::JSON) override; 192 193 void PrintStatisticResult() override; 194 void StartRuntimeStat() override; 195 void StopRuntimeStat() override; 196 size_t GetArrayBufferSize() override; 197 size_t GetHeapTotalSize() override; 198 size_t GetHeapUsedSize() override; 199 200 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; 201 void HandleUncaughtException() override; 202 panda::Global<panda::ObjectRef> GetModuleFromName( 203 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param, 204 const std::string& instanceName, void** instance); 205 private: 206 static NativeEngine* CreateRuntimeFunc(NativeEngine* engine, void* jsEngine); 207 208 EcmaVM* vm_ = nullptr; 209 std::string exceptionStr_; 210 panda::LocalScope topScope_; 211 NativeReference* promiseRejectCallbackRef_ { nullptr }; 212 NativeReference* checkCallbackRef_ { nullptr }; 213 std::unordered_map<NativeModule*, panda::Global<panda::JSValueRef>> loadedModules_; 214 UncaughtExceptionCallback uncaughtExceptionCallback_ { nullptr }; 215 }; 216 217 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_ARK_ARK_NATIVE_ENGINE_H */ 218