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 const std::string& srcEntrance = "");
97
98 void PreloadSystemModule(const std::string& moduleName) override;
99
100 void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
101 const std::string& hapPath, bool isEsMode, const std::string& srcEntrance) override;
102 void PreloadModule(const std::string& moduleName, const std::string& srcPath,
103 const std::string& hapPath, bool isEsMode, bool useCommonTrunk) override;
104 bool PopPreloadObj(const std::string& key, std::unique_ptr<NativeReference>& obj);
105 void StartDebugMode(const DebugOption debugOption) override;
106 void DebuggerConnectionHandler(bool isDebugApp, bool isStartWithDebug);
107 void StopDebugMode();
108 bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
109 bool UnLoadRepairPatch(const std::string& hqfFile) override;
110 bool NotifyHotReloadPage() override;
111 void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
112 bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
113 bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle,
114 const std::string& srcEntrance = "");
115 bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
116 void StopDebugger();
117
118 NativeEngine* GetNativeEnginePointer() const;
119 panda::ecmascript::EcmaVM* GetEcmaVm() const;
120
121 void UpdateModuleNameAndAssetPath(const std::string& moduleName);
122 void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
123 static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer,
124 bool isABC = true);
125 static std::shared_ptr<AbilityBase::FileMapper> GetSafeData(const std::string& path, std::string& fileFullName);
126
127 void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
128 void InitSourceMap(const std::string hqfFilePath);
129 void FreeNativeReference(std::unique_ptr<NativeReference> reference);
130 void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
131 void StartProfiler(const DebugOption debugOption) override;
132 void DebuggerConnectionManager(bool isDebugApp, bool isStartWithDebug, const DebugOption dOption);
133
134 void ReloadFormComponent(); // Reload ArkTS-Card component
135 void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const override;
136
137 static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(napi_env env,
138 const std::string& moduleName, const napi_value* argv, size_t argc);
139 std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
140 const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false,
141 const std::string& srcEntrance = "");
142 std::unique_ptr<NativeReference> LoadSystemModule(
143 const std::string& moduleName, const napi_value* argv = nullptr, size_t argc = 0);
144 void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override;
145 void UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName) override;
146
147 private:
148 void FinishPreload() override;
149 bool Initialize(const Options& options);
150 void Deinitialize();
151 int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
152
153 napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
154 napi_value LoadJsModule(const std::string& path, const std::string& hapPath, const std::string& srcEntrance = "");
155
156 bool preloaded_ = false;
157 bool isBundle_ = true;
158 bool isOhmUrl_ = false;
159 std::string codePath_;
160 std::string moduleName_;
161 std::unique_ptr<NativeReference> methodRequireNapiRef_;
162 std::unordered_map<std::string, NativeReference*> modules_;
163 std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
164 uint32_t instanceId_ = 0;
165 std::string bundleName_;
166 int32_t apiTargetVersion_ = 0;
167 std::map<std::string, std::string> pkgContextInfoJsonStringMap_;
168 std::map<std::string, std::string> packageNameList_;
169 std::map<std::string, std::unique_ptr<NativeReference>> preloadList_;
170
171 static std::atomic<bool> hasInstance;
172
173 private:
174 bool CreateJsEnv(const Options& options);
175 void PreloadAce(const Options& options);
176 bool InitLoop(bool isStage = true);
177 inline bool IsUseAbilityRuntime(const Options& options) const;
178 void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
179 std::shared_ptr<NativeReference>&& sharedNativeRef);
180 void InitConsoleModule();
181 void InitTimerModule();
182 void InitWorkerModule(const Options& options);
183 void ReInitJsEnvImpl(const Options& options);
184 void PostPreload(const Options& options);
185 void LoadAotFile(const Options& options);
186 void SetRequestAotCallback();
187
188 std::string GetSystemKitPath();
189 std::vector<panda::HmsMap> GetSystemKitsMap(uint32_t version);
190 };
191 } // namespace AbilityRuntime
192 } // namespace OHOS
193 #endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
194