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