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