1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#include "js_runtime.h"
17eace7efcSopenharmony_ci
18eace7efcSopenharmony_ci#include <cerrno>
19eace7efcSopenharmony_ci#include <climits>
20eace7efcSopenharmony_ci#include <cstdlib>
21eace7efcSopenharmony_ci#include <fstream>
22eace7efcSopenharmony_ci#include <mutex>
23eace7efcSopenharmony_ci#include <regex>
24eace7efcSopenharmony_ci
25eace7efcSopenharmony_ci#include <atomic>
26eace7efcSopenharmony_ci#include <sys/epoll.h>
27eace7efcSopenharmony_ci#include <unistd.h>
28eace7efcSopenharmony_ci
29eace7efcSopenharmony_ci#include "file_ex.h"
30eace7efcSopenharmony_ci#include "accesstoken_kit.h"
31eace7efcSopenharmony_ci#include "config_policy_utils.h"
32eace7efcSopenharmony_ci#include "constants.h"
33eace7efcSopenharmony_ci#include "connect_server_manager.h"
34eace7efcSopenharmony_ci#include "ecmascript/napi/include/jsnapi.h"
35eace7efcSopenharmony_ci#include "extract_resource_manager.h"
36eace7efcSopenharmony_ci#include "file_mapper.h"
37eace7efcSopenharmony_ci#include "file_path_utils.h"
38eace7efcSopenharmony_ci#include "hdc_register.h"
39eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h"
40eace7efcSopenharmony_ci#include "hitrace_meter.h"
41eace7efcSopenharmony_ci#include "ipc_skeleton.h"
42eace7efcSopenharmony_ci#include "iservice_registry.h"
43eace7efcSopenharmony_ci#include "js_environment.h"
44eace7efcSopenharmony_ci#include "js_module_reader.h"
45eace7efcSopenharmony_ci#include "js_module_searcher.h"
46eace7efcSopenharmony_ci#include "js_quickfix_callback.h"
47eace7efcSopenharmony_ci#include "js_runtime_utils.h"
48eace7efcSopenharmony_ci#include "js_utils.h"
49eace7efcSopenharmony_ci#include "js_worker.h"
50eace7efcSopenharmony_ci#include "module_checker_delegate.h"
51eace7efcSopenharmony_ci#include "napi/native_api.h"
52eace7efcSopenharmony_ci#include "native_engine/impl/ark/ark_native_engine.h"
53eace7efcSopenharmony_ci#include "native_engine/native_create_env.h"
54eace7efcSopenharmony_ci#include "native_engine/native_engine.h"
55eace7efcSopenharmony_ci#include "js_runtime_lite.h"
56eace7efcSopenharmony_ci#include "ohos_js_env_logger.h"
57eace7efcSopenharmony_ci#include "ohos_js_environment_impl.h"
58eace7efcSopenharmony_ci#include "parameters.h"
59eace7efcSopenharmony_ci#include "extractor.h"
60eace7efcSopenharmony_ci#include "system_ability_definition.h"
61eace7efcSopenharmony_ci#include "source_map.h"
62eace7efcSopenharmony_ci#include "source_map_operator.h"
63eace7efcSopenharmony_ci
64eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
65eace7efcSopenharmony_ci#include "hot_reloader.h"
66eace7efcSopenharmony_ci#include "ace_forward_compatibility.h"
67eace7efcSopenharmony_ci#include "declarative_module_preloader.h"
68eace7efcSopenharmony_ci#endif //SUPPORT_SCREEN
69eace7efcSopenharmony_ci
70eace7efcSopenharmony_ci#include "syscap_ts.h"
71eace7efcSopenharmony_ci
72eace7efcSopenharmony_ciusing namespace OHOS::AbilityBase;
73eace7efcSopenharmony_ciusing Extractor = OHOS::AbilityBase::Extractor;
74eace7efcSopenharmony_ci
75eace7efcSopenharmony_cinamespace OHOS {
76eace7efcSopenharmony_cinamespace AbilityRuntime {
77eace7efcSopenharmony_cinamespace {
78eace7efcSopenharmony_ciconstexpr size_t PARAM_TWO = 2;
79eace7efcSopenharmony_ciconstexpr uint8_t SYSCAP_MAX_SIZE = 100;
80eace7efcSopenharmony_ciconstexpr int64_t DEFAULT_GC_POOL_SIZE = 0x10000000; // 256MB
81eace7efcSopenharmony_ciconstexpr int32_t DEFAULT_INTER_VAL = 500;
82eace7efcSopenharmony_ciconstexpr int32_t API8 = 8;
83eace7efcSopenharmony_ciconst std::string SANDBOX_ARK_CACHE_PATH = "/data/storage/ark-cache/";
84eace7efcSopenharmony_ciconst std::string SANDBOX_ARK_PROIFILE_PATH = "/data/storage/ark-profile";
85eace7efcSopenharmony_ciconst std::string DEBUGGER = "@Debugger";
86eace7efcSopenharmony_ci
87eace7efcSopenharmony_ciconstexpr char MERGE_ABC_PATH[] = "/ets/modules.abc";
88eace7efcSopenharmony_ciconstexpr char BUNDLE_INSTALL_PATH[] = "/data/storage/el1/bundle/";
89eace7efcSopenharmony_ciconstexpr const char* PERMISSION_RUN_ANY_CODE = "ohos.permission.RUN_ANY_CODE";
90eace7efcSopenharmony_ci
91eace7efcSopenharmony_ciconst std::string CONFIG_PATH = "/etc/system_kits_config.json";
92eace7efcSopenharmony_ciconst std::string SYSTEM_KITS_CONFIG_PATH = "/system/etc/system_kits_config.json";
93eace7efcSopenharmony_ci
94eace7efcSopenharmony_ciconst std::string SYSTEM_KITS = "systemkits";
95eace7efcSopenharmony_ciconst std::string NAMESPACE = "namespace";
96eace7efcSopenharmony_ciconst std::string TARGET_OHM = "targetohm";
97eace7efcSopenharmony_ciconst std::string SINCE_VERSION = "sinceVersion";
98eace7efcSopenharmony_ci
99eace7efcSopenharmony_ciconstexpr char DEVELOPER_MODE_STATE[] = "const.security.developermode.state";
100eace7efcSopenharmony_cistatic auto PermissionCheckFunc = []() {
101eace7efcSopenharmony_ci    Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
102eace7efcSopenharmony_ci
103eace7efcSopenharmony_ci    int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, PERMISSION_RUN_ANY_CODE);
104eace7efcSopenharmony_ci    if (result == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
105eace7efcSopenharmony_ci        return true;
106eace7efcSopenharmony_ci    } else {
107eace7efcSopenharmony_ci        return false;
108eace7efcSopenharmony_ci    }
109eace7efcSopenharmony_ci};
110eace7efcSopenharmony_ci
111eace7efcSopenharmony_ciint32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char* message)
112eace7efcSopenharmony_ci{
113eace7efcSopenharmony_ci    TAG_LOGI(AAFwkTag::JSRUNTIME, "ArkLog: %{public}s", message);
114eace7efcSopenharmony_ci    return 0;
115eace7efcSopenharmony_ci}
116eace7efcSopenharmony_ci} // namespace
117eace7efcSopenharmony_ci
118eace7efcSopenharmony_cistd::atomic<bool> JsRuntime::hasInstance(false);
119eace7efcSopenharmony_ciJsRuntime::JsRuntime()
120eace7efcSopenharmony_ci{
121eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
122eace7efcSopenharmony_ci}
123eace7efcSopenharmony_ci
124eace7efcSopenharmony_ciJsRuntime::~JsRuntime()
125eace7efcSopenharmony_ci{
126eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
127eace7efcSopenharmony_ci    Deinitialize();
128eace7efcSopenharmony_ci    StopDebugMode();
129eace7efcSopenharmony_ci}
130eace7efcSopenharmony_ci
131eace7efcSopenharmony_cistd::unique_ptr<JsRuntime> JsRuntime::Create(const Options& options)
132eace7efcSopenharmony_ci{
133eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
134eace7efcSopenharmony_ci    std::unique_ptr<JsRuntime> instance;
135eace7efcSopenharmony_ci    JsRuntimeLite::InitJsRuntimeLite(options);
136eace7efcSopenharmony_ci    if (!options.preload && options.isStageModel) {
137eace7efcSopenharmony_ci        auto preloadedInstance = Runtime::GetPreloaded();
138eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
139eace7efcSopenharmony_ci        // reload ace if compatible mode changes
140eace7efcSopenharmony_ci        if (Ace::AceForwardCompatibility::PipelineChanged() && preloadedInstance) {
141eace7efcSopenharmony_ci            preloadedInstance.reset();
142eace7efcSopenharmony_ci        }
143eace7efcSopenharmony_ci#endif
144eace7efcSopenharmony_ci        if (preloadedInstance && preloadedInstance->GetLanguage() == Runtime::Language::JS) {
145eace7efcSopenharmony_ci            instance.reset(static_cast<JsRuntime*>(preloadedInstance.release()));
146eace7efcSopenharmony_ci        } else {
147eace7efcSopenharmony_ci            instance = std::make_unique<JsRuntime>();
148eace7efcSopenharmony_ci        }
149eace7efcSopenharmony_ci    } else {
150eace7efcSopenharmony_ci        instance = std::make_unique<JsRuntime>();
151eace7efcSopenharmony_ci    }
152eace7efcSopenharmony_ci
153eace7efcSopenharmony_ci    if (!instance->Initialize(options)) {
154eace7efcSopenharmony_ci        return std::unique_ptr<JsRuntime>();
155eace7efcSopenharmony_ci    }
156eace7efcSopenharmony_ci    return instance;
157eace7efcSopenharmony_ci}
158eace7efcSopenharmony_ci
159eace7efcSopenharmony_civoid JsRuntime::StartDebugMode(const DebugOption dOption)
160eace7efcSopenharmony_ci{
161eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
162eace7efcSopenharmony_ci    if (!system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
163eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Developer Mode is false.");
164eace7efcSopenharmony_ci        return;
165eace7efcSopenharmony_ci    }
166eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
167eace7efcSopenharmony_ci    if (jsEnv_->GetDebugMode()) {
168eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::JSRUNTIME, "debugMode");
169eace7efcSopenharmony_ci        return;
170eace7efcSopenharmony_ci    }
171eace7efcSopenharmony_ci    // Set instance id to tid after the first instance.
172eace7efcSopenharmony_ci    if (JsRuntime::hasInstance.exchange(true, std::memory_order_relaxed)) {
173eace7efcSopenharmony_ci        instanceId_ = static_cast<uint32_t>(getproctid());
174eace7efcSopenharmony_ci    }
175eace7efcSopenharmony_ci
176eace7efcSopenharmony_ci    bool isStartWithDebug = dOption.isStartWithDebug;
177eace7efcSopenharmony_ci    bool isDebugApp = dOption.isDebugApp;
178eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "Ark VM is starting debug mode [%{public}s]", isStartWithDebug ? "break" : "normal");
179eace7efcSopenharmony_ci    StartDebuggerInWorkerModule(isDebugApp, dOption.isStartWithNative);
180eace7efcSopenharmony_ci    const std::string bundleName = bundleName_;
181eace7efcSopenharmony_ci    uint32_t instanceId = instanceId_;
182eace7efcSopenharmony_ci    auto weak = jsEnv_;
183eace7efcSopenharmony_ci    std::string inputProcessName = bundleName_ != dOption.processName ? dOption.processName : "";
184eace7efcSopenharmony_ci    HdcRegister::Get().StartHdcRegister(bundleName_, inputProcessName, isDebugApp, [bundleName,
185eace7efcSopenharmony_ci            isStartWithDebug, instanceId, weak, isDebugApp] (int socketFd, std::string option) {
186eace7efcSopenharmony_ci            TAG_LOGI(AAFwkTag::JSRUNTIME, "HdcRegister msg, fd= %{public}d, option= %{public}s",
187eace7efcSopenharmony_ci                socketFd, option.c_str());
188eace7efcSopenharmony_ci        if (weak == nullptr) {
189eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::JSRUNTIME, "null weak");
190eace7efcSopenharmony_ci            return;
191eace7efcSopenharmony_ci        }
192eace7efcSopenharmony_ci        if (option.find(DEBUGGER) == std::string::npos) {
193eace7efcSopenharmony_ci            if (isDebugApp) {
194eace7efcSopenharmony_ci                ConnectServerManager::Get().StopConnectServer(false);
195eace7efcSopenharmony_ci            }
196eace7efcSopenharmony_ci            ConnectServerManager::Get().SendDebuggerInfo(isStartWithDebug, isDebugApp);
197eace7efcSopenharmony_ci            ConnectServerManager::Get().StartConnectServer(bundleName, socketFd, false);
198eace7efcSopenharmony_ci        } else {
199eace7efcSopenharmony_ci            if (isDebugApp) {
200eace7efcSopenharmony_ci                weak->StopDebugger(option);
201eace7efcSopenharmony_ci            }
202eace7efcSopenharmony_ci            weak->StartDebugger(option, socketFd, isDebugApp);
203eace7efcSopenharmony_ci        }
204eace7efcSopenharmony_ci    });
205eace7efcSopenharmony_ci    if (isDebugApp) {
206eace7efcSopenharmony_ci        ConnectServerManager::Get().StartConnectServer(bundleName_, -1, true);
207eace7efcSopenharmony_ci    }
208eace7efcSopenharmony_ci
209eace7efcSopenharmony_ci    DebuggerConnectionHandler(isDebugApp, isStartWithDebug);
210eace7efcSopenharmony_ci}
211eace7efcSopenharmony_ci
212eace7efcSopenharmony_civoid JsRuntime::DebuggerConnectionHandler(bool isDebugApp, bool isStartWithDebug)
213eace7efcSopenharmony_ci{
214eace7efcSopenharmony_ci    ConnectServerManager::Get().StoreInstanceMessage(getproctid(), instanceId_);
215eace7efcSopenharmony_ci    EcmaVM* vm = GetEcmaVm();
216eace7efcSopenharmony_ci    auto dTask = jsEnv_->GetDebuggerPostTask();
217eace7efcSopenharmony_ci    panda::JSNApi::DebugOption option = {ARK_DEBUGGER_LIB_PATH, isDebugApp ? isStartWithDebug : false};
218eace7efcSopenharmony_ci    ConnectServerManager::Get().StoreDebuggerInfo(getproctid(), reinterpret_cast<void*>(vm), option, dTask, isDebugApp);
219eace7efcSopenharmony_ci    jsEnv_->NotifyDebugMode(getproctid(), ARK_DEBUGGER_LIB_PATH, instanceId_, isDebugApp, isStartWithDebug);
220eace7efcSopenharmony_ci}
221eace7efcSopenharmony_ci
222eace7efcSopenharmony_civoid JsRuntime::StopDebugMode()
223eace7efcSopenharmony_ci{
224eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
225eace7efcSopenharmony_ci    if (jsEnv_->GetDebugMode()) {
226eace7efcSopenharmony_ci        ConnectServerManager::Get().RemoveInstance(instanceId_);
227eace7efcSopenharmony_ci        StopDebugger();
228eace7efcSopenharmony_ci    }
229eace7efcSopenharmony_ci}
230eace7efcSopenharmony_ci
231eace7efcSopenharmony_civoid JsRuntime::InitConsoleModule()
232eace7efcSopenharmony_ci{
233eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
234eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
235eace7efcSopenharmony_ci    jsEnv_->InitConsoleModule();
236eace7efcSopenharmony_ci}
237eace7efcSopenharmony_ci
238eace7efcSopenharmony_cibool JsRuntime::StartDebugger(bool needBreakPoint, uint32_t instanceId)
239eace7efcSopenharmony_ci{
240eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
241eace7efcSopenharmony_ci    return true;
242eace7efcSopenharmony_ci}
243eace7efcSopenharmony_ci
244eace7efcSopenharmony_civoid JsRuntime::StopDebugger()
245eace7efcSopenharmony_ci{
246eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
247eace7efcSopenharmony_ci    jsEnv_->StopDebugger();
248eace7efcSopenharmony_ci}
249eace7efcSopenharmony_ci
250eace7efcSopenharmony_ciint32_t JsRuntime::JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue)
251eace7efcSopenharmony_ci{
252eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "profiler command parse %{public}s", command.c_str());
253eace7efcSopenharmony_ci    auto findPos = command.find("jsperf");
254eace7efcSopenharmony_ci    if (findPos == std::string::npos) {
255eace7efcSopenharmony_ci        // jsperf command not found, so not to do, return zero.
256eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "jsperf command not found");
257eace7efcSopenharmony_ci        return 0;
258eace7efcSopenharmony_ci    }
259eace7efcSopenharmony_ci
260eace7efcSopenharmony_ci    // match jsperf command
261eace7efcSopenharmony_ci    auto jsPerfStr = command.substr(findPos, command.length() - findPos);
262eace7efcSopenharmony_ci    const std::regex regexJsperf(R"(^jsperf($|\s+($|\d*\s*($|nativeperf.*))))");
263eace7efcSopenharmony_ci    std::match_results<std::string::const_iterator> matchResults;
264eace7efcSopenharmony_ci    if (!std::regex_match(jsPerfStr, matchResults, regexJsperf)) {
265eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "the order not match");
266eace7efcSopenharmony_ci        return defaultValue;
267eace7efcSopenharmony_ci    }
268eace7efcSopenharmony_ci
269eace7efcSopenharmony_ci    // get match resuflt
270eace7efcSopenharmony_ci    std::string jsperfResuflt;
271eace7efcSopenharmony_ci    constexpr size_t matchResultIndex = 1;
272eace7efcSopenharmony_ci    if (matchResults.size() < PARAM_TWO) {
273eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "no results need to be matched");
274eace7efcSopenharmony_ci        return defaultValue;
275eace7efcSopenharmony_ci    }
276eace7efcSopenharmony_ci
277eace7efcSopenharmony_ci    jsperfResuflt = matchResults[matchResultIndex].str();
278eace7efcSopenharmony_ci    // match number result
279eace7efcSopenharmony_ci    const std::regex regexJsperfNum(R"(^\s*(\d+).*)");
280eace7efcSopenharmony_ci    std::match_results<std::string::const_iterator> jsperfMatchResults;
281eace7efcSopenharmony_ci    if (!std::regex_match(jsperfResuflt, jsperfMatchResults, regexJsperfNum)) {
282eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "the jsperf results not match");
283eace7efcSopenharmony_ci        return defaultValue;
284eace7efcSopenharmony_ci    }
285eace7efcSopenharmony_ci
286eace7efcSopenharmony_ci    // get match result
287eace7efcSopenharmony_ci    std::string interval;
288eace7efcSopenharmony_ci    constexpr size_t matchNumResultIndex = 1;
289eace7efcSopenharmony_ci    if (jsperfMatchResults.size() < PARAM_TWO) {
290eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "jsperfMatchResults not match");
291eace7efcSopenharmony_ci        return defaultValue;
292eace7efcSopenharmony_ci    }
293eace7efcSopenharmony_ci
294eace7efcSopenharmony_ci    interval = jsperfMatchResults[matchNumResultIndex].str();
295eace7efcSopenharmony_ci    if (interval.empty()) {
296eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "empty interval");
297eace7efcSopenharmony_ci        return defaultValue;
298eace7efcSopenharmony_ci    }
299eace7efcSopenharmony_ci
300eace7efcSopenharmony_ci    return std::stoi(interval);
301eace7efcSopenharmony_ci}
302eace7efcSopenharmony_ci
303eace7efcSopenharmony_civoid JsRuntime::StartProfiler(const DebugOption dOption)
304eace7efcSopenharmony_ci{
305eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
306eace7efcSopenharmony_ci    if (!system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
307eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Developer Mode is false.");
308eace7efcSopenharmony_ci        return;
309eace7efcSopenharmony_ci    }
310eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
311eace7efcSopenharmony_ci    if (JsRuntime::hasInstance.exchange(true, std::memory_order_relaxed)) {
312eace7efcSopenharmony_ci        instanceId_ = static_cast<uint32_t>(getproctid());
313eace7efcSopenharmony_ci    }
314eace7efcSopenharmony_ci
315eace7efcSopenharmony_ci    bool isStartWithDebug = dOption.isStartWithDebug;
316eace7efcSopenharmony_ci    bool isDebugApp = dOption.isDebugApp;
317eace7efcSopenharmony_ci    StartDebuggerInWorkerModule(isDebugApp, dOption.isStartWithNative);
318eace7efcSopenharmony_ci    const std::string bundleName = bundleName_;
319eace7efcSopenharmony_ci    auto weak = jsEnv_;
320eace7efcSopenharmony_ci    uint32_t instanceId = instanceId_;
321eace7efcSopenharmony_ci    std::string inputProcessName = bundleName_ != dOption.processName ? dOption.processName : "";
322eace7efcSopenharmony_ci    HdcRegister::Get().StartHdcRegister(bundleName_, inputProcessName, isDebugApp,
323eace7efcSopenharmony_ci        [bundleName, isStartWithDebug, instanceId, weak, isDebugApp](int socketFd, std::string option) {
324eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::JSRUNTIME, "HdcRegister msg, fd= %{public}d, option= %{public}s", socketFd, option.c_str());
325eace7efcSopenharmony_ci        if (weak == nullptr) {
326eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
327eace7efcSopenharmony_ci            return;
328eace7efcSopenharmony_ci        }
329eace7efcSopenharmony_ci        if (option.find(DEBUGGER) == std::string::npos) {
330eace7efcSopenharmony_ci            if (isDebugApp) {
331eace7efcSopenharmony_ci                ConnectServerManager::Get().StopConnectServer(false);
332eace7efcSopenharmony_ci            }
333eace7efcSopenharmony_ci            ConnectServerManager::Get().SendDebuggerInfo(isStartWithDebug, isDebugApp);
334eace7efcSopenharmony_ci            ConnectServerManager::Get().StartConnectServer(bundleName, socketFd, false);
335eace7efcSopenharmony_ci        } else {
336eace7efcSopenharmony_ci            if (isDebugApp) {
337eace7efcSopenharmony_ci                weak->StopDebugger(option);
338eace7efcSopenharmony_ci            }
339eace7efcSopenharmony_ci            weak->StartDebugger(option, socketFd, isDebugApp);
340eace7efcSopenharmony_ci        }
341eace7efcSopenharmony_ci    });
342eace7efcSopenharmony_ci
343eace7efcSopenharmony_ci    DebuggerConnectionManager(isDebugApp, isStartWithDebug, dOption);
344eace7efcSopenharmony_ci}
345eace7efcSopenharmony_ci
346eace7efcSopenharmony_civoid JsRuntime::DebuggerConnectionManager(bool isDebugApp, bool isStartWithDebug, const DebugOption dOption)
347eace7efcSopenharmony_ci{
348eace7efcSopenharmony_ci    if (isDebugApp) {
349eace7efcSopenharmony_ci        ConnectServerManager::Get().StartConnectServer(bundleName_, 0, true);
350eace7efcSopenharmony_ci    }
351eace7efcSopenharmony_ci    ConnectServerManager::Get().StoreInstanceMessage(getproctid(), instanceId_);
352eace7efcSopenharmony_ci    JsEnv::JsEnvironment::PROFILERTYPE profiler = JsEnv::JsEnvironment::PROFILERTYPE::PROFILERTYPE_HEAP;
353eace7efcSopenharmony_ci    int32_t interval = 0;
354eace7efcSopenharmony_ci    const std::string profilerCommand("profile");
355eace7efcSopenharmony_ci    if (dOption.perfCmd.find(profilerCommand) != std::string::npos) {
356eace7efcSopenharmony_ci        profiler = JsEnv::JsEnvironment::PROFILERTYPE::PROFILERTYPE_CPU;
357eace7efcSopenharmony_ci        interval = JsperfProfilerCommandParse(dOption.perfCmd, DEFAULT_INTER_VAL);
358eace7efcSopenharmony_ci    }
359eace7efcSopenharmony_ci    EcmaVM* vm = GetEcmaVm();
360eace7efcSopenharmony_ci    auto dTask = jsEnv_->GetDebuggerPostTask();
361eace7efcSopenharmony_ci    panda::JSNApi::DebugOption option = {ARK_DEBUGGER_LIB_PATH, isDebugApp ? isStartWithDebug : false};
362eace7efcSopenharmony_ci    ConnectServerManager::Get().StoreDebuggerInfo(getproctid(), reinterpret_cast<void*>(vm), option, dTask, isDebugApp);
363eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "profiler:%{public}d interval:%{public}d", profiler, interval);
364eace7efcSopenharmony_ci    jsEnv_->StartProfiler(ARK_DEBUGGER_LIB_PATH, instanceId_, profiler, interval, getproctid(), isDebugApp);
365eace7efcSopenharmony_ci}
366eace7efcSopenharmony_ci
367eace7efcSopenharmony_cibool JsRuntime::GetFileBuffer(const std::string& filePath, std::string& fileFullName, std::vector<uint8_t>& buffer,
368eace7efcSopenharmony_ci                              bool isABC)
369eace7efcSopenharmony_ci{
370eace7efcSopenharmony_ci    Extractor extractor(filePath);
371eace7efcSopenharmony_ci    if (!extractor.Init()) {
372eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Extractor of %{private}s init failed", filePath.c_str());
373eace7efcSopenharmony_ci        return false;
374eace7efcSopenharmony_ci    }
375eace7efcSopenharmony_ci
376eace7efcSopenharmony_ci    std::vector<std::string> fileNames;
377eace7efcSopenharmony_ci    if (isABC) {
378eace7efcSopenharmony_ci        extractor.GetSpecifiedTypeFiles(fileNames, ".abc");
379eace7efcSopenharmony_ci    } else {
380eace7efcSopenharmony_ci        extractor.GetSpecifiedTypeFiles(fileNames, ".map");
381eace7efcSopenharmony_ci    }
382eace7efcSopenharmony_ci    if (fileNames.empty()) {
383eace7efcSopenharmony_ci        TAG_LOGW(
384eace7efcSopenharmony_ci            AAFwkTag::JSRUNTIME, "no .abc in hap/hqf %{private}s", filePath.c_str());
385eace7efcSopenharmony_ci        return true;
386eace7efcSopenharmony_ci    }
387eace7efcSopenharmony_ci
388eace7efcSopenharmony_ci    std::string fileName = fileNames.front();
389eace7efcSopenharmony_ci    fileFullName = filePath + "/" + fileName;
390eace7efcSopenharmony_ci    std::unique_ptr<uint8_t[]> data;
391eace7efcSopenharmony_ci    size_t dataLen = 0;
392eace7efcSopenharmony_ci    if (!extractor.ExtractToBufByName(fileName, data, dataLen)) {
393eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Extract %{public}s failed", fileFullName.c_str());
394eace7efcSopenharmony_ci        return false;
395eace7efcSopenharmony_ci    }
396eace7efcSopenharmony_ci
397eace7efcSopenharmony_ci    buffer.assign(data.get(), data.get() + dataLen);
398eace7efcSopenharmony_ci    return true;
399eace7efcSopenharmony_ci}
400eace7efcSopenharmony_ci
401eace7efcSopenharmony_cistd::shared_ptr<AbilityBase::FileMapper> JsRuntime::GetSafeData(const std::string& path, std::string& fileFullName)
402eace7efcSopenharmony_ci{
403eace7efcSopenharmony_ci    bool newCreate = false;
404eace7efcSopenharmony_ci    auto extractor = ExtractorUtil::GetExtractor(path, newCreate, true);
405eace7efcSopenharmony_ci    if (extractor == nullptr) {
406eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Get extractor failed. path: %{private}s", path.c_str());
407eace7efcSopenharmony_ci        return nullptr;
408eace7efcSopenharmony_ci    }
409eace7efcSopenharmony_ci
410eace7efcSopenharmony_ci    std::vector<std::string> fileNames;
411eace7efcSopenharmony_ci    extractor->GetSpecifiedTypeFiles(fileNames, ".abc");
412eace7efcSopenharmony_ci    if (fileNames.empty()) {
413eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::JSRUNTIME, "There's no abc file in hap or hqf: %{private}s", path.c_str());
414eace7efcSopenharmony_ci        return nullptr;
415eace7efcSopenharmony_ci    }
416eace7efcSopenharmony_ci    std::string fileName = fileNames.front();
417eace7efcSopenharmony_ci    fileFullName = path + "/" + fileName;
418eace7efcSopenharmony_ci
419eace7efcSopenharmony_ci    auto safeData = extractor->GetSafeData(fileName);
420eace7efcSopenharmony_ci    if (safeData == nullptr) {
421eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Get safe data failed. path: %{private}s", path.c_str());
422eace7efcSopenharmony_ci        return nullptr;
423eace7efcSopenharmony_ci    }
424eace7efcSopenharmony_ci
425eace7efcSopenharmony_ci    return safeData;
426eace7efcSopenharmony_ci}
427eace7efcSopenharmony_ci
428eace7efcSopenharmony_cibool JsRuntime::LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath)
429eace7efcSopenharmony_ci{
430eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
431eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
432eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(vm, false);
433eace7efcSopenharmony_ci
434eace7efcSopenharmony_ci    InitSourceMap(hqfFile);
435eace7efcSopenharmony_ci
436eace7efcSopenharmony_ci    std::string patchFile;
437eace7efcSopenharmony_ci    auto hqfSafeData = GetSafeData(hqfFile, patchFile);
438eace7efcSopenharmony_ci    if (hqfSafeData == nullptr) {
439eace7efcSopenharmony_ci        if (patchFile.empty()) {
440eace7efcSopenharmony_ci            TAG_LOGI(AAFwkTag::JSRUNTIME, "No need to load patch cause no ets. path: %{private}s", hqfFile.c_str());
441eace7efcSopenharmony_ci            return true;
442eace7efcSopenharmony_ci        }
443eace7efcSopenharmony_ci        return false;
444eace7efcSopenharmony_ci    }
445eace7efcSopenharmony_ci
446eace7efcSopenharmony_ci    std::string baseFile;
447eace7efcSopenharmony_ci    auto hapSafeData = GetSafeData(hapPath, baseFile);
448eace7efcSopenharmony_ci    if (hapSafeData == nullptr) {
449eace7efcSopenharmony_ci        return false;
450eace7efcSopenharmony_ci    }
451eace7efcSopenharmony_ci
452eace7efcSopenharmony_ci    std::string resolvedHapPath;
453eace7efcSopenharmony_ci    auto position = hapPath.find(".hap");
454eace7efcSopenharmony_ci    if (position != std::string::npos) {
455eace7efcSopenharmony_ci        resolvedHapPath = hapPath.substr(0, position) + MERGE_ABC_PATH;
456eace7efcSopenharmony_ci    }
457eace7efcSopenharmony_ci
458eace7efcSopenharmony_ci    auto hspPosition = hapPath.find(".hsp");
459eace7efcSopenharmony_ci    if (hspPosition != std::string::npos) {
460eace7efcSopenharmony_ci        resolvedHapPath = hapPath.substr(0, hspPosition) + MERGE_ABC_PATH;
461eace7efcSopenharmony_ci    }
462eace7efcSopenharmony_ci
463eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "LoadPatch, patchFile: %{private}s, baseFile: %{private}s",
464eace7efcSopenharmony_ci        patchFile.c_str(), resolvedHapPath.c_str());
465eace7efcSopenharmony_ci    auto ret = panda::JSNApi::LoadPatch(vm, patchFile, hqfSafeData->GetDataPtr(), hqfSafeData->GetDataLen(),
466eace7efcSopenharmony_ci        resolvedHapPath, hapSafeData->GetDataPtr(), hapSafeData->GetDataLen());
467eace7efcSopenharmony_ci    if (ret != panda::JSNApi::PatchErrorCode::SUCCESS) {
468eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "LoadPatch failed:%{public}d", static_cast<int32_t>(ret));
469eace7efcSopenharmony_ci        return false;
470eace7efcSopenharmony_ci    }
471eace7efcSopenharmony_ci
472eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "Load patch %{private}s succeed", patchFile.c_str());
473eace7efcSopenharmony_ci    return true;
474eace7efcSopenharmony_ci}
475eace7efcSopenharmony_ci
476eace7efcSopenharmony_cibool JsRuntime::UnLoadRepairPatch(const std::string& hqfFile)
477eace7efcSopenharmony_ci{
478eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
479eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
480eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(vm, false);
481eace7efcSopenharmony_ci
482eace7efcSopenharmony_ci    Extractor extractor(hqfFile);
483eace7efcSopenharmony_ci    if (!extractor.Init()) {
484eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Extractor of %{private}s init failed", hqfFile.c_str());
485eace7efcSopenharmony_ci        return false;
486eace7efcSopenharmony_ci    }
487eace7efcSopenharmony_ci
488eace7efcSopenharmony_ci    std::vector<std::string> fileNames;
489eace7efcSopenharmony_ci    extractor.GetSpecifiedTypeFiles(fileNames, ".abc");
490eace7efcSopenharmony_ci    if (fileNames.empty()) {
491eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::JSRUNTIME, "no .abc in hqf %{private}s", hqfFile.c_str());
492eace7efcSopenharmony_ci        return true;
493eace7efcSopenharmony_ci    }
494eace7efcSopenharmony_ci
495eace7efcSopenharmony_ci    for (const auto &fileName : fileNames) {
496eace7efcSopenharmony_ci        std::string patchFile = hqfFile + "/" + fileName;
497eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "UnloadPatch, patchFile: %{private}s", patchFile.c_str());
498eace7efcSopenharmony_ci        auto ret = panda::JSNApi::UnloadPatch(vm, patchFile);
499eace7efcSopenharmony_ci        if (ret != panda::JSNApi::PatchErrorCode::SUCCESS) {
500eace7efcSopenharmony_ci            TAG_LOGW(AAFwkTag::JSRUNTIME, "UnLoadPatch failed with %{public}d", static_cast<int32_t>(ret));
501eace7efcSopenharmony_ci        }
502eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "UnLoad patch %{private}s succeed", patchFile.c_str());
503eace7efcSopenharmony_ci    }
504eace7efcSopenharmony_ci
505eace7efcSopenharmony_ci    return true;
506eace7efcSopenharmony_ci}
507eace7efcSopenharmony_ci
508eace7efcSopenharmony_cibool JsRuntime::NotifyHotReloadPage()
509eace7efcSopenharmony_ci{
510eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
511eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
512eace7efcSopenharmony_ci    Ace::HotReloader::HotReload();
513eace7efcSopenharmony_ci#endif // SUPPORT_SCREEN
514eace7efcSopenharmony_ci    return true;
515eace7efcSopenharmony_ci}
516eace7efcSopenharmony_ci
517eace7efcSopenharmony_cibool JsRuntime::LoadScript(const std::string& path, std::vector<uint8_t>* buffer, bool isBundle)
518eace7efcSopenharmony_ci{
519eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "path: %{private}s", path.c_str());
520eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(jsEnv_, false);
521eace7efcSopenharmony_ci    return jsEnv_->LoadScript(path, buffer, isBundle);
522eace7efcSopenharmony_ci}
523eace7efcSopenharmony_ci
524eace7efcSopenharmony_cibool JsRuntime::LoadScript(const std::string& path, uint8_t* buffer, size_t len, bool isBundle,
525eace7efcSopenharmony_ci    const std::string& srcEntrance)
526eace7efcSopenharmony_ci{
527eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "path: %{private}s", path.c_str());
528eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(jsEnv_, false);
529eace7efcSopenharmony_ci    if (isOhmUrl_ && !moduleName_.empty()) {
530eace7efcSopenharmony_ci        auto vm = GetEcmaVm();
531eace7efcSopenharmony_ci        CHECK_POINTER_AND_RETURN(vm, false);
532eace7efcSopenharmony_ci        std::string srcFilename = "";
533eace7efcSopenharmony_ci        srcFilename = BUNDLE_INSTALL_PATH + moduleName_ + MERGE_ABC_PATH;
534eace7efcSopenharmony_ci        return panda::JSNApi::ExecuteSecureWithOhmUrl(vm, buffer, len, srcFilename, srcEntrance);
535eace7efcSopenharmony_ci    }
536eace7efcSopenharmony_ci    return jsEnv_->LoadScript(path, buffer, len, isBundle);
537eace7efcSopenharmony_ci}
538eace7efcSopenharmony_ci
539eace7efcSopenharmony_cistd::unique_ptr<NativeReference> JsRuntime::LoadSystemModuleByEngine(
540eace7efcSopenharmony_ci    napi_env env, const std::string& moduleName, const napi_value* argv, size_t argc)
541eace7efcSopenharmony_ci{
542eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "ModuleName %{public}s", moduleName.c_str());
543eace7efcSopenharmony_ci    if (env == nullptr) {
544eace7efcSopenharmony_ci        TAG_LOGI(AAFwkTag::JSRUNTIME, "invalid engine");
545eace7efcSopenharmony_ci        return nullptr;
546eace7efcSopenharmony_ci    }
547eace7efcSopenharmony_ci
548eace7efcSopenharmony_ci    napi_value globalObj = nullptr;
549eace7efcSopenharmony_ci    napi_get_global(env, &globalObj);
550eace7efcSopenharmony_ci    napi_value propertyValue = nullptr;
551eace7efcSopenharmony_ci    napi_get_named_property(env, globalObj, "requireNapi", &propertyValue);
552eace7efcSopenharmony_ci
553eace7efcSopenharmony_ci    std::unique_ptr<NativeReference> methodRequireNapiRef_;
554eace7efcSopenharmony_ci    napi_ref tmpRef = nullptr;
555eace7efcSopenharmony_ci    napi_create_reference(env, propertyValue, 1, &tmpRef);
556eace7efcSopenharmony_ci    methodRequireNapiRef_.reset(reinterpret_cast<NativeReference*>(tmpRef));
557eace7efcSopenharmony_ci    if (!methodRequireNapiRef_) {
558eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to create reference for global.requireNapi");
559eace7efcSopenharmony_ci        return nullptr;
560eace7efcSopenharmony_ci    }
561eace7efcSopenharmony_ci
562eace7efcSopenharmony_ci    napi_value className = nullptr;
563eace7efcSopenharmony_ci    napi_create_string_utf8(env, moduleName.c_str(), moduleName.length(), &className);
564eace7efcSopenharmony_ci    auto refValue = methodRequireNapiRef_->GetNapiValue();
565eace7efcSopenharmony_ci    napi_value args[1] = { className };
566eace7efcSopenharmony_ci    napi_value classValue = nullptr;
567eace7efcSopenharmony_ci    napi_call_function(env, globalObj, refValue, 1, args, &classValue);
568eace7efcSopenharmony_ci    napi_value instanceValue = nullptr;
569eace7efcSopenharmony_ci    napi_new_instance(env, classValue, argc, argv, &instanceValue);
570eace7efcSopenharmony_ci    if (instanceValue == nullptr) {
571eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to create object instance");
572eace7efcSopenharmony_ci        return nullptr;
573eace7efcSopenharmony_ci    }
574eace7efcSopenharmony_ci
575eace7efcSopenharmony_ci    napi_ref resultRef = nullptr;
576eace7efcSopenharmony_ci    napi_create_reference(env, instanceValue, 1, &resultRef);
577eace7efcSopenharmony_ci    return std::unique_ptr<NativeReference>(reinterpret_cast<NativeReference*>(resultRef));
578eace7efcSopenharmony_ci}
579eace7efcSopenharmony_ci
580eace7efcSopenharmony_civoid JsRuntime::FinishPreload()
581eace7efcSopenharmony_ci{
582eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
583eace7efcSopenharmony_ci    CHECK_POINTER(vm);
584eace7efcSopenharmony_ci    panda::JSNApi::PreFork(vm);
585eace7efcSopenharmony_ci}
586eace7efcSopenharmony_ci
587eace7efcSopenharmony_civoid JsRuntime::PostPreload(const Options& options)
588eace7efcSopenharmony_ci{
589eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
590eace7efcSopenharmony_ci    CHECK_POINTER(vm);
591eace7efcSopenharmony_ci    auto env = GetNapiEnv();
592eace7efcSopenharmony_ci    CHECK_POINTER(env);
593eace7efcSopenharmony_ci    panda::RuntimeOption postOption;
594eace7efcSopenharmony_ci    postOption.SetBundleName(options.bundleName);
595eace7efcSopenharmony_ci    if (!options.arkNativeFilePath.empty()) {
596eace7efcSopenharmony_ci        std::string sandBoxAnFilePath = SANDBOX_ARK_CACHE_PATH + options.arkNativeFilePath;
597eace7efcSopenharmony_ci        postOption.SetAnDir(sandBoxAnFilePath);
598eace7efcSopenharmony_ci    }
599eace7efcSopenharmony_ci    if (options.isMultiThread) {
600eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "Multi-Thread Mode: %{public}d", options.isMultiThread);
601eace7efcSopenharmony_ci        panda::JSNApi::SetMultiThreadCheck();
602eace7efcSopenharmony_ci    }
603eace7efcSopenharmony_ci    if (options.isErrorInfoEnhance) {
604eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "Start Error-Info-Enhance Mode: %{public}d.", options.isErrorInfoEnhance);
605eace7efcSopenharmony_ci        panda::JSNApi::SetErrorInfoEnhance();
606eace7efcSopenharmony_ci    }
607eace7efcSopenharmony_ci    bool profileEnabled = OHOS::system::GetBoolParameter("ark.profile", false);
608eace7efcSopenharmony_ci    postOption.SetEnableProfile(profileEnabled);
609eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "ASMM JIT Verify PostFork, jitEnabled: %{public}d", options.jitEnabled);
610eace7efcSopenharmony_ci    postOption.SetEnableJIT(options.jitEnabled);
611eace7efcSopenharmony_ci    postOption.SetAOTCompileStatusMap(options.aotCompileStatusMap);
612eace7efcSopenharmony_ci    {
613eace7efcSopenharmony_ci        HITRACE_METER_NAME(HITRACE_TAG_APP, "panda::JSNApi::PostFork");
614eace7efcSopenharmony_ci        panda::JSNApi::PostFork(vm, postOption);
615eace7efcSopenharmony_ci    }
616eace7efcSopenharmony_ci    reinterpret_cast<NativeEngine*>(env)->ReinitUVLoop();
617eace7efcSopenharmony_ci    uv_loop_s* loop = nullptr;
618eace7efcSopenharmony_ci    napi_get_uv_event_loop(env, &loop);
619eace7efcSopenharmony_ci    panda::JSNApi::SetLoop(vm, loop);
620eace7efcSopenharmony_ci}
621eace7efcSopenharmony_ci
622eace7efcSopenharmony_civoid JsRuntime::LoadAotFile(const Options& options)
623eace7efcSopenharmony_ci{
624eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
625eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
626eace7efcSopenharmony_ci    CHECK_POINTER(vm);
627eace7efcSopenharmony_ci    if (options.hapPath.empty()) {
628eace7efcSopenharmony_ci        return;
629eace7efcSopenharmony_ci    }
630eace7efcSopenharmony_ci
631eace7efcSopenharmony_ci    bool newCreate = false;
632eace7efcSopenharmony_ci    std::string loadPath = ExtractorUtil::GetLoadFilePath(options.hapPath);
633eace7efcSopenharmony_ci    std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true);
634eace7efcSopenharmony_ci    if (extractor != nullptr && newCreate) {
635eace7efcSopenharmony_ci        panda::JSNApi::LoadAotFile(vm, options.moduleName);
636eace7efcSopenharmony_ci    }
637eace7efcSopenharmony_ci}
638eace7efcSopenharmony_ci
639eace7efcSopenharmony_cibool JsRuntime::Initialize(const Options& options)
640eace7efcSopenharmony_ci{
641eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
642eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
643eace7efcSopenharmony_ci    if (Ace::AceForwardCompatibility::PipelineChanged()) {
644eace7efcSopenharmony_ci        preloaded_ = false;
645eace7efcSopenharmony_ci    }
646eace7efcSopenharmony_ci#endif
647eace7efcSopenharmony_ci    if (!preloaded_) {
648eace7efcSopenharmony_ci        if (!CreateJsEnv(options)) {
649eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "Create jsEnv failed");
650eace7efcSopenharmony_ci            return false;
651eace7efcSopenharmony_ci        }
652eace7efcSopenharmony_ci    }
653eace7efcSopenharmony_ci    apiTargetVersion_ = options.apiTargetVersion;
654eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "Initialize: %{public}d", apiTargetVersion_);
655eace7efcSopenharmony_ci    bool isModular = false;
656eace7efcSopenharmony_ci    if (IsUseAbilityRuntime(options)) {
657eace7efcSopenharmony_ci        auto env = GetNapiEnv();
658eace7efcSopenharmony_ci        auto nativeEngine = reinterpret_cast<NativeEngine*>(env);
659eace7efcSopenharmony_ci        CHECK_POINTER_AND_RETURN(nativeEngine, false);
660eace7efcSopenharmony_ci
661eace7efcSopenharmony_ci        auto vm = GetEcmaVm();
662eace7efcSopenharmony_ci        CHECK_POINTER_AND_RETURN(vm, false);
663eace7efcSopenharmony_ci
664eace7efcSopenharmony_ci        if (preloaded_) {
665eace7efcSopenharmony_ci            PostPreload(options);
666eace7efcSopenharmony_ci        }
667eace7efcSopenharmony_ci        HandleScope handleScope(*this);
668eace7efcSopenharmony_ci        napi_value globalObj = nullptr;
669eace7efcSopenharmony_ci        napi_get_global(env, &globalObj);
670eace7efcSopenharmony_ci        CHECK_POINTER_AND_RETURN(globalObj, false);
671eace7efcSopenharmony_ci
672eace7efcSopenharmony_ci        if (!preloaded_) {
673eace7efcSopenharmony_ci            InitSyscapModule(env);
674eace7efcSopenharmony_ci
675eace7efcSopenharmony_ci            // Simple hook function 'isSystemplugin'
676eace7efcSopenharmony_ci            const char* moduleName = "JsRuntime";
677eace7efcSopenharmony_ci            BindNativeFunction(env, globalObj, "isSystemplugin", moduleName,
678eace7efcSopenharmony_ci                [](napi_env env, napi_callback_info cbinfo) -> napi_value {
679eace7efcSopenharmony_ci                    return CreateJsUndefined(env);
680eace7efcSopenharmony_ci                });
681eace7efcSopenharmony_ci
682eace7efcSopenharmony_ci            napi_value propertyValue = nullptr;
683eace7efcSopenharmony_ci            napi_get_named_property(env, globalObj, "requireNapi", &propertyValue);
684eace7efcSopenharmony_ci            napi_ref tmpRef = nullptr;
685eace7efcSopenharmony_ci            napi_create_reference(env, propertyValue, 1, &tmpRef);
686eace7efcSopenharmony_ci            methodRequireNapiRef_.reset(reinterpret_cast<NativeReference*>(tmpRef));
687eace7efcSopenharmony_ci            if (!methodRequireNapiRef_) {
688eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to create reference for global.requireNapi");
689eace7efcSopenharmony_ci                return false;
690eace7efcSopenharmony_ci            }
691eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::JSRUNTIME, "PreloadAce start");
692eace7efcSopenharmony_ci            PreloadAce(options);
693eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::JSRUNTIME, "PreloadAce end");
694eace7efcSopenharmony_ci            nativeEngine->RegisterPermissionCheck(PermissionCheckFunc);
695eace7efcSopenharmony_ci        }
696eace7efcSopenharmony_ci
697eace7efcSopenharmony_ci        if (!options.preload) {
698eace7efcSopenharmony_ci            isBundle_ = options.isBundle;
699eace7efcSopenharmony_ci            bundleName_ = options.bundleName;
700eace7efcSopenharmony_ci            codePath_ = options.codePath;
701eace7efcSopenharmony_ci            panda::JSNApi::SetSearchHapPathTracker(
702eace7efcSopenharmony_ci                vm, [options](const std::string moduleName, std::string &hapPath) -> bool {
703eace7efcSopenharmony_ci                    if (options.hapModulePath.find(moduleName) == options.hapModulePath.end()) {
704eace7efcSopenharmony_ci                        return false;
705eace7efcSopenharmony_ci                    }
706eace7efcSopenharmony_ci                    hapPath = options.hapModulePath.find(moduleName)->second;
707eace7efcSopenharmony_ci                    return true;
708eace7efcSopenharmony_ci                });
709eace7efcSopenharmony_ci            ReInitJsEnvImpl(options);
710eace7efcSopenharmony_ci            LoadAotFile(options);
711eace7efcSopenharmony_ci            panda::JSNApi::SetBundle(vm, options.isBundle);
712eace7efcSopenharmony_ci            panda::JSNApi::SetBundleName(vm, options.bundleName);
713eace7efcSopenharmony_ci            panda::JSNApi::SetHostResolveBufferTracker(
714eace7efcSopenharmony_ci                vm, JsModuleReader(options.bundleName, options.hapPath, options.isUnique));
715eace7efcSopenharmony_ci            isModular = !panda::JSNApi::IsBundle(vm);
716eace7efcSopenharmony_ci            std::vector<panda::HmsMap> systemKitsMap = GetSystemKitsMap(apiTargetVersion_);
717eace7efcSopenharmony_ci            panda::JSNApi::SetHmsModuleList(vm, systemKitsMap);
718eace7efcSopenharmony_ci            std::map<std::string, std::vector<std::vector<std::string>>> pkgContextInfoMap;
719eace7efcSopenharmony_ci            std::map<std::string, std::string> pkgAliasMap;
720eace7efcSopenharmony_ci            pkgContextInfoJsonStringMap_ = options.pkgContextInfoJsonStringMap;
721eace7efcSopenharmony_ci            packageNameList_ = options.packageNameList;
722eace7efcSopenharmony_ci            JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(
723eace7efcSopenharmony_ci                options.pkgContextInfoJsonStringMap, pkgContextInfoMap, pkgAliasMap);
724eace7efcSopenharmony_ci            panda::JSNApi::SetpkgContextInfoList(vm, pkgContextInfoMap);
725eace7efcSopenharmony_ci            panda::JSNApi::SetPkgAliasList(vm, pkgAliasMap);
726eace7efcSopenharmony_ci            panda::JSNApi::SetPkgNameList(vm, options.packageNameList);
727eace7efcSopenharmony_ci        }
728eace7efcSopenharmony_ci    }
729eace7efcSopenharmony_ci
730eace7efcSopenharmony_ci    if (!preloaded_) {
731eace7efcSopenharmony_ci        InitConsoleModule();
732eace7efcSopenharmony_ci    }
733eace7efcSopenharmony_ci
734eace7efcSopenharmony_ci    if (!options.preload) {
735eace7efcSopenharmony_ci        auto operatorObj = std::make_shared<JsEnv::SourceMapOperator>(options.bundleName, isModular,
736eace7efcSopenharmony_ci                                                                      options.isDebugVersion);
737eace7efcSopenharmony_ci        InitSourceMap(operatorObj);
738eace7efcSopenharmony_ci
739eace7efcSopenharmony_ci        if (options.isUnique) {
740eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::JSRUNTIME, "Not supported TimerModule when form render");
741eace7efcSopenharmony_ci        } else {
742eace7efcSopenharmony_ci            InitTimerModule();
743eace7efcSopenharmony_ci        }
744eace7efcSopenharmony_ci
745eace7efcSopenharmony_ci        InitWorkerModule(options);
746eace7efcSopenharmony_ci        SetModuleLoadChecker(options.moduleCheckerDelegate);
747eace7efcSopenharmony_ci        SetRequestAotCallback();
748eace7efcSopenharmony_ci
749eace7efcSopenharmony_ci        if (!InitLoop(options.isStageModel)) {
750eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "Init loop failed");
751eace7efcSopenharmony_ci            return false;
752eace7efcSopenharmony_ci        }
753eace7efcSopenharmony_ci    }
754eace7efcSopenharmony_ci
755eace7efcSopenharmony_ci    preloaded_ = options.preload;
756eace7efcSopenharmony_ci    return true;
757eace7efcSopenharmony_ci}
758eace7efcSopenharmony_ci
759eace7efcSopenharmony_cibool JsRuntime::CreateJsEnv(const Options& options)
760eace7efcSopenharmony_ci{
761eace7efcSopenharmony_ci    panda::RuntimeOption pandaOption;
762eace7efcSopenharmony_ci    int arkProperties = OHOS::system::GetIntParameter<int>("persist.ark.properties", -1);
763eace7efcSopenharmony_ci    std::string bundleName = OHOS::system::GetParameter("persist.ark.arkbundlename", "");
764eace7efcSopenharmony_ci    std::string memConfigProperty = OHOS::system::GetParameter("persist.ark.mem_config_property", "");
765eace7efcSopenharmony_ci    size_t gcThreadNum = OHOS::system::GetUintParameter<size_t>("persist.ark.gcthreads", 7);
766eace7efcSopenharmony_ci    size_t longPauseTime = OHOS::system::GetUintParameter<size_t>("persist.ark.longpausetime", 40);
767eace7efcSopenharmony_ci    pandaOption.SetArkProperties(arkProperties);
768eace7efcSopenharmony_ci    pandaOption.SetArkBundleName(bundleName);
769eace7efcSopenharmony_ci    pandaOption.SetMemConfigProperty(memConfigProperty);
770eace7efcSopenharmony_ci    pandaOption.SetGcThreadNum(gcThreadNum);
771eace7efcSopenharmony_ci    pandaOption.SetLongPauseTime(longPauseTime);
772eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "ark properties=%{public}d bundlename=%{public}s",
773eace7efcSopenharmony_ci        arkProperties, bundleName.c_str());
774eace7efcSopenharmony_ci    pandaOption.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC);
775eace7efcSopenharmony_ci    pandaOption.SetGcPoolSize(DEFAULT_GC_POOL_SIZE);
776eace7efcSopenharmony_ci    pandaOption.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::FOLLOW);
777eace7efcSopenharmony_ci    pandaOption.SetLogBufPrint(PrintVmLog);
778eace7efcSopenharmony_ci
779eace7efcSopenharmony_ci    bool asmInterpreterEnabled = OHOS::system::GetBoolParameter("persist.ark.asminterpreter", true);
780eace7efcSopenharmony_ci    std::string asmOpcodeDisableRange = OHOS::system::GetParameter("persist.ark.asmopcodedisablerange", "");
781eace7efcSopenharmony_ci    pandaOption.SetEnableAsmInterpreter(asmInterpreterEnabled);
782eace7efcSopenharmony_ci    pandaOption.SetAsmOpcodeDisableRange(asmOpcodeDisableRange);
783eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "ASMM JIT Verify CreateJsEnv, jitEnabled: %{public}d", options.jitEnabled);
784eace7efcSopenharmony_ci    pandaOption.SetEnableJIT(options.jitEnabled);
785eace7efcSopenharmony_ci
786eace7efcSopenharmony_ci    if (options.isMultiThread) {
787eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "Start Multi Thread Mode: %{public}d", options.isMultiThread);
788eace7efcSopenharmony_ci        panda::JSNApi::SetMultiThreadCheck();
789eace7efcSopenharmony_ci    }
790eace7efcSopenharmony_ci
791eace7efcSopenharmony_ci    if (options.isErrorInfoEnhance) {
792eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "Start Error Info Enhance Mode: %{public}d.", options.isErrorInfoEnhance);
793eace7efcSopenharmony_ci        panda::JSNApi::SetErrorInfoEnhance();
794eace7efcSopenharmony_ci    }
795eace7efcSopenharmony_ci
796eace7efcSopenharmony_ci    if (IsUseAbilityRuntime(options)) {
797eace7efcSopenharmony_ci        // aot related
798eace7efcSopenharmony_ci        bool aotEnabled = OHOS::system::GetBoolParameter("persist.ark.aot", true);
799eace7efcSopenharmony_ci        pandaOption.SetEnableAOT(aotEnabled);
800eace7efcSopenharmony_ci        pandaOption.SetProfileDir(SANDBOX_ARK_PROIFILE_PATH);
801eace7efcSopenharmony_ci    }
802eace7efcSopenharmony_ci
803eace7efcSopenharmony_ci    OHOSJsEnvLogger::RegisterJsEnvLogger();
804eace7efcSopenharmony_ci    jsEnv_ = std::make_shared<JsEnv::JsEnvironment>(std::make_unique<OHOSJsEnvironmentImpl>(options.eventRunner));
805eace7efcSopenharmony_ci    if (jsEnv_ == nullptr || !jsEnv_->Initialize(pandaOption, static_cast<void*>(this))) {
806eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Init jsEnv failed");
807eace7efcSopenharmony_ci        return false;
808eace7efcSopenharmony_ci    }
809eace7efcSopenharmony_ci
810eace7efcSopenharmony_ci    return true;
811eace7efcSopenharmony_ci}
812eace7efcSopenharmony_ci
813eace7efcSopenharmony_civoid JsRuntime::PreloadAce(const Options& options)
814eace7efcSopenharmony_ci{
815eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
816eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
817eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
818eace7efcSopenharmony_ci    if (options.loadAce) {
819eace7efcSopenharmony_ci        // ArkTsCard start
820eace7efcSopenharmony_ci        if (options.isUnique) {
821eace7efcSopenharmony_ci            OHOS::Ace::DeclarativeModulePreloader::PreloadCard(
822eace7efcSopenharmony_ci                *nativeEngine, options.bundleName, options.pkgContextInfoJsonStringMap);
823eace7efcSopenharmony_ci        } else {
824eace7efcSopenharmony_ci            OHOS::Ace::DeclarativeModulePreloader::Preload(*nativeEngine);
825eace7efcSopenharmony_ci        }
826eace7efcSopenharmony_ci        // ArkTsCard end
827eace7efcSopenharmony_ci    }
828eace7efcSopenharmony_ci#endif
829eace7efcSopenharmony_ci}
830eace7efcSopenharmony_ci
831eace7efcSopenharmony_civoid JsRuntime::ReloadFormComponent()
832eace7efcSopenharmony_ci{
833eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
834eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
835eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
836eace7efcSopenharmony_ci    // ArkTsCard update condition, need to reload new component
837eace7efcSopenharmony_ci#ifdef SUPPORT_SCREEN
838eace7efcSopenharmony_ci    OHOS::Ace::DeclarativeModulePreloader::ReloadCard(*nativeEngine, bundleName_, pkgContextInfoJsonStringMap_);
839eace7efcSopenharmony_ci#endif
840eace7efcSopenharmony_ci}
841eace7efcSopenharmony_ci
842eace7efcSopenharmony_cibool JsRuntime::InitLoop(bool isStage)
843eace7efcSopenharmony_ci{
844eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
845eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(jsEnv_, false);
846eace7efcSopenharmony_ci    return jsEnv_->InitLoop(isStage);
847eace7efcSopenharmony_ci}
848eace7efcSopenharmony_ci
849eace7efcSopenharmony_civoid JsRuntime::SetAppLibPath(const AppLibPathMap& appLibPaths, const bool& isSystemApp)
850eace7efcSopenharmony_ci{
851eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "Set library path");
852eace7efcSopenharmony_ci
853eace7efcSopenharmony_ci    if (appLibPaths.size() == 0) {
854eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::JSRUNTIME, "no lib path to set");
855eace7efcSopenharmony_ci        return;
856eace7efcSopenharmony_ci    }
857eace7efcSopenharmony_ci
858eace7efcSopenharmony_ci    auto moduleManager = NativeModuleManager::GetInstance();
859eace7efcSopenharmony_ci    if (moduleManager == nullptr) {
860eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null moduleManager");
861eace7efcSopenharmony_ci        return;
862eace7efcSopenharmony_ci    }
863eace7efcSopenharmony_ci
864eace7efcSopenharmony_ci    for (const auto &appLibPath : appLibPaths) {
865eace7efcSopenharmony_ci        moduleManager->SetAppLibPath(appLibPath.first, appLibPath.second, isSystemApp);
866eace7efcSopenharmony_ci    }
867eace7efcSopenharmony_ci}
868eace7efcSopenharmony_ci
869eace7efcSopenharmony_civoid JsRuntime::InitSourceMap(const std::shared_ptr<JsEnv::SourceMapOperator> operatorObj)
870eace7efcSopenharmony_ci{
871eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
872eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
873eace7efcSopenharmony_ci    jsEnv_->InitSourceMap(operatorObj);
874eace7efcSopenharmony_ci    JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData);
875eace7efcSopenharmony_ci    JsEnv::SourceMap::RegisterGetHapPathCallback(JsModuleReader::GetHapPathList);
876eace7efcSopenharmony_ci}
877eace7efcSopenharmony_ci
878eace7efcSopenharmony_civoid JsRuntime::InitSourceMap(const std::string hqfFilePath)
879eace7efcSopenharmony_ci{
880eace7efcSopenharmony_ci    std::string patchSoureMapFile;
881eace7efcSopenharmony_ci    std::vector<uint8_t> soureMapBuffer;
882eace7efcSopenharmony_ci    if (!GetFileBuffer(hqfFilePath, patchSoureMapFile, soureMapBuffer, false)) {
883eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "get patchSoureMap file buffer failed");
884eace7efcSopenharmony_ci        return;
885eace7efcSopenharmony_ci    }
886eace7efcSopenharmony_ci    std::string str(soureMapBuffer.begin(), soureMapBuffer.end());
887eace7efcSopenharmony_ci    auto sourceMapOperator = jsEnv_->GetSourceMapOperator();
888eace7efcSopenharmony_ci    if (sourceMapOperator != nullptr) {
889eace7efcSopenharmony_ci        auto sourceMapObj = sourceMapOperator->GetSourceMapObj();
890eace7efcSopenharmony_ci        if (sourceMapObj != nullptr) {
891eace7efcSopenharmony_ci            sourceMapObj->SplitSourceMap(str);
892eace7efcSopenharmony_ci        }
893eace7efcSopenharmony_ci    }
894eace7efcSopenharmony_ci}
895eace7efcSopenharmony_ci
896eace7efcSopenharmony_civoid JsRuntime::Deinitialize()
897eace7efcSopenharmony_ci{
898eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
899eace7efcSopenharmony_ci    for (auto it = modules_.begin(); it != modules_.end(); it = modules_.erase(it)) {
900eace7efcSopenharmony_ci        delete it->second;
901eace7efcSopenharmony_ci        it->second = nullptr;
902eace7efcSopenharmony_ci    }
903eace7efcSopenharmony_ci
904eace7efcSopenharmony_ci    methodRequireNapiRef_.reset();
905eace7efcSopenharmony_ci
906eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
907eace7efcSopenharmony_ci    jsEnv_->DeInitLoop();
908eace7efcSopenharmony_ci}
909eace7efcSopenharmony_ci
910eace7efcSopenharmony_cinapi_value JsRuntime::LoadJsBundle(const std::string& path, const std::string& hapPath, bool useCommonChunk)
911eace7efcSopenharmony_ci{
912eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
913eace7efcSopenharmony_ci    auto env = GetNapiEnv();
914eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(env, nullptr);
915eace7efcSopenharmony_ci    napi_value globalObj = nullptr;
916eace7efcSopenharmony_ci    napi_get_global(env, &globalObj);
917eace7efcSopenharmony_ci    napi_value exports = nullptr;
918eace7efcSopenharmony_ci    napi_create_object(env, &exports);
919eace7efcSopenharmony_ci    napi_set_named_property(env, globalObj, "exports", exports);
920eace7efcSopenharmony_ci
921eace7efcSopenharmony_ci    if (!RunScript(path, hapPath, useCommonChunk)) {
922eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to run script: %{private}s", path.c_str());
923eace7efcSopenharmony_ci        return nullptr;
924eace7efcSopenharmony_ci    }
925eace7efcSopenharmony_ci
926eace7efcSopenharmony_ci    napi_value exportsObj = nullptr;
927eace7efcSopenharmony_ci    napi_get_named_property(env, globalObj, "exports", &exportsObj);
928eace7efcSopenharmony_ci    if (exportsObj == nullptr) {
929eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to get exports objcect: %{private}s", path.c_str());
930eace7efcSopenharmony_ci        return nullptr;
931eace7efcSopenharmony_ci    }
932eace7efcSopenharmony_ci
933eace7efcSopenharmony_ci    napi_value exportObj = nullptr;
934eace7efcSopenharmony_ci    napi_get_named_property(env, exportsObj, "default", &exportObj);
935eace7efcSopenharmony_ci    if (exportObj == nullptr) {
936eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to get default objcect: %{private}s", path.c_str());
937eace7efcSopenharmony_ci        return nullptr;
938eace7efcSopenharmony_ci    }
939eace7efcSopenharmony_ci
940eace7efcSopenharmony_ci    return exportObj;
941eace7efcSopenharmony_ci}
942eace7efcSopenharmony_ci
943eace7efcSopenharmony_cinapi_value JsRuntime::LoadJsModule(const std::string& path, const std::string& hapPath, const std::string& srcEntrance)
944eace7efcSopenharmony_ci{
945eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
946eace7efcSopenharmony_ci    if (!RunScript(path, hapPath, false, srcEntrance)) {
947eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to run script: %{private}s", path.c_str());
948eace7efcSopenharmony_ci        return nullptr;
949eace7efcSopenharmony_ci    }
950eace7efcSopenharmony_ci
951eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
952eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(vm, nullptr);
953eace7efcSopenharmony_ci    panda::Local<panda::ObjectRef> exportObj;
954eace7efcSopenharmony_ci    if (isOhmUrl_) {
955eace7efcSopenharmony_ci        exportObj = panda::JSNApi::GetExportObjectFromOhmUrl(vm, srcEntrance, "default");
956eace7efcSopenharmony_ci    } else {
957eace7efcSopenharmony_ci        exportObj = panda::JSNApi::GetExportObject(vm, path, "default");
958eace7efcSopenharmony_ci    }
959eace7efcSopenharmony_ci
960eace7efcSopenharmony_ci    if (exportObj->IsNull()) {
961eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Get export object failed");
962eace7efcSopenharmony_ci        return nullptr;
963eace7efcSopenharmony_ci    }
964eace7efcSopenharmony_ci
965eace7efcSopenharmony_ci    auto env = GetNapiEnv();
966eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(env, nullptr);
967eace7efcSopenharmony_ci    return ArkNativeEngine::ArkValueToNapiValue(env, exportObj);
968eace7efcSopenharmony_ci}
969eace7efcSopenharmony_ci
970eace7efcSopenharmony_cistd::unique_ptr<NativeReference> JsRuntime::LoadModule(const std::string& moduleName, const std::string& modulePath,
971eace7efcSopenharmony_ci    const std::string& hapPath, bool esmodule, bool useCommonChunk, const std::string& srcEntrance)
972eace7efcSopenharmony_ci{
973eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
974eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "Load module(%{public}s, %{private}s, %{private}s, %{public}s)",
975eace7efcSopenharmony_ci        moduleName.c_str(), modulePath.c_str(), hapPath.c_str(), esmodule ? "true" : "false");
976eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
977eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(vm, std::unique_ptr<NativeReference>());
978eace7efcSopenharmony_ci    // use for debugger, js engine need to know load module to handle debug event
979eace7efcSopenharmony_ci    panda::JSNApi::NotifyLoadModule(vm);
980eace7efcSopenharmony_ci    auto env = GetNapiEnv();
981eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(env, std::unique_ptr<NativeReference>());
982eace7efcSopenharmony_ci    isOhmUrl_ = panda::JSNApi::IsOhmUrl(srcEntrance);
983eace7efcSopenharmony_ci
984eace7efcSopenharmony_ci    HandleScope handleScope(*this);
985eace7efcSopenharmony_ci
986eace7efcSopenharmony_ci    std::string path = moduleName;
987eace7efcSopenharmony_ci    auto pos = path.find("::");
988eace7efcSopenharmony_ci    if (pos != std::string::npos) {
989eace7efcSopenharmony_ci        path.erase(pos, path.size() - pos);
990eace7efcSopenharmony_ci        moduleName_ = path;
991eace7efcSopenharmony_ci    }
992eace7efcSopenharmony_ci
993eace7efcSopenharmony_ci    napi_value classValue = nullptr;
994eace7efcSopenharmony_ci
995eace7efcSopenharmony_ci    auto it = modules_.find(modulePath);
996eace7efcSopenharmony_ci    if (it != modules_.end()) {
997eace7efcSopenharmony_ci        classValue = it->second->GetNapiValue();
998eace7efcSopenharmony_ci    } else {
999eace7efcSopenharmony_ci        std::string fileName;
1000eace7efcSopenharmony_ci        if (!hapPath.empty()) {
1001eace7efcSopenharmony_ci            fileName.append(codePath_).append(Constants::FILE_SEPARATOR).append(modulePath);
1002eace7efcSopenharmony_ci            std::regex pattern(std::string(Constants::FILE_DOT) + std::string(Constants::FILE_SEPARATOR));
1003eace7efcSopenharmony_ci            fileName = std::regex_replace(fileName, pattern, "");
1004eace7efcSopenharmony_ci        } else {
1005eace7efcSopenharmony_ci            if (!MakeFilePath(codePath_, modulePath, fileName)) {
1006eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to make module file path: %{private}s", modulePath.c_str());
1007eace7efcSopenharmony_ci                return std::unique_ptr<NativeReference>();
1008eace7efcSopenharmony_ci            }
1009eace7efcSopenharmony_ci        }
1010eace7efcSopenharmony_ci        classValue = esmodule ? LoadJsModule(fileName, hapPath, srcEntrance)
1011eace7efcSopenharmony_ci            : LoadJsBundle(fileName, hapPath, useCommonChunk);
1012eace7efcSopenharmony_ci        if (classValue == nullptr) {
1013eace7efcSopenharmony_ci            return std::unique_ptr<NativeReference>();
1014eace7efcSopenharmony_ci        }
1015eace7efcSopenharmony_ci
1016eace7efcSopenharmony_ci        napi_ref tmpRef = nullptr;
1017eace7efcSopenharmony_ci        napi_create_reference(env, classValue, 1, &tmpRef);
1018eace7efcSopenharmony_ci        modules_.emplace(modulePath, reinterpret_cast<NativeReference*>(tmpRef));
1019eace7efcSopenharmony_ci    }
1020eace7efcSopenharmony_ci
1021eace7efcSopenharmony_ci    napi_value instanceValue = nullptr;
1022eace7efcSopenharmony_ci    napi_new_instance(env, classValue, 0, nullptr, &instanceValue);
1023eace7efcSopenharmony_ci    if (instanceValue == nullptr) {
1024eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to create object instance");
1025eace7efcSopenharmony_ci        return std::unique_ptr<NativeReference>();
1026eace7efcSopenharmony_ci    }
1027eace7efcSopenharmony_ci
1028eace7efcSopenharmony_ci    napi_ref resultRef = nullptr;
1029eace7efcSopenharmony_ci    napi_create_reference(env, instanceValue, 1, &resultRef);
1030eace7efcSopenharmony_ci    return std::unique_ptr<NativeReference>(reinterpret_cast<NativeReference*>(resultRef));
1031eace7efcSopenharmony_ci}
1032eace7efcSopenharmony_ci
1033eace7efcSopenharmony_cistd::unique_ptr<NativeReference> JsRuntime::LoadSystemModule(
1034eace7efcSopenharmony_ci    const std::string& moduleName, const napi_value* argv, size_t argc)
1035eace7efcSopenharmony_ci{
1036eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "SystemModule %{public}s", moduleName.c_str());
1037eace7efcSopenharmony_ci    napi_env env = GetNapiEnv();
1038eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(env, std::unique_ptr<NativeReference>());
1039eace7efcSopenharmony_ci
1040eace7efcSopenharmony_ci    HandleScope handleScope(*this);
1041eace7efcSopenharmony_ci
1042eace7efcSopenharmony_ci    napi_value className = nullptr;
1043eace7efcSopenharmony_ci    napi_create_string_utf8(env, moduleName.c_str(), moduleName.length(), &className);
1044eace7efcSopenharmony_ci    napi_value globalObj = nullptr;
1045eace7efcSopenharmony_ci    napi_get_global(env, &globalObj);
1046eace7efcSopenharmony_ci    napi_value refValue = methodRequireNapiRef_->GetNapiValue();
1047eace7efcSopenharmony_ci    napi_value args[1] = { className };
1048eace7efcSopenharmony_ci    napi_value classValue = nullptr;
1049eace7efcSopenharmony_ci    napi_call_function(env, globalObj, refValue, 1, args, &classValue);
1050eace7efcSopenharmony_ci    napi_value instanceValue = nullptr;
1051eace7efcSopenharmony_ci    napi_new_instance(env, classValue, argc, argv, &instanceValue);
1052eace7efcSopenharmony_ci    if (instanceValue == nullptr) {
1053eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to create object instance");
1054eace7efcSopenharmony_ci        return std::unique_ptr<NativeReference>();
1055eace7efcSopenharmony_ci    }
1056eace7efcSopenharmony_ci
1057eace7efcSopenharmony_ci    napi_ref resultRef = nullptr;
1058eace7efcSopenharmony_ci    napi_create_reference(env, instanceValue, 1, &resultRef);
1059eace7efcSopenharmony_ci    return std::unique_ptr<NativeReference>(reinterpret_cast<NativeReference*>(resultRef));
1060eace7efcSopenharmony_ci}
1061eace7efcSopenharmony_ci
1062eace7efcSopenharmony_cibool JsRuntime::RunScript(const std::string& srcPath, const std::string& hapPath, bool useCommonChunk,
1063eace7efcSopenharmony_ci    const std::string& srcEntrance)
1064eace7efcSopenharmony_ci{
1065eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1066eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1067eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(vm, false);
1068eace7efcSopenharmony_ci    std::string commonsPath = std::string(Constants::LOCAL_CODE_PATH) + "/" + moduleName_ + "/ets/commons.abc";
1069eace7efcSopenharmony_ci    std::string vendorsPath = std::string(Constants::LOCAL_CODE_PATH) + "/" + moduleName_ + "/ets/vendors.abc";
1070eace7efcSopenharmony_ci    if (hapPath.empty()) {
1071eace7efcSopenharmony_ci        if (useCommonChunk) {
1072eace7efcSopenharmony_ci            (void)LoadScript(commonsPath);
1073eace7efcSopenharmony_ci            (void)LoadScript(vendorsPath);
1074eace7efcSopenharmony_ci        }
1075eace7efcSopenharmony_ci        return LoadScript(srcPath);
1076eace7efcSopenharmony_ci    }
1077eace7efcSopenharmony_ci
1078eace7efcSopenharmony_ci    bool newCreate = false;
1079eace7efcSopenharmony_ci    std::string loadPath = ExtractorUtil::GetLoadFilePath(hapPath);
1080eace7efcSopenharmony_ci    std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true);
1081eace7efcSopenharmony_ci    if (!extractor) {
1082eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Get extractor failed. hapPath[%{private}s]", hapPath.c_str());
1083eace7efcSopenharmony_ci        return false;
1084eace7efcSopenharmony_ci    }
1085eace7efcSopenharmony_ci    if (newCreate) {
1086eace7efcSopenharmony_ci        panda::JSNApi::LoadAotFile(vm, moduleName_);
1087eace7efcSopenharmony_ci        auto resourceManager = AbilityBase::ExtractResourceManager::GetExtractResourceManager().GetGlobalObject();
1088eace7efcSopenharmony_ci        if (resourceManager) {
1089eace7efcSopenharmony_ci            resourceManager->AddResource(loadPath.c_str());
1090eace7efcSopenharmony_ci        }
1091eace7efcSopenharmony_ci    }
1092eace7efcSopenharmony_ci
1093eace7efcSopenharmony_ci    auto func = [&](std::string modulePath, const std::string abcPath) {
1094eace7efcSopenharmony_ci        bool useSafeMempry = apiTargetVersion_ == 0 || apiTargetVersion_ > API8;
1095eace7efcSopenharmony_ci        if (!extractor->IsHapCompress(modulePath) && useSafeMempry) {
1096eace7efcSopenharmony_ci            auto safeData = extractor->GetSafeData(modulePath);
1097eace7efcSopenharmony_ci            if (!safeData) {
1098eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::JSRUNTIME, "Get safeData abc file failed");
1099eace7efcSopenharmony_ci                return false;
1100eace7efcSopenharmony_ci            }
1101eace7efcSopenharmony_ci            return LoadScript(abcPath, safeData->GetDataPtr(), safeData->GetDataLen(), isBundle_, srcEntrance);
1102eace7efcSopenharmony_ci        } else {
1103eace7efcSopenharmony_ci            std::unique_ptr<uint8_t[]> data;
1104eace7efcSopenharmony_ci            size_t dataLen = 0;
1105eace7efcSopenharmony_ci            if (!extractor->ExtractToBufByName(modulePath, data, dataLen)) {
1106eace7efcSopenharmony_ci                TAG_LOGE(AAFwkTag::JSRUNTIME, "Get File  Buffer abc file failed");
1107eace7efcSopenharmony_ci                return false;
1108eace7efcSopenharmony_ci            }
1109eace7efcSopenharmony_ci            std::vector<uint8_t> buffer;
1110eace7efcSopenharmony_ci            buffer.assign(data.get(), data.get() + dataLen);
1111eace7efcSopenharmony_ci
1112eace7efcSopenharmony_ci            return LoadScript(abcPath, &buffer, isBundle_);
1113eace7efcSopenharmony_ci        }
1114eace7efcSopenharmony_ci    };
1115eace7efcSopenharmony_ci
1116eace7efcSopenharmony_ci    if (useCommonChunk) {
1117eace7efcSopenharmony_ci        (void)func(commonsPath, commonsPath);
1118eace7efcSopenharmony_ci        (void)func(vendorsPath, vendorsPath);
1119eace7efcSopenharmony_ci    }
1120eace7efcSopenharmony_ci
1121eace7efcSopenharmony_ci    std::string path = srcPath;
1122eace7efcSopenharmony_ci    if (!isBundle_) {
1123eace7efcSopenharmony_ci        if (moduleName_.empty()) {
1124eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "moduleName is hole");
1125eace7efcSopenharmony_ci            return false;
1126eace7efcSopenharmony_ci        }
1127eace7efcSopenharmony_ci        path = BUNDLE_INSTALL_PATH + moduleName_ + MERGE_ABC_PATH;
1128eace7efcSopenharmony_ci        panda::JSNApi::SetAssetPath(vm, path);
1129eace7efcSopenharmony_ci        panda::JSNApi::SetModuleName(vm, moduleName_);
1130eace7efcSopenharmony_ci    }
1131eace7efcSopenharmony_ci    return func(path, srcPath);
1132eace7efcSopenharmony_ci}
1133eace7efcSopenharmony_ci
1134eace7efcSopenharmony_cibool JsRuntime::RunSandboxScript(const std::string& path, const std::string& hapPath)
1135eace7efcSopenharmony_ci{
1136eace7efcSopenharmony_ci    std::string fileName;
1137eace7efcSopenharmony_ci    if (!hapPath.empty()) {
1138eace7efcSopenharmony_ci        fileName.append(codePath_).append(Constants::FILE_SEPARATOR).append(path);
1139eace7efcSopenharmony_ci        std::regex pattern(std::string(Constants::FILE_DOT) + std::string(Constants::FILE_SEPARATOR));
1140eace7efcSopenharmony_ci        fileName = std::regex_replace(fileName, pattern, "");
1141eace7efcSopenharmony_ci    } else {
1142eace7efcSopenharmony_ci        if (!MakeFilePath(codePath_, path, fileName)) {
1143eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to make module file path: %{private}s", path.c_str());
1144eace7efcSopenharmony_ci            return false;
1145eace7efcSopenharmony_ci        }
1146eace7efcSopenharmony_ci    }
1147eace7efcSopenharmony_ci
1148eace7efcSopenharmony_ci    if (!RunScript(fileName, hapPath)) {
1149eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "Failed to run script: %{public}s", fileName.c_str());
1150eace7efcSopenharmony_ci        return false;
1151eace7efcSopenharmony_ci    }
1152eace7efcSopenharmony_ci    return true;
1153eace7efcSopenharmony_ci}
1154eace7efcSopenharmony_ci
1155eace7efcSopenharmony_civoid JsRuntime::PostTask(const std::function<void()>& task, const std::string& name, int64_t delayTime)
1156eace7efcSopenharmony_ci{
1157eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1158eace7efcSopenharmony_ci    jsEnv_->PostTask(task, name, delayTime);
1159eace7efcSopenharmony_ci}
1160eace7efcSopenharmony_ci
1161eace7efcSopenharmony_civoid JsRuntime::PostSyncTask(const std::function<void()>& task, const std::string& name)
1162eace7efcSopenharmony_ci{
1163eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1164eace7efcSopenharmony_ci    jsEnv_->PostSyncTask(task, name);
1165eace7efcSopenharmony_ci}
1166eace7efcSopenharmony_ci
1167eace7efcSopenharmony_civoid JsRuntime::RemoveTask(const std::string& name)
1168eace7efcSopenharmony_ci{
1169eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1170eace7efcSopenharmony_ci    jsEnv_->RemoveTask(name);
1171eace7efcSopenharmony_ci}
1172eace7efcSopenharmony_ci
1173eace7efcSopenharmony_civoid JsRuntime::DumpCpuProfile()
1174eace7efcSopenharmony_ci{
1175eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1176eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
1177eace7efcSopenharmony_ci    nativeEngine->DumpCpuProfile();
1178eace7efcSopenharmony_ci}
1179eace7efcSopenharmony_ci
1180eace7efcSopenharmony_civoid JsRuntime::DumpHeapSnapshot(bool isPrivate)
1181eace7efcSopenharmony_ci{
1182eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1183eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
1184eace7efcSopenharmony_ci    nativeEngine->DumpHeapSnapshot(true, DumpFormat::JSON, isPrivate, false);
1185eace7efcSopenharmony_ci}
1186eace7efcSopenharmony_ci
1187eace7efcSopenharmony_civoid JsRuntime::DumpHeapSnapshot(uint32_t tid, bool isFullGC)
1188eace7efcSopenharmony_ci{
1189eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1190eace7efcSopenharmony_ci    CHECK_POINTER(vm);
1191eace7efcSopenharmony_ci    panda::ecmascript::DumpSnapShotOption dumpOption;
1192eace7efcSopenharmony_ci    dumpOption.dumpFormat = panda::ecmascript::DumpFormat::JSON;
1193eace7efcSopenharmony_ci    dumpOption.isVmMode = true;
1194eace7efcSopenharmony_ci    dumpOption.isPrivate = false;
1195eace7efcSopenharmony_ci    dumpOption.captureNumericValue = true;
1196eace7efcSopenharmony_ci    dumpOption.isFullGC = isFullGC;
1197eace7efcSopenharmony_ci    dumpOption.isSync = false;
1198eace7efcSopenharmony_ci    DFXJSNApi::DumpHeapSnapshot(vm, dumpOption, tid);
1199eace7efcSopenharmony_ci}
1200eace7efcSopenharmony_ci
1201eace7efcSopenharmony_civoid JsRuntime::ForceFullGC(uint32_t tid)
1202eace7efcSopenharmony_ci{
1203eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1204eace7efcSopenharmony_ci    CHECK_POINTER(vm);
1205eace7efcSopenharmony_ci    DFXJSNApi::TriggerGC(vm, tid);
1206eace7efcSopenharmony_ci}
1207eace7efcSopenharmony_ci
1208eace7efcSopenharmony_civoid JsRuntime::DestroyHeapProfiler()
1209eace7efcSopenharmony_ci{
1210eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1211eace7efcSopenharmony_ci    jsEnv_->DestroyHeapProfiler();
1212eace7efcSopenharmony_ci}
1213eace7efcSopenharmony_ci
1214eace7efcSopenharmony_civoid JsRuntime::ForceFullGC()
1215eace7efcSopenharmony_ci{
1216eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1217eace7efcSopenharmony_ci    CHECK_POINTER(vm);
1218eace7efcSopenharmony_ci    panda::JSNApi::TriggerGC(vm, panda::ecmascript::GCReason::TRIGGER_BY_ABILITY,
1219eace7efcSopenharmony_ci        panda::JSNApi::TRIGGER_GC_TYPE::FULL_GC);
1220eace7efcSopenharmony_ci}
1221eace7efcSopenharmony_ci
1222eace7efcSopenharmony_civoid JsRuntime::AllowCrossThreadExecution()
1223eace7efcSopenharmony_ci{
1224eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1225eace7efcSopenharmony_ci    CHECK_POINTER(vm);
1226eace7efcSopenharmony_ci    panda::JSNApi::AllowCrossThreadExecution(vm);
1227eace7efcSopenharmony_ci}
1228eace7efcSopenharmony_ci
1229eace7efcSopenharmony_civoid JsRuntime::GetHeapPrepare()
1230eace7efcSopenharmony_ci{
1231eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1232eace7efcSopenharmony_ci    jsEnv_->GetHeapPrepare();
1233eace7efcSopenharmony_ci}
1234eace7efcSopenharmony_ci
1235eace7efcSopenharmony_civoid JsRuntime::NotifyApplicationState(bool isBackground)
1236eace7efcSopenharmony_ci{
1237eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1238eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
1239eace7efcSopenharmony_ci    nativeEngine->NotifyApplicationState(isBackground);
1240eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "isBackground %{public}d", isBackground);
1241eace7efcSopenharmony_ci}
1242eace7efcSopenharmony_ci
1243eace7efcSopenharmony_cibool JsRuntime::SuspendVM(uint32_t tid)
1244eace7efcSopenharmony_ci{
1245eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1246eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(nativeEngine, false);
1247eace7efcSopenharmony_ci    return nativeEngine->SuspendVMById(tid);
1248eace7efcSopenharmony_ci}
1249eace7efcSopenharmony_ci
1250eace7efcSopenharmony_civoid JsRuntime::ResumeVM(uint32_t tid)
1251eace7efcSopenharmony_ci{
1252eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1253eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
1254eace7efcSopenharmony_ci    nativeEngine->ResumeVMById(tid);
1255eace7efcSopenharmony_ci}
1256eace7efcSopenharmony_ci
1257eace7efcSopenharmony_civoid JsRuntime::PreloadSystemModule(const std::string& moduleName)
1258eace7efcSopenharmony_ci{
1259eace7efcSopenharmony_ci    HandleScope handleScope(*this);
1260eace7efcSopenharmony_ci    auto env = GetNapiEnv();
1261eace7efcSopenharmony_ci    CHECK_POINTER(env);
1262eace7efcSopenharmony_ci    napi_value className = nullptr;
1263eace7efcSopenharmony_ci    napi_create_string_utf8(env, moduleName.c_str(), moduleName.length(), &className);
1264eace7efcSopenharmony_ci    napi_value globalObj = nullptr;
1265eace7efcSopenharmony_ci    napi_get_global(env, &globalObj);
1266eace7efcSopenharmony_ci    napi_value refValue = methodRequireNapiRef_->GetNapiValue();
1267eace7efcSopenharmony_ci    napi_value args[1] = { className };
1268eace7efcSopenharmony_ci    napi_call_function(env, globalObj, refValue, 1, args, nullptr);
1269eace7efcSopenharmony_ci}
1270eace7efcSopenharmony_ci
1271eace7efcSopenharmony_ciNativeEngine& JsRuntime::GetNativeEngine() const
1272eace7efcSopenharmony_ci{
1273eace7efcSopenharmony_ci    return *GetNativeEnginePointer();
1274eace7efcSopenharmony_ci}
1275eace7efcSopenharmony_ci
1276eace7efcSopenharmony_cinapi_env JsRuntime::GetNapiEnv() const
1277eace7efcSopenharmony_ci{
1278eace7efcSopenharmony_ci    return reinterpret_cast<napi_env>(GetNativeEnginePointer());
1279eace7efcSopenharmony_ci}
1280eace7efcSopenharmony_ci
1281eace7efcSopenharmony_ciNativeEngine* JsRuntime::GetNativeEnginePointer() const
1282eace7efcSopenharmony_ci{
1283eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(jsEnv_, nullptr);
1284eace7efcSopenharmony_ci    return jsEnv_->GetNativeEngine();
1285eace7efcSopenharmony_ci}
1286eace7efcSopenharmony_ci
1287eace7efcSopenharmony_cipanda::ecmascript::EcmaVM* JsRuntime::GetEcmaVm() const
1288eace7efcSopenharmony_ci{
1289eace7efcSopenharmony_ci    CHECK_POINTER_AND_RETURN(jsEnv_, nullptr);
1290eace7efcSopenharmony_ci    return jsEnv_->GetVM();
1291eace7efcSopenharmony_ci}
1292eace7efcSopenharmony_ci
1293eace7efcSopenharmony_cibool JsRuntime::IsUseAbilityRuntime(const Options& options) const
1294eace7efcSopenharmony_ci{
1295eace7efcSopenharmony_ci    return (options.isStageModel) || (options.isTestFramework);
1296eace7efcSopenharmony_ci}
1297eace7efcSopenharmony_ci
1298eace7efcSopenharmony_civoid JsRuntime::UpdateModuleNameAndAssetPath(const std::string& moduleName)
1299eace7efcSopenharmony_ci{
1300eace7efcSopenharmony_ci    if (isBundle_) {
1301eace7efcSopenharmony_ci        return;
1302eace7efcSopenharmony_ci    }
1303eace7efcSopenharmony_ci
1304eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1305eace7efcSopenharmony_ci    if (!vm || moduleName.empty()) {
1306eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "vm is nullptr or moduleName is empty");
1307eace7efcSopenharmony_ci        return;
1308eace7efcSopenharmony_ci    }
1309eace7efcSopenharmony_ci
1310eace7efcSopenharmony_ci    moduleName_ = moduleName;
1311eace7efcSopenharmony_ci    std::string path = BUNDLE_INSTALL_PATH + moduleName_ + MERGE_ABC_PATH;
1312eace7efcSopenharmony_ci    panda::JSNApi::SetAssetPath(vm, path);
1313eace7efcSopenharmony_ci    panda::JSNApi::SetModuleName(vm, moduleName_);
1314eace7efcSopenharmony_ci}
1315eace7efcSopenharmony_ci
1316eace7efcSopenharmony_civoid JsRuntime::RegisterUncaughtExceptionHandler(const JsEnv::UncaughtExceptionInfo& uncaughtExceptionInfo)
1317eace7efcSopenharmony_ci{
1318eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1319eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1320eace7efcSopenharmony_ci    jsEnv_->RegisterUncaughtExceptionHandler(uncaughtExceptionInfo);
1321eace7efcSopenharmony_ci}
1322eace7efcSopenharmony_ci
1323eace7efcSopenharmony_civoid JsRuntime::RegisterQuickFixQueryFunc(const std::map<std::string, std::string>& moduleAndPath)
1324eace7efcSopenharmony_ci{
1325eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1326eace7efcSopenharmony_ci    auto vm = GetEcmaVm();
1327eace7efcSopenharmony_ci    CHECK_POINTER(vm);
1328eace7efcSopenharmony_ci    for (auto it = moduleAndPath.begin(); it != moduleAndPath.end(); it++) {
1329eace7efcSopenharmony_ci        std::string hqfFile(AbilityBase::GetLoadPath(it->second));
1330eace7efcSopenharmony_ci        InitSourceMap(hqfFile);
1331eace7efcSopenharmony_ci    }
1332eace7efcSopenharmony_ci    panda::JSNApi::RegisterQuickFixQueryFunc(vm, JsQuickfixCallback(moduleAndPath));
1333eace7efcSopenharmony_ci}
1334eace7efcSopenharmony_ci
1335eace7efcSopenharmony_cibool JsRuntime::ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content)
1336eace7efcSopenharmony_ci{
1337eace7efcSopenharmony_ci    // Source map relative path, FA: "/assets/js", Stage: "/ets"
1338eace7efcSopenharmony_ci    if (hapPath.empty()) {
1339eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "empty hapPath");
1340eace7efcSopenharmony_ci        return false;
1341eace7efcSopenharmony_ci    }
1342eace7efcSopenharmony_ci    bool newCreate = false;
1343eace7efcSopenharmony_ci    std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(
1344eace7efcSopenharmony_ci        ExtractorUtil::GetLoadFilePath(hapPath), newCreate);
1345eace7efcSopenharmony_ci    if (extractor == nullptr) {
1346eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "hap's path: %{public}s, get extractor failed", hapPath.c_str());
1347eace7efcSopenharmony_ci        return false;
1348eace7efcSopenharmony_ci    }
1349eace7efcSopenharmony_ci    std::unique_ptr<uint8_t[]> dataPtr = nullptr;
1350eace7efcSopenharmony_ci    size_t len = 0;
1351eace7efcSopenharmony_ci    if (!extractor->ExtractToBufByName(sourceMapPath, dataPtr, len)) {
1352eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME, "can't find source map, and switch to stage model");
1353eace7efcSopenharmony_ci        std::string tempPath = std::regex_replace(sourceMapPath, std::regex("ets"), "assets/js");
1354eace7efcSopenharmony_ci        if (!extractor->ExtractToBufByName(tempPath, dataPtr, len)) {
1355eace7efcSopenharmony_ci            TAG_LOGD(AAFwkTag::JSRUNTIME, "get mergeSourceMapData fileBuffer failed, map path: %{private}s",
1356eace7efcSopenharmony_ci                tempPath.c_str());
1357eace7efcSopenharmony_ci            return false;
1358eace7efcSopenharmony_ci        }
1359eace7efcSopenharmony_ci    }
1360eace7efcSopenharmony_ci    content.assign(dataPtr.get(), dataPtr.get() + len);
1361eace7efcSopenharmony_ci    return true;
1362eace7efcSopenharmony_ci}
1363eace7efcSopenharmony_ci
1364eace7efcSopenharmony_civoid JsRuntime::FreeNativeReference(std::unique_ptr<NativeReference> reference)
1365eace7efcSopenharmony_ci{
1366eace7efcSopenharmony_ci    FreeNativeReference(std::move(reference), nullptr);
1367eace7efcSopenharmony_ci}
1368eace7efcSopenharmony_ci
1369eace7efcSopenharmony_civoid JsRuntime::FreeNativeReference(std::shared_ptr<NativeReference>&& reference)
1370eace7efcSopenharmony_ci{
1371eace7efcSopenharmony_ci    FreeNativeReference(nullptr, std::move(reference));
1372eace7efcSopenharmony_ci}
1373eace7efcSopenharmony_ci
1374eace7efcSopenharmony_cistruct JsNativeReferenceDeleterObject {
1375eace7efcSopenharmony_ci    std::unique_ptr<NativeReference> uniqueNativeRef_ = nullptr;
1376eace7efcSopenharmony_ci    std::shared_ptr<NativeReference> sharedNativeRef_ = nullptr;
1377eace7efcSopenharmony_ci};
1378eace7efcSopenharmony_ci
1379eace7efcSopenharmony_civoid JsRuntime::FreeNativeReference(std::unique_ptr<NativeReference> uniqueNativeRef,
1380eace7efcSopenharmony_ci    std::shared_ptr<NativeReference>&& sharedNativeRef)
1381eace7efcSopenharmony_ci{
1382eace7efcSopenharmony_ci    if (uniqueNativeRef == nullptr && sharedNativeRef == nullptr) {
1383eace7efcSopenharmony_ci        TAG_LOGW(AAFwkTag::JSRUNTIME, "invalid nativeRef");
1384eace7efcSopenharmony_ci        return;
1385eace7efcSopenharmony_ci    }
1386eace7efcSopenharmony_ci
1387eace7efcSopenharmony_ci    auto nativeEngine = GetNativeEnginePointer();
1388eace7efcSopenharmony_ci    CHECK_POINTER(nativeEngine);
1389eace7efcSopenharmony_ci    auto uvLoop = nativeEngine->GetUVLoop();
1390eace7efcSopenharmony_ci    CHECK_POINTER(uvLoop);
1391eace7efcSopenharmony_ci
1392eace7efcSopenharmony_ci    auto work = new (std::nothrow) uv_work_t;
1393eace7efcSopenharmony_ci    if (work == nullptr) {
1394eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null work");
1395eace7efcSopenharmony_ci        return;
1396eace7efcSopenharmony_ci    }
1397eace7efcSopenharmony_ci
1398eace7efcSopenharmony_ci    auto cb = new (std::nothrow) JsNativeReferenceDeleterObject();
1399eace7efcSopenharmony_ci    if (cb == nullptr) {
1400eace7efcSopenharmony_ci        TAG_LOGE(AAFwkTag::JSRUNTIME, "null cb");
1401eace7efcSopenharmony_ci        delete work;
1402eace7efcSopenharmony_ci        work = nullptr;
1403eace7efcSopenharmony_ci        return;
1404eace7efcSopenharmony_ci    }
1405eace7efcSopenharmony_ci
1406eace7efcSopenharmony_ci    if (uniqueNativeRef != nullptr) {
1407eace7efcSopenharmony_ci        cb->uniqueNativeRef_ = std::move(uniqueNativeRef);
1408eace7efcSopenharmony_ci    }
1409eace7efcSopenharmony_ci    if (sharedNativeRef != nullptr) {
1410eace7efcSopenharmony_ci        cb->sharedNativeRef_ = std::move(sharedNativeRef);
1411eace7efcSopenharmony_ci    }
1412eace7efcSopenharmony_ci    work->data = reinterpret_cast<void*>(cb);
1413eace7efcSopenharmony_ci    int ret = uv_queue_work(uvLoop, work, [](uv_work_t *work) {},
1414eace7efcSopenharmony_ci    [](uv_work_t *work, int status) {
1415eace7efcSopenharmony_ci        if (work != nullptr) {
1416eace7efcSopenharmony_ci            if (work->data != nullptr) {
1417eace7efcSopenharmony_ci                delete reinterpret_cast<JsNativeReferenceDeleterObject*>(work->data);
1418eace7efcSopenharmony_ci                work->data = nullptr;
1419eace7efcSopenharmony_ci            }
1420eace7efcSopenharmony_ci            delete work;
1421eace7efcSopenharmony_ci            work = nullptr;
1422eace7efcSopenharmony_ci        }
1423eace7efcSopenharmony_ci    });
1424eace7efcSopenharmony_ci    if (ret != 0) {
1425eace7efcSopenharmony_ci        delete reinterpret_cast<JsNativeReferenceDeleterObject*>(work->data);
1426eace7efcSopenharmony_ci        work->data = nullptr;
1427eace7efcSopenharmony_ci        delete work;
1428eace7efcSopenharmony_ci        work = nullptr;
1429eace7efcSopenharmony_ci    }
1430eace7efcSopenharmony_ci}
1431eace7efcSopenharmony_ci
1432eace7efcSopenharmony_civoid JsRuntime::InitTimerModule()
1433eace7efcSopenharmony_ci{
1434eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1435eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1436eace7efcSopenharmony_ci    jsEnv_->InitTimerModule();
1437eace7efcSopenharmony_ci}
1438eace7efcSopenharmony_ci
1439eace7efcSopenharmony_civoid JsRuntime::InitWorkerModule(const Options& options)
1440eace7efcSopenharmony_ci{
1441eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1442eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1443eace7efcSopenharmony_ci    std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
1444eace7efcSopenharmony_ci    workerInfo->codePath = panda::panda_file::StringPacProtect(options.codePath);
1445eace7efcSopenharmony_ci    workerInfo->isDebugVersion = options.isDebugVersion;
1446eace7efcSopenharmony_ci    workerInfo->isBundle = options.isBundle;
1447eace7efcSopenharmony_ci    workerInfo->packagePathStr = options.packagePathStr;
1448eace7efcSopenharmony_ci    workerInfo->assetBasePathStr = options.assetBasePathStr;
1449eace7efcSopenharmony_ci    workerInfo->hapPath = panda::panda_file::StringPacProtect(options.hapPath);
1450eace7efcSopenharmony_ci    workerInfo->isStageModel = panda::panda_file::BoolPacProtect(options.isStageModel);
1451eace7efcSopenharmony_ci    workerInfo->moduleName = options.moduleName;
1452eace7efcSopenharmony_ci    workerInfo->apiTargetVersion = panda::panda_file::DataProtect(static_cast<uintptr_t>(options.apiTargetVersion));
1453eace7efcSopenharmony_ci    if (options.isJsFramework) {
1454eace7efcSopenharmony_ci        SetJsFramework();
1455eace7efcSopenharmony_ci    }
1456eace7efcSopenharmony_ci    jsEnv_->InitWorkerModule(workerInfo);
1457eace7efcSopenharmony_ci}
1458eace7efcSopenharmony_ci
1459eace7efcSopenharmony_civoid JsRuntime::ReInitJsEnvImpl(const Options& options)
1460eace7efcSopenharmony_ci{
1461eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1462eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1463eace7efcSopenharmony_ci    jsEnv_->ReInitJsEnvImpl(std::make_unique<OHOSJsEnvironmentImpl>(options.eventRunner));
1464eace7efcSopenharmony_ci}
1465eace7efcSopenharmony_ci
1466eace7efcSopenharmony_civoid JsRuntime::SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> moduleCheckerDelegate) const
1467eace7efcSopenharmony_ci{
1468eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1469eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1470eace7efcSopenharmony_ci    jsEnv_->SetModuleLoadChecker(moduleCheckerDelegate);
1471eace7efcSopenharmony_ci}
1472eace7efcSopenharmony_ci
1473eace7efcSopenharmony_civoid JsRuntime::SetRequestAotCallback()
1474eace7efcSopenharmony_ci{
1475eace7efcSopenharmony_ci    HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__);
1476eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1477eace7efcSopenharmony_ci    auto callback = [](const std::string& bundleName, const std::string& moduleName, int32_t triggerMode) -> int32_t {
1478eace7efcSopenharmony_ci        auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1479eace7efcSopenharmony_ci        if (systemAbilityMgr == nullptr) {
1480eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "get SaMgr failed");
1481eace7efcSopenharmony_ci            return ERR_INVALID_VALUE;
1482eace7efcSopenharmony_ci        }
1483eace7efcSopenharmony_ci
1484eace7efcSopenharmony_ci        auto remoteObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
1485eace7efcSopenharmony_ci        if (remoteObj == nullptr) {
1486eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "null remoteObject");
1487eace7efcSopenharmony_ci            return ERR_INVALID_VALUE;
1488eace7efcSopenharmony_ci        }
1489eace7efcSopenharmony_ci
1490eace7efcSopenharmony_ci        auto bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObj);
1491eace7efcSopenharmony_ci        if (bundleMgr == nullptr) {
1492eace7efcSopenharmony_ci            TAG_LOGE(AAFwkTag::JSRUNTIME, "get bms failed");
1493eace7efcSopenharmony_ci            return ERR_INVALID_VALUE;
1494eace7efcSopenharmony_ci        }
1495eace7efcSopenharmony_ci
1496eace7efcSopenharmony_ci        TAG_LOGD(AAFwkTag::JSRUNTIME,
1497eace7efcSopenharmony_ci            "Reset compile status, bundleName: %{public}s, moduleName: %{public}s, triggerMode: %{public}d",
1498eace7efcSopenharmony_ci            bundleName.c_str(), moduleName.c_str(), triggerMode);
1499eace7efcSopenharmony_ci        return bundleMgr->ResetAOTCompileStatus(bundleName, moduleName, triggerMode);
1500eace7efcSopenharmony_ci    };
1501eace7efcSopenharmony_ci
1502eace7efcSopenharmony_ci    jsEnv_->SetRequestAotCallback(callback);
1503eace7efcSopenharmony_ci}
1504eace7efcSopenharmony_ci
1505eace7efcSopenharmony_civoid JsRuntime::SetDeviceDisconnectCallback(const std::function<bool()> &cb)
1506eace7efcSopenharmony_ci{
1507eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
1508eace7efcSopenharmony_ci    CHECK_POINTER(jsEnv_);
1509eace7efcSopenharmony_ci    jsEnv_->SetDeviceDisconnectCallback(cb);
1510eace7efcSopenharmony_ci}
1511eace7efcSopenharmony_ci
1512eace7efcSopenharmony_cistd::string JsRuntime::GetSystemKitPath()
1513eace7efcSopenharmony_ci{
1514eace7efcSopenharmony_ci    char buf[MAX_PATH_LEN] = { 0 };
1515eace7efcSopenharmony_ci    char *configPath = GetOneCfgFile(CONFIG_PATH.c_str(), buf, MAX_PATH_LEN);
1516eace7efcSopenharmony_ci    if (configPath == nullptr || configPath[0] == '\0' || strlen(configPath) > MAX_PATH_LEN) {
1517eace7efcSopenharmony_ci        return SYSTEM_KITS_CONFIG_PATH;
1518eace7efcSopenharmony_ci    }
1519eace7efcSopenharmony_ci    return configPath;
1520eace7efcSopenharmony_ci}
1521eace7efcSopenharmony_ci
1522eace7efcSopenharmony_cistd::vector<panda::HmsMap> JsRuntime::GetSystemKitsMap(uint32_t version)
1523eace7efcSopenharmony_ci{
1524eace7efcSopenharmony_ci    std::vector<panda::HmsMap> systemKitsMap;
1525eace7efcSopenharmony_ci    nlohmann::json jsonBuf;
1526eace7efcSopenharmony_ci    std::string configPath = GetSystemKitPath();
1527eace7efcSopenharmony_ci    if (configPath == "" || access(configPath.c_str(), F_OK) != 0) {
1528eace7efcSopenharmony_ci        return systemKitsMap;
1529eace7efcSopenharmony_ci    }
1530eace7efcSopenharmony_ci
1531eace7efcSopenharmony_ci    std::fstream in;
1532eace7efcSopenharmony_ci    char errBuf[256];
1533eace7efcSopenharmony_ci    errBuf[0] = '\0';
1534eace7efcSopenharmony_ci    in.open(configPath, std::ios_base::in);
1535eace7efcSopenharmony_ci    if (!in.is_open()) {
1536eace7efcSopenharmony_ci        strerror_r(errno, errBuf, sizeof(errBuf));
1537eace7efcSopenharmony_ci        return systemKitsMap;
1538eace7efcSopenharmony_ci    }
1539eace7efcSopenharmony_ci
1540eace7efcSopenharmony_ci    in.seekg(0, std::ios::end);
1541eace7efcSopenharmony_ci    int64_t size = in.tellg();
1542eace7efcSopenharmony_ci    if (size <= 0) {
1543eace7efcSopenharmony_ci        in.close();
1544eace7efcSopenharmony_ci        return systemKitsMap;
1545eace7efcSopenharmony_ci    }
1546eace7efcSopenharmony_ci
1547eace7efcSopenharmony_ci    in.seekg(0, std::ios::beg);
1548eace7efcSopenharmony_ci    jsonBuf = nlohmann::json::parse(in, nullptr, false);
1549eace7efcSopenharmony_ci    in.close();
1550eace7efcSopenharmony_ci    if (jsonBuf.is_discarded()) {
1551eace7efcSopenharmony_ci        return systemKitsMap;
1552eace7efcSopenharmony_ci    }
1553eace7efcSopenharmony_ci
1554eace7efcSopenharmony_ci    if (!jsonBuf.contains(SYSTEM_KITS)) {
1555eace7efcSopenharmony_ci        return systemKitsMap;
1556eace7efcSopenharmony_ci    }
1557eace7efcSopenharmony_ci    for (auto &item : jsonBuf.at(SYSTEM_KITS).items()) {
1558eace7efcSopenharmony_ci        nlohmann::json& jsonObject = item.value();
1559eace7efcSopenharmony_ci        if (!jsonObject.contains(NAMESPACE) || !jsonObject.at(NAMESPACE).is_string() ||
1560eace7efcSopenharmony_ci            !jsonObject.contains(TARGET_OHM) || !jsonObject.at(TARGET_OHM).is_string() ||
1561eace7efcSopenharmony_ci            !jsonObject.contains(SINCE_VERSION) || !jsonObject.at(SINCE_VERSION).is_number()) {
1562eace7efcSopenharmony_ci            continue;
1563eace7efcSopenharmony_ci        }
1564eace7efcSopenharmony_ci        uint32_t sinceVersion = jsonObject.at(SINCE_VERSION).get<uint32_t>();
1565eace7efcSopenharmony_ci        if (version >= sinceVersion) {
1566eace7efcSopenharmony_ci            panda::HmsMap hmsMap = {
1567eace7efcSopenharmony_ci                .originalPath = jsonObject.at(NAMESPACE).get<std::string>(),
1568eace7efcSopenharmony_ci                .targetPath = jsonObject.at(TARGET_OHM).get<std::string>(),
1569eace7efcSopenharmony_ci                .sinceVersion = sinceVersion
1570eace7efcSopenharmony_ci            };
1571eace7efcSopenharmony_ci            systemKitsMap.emplace_back(hmsMap);
1572eace7efcSopenharmony_ci        }
1573eace7efcSopenharmony_ci    }
1574eace7efcSopenharmony_ci    TAG_LOGD(AAFwkTag::JSRUNTIME, "The size of the map is %{public}zu", systemKitsMap.size());
1575eace7efcSopenharmony_ci    return systemKitsMap;
1576eace7efcSopenharmony_ci}
1577eace7efcSopenharmony_ci
1578eace7efcSopenharmony_civoid JsRuntime::UpdatePkgContextInfoJson(std::string moduleName, std::string hapPath, std::string packageName)
1579eace7efcSopenharmony_ci{
1580eace7efcSopenharmony_ci    auto iterator = pkgContextInfoJsonStringMap_.find(moduleName);
1581eace7efcSopenharmony_ci    if (iterator == pkgContextInfoJsonStringMap_.end()) {
1582eace7efcSopenharmony_ci        pkgContextInfoJsonStringMap_[moduleName] = hapPath;
1583eace7efcSopenharmony_ci        packageNameList_[moduleName] = packageName;
1584eace7efcSopenharmony_ci        auto vm = GetEcmaVm();
1585eace7efcSopenharmony_ci        std::map<std::string, std::vector<std::vector<std::string>>> pkgContextInfoMap;
1586eace7efcSopenharmony_ci        std::map<std::string, std::string> pkgAliasMap;
1587eace7efcSopenharmony_ci        JsRuntimeLite::GetInstance().GetPkgContextInfoListMap(
1588eace7efcSopenharmony_ci            pkgContextInfoJsonStringMap_, pkgContextInfoMap, pkgAliasMap);
1589eace7efcSopenharmony_ci        panda::JSNApi::SetpkgContextInfoList(vm, pkgContextInfoMap);
1590eace7efcSopenharmony_ci        panda::JSNApi::SetPkgAliasList(vm, pkgAliasMap);
1591eace7efcSopenharmony_ci        panda::JSNApi::SetPkgNameList(vm, packageNameList_);
1592eace7efcSopenharmony_ci    }
1593eace7efcSopenharmony_ci}
1594eace7efcSopenharmony_ci} // namespace AbilityRuntime
1595eace7efcSopenharmony_ci} // namespace OHOS
1596