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 class FileMapper;
43 } // namespace AbilityBase
44
45 namespace JsEnv {
46 class JsEnvironment;
47 class SourceMapOperator;
48 struct UncaughtExceptionInfo;
49 } // namespace JsEnv
50
51 using AppLibPathMap = std::map<std::string, std::vector<std::string>>;
52
53 namespace AbilityRuntime {
54 class TimerTask;
55
DetachCallbackFunc(napi_env env,void * value,void *)56 inline void *DetachCallbackFunc(napi_env env, void *value, void *)
57 {
58 return value;
59 }
60
61 class JsRuntime : public Runtime {
62 public:
63 static std::unique_ptr<JsRuntime> Create(const Options& options);
64
65 static void SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false);
66
67 static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content);
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() override;
84 void DestroyHeapProfiler() override;
85 void ForceFullGC() override;
86 void ForceFullGC(uint32_t tid) override;
87 void DumpHeapSnapshot(uint32_t tid, bool isFullGC) override;
88 void AllowCrossThreadExecution() override;
89 void GetHeapPrepare() override;
90 void NotifyApplicationState(bool isBackground) override;
91 bool SuspendVM(uint32_t tid) override;
92 void ResumeVM(uint32_t tid) override;
93
94 bool RunSandboxScript(const std::string& path, const std::string& hapPath);
95 bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
96
97 void PreloadSystemModule(const std::string& moduleName) override;
98
99 void StartDebugMode(const DebugOption debugOption) override;
100 void DebuggerConnectionHandler(bool isDebugApp, bool isStartWithDebug);
101 void StopDebugMode();
102 bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
103 bool UnLoadRepairPatch(const std::string& hqfFile) override;
104 bool NotifyHotReloadPage() override;
105 void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
106 bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
107 bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle);
108 bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
109 void StopDebugger();
110
111 NativeEngine* GetNativeEnginePointer() const;
112 panda::ecmascript::EcmaVM* GetEcmaVm() const;
113
114 void UpdateModuleNameAndAssetPath(const std::string& moduleName);
115 void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
116 static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer,
117 bool isABC = true);
118 static std::shared_ptr<AbilityBase::FileMapper> GetSafeData(const std::string& path, std::string& fileFullName);
119
120 void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
121 void InitSourceMap(const std::string hqfFilePath);
122 void FreeNativeReference(std::unique_ptr<NativeReference> reference);
123 void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
124 void StartProfiler(const DebugOption debugOption) override;
125 void DebuggerConnectionManager(bool isDebugApp, bool isStartWithDebug, const DebugOption dOption);
126
127 void ReloadFormComponent(); // Reload ArkTS-Card component
128 void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const override;
129
130 static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(napi_env env,
131 const std::string& moduleName, const napi_value* argv, size_t argc);
132 std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
133 const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false);
134 std::unique_ptr<NativeReference> LoadSystemModule(
135 const std::string& moduleName, const napi_value* argv = nullptr, size_t argc = 0);
136 void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override;
137 void UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName) override;
138
139 private:
140 void FinishPreload() override;
141
142 bool Initialize(const Options& options);
143 void Deinitialize();
144
145 int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
146
147 napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
148 napi_value LoadJsModule(const std::string& path, const std::string& hapPath);
149
150 bool preloaded_ = false;
151 bool isBundle_ = true;
152 std::string codePath_;
153 std::string moduleName_;
154 std::unique_ptr<NativeReference> methodRequireNapiRef_;
155 std::unordered_map<std::string, NativeReference*> modules_;
156 std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
157 uint32_t instanceId_ = 0;
158 std::string bundleName_;
159 int32_t apiTargetVersion_ = 0;
160 std::map<std::string, std::string> pkgContextInfoJsonStringMap_;
161 std::map<std::string, std::string> packageNameList_;
162
163 static std::atomic<bool> hasInstance;
164
165 private:
166 bool CreateJsEnv(const Options& options);
167 void PreloadAce(const Options& options);
168 bool InitLoop(bool isStage = true);
169 inline bool IsUseAbilityRuntime(const Options& options) const;
170 void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
171 std::shared_ptr<NativeReference>&& sharedNativeRef);
172 void InitConsoleModule();
173 void InitTimerModule();
174 void InitWorkerModule(const Options& options);
175 void ReInitJsEnvImpl(const Options& options);
176 void PostPreload(const Options& options);
177 void LoadAotFile(const Options& options);
178 void SetRequestAotCallback();
179
180 std::string GetSystemKitPath();
181 std::vector<panda::HmsMap> GetSystemKitsMap(uint32_t version);
182 };
183 } // namespace AbilityRuntime
184 } // namespace OHOS
185 #endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
186