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_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 SerializeData { 23 public: SerializeData(size_t size,uint8_t * data)24 SerializeData(size_t size, uint8_t* data) : dataSize_(size), value_(data) {} 25 ~SerializeData() = default; 26 GetData()27 uint8_t* GetData() const 28 { 29 return value_.get(); 30 } GetSize()31 size_t GetSize() const 32 { 33 return dataSize_; 34 } 35 36 private: 37 struct Deleter { operatorDeleter38 void operator()(uint8_t* ptr) const 39 { 40 free(ptr); 41 } 42 }; 43 44 size_t dataSize_; 45 std::unique_ptr<uint8_t, Deleter> value_; 46 }; 47 48 class QuickJSNativeEngine : public NativeEngine { 49 public: 50 QuickJSNativeEngine(JSRuntime* runtime, JSContext* contex, void* jsEngine); 51 virtual ~QuickJSNativeEngine(); 52 53 JSRuntime* GetRuntime(); 54 JSContext* GetContext(); 55 56 virtual void Loop(LoopMode mode, bool needSync = false) override; 57 58 virtual NativeValue* GetGlobal() override; 59 virtual NativeValue* CreateNull() override; 60 virtual NativeValue* CreateUndefined() override; 61 virtual NativeValue* CreateBoolean(bool value) override; 62 virtual NativeValue* CreateNumber(int32_t value) override; 63 virtual NativeValue* CreateNumber(uint32_t value) override; 64 virtual NativeValue* CreateNumber(int64_t value) override; 65 virtual NativeValue* CreateNumber(double value) override; 66 virtual NativeValue* CreateBigInt(int64_t value) override; 67 virtual NativeValue* CreateBigInt(uint64_t value) override; 68 virtual NativeValue* CreateString(const char* value, size_t length) override; 69 virtual NativeValue* CreateString16(const char16_t* value, size_t length) override; 70 virtual NativeValue* CreateSymbol(NativeValue* value) override; 71 virtual NativeValue* CreateExternal(void* value, NativeFinalize callback, void* hint) override; 72 73 virtual NativeValue* CreateObject() override; 74 virtual NativeValue* CreateFunction(const char* name, size_t length, NativeCallback cb, void* value) override; 75 virtual NativeValue* CreateArray(size_t length) override; 76 77 virtual NativeValue* CreateArrayBuffer(void** value, size_t length) override; 78 virtual NativeValue* CreateArrayBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 79 virtual NativeValue* CreateBuffer(void** value, size_t length) override; 80 virtual NativeValue* CreateBufferCopy(void** value, size_t length, const void* data) override; 81 virtual NativeValue* CreateBufferExternal(void* value, size_t length, NativeFinalize cb, void* hint) override; 82 virtual NativeValue* CreateTypedArray(NativeTypedArrayType type, 83 NativeValue* value, 84 size_t length, 85 size_t offset) override; 86 virtual NativeValue* CreateDataView(NativeValue* value, size_t length, size_t offset) override; 87 virtual NativeValue* CreatePromise(NativeDeferred** deferred) override; 88 virtual void SetPromiseRejectCallback(NativeReference* rejectCallbackRef, 89 NativeReference* checkCallbackRef) override; 90 91 virtual NativeValue* CreateError(NativeValue* code, NativeValue* Message) override; 92 virtual NativeValue* CreateInstance(NativeValue* constructor, NativeValue* const* argv, size_t argc) override; 93 94 virtual NativeReference* CreateReference(NativeValue* value, uint32_t initialRefcount, 95 NativeFinalize callback = nullptr, void* data = nullptr, void* hint = nullptr) override; 96 virtual NativeValue* CallFunction( 97 NativeValue* thisVar, NativeValue* function, NativeValue* const* argv, size_t argc) override; 98 99 virtual NativeValue* DefineClass(const char* name, NativeCallback callback, void* data, 100 const NativePropertyDescriptor* properties, size_t length) override; 101 102 virtual NativeValue* RunScript(NativeValue* script) override; 103 virtual NativeValue* RunBufferScript(std::vector<uint8_t>& buffer) override; 104 virtual NativeValue* RunActor(std::vector<uint8_t>& buffer, const char *descriptor) override; 105 106 void SetPackagePath(const std::string& packagePath); 107 108 virtual bool Throw(NativeValue* error) override; 109 virtual bool Throw(NativeErrorType type, const char* code, const char* message) override; 110 111 virtual void* CreateRuntime() override; 112 bool CheckTransferList(JSValue transferList); 113 bool DetachTransferList(JSValue transferList); 114 virtual NativeValue* Serialize(NativeEngine* context, NativeValue* value, NativeValue* transfer) override; 115 virtual NativeValue* Deserialize(NativeEngine* context, NativeValue* recorder) override; 116 virtual void DeleteSerializationData(NativeValue* value) const override; 117 virtual ExceptionInfo* GetExceptionForWorker() const override; 118 virtual NativeValue* LoadModule(NativeValue* str, const std::string& fileName) override; 119 120 static NativeValue* JSValueToNativeValue(QuickJSNativeEngine* engine, JSValue value); 121 virtual NativeValue* ValueToNativeValue(JSValueWrapper& value) override; 122 JSValue GetModuleFromName( 123 const std::string& moduleName, bool isAppModule, const std::string& id, const std::string& param, 124 const std::string& instanceName, void** instance); 125 JSValue LoadModuleByName( 126 const std::string& moduleName, bool isAppModule, const std::string& param, 127 const std::string& instanceName, void* instance); 128 129 virtual NativeValue* CreateDate(double time) override; 130 virtual NativeValue* CreateBigWords(int sign_bit, size_t word_count, const uint64_t* words) override; 131 virtual bool TriggerFatalException(NativeValue* error) override; 132 virtual bool AdjustExternalMemory(int64_t ChangeInBytes, int64_t* AdjustedValue) override; 133 134 void StartCpuProfiler(const std::string fileName = "") override {} StopCpuProfiler()135 void StopCpuProfiler() override {} 136 ResumeVM()137 void ResumeVM() override {} SuspendVM()138 bool SuspendVM() override 139 { 140 return false; 141 } IsSuspended()142 bool IsSuspended() override 143 { 144 return false; 145 } CheckSafepoint()146 bool CheckSafepoint() override 147 { 148 return false; 149 } 150 151 void DumpHeapSnapShot(const std::string &path, bool isVmMode = true, 152 DumpFormat dumpFormat = DumpFormat::JSON) override {} BuildNativeAndJsBackStackTrace()153 std::string BuildNativeAndJsBackStackTrace() override 154 { 155 return nullptr; 156 } 157 bool StartHeapTracking(double timeInterval, bool isVmMode = true) override 158 { 159 return false; 160 } 161 bool StopHeapTracking(const std::string &filePath, DumpFormat dumpFormat = DumpFormat::JSON) override 162 { 163 return false; 164 } 165 PrintStatisticResult()166 void PrintStatisticResult() override {} StartRuntimeStat()167 void StartRuntimeStat() override {} StopRuntimeStat()168 void StopRuntimeStat() override {} GetArrayBufferSize()169 size_t GetArrayBufferSize() override 170 { 171 return 0; 172 } GetHeapTotalSize()173 size_t GetHeapTotalSize() override 174 { 175 return 0; 176 } GetHeapUsedSize()177 size_t GetHeapUsedSize() override 178 { 179 return 0; 180 } 181 RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback)182 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override {} HandleUncaughtException()183 void HandleUncaughtException() override {} 184 private: 185 static NativeEngine* CreateRuntimeFunc(NativeEngine* engine, void* jsEngine); 186 187 JSRuntime* runtime_; 188 JSContext* context_; 189 }; 190 191 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_IMPL_QUICKJS_QUICKJS_NATIVE_ENGINE_H */ 192