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