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_QUICKJS_QUICKJS_NATIVE_ENGINE_H 17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_QUICKJS_NATIVE_ENGINE_H 18 19 #include "native_engine/native_engine.h" 20 #include "quickjs_headers.h" 21 22 class QuickJSNativeEngine : public NativeEngine { 23 public: 24 QuickJSNativeEngine(JSRuntime* runtime, JSContext* contex, void* jsEngine); 25 QuickJSNativeEngine(NativeEngineInterface* engineImpl, void* jsEngine, bool isAppModule); 26 virtual ~QuickJSNativeEngine(); 27 28 JSRuntime* GetRuntime(); 29 JSContext* GetContext(); 30 31 void Loop(LoopMode mode, bool needSync = false) override; 32 33 NativeValue* GetGlobal() override; 34 NativeValue* CreateNull() override; 35 NativeValue* CreateUndefined() override; 36 NativeValue* CreateBoolean(bool value) override; 37 NativeValue* CreateNumber(int32_t value) override; 38 NativeValue* CreateNumber(uint32_t value) override; 39 NativeValue* CreateNumber(int64_t value) override; 40 NativeValue* CreateNumber(double value) override; 41 NativeValue* CreateBigInt(int64_t value) override; 42 NativeValue* CreateBigInt(uint64_t value) override; 43 NativeValue* CreateString(const char* value, size_t length) override; 44 NativeValue* CreateString16(const char16_t* value, size_t length) override; 45 NativeValue* CreateSymbol(NativeValue* value) override; 46 NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint, 47 size_t nativeBindingSize = 0) override; 48 49 NativeValue* CreateObject() override; 50 NativeValue* CreateNativeBindingObject(void* detach, void* attach) override; CreateNBObject(DetachCallback detach,AttachCallback attach)51 NativeValue* CreateNBObject(DetachCallback detach, AttachCallback attach) override { return nullptr; } 52 NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override; 53 NativeValue* CreateArray(size_t length) override; 54 55 NativeValue* CreateArrayBuffer(void** value, size_t length) override; 56 NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 57 NativeValue* CreateBuffer(void** value, size_t length) override; 58 NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override; 59 NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 60 NativeValue* CreateTypedArray(NativeTypedArrayType type, 61 NativeValue* value, 62 size_t length, 63 size_t offset) override; 64 NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override; 65 NativeValue* CreatePromise(NativeDeferred** deferred) override; 66 void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, 67 NativeReference* checkCallbackRef) override; 68 69 NativeValue* CreateError(NativeValue* code, NativeValue* Message) override; 70 NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override; 71 72 NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, 73 NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override; CallInitTaskFunc(NativeEngine * engine,NativeValue * func)74 bool CallInitTaskFunc(NativeEngine* engine, NativeValue* func) override 75 { 76 return false; 77 } 78 NativeValue* CallFunction( 79 NativeValue* thisVar, NativeValue* function, NativeValue* const* argv, size_t argc) override; 80 81 NativeValue* DefineClass(const char* name, NativeCallback callback, void* data, 82 const NativePropertyDescriptor* properties, size_t length) override; 83 84 NativeValue* RunScript(NativeValue* script) override; RunScriptPath(const char * path)85 NativeValue* RunScriptPath(const char* path) override 86 { 87 return nullptr; 88 } RunScriptBuffer(const char * path,std::vector<uint8_t> & buffer,bool isBundle)89 NativeValue* RunScriptBuffer(const char* path, std::vector<uint8_t>& buffer, bool isBundle) override 90 { 91 return nullptr; 92 } 93 NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override; 94 NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override; 95 96 void SetPackagePath(const std::vector<std::string>& packagePath); 97 98 bool Throw(NativeValue* error) override; 99 bool Throw(NativeErrorType type, const char* code, const char* message) override; 100 101 void* CreateRuntime() override; 102 bool CheckTransferList(JSValue transferList); 103 bool DetachTransferList(JSValue transferList); 104 NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override; 105 NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override; 106 void DeleteSerializationData(NativeValue* value) const override; 107 ExceptionInfo* GetExceptionForWorker() const override; 108 NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override; 109 110 static NativeValue* JSValueToNativeValue(QuickJSNativeEngine* engine, JSValue value); 111 NativeValue* ValueToNativeValue(JSValueWrapper& value) override; 112 JSValue GetModuleFromName( 113 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param, 114 const std::string& instanceName, void** instance); 115 JSValue LoadModuleByName( 116 const std::string& moduleName, bool isAppModule, const std::string& param, 117 const std::string& instanceName, void* instance); 118 119 NativeValue* CreateDate(double time) override; 120 NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override; 121 bool TriggerFatalException(NativeValue* error) override; 122 bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override; 123 124 void StartCpuProfiler(const std::string& fileName = "") override; 125 void StopCpuProfiler() override; 126 127 void ResumeVM() override; 128 bool SuspendVM() override; 129 bool IsSuspended() override; 130 bool CheckSafepoint() override; 131 132 void DumpHeapSnapshot(const std::string& path, bool isVmMode = true, 133 DumpFormat dumpFormat = DumpFormat::JSON) override; 134 void DumpHeapSnapshot(bool isVmMode = true, DumpFormat dumpFormat = DumpFormat::JSON, 135 bool isPrivate = false) override; 136 bool BuildNativeAndJsStackTrace(std::string& stackTraceStr) override; 137 bool BuildJsStackTrace(std::string& stackTraceStr) override; 138 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrameInfo>& jsFrames) override; 139 bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override; 140 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override; 141 bool StopHeapTracking(const std::string& filePath) override; 142 143 void PrintStatisticResult() override; 144 void StartRuntimeStat() override; 145 void StopRuntimeStat() override; 146 size_t GetArrayBufferSize() override; 147 size_t GetHeapTotalSize() override; 148 size_t GetHeapUsedSize() override; NotifyApplicationState(bool inBackground)149 void NotifyApplicationState(bool inBackground) override {} NotifyIdleTime(int idleMicroSec)150 void NotifyIdleTime(int idleMicroSec) override {} 151 void NotifyMemoryPressure(bool inHighMemoryPressure = false) override {} 152 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; 153 void HandleUncaughtException() override; 154 }; 155 156 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_QUICKJS_NATIVE_ENGINE_H */ 157