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_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_RUNTIME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_RUNTIME_H 18 19 #if defined(PREVIEW) 20 #include <map> 21 #include "frameworks/bridge/declarative_frontend/engine/jsi/utils/jsi_module_searcher.h" 22 #endif 23 #include <memory> 24 25 #include "ecmascript/napi/include/jsnapi.h" 26 27 #include "frameworks/bridge/js_frontend/engine/jsi/js_runtime.h" 28 29 namespace panda::ecmascript { 30 class EcmaVM; 31 } // namespace panda::ecmascript 32 33 // NOLINTNEXTLINE(readability-identifier-naming) 34 namespace OHOS::Ace::Framework { 35 using panda::ArrayRef; 36 using panda::BooleanRef; 37 using panda::EscapeLocalScope; 38 using panda::FunctionRef; 39 using panda::Global; 40 using panda::IntegerRef; 41 using panda::JSExecutionScope; 42 using panda::JSNApi; 43 using panda::JSON; 44 using panda::JSValueRef; 45 using panda::Local; 46 using panda::LocalScope; 47 using panda::NativePointerRef; 48 using panda::NumberRef; 49 using panda::ObjectRef; 50 using panda::RuntimeOption; 51 using panda::StringRef; 52 using panda::ecmascript::EcmaVM; 53 class PandaFunctionData; 54 55 using DebuggerPostTask = std::function<void(std::function<void()>&&)>; 56 57 // NOLINTNEXTLINE(fuchsia-multiple-inheritance) 58 class ArkJSRuntime final : public JsRuntime, public std::enable_shared_from_this<ArkJSRuntime> { 59 public: 60 using ErrorEventHandler = std::function<void(const std::string&, const std::string&)>; 61 #if !defined(WINDOWS_PLATFORM) 62 bool StartDebugger(const char* libraryPath, EcmaVM* vm) const; 63 #endif 64 bool Initialize(const std::string& libraryPath, bool isDebugMode, int32_t instanceId) override; 65 bool InitializeFromExistVM(EcmaVM* vm); 66 void Reset() override; 67 void SetLogPrint(LOG_PRINT out) override; 68 bool StartDebugger() override; 69 shared_ptr<JsValue> EvaluateJsCode(const std::string& src) override; 70 bool EvaluateJsCode( 71 const uint8_t* buffer, int32_t size, const std::string& filePath = "", bool needUpdate = false) override; 72 bool ExecuteJsBin(const std::string& fileName, 73 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) override; 74 shared_ptr<JsValue> GetGlobal() override; 75 void RunGC() override; 76 void RunFullGC() override; 77 78 shared_ptr<JsValue> NewNumber(double d) override; 79 shared_ptr<JsValue> NewInt32(int32_t value) override; 80 shared_ptr<JsValue> NewBoolean(bool value) override; 81 shared_ptr<JsValue> NewNull() override; 82 shared_ptr<JsValue> NewUndefined() override; 83 shared_ptr<JsValue> NewString(const std::string& str) override; 84 shared_ptr<JsValue> ParseJson(const std::string& str) override; 85 shared_ptr<JsValue> NewObject() override; 86 shared_ptr<JsValue> NewArray() override; 87 shared_ptr<JsValue> NewFunction(RegisterFunctionType func) override; 88 shared_ptr<JsValue> NewNativePointer(void* ptr) override; 89 void ThrowError(const std::string& msg, int32_t code) override; 90 void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) override; 91 void HandleUncaughtException( 92 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) override; 93 bool HasPendingException() override; 94 void ExecutePendingJob() override; 95 void DumpHeapSnapshot(bool isPrivate) override; 96 bool ExecuteModuleBuffer(const uint8_t *data, int32_t size, const std::string &filename, bool needUpdate = false); 97 GetEcmaVm()98 const EcmaVM* GetEcmaVm() const 99 { 100 return vm_; 101 } 102 SetAssetPath(const std::string & assetPath)103 void SetAssetPath(const std::string& assetPath) 104 { 105 panda::JSNApi::SetAssetPath(vm_, assetPath); 106 } 107 SetBundleName(const std::string & bundleName)108 void SetBundleName(const std::string& bundleName) 109 { 110 panda::JSNApi::SetBundleName(vm_, bundleName); 111 } 112 SetBundle(bool isBundle)113 void SetBundle(bool isBundle) 114 { 115 panda::JSNApi::SetBundle(vm_, isBundle); 116 } 117 SetModuleName(const std::string & moduleName)118 void SetModuleName(const std::string& moduleName) 119 { 120 panda::JSNApi::SetModuleName(vm_, moduleName); 121 } 122 SetDebuggerPostTask(DebuggerPostTask && task)123 void SetDebuggerPostTask(DebuggerPostTask&& task) 124 { 125 debuggerPostTask_ = std::move(task); 126 } 127 SetErrorEventHandler(ErrorEventHandler && errorCallback)128 void SetErrorEventHandler(ErrorEventHandler&& errorCallback) override 129 { 130 errorCallback_ = std::move(errorCallback); 131 } 132 GetErrorEventHandler()133 const ErrorEventHandler& GetErrorEventHandler() 134 { 135 return errorCallback_; 136 } 137 SetDebugMode(bool isDebugMode)138 void SetDebugMode(bool isDebugMode) 139 { 140 isDebugMode_ = isDebugMode; 141 } 142 SetInstanceId(int32_t instanceId)143 void SetInstanceId(int32_t instanceId) 144 { 145 instanceId_ = instanceId; 146 } 147 SetLanguage(const std::string & language)148 void SetLanguage(const std::string& language) 149 { 150 language_ = language; 151 } 152 153 #if defined(PREVIEW) SetPreviewFlag(bool flag)154 void SetPreviewFlag(bool flag) 155 { 156 isComponentPreview_ = flag; 157 } 158 GetPreviewFlag()159 bool GetPreviewFlag() const 160 { 161 return isComponentPreview_; 162 } 163 GetRequiredComponent()164 std::string GetRequiredComponent() const 165 { 166 return requiredComponent_; 167 } 168 SetRequiredComponent(const std::string & componentName)169 void SetRequiredComponent(const std::string &componentName) 170 { 171 requiredComponent_ = componentName; 172 } 173 AddPreviewComponent(const std::string & componentName,const panda::Global<panda::ObjectRef> & componentObj)174 void AddPreviewComponent(const std::string &componentName, const panda::Global<panda::ObjectRef> &componentObj) 175 { 176 previewComponents_.emplace(componentName, componentObj); 177 } 178 GetPreviewComponent(EcmaVM * vm,const std::string & componentName)179 panda::Global<panda::ObjectRef> GetPreviewComponent(EcmaVM* vm, const std::string &componentName) 180 { 181 auto iter = previewComponents_.find(componentName); 182 if (iter != previewComponents_.end()) { 183 auto retVal = iter->second; 184 previewComponents_.erase(iter); 185 return retVal; 186 } 187 panda::Global<panda::ObjectRef> undefined(vm, panda::JSValueRef::Undefined(vm)); 188 return undefined; 189 } 190 AddRootView(const panda::Global<panda::ObjectRef> & RootView)191 void AddRootView(const panda::Global<panda::ObjectRef> &RootView) 192 { 193 RootView_ = RootView; 194 } 195 GetRootView()196 panda::Global<panda::ObjectRef> GetRootView() 197 { 198 return RootView_; 199 } 200 #endif 201 202 private: 203 EcmaVM* vm_ = nullptr; 204 int32_t instanceId_ = 0; 205 std::string language_; 206 LOG_PRINT print_ { nullptr }; 207 UncaughtExceptionCallback uncaughtErrorHandler_ { nullptr }; 208 std::string libPath_ {}; 209 bool usingExistVM_ = false; 210 bool isDebugMode_ = false; 211 DebuggerPostTask debuggerPostTask_; 212 ErrorEventHandler errorCallback_; 213 #if defined(PREVIEW) 214 bool isComponentPreview_ = false; 215 std::string requiredComponent_ {}; 216 std::multimap<std::string, panda::Global<panda::ObjectRef>> previewComponents_; 217 panda::Global<panda::ObjectRef> RootView_; 218 #endif 219 }; 220 221 class PandaFunctionData { 222 public: PandaFunctionData(std::weak_ptr<ArkJSRuntime> runtime,RegisterFunctionType func)223 PandaFunctionData(std::weak_ptr<ArkJSRuntime> runtime, RegisterFunctionType func) 224 : runtime_(runtime), func_(std::move(func)) 225 {} 226 227 ~PandaFunctionData() = default; 228 229 NO_COPY_SEMANTIC(PandaFunctionData); 230 NO_MOVE_SEMANTIC(PandaFunctionData); 231 232 private: 233 Local<JSValueRef> Callback(panda::JsiRuntimeCallInfo* info) const; 234 std::weak_ptr<ArkJSRuntime> runtime_; 235 RegisterFunctionType func_; 236 friend Local<JSValueRef> FunctionCallback(panda::JsiRuntimeCallInfo* info); 237 }; 238 } // namespace OHOS::Ace::Framework 239 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_ARK_JS_RUNTIME_H 240