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_JS_RUNTIME_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H 18 19 #include <functional> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 // NOLINTNEXTLINE(readability-identifier-naming) 25 namespace OHOS::Ace::Framework { 26 class JsValue; 27 class JsRuntime; 28 29 using std::shared_ptr; 30 using RegisterFunctionType = std::function<shared_ptr<JsValue>(shared_ptr<JsRuntime>, shared_ptr<JsValue>, 31 const std::vector<shared_ptr<JsValue>> &, int32_t)>; 32 using LOG_PRINT = int (*)(int id, int level, const char *tag, const char *fmt, const char *message); 33 using UncaughtExceptionCallback = std::function<void(shared_ptr<JsValue>, std::shared_ptr<JsRuntime>)>; 34 35 // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions, hicpp-special-member-functions) 36 class JsRuntime { 37 public: 38 virtual ~JsRuntime() = default; 39 40 // Prepare js environment, returns true if success. 41 virtual bool Initialize(const std::string &libraryPath, bool isDebugMode, int32_t instanceId = 0) = 0; 42 virtual void Reset() = 0; 43 virtual void SetLogPrint(LOG_PRINT out) = 0; 44 virtual bool StartDebugger() = 0; 45 46 // Evaluate a piece of js code, returns true if success. 47 // If exception occurs during execution, it is handled inside this interface. 48 virtual shared_ptr<JsValue> EvaluateJsCode(const std::string &src) = 0; 49 virtual bool EvaluateJsCode( 50 const uint8_t* buffer, int32_t size, const std::string& filePath = "", bool needUpdate = false) = 0; 51 52 virtual bool ExecuteJsBin([[maybe_unused]] const std::string &fileName, 53 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) 54 { 55 return true; 56 } 57 58 // Get the global object. 59 virtual shared_ptr<JsValue> GetGlobal() = 0; 60 virtual void RunGC() = 0; RunFullGC()61 virtual void RunFullGC() {} 62 63 virtual shared_ptr<JsValue> NewNumber(double d) = 0; 64 virtual shared_ptr<JsValue> NewInt32(int32_t value) = 0; 65 virtual shared_ptr<JsValue> NewBoolean(bool value) = 0; 66 virtual shared_ptr<JsValue> NewNull() = 0; 67 virtual shared_ptr<JsValue> NewUndefined() = 0; 68 virtual shared_ptr<JsValue> NewString(const std::string &str) = 0; 69 virtual shared_ptr<JsValue> ParseJson(const std::string &str) = 0; 70 virtual shared_ptr<JsValue> NewObject() = 0; 71 virtual shared_ptr<JsValue> NewArray() = 0; 72 virtual shared_ptr<JsValue> NewFunction(RegisterFunctionType func) = 0; 73 virtual shared_ptr<JsValue> NewNativePointer(void *ptr) = 0; 74 virtual void ThrowError(const std::string& msg, int32_t code) = 0; 75 virtual void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) = 0; 76 virtual void HandleUncaughtException( 77 const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) = 0; 78 virtual bool HasPendingException() = 0; 79 virtual void ExecutePendingJob() = 0; DumpHeapSnapshot(bool isPrivate)80 virtual void DumpHeapSnapshot(bool isPrivate) {} SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)81 virtual void SetErrorEventHandler( 82 std::function<void(const std::string&, const std::string&)>&& errorCallback) {} 83 84 // Set c++ data to js environment. SetEmbedderData(void * data)85 void SetEmbedderData(void *data) 86 { 87 embedderData_ = data; 88 } 89 // Get c++ data from js environment. GetEmbedderData()90 void *GetEmbedderData() const 91 { 92 return embedderData_; 93 } 94 95 private: 96 void *embedderData_ = nullptr; 97 }; 98 } // namespace OHOS::Ace::Framework 99 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H 100