• 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(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     // Get the global object.
61     virtual shared_ptr<JsValue> GetGlobal() = 0;
62     virtual void RunGC() = 0;
RunFullGC()63     virtual void RunFullGC() {}
64 
65     virtual shared_ptr<JsValue> NewNumber(double d) = 0;
66     virtual shared_ptr<JsValue> NewInt32(int32_t value) = 0;
67     virtual shared_ptr<JsValue> NewBoolean(bool value) = 0;
68     virtual shared_ptr<JsValue> NewNull() = 0;
69     virtual shared_ptr<JsValue> NewUndefined() = 0;
70     virtual shared_ptr<JsValue> NewString(const std::string &str) = 0;
71     virtual shared_ptr<JsValue> ParseJson(const std::string &str) = 0;
72     virtual shared_ptr<JsValue> NewObject() = 0;
73     virtual shared_ptr<JsValue> NewArray() = 0;
74     virtual shared_ptr<JsValue> NewFunction(RegisterFunctionType func) = 0;
75     virtual shared_ptr<JsValue> NewNativePointer(void *ptr) = 0;
76     virtual void ThrowError(const std::string& msg, int32_t code) = 0;
77     virtual void RegisterUncaughtExceptionHandler(UncaughtExceptionCallback callback) = 0;
78     virtual void HandleUncaughtException(panda::TryCatch& tryCatch,
79         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) = 0;
80     virtual void HandleUncaughtExceptionWithoutNativeEngine(panda::TryCatch& tryCatch,
81         const std::function<void(const std::string&, int32_t)>& errorCallback = nullptr) = 0;
82     virtual bool HasPendingException() = 0;
83     virtual void ExecutePendingJob() = 0;
DumpHeapSnapshot(bool isPrivate)84     virtual void DumpHeapSnapshot(bool isPrivate) {}
DestroyHeapProfiler()85     virtual void DestroyHeapProfiler() {}
ForceFullGC()86     virtual void ForceFullGC() {}
SetErrorEventHandler(std::function<void (const std::string &,const std::string &)> && errorCallback)87     virtual void SetErrorEventHandler(
88         std::function<void(const std::string&, const std::string&)>&& errorCallback) {}
89 
90     // Set c++ data to js environment.
SetEmbedderData(void * data)91     void SetEmbedderData(void *data)
92     {
93         embedderData_ = data;
94     }
95     // Get c++ data from js environment.
GetEmbedderData()96     void *GetEmbedderData() const
97     {
98         return embedderData_;
99     }
100 
101 private:
102     void *embedderData_ = nullptr;
103 };
104 }  // namespace OHOS::Ace::Framework
105 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_ENGINE_JSI_JS_RUNTIME_H
106