• 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 ErrorObject;
49 struct UncaughtExceptionInfo;
50 using UncatchableTask = std::function<void(std::string summary, const JsEnv::ErrorObject errorObject)>;
51 } // namespace JsEnv
52 
53 using AppLibPathMap = std::map<std::string, std::vector<std::string>>;
54 
55 namespace AbilityRuntime {
56 class TimerTask;
57 
DetachCallbackFunc(napi_env env,void * value,void *)58 inline void *DetachCallbackFunc(napi_env env, void *value, void *)
59 {
60     return value;
61 }
62 
63 class JsRuntime : public Runtime {
64 public:
65     using Runtime::PreloadModule;
66     static std::unique_ptr<JsRuntime> Create(const Options& options);
67 
68     static void SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp = false);
69 
70     static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content);
71     JsRuntime();
72     ~JsRuntime() override;
73 
74     NativeEngine& GetNativeEngine() const;
75     napi_env GetNapiEnv() const;
76 
GetLanguage()77     Language GetLanguage() const override
78     {
79         return Language::JS;
80     }
81 
82     void PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime);
83     void PostSyncTask(const std::function<void()>& task, const std::string& name);
84     void RemoveTask(const std::string& name);
85     void DumpHeapSnapshot(bool isPrivate) override;
86     void DumpCpuProfile() override;
87     void DestroyHeapProfiler() override;
88     void ForceFullGC() override;
89     void ForceFullGC(uint32_t tid) override;
90     void DumpHeapSnapshot(uint32_t tid, bool isFullGC, bool isBinary = false) override;
91     void AllowCrossThreadExecution() override;
92     void GetHeapPrepare() override;
93     void NotifyApplicationState(bool isBackground) override;
94     bool SuspendVM(uint32_t tid) override;
95     void ResumeVM(uint32_t tid) override;
96 
97     bool RunSandboxScript(const std::string& path, const std::string& hapPath);
98     bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false,
99         const std::string& srcEntrance = "");
100 
101     void PreloadSystemModule(const std::string& moduleName) override;
102 
103     void PreloadMainAbility(const std::string& moduleName, const std::string& srcPath,
104         const std::string& hapPath,  bool isEsMode, const std::string& srcEntrance) override;
105     void PreloadModule(const std::string& moduleName, const std::string& srcPath,
106         const std::string& hapPath, bool isEsMode, bool useCommonTrunk) override;
107     bool PopPreloadObj(const std::string& key, std::unique_ptr<NativeReference>& obj);
108     void StartDebugMode(const DebugOption debugOption) override;
109     void SetDebugOption(const DebugOption debugOption) override;
110     void StartLocalDebugMode(bool isDebugFromLocal) override;
111     void DebuggerConnectionHandler(bool isDebugApp, bool isStartWithDebug);
112     void StopDebugMode();
113     bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
114     bool UnLoadRepairPatch(const std::string& hqfFile) override;
115     bool NotifyHotReloadPage() override;
116     void RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo);
117     void RegisterUncatchableExceptionHandler(const JsEnv::UncatchableTask& uncatchableTask);
118     bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
119     bool LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle,
120         const std::string& srcEntrance = "");
121     bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
122     void StopDebugger();
123 
124     NativeEngine* GetNativeEnginePointer() const;
125     panda::ecmascript::EcmaVM* GetEcmaVm() const;
126 
127     void UpdateModuleNameAndAssetPath(const std::string& moduleName);
128     void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
129     static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer,
130                               bool isABC = true);
131     static std::shared_ptr<AbilityBase::FileMapper> GetSafeData(const std::string& path, std::string& fileFullName);
132 
133     void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
134     void InitSourceMap(const std::string hqfFilePath);
135     void FreeNativeReference(std::unique_ptr<NativeReference> reference);
136     void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
137     void StartProfiler(const DebugOption debugOption) override;
SetExtensionApiCheckCallback(const std::function<bool (const std::string & className,const std::string & fileName)> & cb)138     void SetExtensionApiCheckCallback(
139         const std::function<bool(const std::string &className, const std::string &fileName)> &cb) override {}
140     void DebuggerConnectionManager(bool isDebugApp, bool isStartWithDebug, const DebugOption dOption);
141 
142     void ReloadFormComponent(); // Reload ArkTS-Card component
143     void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const override;
144 
145     static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(napi_env env,
146         const std::string& moduleName, const napi_value* argv, size_t argc);
147     std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
148         const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false,
149         const std::string& srcEntrance = "");
150     std::unique_ptr<NativeReference> LoadSystemModule(
151         const std::string& moduleName, const napi_value* argv = nullptr, size_t argc = 0);
152 
153     bool ExecuteSecureWithOhmUrl(const std::string &moduleName, const std::string &hapPath,
154         const std::string &srcEntrance);
155     napi_value GetExportObjectFromOhmUrl(const std::string &srcEntrance, const std::string &key);
156 
157     void SetDeviceDisconnectCallback(const std::function<bool()> &cb) override;
158     void SetStopPreloadSoCallback(const std::function<void()> &callback) override;
159     void SetPkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName);
160     void UpdatePkgContextInfoJson(const std::string& moduleName, const std::string& hapPath,
161         const std::string& packageName);
162 
163 private:
164     void FinishPreload() override;
165     bool Initialize(const Options& options);
166     void Deinitialize();
167     int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
168 
169     napi_value LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
170     napi_value LoadJsModule(const std::string& path, const std::string& hapPath, const std::string& srcEntrance = "");
171 
172     bool preloaded_ = false;
173     bool isBundle_ = true;
174     bool isOhmUrl_ = false;
175     std::string codePath_;
176     std::string moduleName_;
177     std::unique_ptr<NativeReference> methodRequireNapiRef_;
178     std::unordered_map<std::string, NativeReference*> modules_;
179     std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
180     uint32_t instanceId_ = 0;
181     std::string bundleName_;
182     int32_t apiTargetVersion_ = 0;
183     std::map<std::string, std::string> pkgContextInfoJsonStringMap_;
184     std::map<std::string, std::string> packageNameList_;
185     std::map<std::string, std::unique_ptr<NativeReference>> preloadList_;
186 
187     static std::atomic<bool> hasInstance;
188     DebugOption debugOption_;
189 
190 private:
191     bool CreateJsEnv(const Options& options);
192     void PreloadAce(const Options& options);
193     bool InitLoop(bool isStage = true);
194     inline bool IsUseAbilityRuntime(const Options& options) const;
195     void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
196         std::shared_ptr<NativeReference>&& sharedNativeRef);
197     void InitConsoleModule();
198     void InitTimerModule();
199     void InitWorkerModule(const Options& options);
200     void ReInitJsEnvImpl(const Options& options);
201     void PostPreload(const Options& options);
202     void LoadAotFile(const Options& options);
203     void SetRequestAotCallback();
204 
205     std::string GetSystemKitPath();
206     std::vector<panda::HmsMap> GetSystemKitsMap(uint32_t version);
207 };
208 } // namespace AbilityRuntime
209 } // namespace OHOS
210 #endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
211