1 /*
2 * Copyright (c) 2021-2023 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 OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
17 #define OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
18
19 #include <cstdint>
20 #include <functional>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <vector>
25
26 #include "native_engine/native_engine.h"
27 #include "runtime.h"
28
29 namespace panda::ecmascript {
30 class EcmaVM;
31 } // namespace panda::ecmascript
32 namespace panda {
33 struct HmsMap;
34 }
35 namespace OHOS {
36 namespace AppExecFwk {
37 class EventHandler;
38 } // namespace AppExecFwk
39
40 namespace AbilityBase {
41 class Extractor;
42 } // namespace AbilityBase
43
44 namespace JsEnv {
45 class JsEnvironment;
46 class SourceMapOperator;
47 struct UncaughtExceptionInfo;
48 } // namespace JsEnv
49
50 using AppLibPathMap = std::map<std::string, std::vector<std::string>>;
51
52 namespace AbilityRuntime {
53 class TimerTask;
54
DetachCallbackFunc(napi_env env,void * value,void *)55 inline void *DetachCallbackFunc(napi_env env, void *value, void *)
56 {
57 return value;
58 }
59
60 class JsRuntime : public Runtime {
61 public:
62 static std::unique_ptr<JsRuntime> Create(const Options& options);
63
64 static void SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false);
65
66 static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content);
67
68 JsRuntime();
69 ~JsRuntime() override;
70
71 NativeEngine& GetNativeEngine() const;
72 napi_env GetNapiEnv() const;
73
GetLanguage()74 Language GetLanguage() const override
75 {
76 return Language::JS;
77 }
78
79 void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime);
80 void PostSyncTask(const std::function<void()>& task, const std::string& name);
81 void RemoveTask(const std::string& name);
82 void DumpHeapSnapshot(bool isPrivate) override;
83 void DumpCpuProfile(bool isPrivate) override;
84 void DestroyHeapProfiler() override;
85 void ForceFullGC() override;
86 void AllowCrossThreadExecution() override;
87 void GetHeapPrepare() override;
88 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrames>& jsFrames) override;
89 void NotifyApplicationState(bool isBackground) override;
90 bool SuspendVM(uint32_t tid) override;
91 void ResumeVM(uint32_t tid) override;
92
93 bool RunSandboxScript(const std::string& path, const std::string& hapPath);
94 bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
95
96 void PreloadSystemModule(const std::string& moduleName) override;
97
98 void StartDebugMode(bool needBreakPoint, const std::string &processName, bool isDebug = true) override;
99 void StopDebugMode();
100 bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
101 bool UnLoadRepairPatch(const std::string& hqfFile) override;
102 bool NotifyHotReloadPage() override;
103 void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
104 bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
105 bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle);
106 bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
107 void StopDebugger();
108
109 NativeEngine* GetNativeEnginePointer() const;
110 panda::ecmascript::EcmaVM* GetEcmaVm() const;
111
112 void UpdateModuleNameAndAssetPath(const std::string& moduleName);
113 void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
114 static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer);
115
116 void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
117 void FreeNativeReference(std::unique_ptr<NativeReference> reference);
118 void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
119 void StartProfiler(
120 const std::string &perfCmd, bool needBreakPoint, const std::string &processName, bool isDebug = true) override;
121
122 void ReloadFormComponent(); // Reload ArkTS-Card component
123 void DoCleanWorkAfterStageCleaned() override;
124 void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate) const override;
125
126 static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(napi_env env,
127 const std::string& moduleName, const napi_value* argv, size_t argc);
128 std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
129 const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false);
130 std::unique_ptr<NativeReference> LoadSystemModule(
131 const std::string& moduleName, const napi_value* argv = nullptr, size_t argc = 0);
132 void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override;
133
134 private:
135 void FinishPreload() override;
136
137 bool Initialize(const Options& options);
138 void Deinitialize();
139
140 int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
141
142 napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
143 napi_value LoadJsModule(const std::string& path, const std::string& hapPath);
144
145 bool preloaded_ = false;
146 bool isBundle_ = true;
147 std::string codePath_;
148 std::string moduleName_;
149 std::unique_ptr<NativeReference> methodRequireNapiRef_;
150 std::unordered_map<std::string, NativeReference*> modules_;
151 std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
152 uint32_t instanceId_ = 0;
153 std::string bundleName_;
154 int32_t apiTargetVersion_ = 0;
155
156 static std::atomic<bool> hasInstance;
157
158 private:
159 bool CreateJsEnv(const Options& options);
160 void PreloadAce(const Options& options);
161 bool InitLoop();
162 inline bool IsUseAbilityRuntime(const Options& options) const;
163 void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
164 std::shared_ptr<NativeReference>&& sharedNativeRef);
165 void InitConsoleModule();
166 void InitTimerModule();
167 void InitWorkerModule(const Options& options);
168 void ReInitJsEnvImpl(const Options& options);
169 void PostPreload(const Options& options);
170 void LoadAotFile(const Options& options);
171 void SetRequestAotCallback();
172
173 std::vector<panda::HmsMap> GetSystemKitsMap(uint32_t version);
174 };
175 } // namespace AbilityRuntime
176 } // namespace OHOS
177 #endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
178