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 OHOS {
33 namespace AppExecFwk {
34 class EventHandler;
35 } // namespace AppExecFwk
36
37 namespace AbilityBase {
38 class Extractor;
39 } // namespace AbilityBase
40
41 namespace JsEnv {
42 class JsEnvironment;
43 class SourceMapOperator;
44 struct UncaughtExceptionInfo;
45 } // namespace JsEnv
46
47 using AppLibPathMap = std::map<std::string, std::vector<std::string>>;
48
49 namespace AbilityRuntime {
50 class TimerTask;
51
DetachCallbackFunc(NativeEngine * engine,void * value,void *)52 inline void *DetachCallbackFunc(NativeEngine *engine, void *value, void *)
53 {
54 return value;
55 }
56
57 class JsRuntime : public Runtime {
58 public:
59 static std::unique_ptr<JsRuntime> Create(const Options& options);
60
61 static std::unique_ptr<NativeReference> LoadSystemModuleByEngine(NativeEngine* engine,
62 const std::string& moduleName, NativeValue* const* argv, size_t argc);
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
GetLanguage()73 Language GetLanguage() const override
74 {
75 return Language::JS;
76 }
77
78 std::unique_ptr<NativeReference> LoadModule(const std::string& moduleName, const std::string& modulePath,
79 const std::string& hapPath, bool esmodule = false, bool useCommonChunk = false);
80 std::unique_ptr<NativeReference> LoadSystemModule(
81 const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0);
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 bool BuildJsStackInfoList(uint32_t tid, std::vector<JsFrames>& jsFrames) override;
87 void NotifyApplicationState(bool isBackground) override;
88 bool SuspendVM(uint32_t tid) override;
89 void ResumeVM(uint32_t tid) override;
90
91 bool RunSandboxScript(const std::string& path, const std::string& hapPath);
92 bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
93
94 void PreloadSystemModule(const std::string& moduleName) override;
95 void StartDebugMode(bool needBreakPoint) override;
96 void StopDebugMode();
97 bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override;
98 bool UnLoadRepairPatch(const std::string& hqfFile) override;
99 bool NotifyHotReloadPage() override;
100 void RegisterUncaughtExceptionHandler(JsEnv::UncaughtExceptionInfo uncaughtExceptionInfo);
101 bool LoadScript(const std::string& path, std::vector<uint8_t>* buffer = nullptr, bool isBundle = false);
102 bool StartDebugger(bool needBreakPoint, uint32_t instanceId);
103 void StopDebugger();
104 bool LoadScript(const std::string& path, uint8_t *buffer, size_t len, bool isBundle);
105
106 NativeEngine* GetNativeEnginePointer() const;
107 panda::ecmascript::EcmaVM* GetEcmaVm() const;
108
109 void UpdateModuleNameAndAssetPath(const std::string& moduleName);
110 void RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath) override;
111 static bool GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer);
112
113 void InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorImpl);
114 void FreeNativeReference(std::unique_ptr<NativeReference> reference);
115 void FreeNativeReference(std::shared_ptr<NativeReference>&& reference);
116 void StartProfiler(const std::string &perfCmd) override;
117
118 void ReloadFormComponent(); // Reload ArkTS-Card component
119 void DoCleanWorkAfterStageCleaned() override;
120 void SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate) const override;
121
122 private:
123 void FinishPreload() override;
124
125 bool Initialize(const Options& options);
126 void Deinitialize();
127
128 NativeValue* LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk = false);
129 NativeValue* LoadJsModule(const std::string& path, const std::string& hapPath);
130 int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue);
131
132 bool debugMode_ = false;
133 bool preloaded_ = false;
134 bool isBundle_ = true;
135 std::string codePath_;
136 std::string moduleName_;
137 std::unique_ptr<NativeReference> methodRequireNapiRef_;
138 std::unordered_map<std::string, NativeReference*> modules_;
139 std::shared_ptr<JsEnv::JsEnvironment> jsEnv_ = nullptr;
140 uint32_t instanceId_ = 0;
141 std::string bundleName_;
142 int32_t apiTargetVersion_ = 0;
143
144 static std::atomic<bool> hasInstance;
145
146 private:
147 bool CreateJsEnv(const Options& options);
148 void PreloadAce(const Options& options);
149 bool InitLoop();
150 inline bool IsUseAbilityRuntime(const Options& options) const;
151 void FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
152 std::shared_ptr<NativeReference>&& sharedNativeRef);
153 void InitConsoleModule();
154 void InitTimerModule();
155 void InitWorkerModule(const Options& options);
156 void ReInitJsEnvImpl(const Options& options);
157 };
158 } // namespace AbilityRuntime
159 } // namespace OHOS
160
161 #endif // OHOS_ABILITY_RUNTIME_JS_RUNTIME_H
162