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