1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "js_runtime_lite.h"
17
18 #include <regex>
19
20 #include "bundle_mgr_interface.h"
21 #include "hilog_tag_wrapper.h"
22 #include "iservice_registry.h"
23 #include "js_environment.h"
24 #include "js_module_reader.h"
25 #include "js_worker.h"
26 #include "ohos_js_env_logger.h"
27 #include "ohos_js_environment_impl.h"
28 #include "parameters.h"
29 #include "system_ability_definition.h"
30 #include "native_engine/native_create_env.h"
31
32 using Extractor = OHOS::AbilityBase::Extractor;
33 using ExtractorUtil = OHOS::AbilityBase::ExtractorUtil;
34
35 namespace OHOS {
36 namespace AbilityRuntime {
37 namespace {
38 constexpr int64_t DEFAULT_GC_POOL_SIZE = 0x10000000; // 256MB
39 const std::string SANDBOX_ARK_PROIFILE_PATH = "/data/storage/ark-profile";
40 const std::string PACKAGE_NAME = "packageName";
41 const std::string BUNDLE_NAME = "bundleName";
42 const std::string MODULE_NAME = "moduleName";
43 const std::string VERSION = "version";
44 const std::string ENTRY_PATH = "entryPath";
45 const std::string IS_SO = "isSO";
46 const std::string DEPENDENCY_ALIAS = "dependencyAlias";
PrintVmLog(int32_t,int32_t,const char *,const char *,const char * message)47 int32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char* message)
48 {
49 TAG_LOGI(AAFwkTag::JSRUNTIME, "ArkLog: %{public}s", message);
50 return 0;
51 }
52 }
JsRuntimeLite()53 JsRuntimeLite::JsRuntimeLite()
54 {}
55
~JsRuntimeLite()56 JsRuntimeLite::~JsRuntimeLite()
57 {
58 std::lock_guard<std::mutex> lock(envMutex_);
59 for (auto it : envMap_) {
60 it.second.reset();
61 it.second = nullptr;
62 }
63 envMap_.clear();
64 threadIds_.clear();
65 }
66
GetInstance()67 JsRuntimeLite& JsRuntimeLite::GetInstance()
68 {
69 static JsRuntimeLite jsRuntimeLite;
70 return jsRuntimeLite;
71 }
72
CreateNapiEnv(napi_env * env)73 napi_status CreateNapiEnv(napi_env *env)
74 {
75 TAG_LOGD(AAFwkTag::JSRUNTIME, "Called");
76 if (env == nullptr) {
77 TAG_LOGE(AAFwkTag::JSRUNTIME, "null env");
78 return napi_status::napi_invalid_arg;
79 }
80 auto options = JsRuntimeLite::GetInstance().GetChildOptions();
81 if (options == nullptr) {
82 TAG_LOGE(AAFwkTag::JSRUNTIME, "null options");
83 return napi_status::napi_generic_failure;
84 }
85 std::shared_ptr<OHOS::JsEnv::JsEnvironment> jsEnv = nullptr;
86 auto errCode = JsRuntimeLite::GetInstance().CreateJsEnv(*options, jsEnv);
87 if (errCode != napi_status::napi_ok) {
88 TAG_LOGE(AAFwkTag::JSRUNTIME, "CreateJsEnv failed");
89 return errCode;
90 }
91 *env = reinterpret_cast<napi_env>(jsEnv->GetNativeEngine());
92 if (env == nullptr) {
93 TAG_LOGE(AAFwkTag::JSRUNTIME, "null env");
94 return napi_status::napi_generic_failure;
95 }
96 return JsRuntimeLite::GetInstance().Init(*options, *env);
97 }
98
DestroyNapiEnv(napi_env * env)99 napi_status DestroyNapiEnv(napi_env *env)
100 {
101 TAG_LOGD(AAFwkTag::JSRUNTIME, "Called");
102 if (env == nullptr) {
103 TAG_LOGE(AAFwkTag::JSRUNTIME, "null env");
104 return napi_status::napi_invalid_arg;
105 }
106 auto errCode = JsRuntimeLite::GetInstance().RemoveJsEnv(*env);
107 if (errCode == napi_status::napi_ok) {
108 *env = nullptr;
109 }
110 return errCode;
111 }
112
InitJsRuntimeLite(const Options & options)113 void JsRuntimeLite::InitJsRuntimeLite(const Options& options)
114 {
115 if (options.isUnique) {
116 return;
117 }
118 GetInstance().SetChildOptions(options);
119 NativeCreateEnv::RegCreateNapiEnvCallback(CreateNapiEnv);
120 NativeCreateEnv::RegDestroyNapiEnvCallback(DestroyNapiEnv);
121 }
122
CreateJsEnv(const Options & options,std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)123 napi_status JsRuntimeLite::CreateJsEnv(const Options& options, std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
124 {
125 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
126 panda::RuntimeOption pandaOption;
127 int arkProperties = OHOS::system::GetIntParameter<int>("persist.ark.properties", -1);
128 std::string bundleName = OHOS::system::GetParameter("persist.ark.arkbundlename", "");
129 std::string memConfigProperty = OHOS::system::GetParameter("persist.ark.mem_config_property", "");
130 size_t gcThreadNum = OHOS::system::GetUintParameter<size_t>("persist.ark.gcthreads", 7);
131 size_t longPauseTime = OHOS::system::GetUintParameter<size_t>("persist.ark.longpausetime", 40);
132 pandaOption.SetArkProperties(arkProperties);
133 pandaOption.SetArkBundleName(bundleName);
134 pandaOption.SetMemConfigProperty(memConfigProperty);
135 pandaOption.SetGcThreadNum(gcThreadNum);
136 pandaOption.SetLongPauseTime(longPauseTime);
137 TAG_LOGI(AAFwkTag::JSRUNTIME, "ark properties = %{public}d bundleName = %{public}s",
138 arkProperties, bundleName.c_str());
139 pandaOption.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC);
140 pandaOption.SetGcPoolSize(DEFAULT_GC_POOL_SIZE);
141 pandaOption.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::FOLLOW);
142 pandaOption.SetLogBufPrint(PrintVmLog);
143
144 bool asmInterpreterEnabled = OHOS::system::GetBoolParameter("persist.ark.asminterpreter", true);
145 std::string asmOpcodeDisableRange = OHOS::system::GetParameter("persist.ark.asmopcodedisablerange", "");
146 pandaOption.SetEnableAsmInterpreter(asmInterpreterEnabled);
147 pandaOption.SetAsmOpcodeDisableRange(asmOpcodeDisableRange);
148 pandaOption.SetEnableJIT(options.jitEnabled);
149
150 bool useAbilityRuntime = (options.isStageModel) || (options.isTestFramework);
151 if (useAbilityRuntime) {
152 bool aotEnabled = OHOS::system::GetBoolParameter("persist.ark.aot", true);
153 pandaOption.SetEnableAOT(aotEnabled);
154 pandaOption.SetProfileDir(SANDBOX_ARK_PROIFILE_PATH);
155 }
156
157 OHOSJsEnvLogger::RegisterJsEnvLogger();
158 // options eventRunner is nullptr
159 jsEnv = std::make_shared<JsEnv::JsEnvironment>(std::make_unique<OHOSJsEnvironmentImpl>(options.eventRunner));
160 if (jsEnv == nullptr || !jsEnv->Initialize(pandaOption, static_cast<void*>(this))
161 || jsEnv->GetNativeEngine() == nullptr) {
162 TAG_LOGE(AAFwkTag::JSRUNTIME, "Initialize js environment failed");
163 return napi_status::napi_ok;
164 }
165 jsEnv->GetNativeEngine()->MarkNativeThread();
166 return AddEnv(reinterpret_cast<napi_env>(jsEnv->GetNativeEngine()), jsEnv);
167 }
168
Init(const Options & options,napi_env env)169 napi_status JsRuntimeLite::Init(const Options& options, napi_env env)
170 {
171 auto jsEnv = GetJsEnv(env);
172 if (jsEnv == nullptr) {
173 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
174 return napi_status::napi_generic_failure;
175 }
176
177 auto vm = GetEcmaVm(jsEnv);
178 if (!vm) {
179 TAG_LOGE(AAFwkTag::JSRUNTIME, "null vm");
180 return napi_status::napi_generic_failure;
181 }
182
183 bool isModular = false;
184 if (!options.preload) {
185 LoadAotFile(options, jsEnv);
186 panda::JSNApi::SetBundle(vm, options.isBundle);
187 panda::JSNApi::SetBundleName(vm, options.bundleName);
188 panda::JSNApi::SetHostResolveBufferTracker(
189 vm, JsModuleReader(options.bundleName, options.hapPath, options.isUnique));
190 isModular = !panda::JSNApi::IsBundle(vm);
191 panda::JSNApi::SetSearchHapPathTracker(
192 vm, [options](const std::string moduleName, std::string &hapPath) -> bool {
193 if (options.hapModulePath.find(moduleName) == options.hapModulePath.end()) {
194 return false;
195 }
196 hapPath = options.hapModulePath.find(moduleName)->second;
197 return true;
198 });
199 std::map<std::string, std::vector<std::vector<std::string>>> pkgContextInfoMap;
200 std::map<std::string, std::string> pkgAliasMap;
201 GetPkgContextInfoListMap(options.pkgContextInfoJsonStringMap, pkgContextInfoMap, pkgAliasMap);
202 panda::JSNApi::SetpkgContextInfoList(vm, pkgContextInfoMap);
203 panda::JSNApi::SetPkgAliasList(vm, pkgAliasMap);
204 panda::JSNApi::SetPkgNameList(vm, options.packageNameList);
205 }
206
207 if (!preloaded_) {
208 InitConsoleModule(jsEnv);
209 }
210
211 if (!options.preload) {
212 if (!options.isUnique) {
213 InitTimerModule(jsEnv);
214 }
215 InitWorkerModule(options, jsEnv);
216 SetModuleLoadChecker(options.moduleCheckerDelegate, jsEnv);
217 SetRequestAotCallback(jsEnv);
218
219 if (!InitLoop(jsEnv)) {
220 TAG_LOGE(AAFwkTag::JSRUNTIME, "Init loop failed");
221 return napi_status::napi_generic_failure;
222 }
223 }
224
225 preloaded_ = options.preload;
226 return napi_status::napi_ok;
227 }
228
AddEnv(napi_env env,std::shared_ptr<JsEnv::JsEnvironment> jsEnv)229 napi_status JsRuntimeLite::AddEnv(napi_env env, std::shared_ptr<JsEnv::JsEnvironment> jsEnv)
230 {
231 std::lock_guard<std::mutex> lock(envMutex_);
232 pid_t threadId = gettid();
233 if (threadIds_.find(threadId) != threadIds_.end()) {
234 TAG_LOGE(AAFwkTag::JSRUNTIME, "already created");
235 return napi_status::napi_create_ark_runtime_only_one_env_per_thread;
236 }
237 threadIds_.insert(threadId);
238 TAG_LOGD(AAFwkTag::JSRUNTIME, "add threadId %{public}d", threadId);
239 auto it = envMap_.find(env);
240 if (it == envMap_.end()) {
241 envMap_[env] = jsEnv;
242 return napi_status::napi_ok;
243 }
244 return napi_status::napi_generic_failure;
245 }
246
RemoveJsEnv(napi_env env)247 napi_status JsRuntimeLite::RemoveJsEnv(napi_env env)
248 {
249 std::lock_guard<std::mutex> lock(envMutex_);
250 pid_t threadId = gettid();
251 TAG_LOGD(AAFwkTag::JSRUNTIME, "remove threadId %{public}d", threadId);
252 threadIds_.erase(threadId);
253 auto it = envMap_.find(env);
254 if (it != envMap_.end()) {
255 it->second.reset();
256 it->second = nullptr;
257 envMap_.erase(env);
258 return napi_status::napi_ok;
259 }
260 return napi_status::napi_destroy_ark_runtime_env_not_exist;
261 }
262
GetEcmaVm(const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv) const263 panda::ecmascript::EcmaVM* JsRuntimeLite::GetEcmaVm(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv) const
264 {
265 if (jsEnv == nullptr) {
266 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
267 return nullptr;
268 }
269 return jsEnv->GetVM();
270 }
271
GetJsEnv(napi_env env)272 std::shared_ptr<JsEnv::JsEnvironment> JsRuntimeLite::GetJsEnv(napi_env env)
273 {
274 std::lock_guard<std::mutex> lock(envMutex_);
275 auto jsEnv = envMap_.find(env);
276 if (jsEnv != envMap_.end()) {
277 return jsEnv->second;
278 }
279 return nullptr;
280 }
281
LoadAotFile(const Options & options,const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)282 void JsRuntimeLite::LoadAotFile(const Options& options, const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
283 {
284 auto vm = GetEcmaVm(jsEnv);
285 if (!vm || options.hapPath.empty()) {
286 return;
287 }
288
289 bool newCreate = false;
290 std::string loadPath = ExtractorUtil::GetLoadFilePath(options.hapPath);
291 std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true);
292 if (extractor != nullptr && newCreate) {
293 panda::JSNApi::LoadAotFile(vm, options.moduleName);
294 }
295 }
296
InitConsoleModule(const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)297 void JsRuntimeLite::InitConsoleModule(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
298 {
299 if (jsEnv == nullptr) {
300 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
301 return;
302 }
303 jsEnv->InitConsoleModule();
304 }
305
InitTimerModule(const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)306 void JsRuntimeLite::InitTimerModule(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
307 {
308 if (jsEnv == nullptr) {
309 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
310 return;
311 }
312 jsEnv->InitTimerModule();
313 }
314
SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate> & moduleCheckerDelegate,const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)315 void JsRuntimeLite::SetModuleLoadChecker(const std::shared_ptr<ModuleCheckerDelegate>& moduleCheckerDelegate,
316 const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
317 {
318 if (jsEnv == nullptr) {
319 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
320 return;
321 }
322 jsEnv->SetModuleLoadChecker(moduleCheckerDelegate);
323 }
324
SetRequestAotCallback(const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)325 void JsRuntimeLite::SetRequestAotCallback(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
326 {
327 if (jsEnv == nullptr) {
328 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
329 return;
330 }
331 auto callback = [](const std::string& bundleName, const std::string& moduleName, int32_t triggerMode) -> int32_t {
332 auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
333 if (systemAbilityMgr == nullptr) {
334 TAG_LOGE(AAFwkTag::JSRUNTIME, "null SaMgr");
335 return ERR_INVALID_VALUE;
336 }
337
338 auto remoteObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
339 if (remoteObj == nullptr) {
340 TAG_LOGE(AAFwkTag::JSRUNTIME, "null remoteObj");
341 return ERR_INVALID_VALUE;
342 }
343
344 auto bundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObj);
345 if (bundleMgr == nullptr) {
346 TAG_LOGE(AAFwkTag::JSRUNTIME, "null bms");
347 return ERR_INVALID_VALUE;
348 }
349
350 TAG_LOGD(AAFwkTag::JSRUNTIME,
351 "Reset compile status, bundleName: %{public}s, moduleName: %{public}s, triggerMode: %{public}d",
352 bundleName.c_str(), moduleName.c_str(), triggerMode);
353 return bundleMgr->ResetAOTCompileStatus(bundleName, moduleName, triggerMode);
354 };
355
356 jsEnv->SetRequestAotCallback(callback);
357 }
358
InitLoop(const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)359 bool JsRuntimeLite::InitLoop(const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
360 {
361 if (jsEnv == nullptr) {
362 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
363 return false;
364 }
365 return jsEnv->InitLoop();
366 }
367
InitWorkerModule(const Options & options,const std::shared_ptr<JsEnv::JsEnvironment> & jsEnv)368 void JsRuntimeLite::InitWorkerModule(const Options& options, const std::shared_ptr<JsEnv::JsEnvironment>& jsEnv)
369 {
370 if (jsEnv == nullptr) {
371 TAG_LOGE(AAFwkTag::JSRUNTIME, "null jsEnv");
372 return;
373 }
374
375 std::shared_ptr<JsEnv::WorkerInfo> workerInfo = std::make_shared<JsEnv::WorkerInfo>();
376 workerInfo->codePath = panda::panda_file::StringPacProtect(options.codePath);
377 workerInfo->isDebugVersion = options.isDebugVersion;
378 workerInfo->isBundle = options.isBundle;
379 workerInfo->packagePathStr = options.packagePathStr;
380 workerInfo->assetBasePathStr = options.assetBasePathStr;
381 workerInfo->hapPath = panda::panda_file::StringPacProtect(options.hapPath);
382 workerInfo->isStageModel = panda::panda_file::BoolPacProtect(options.isStageModel);
383 workerInfo->moduleName = options.moduleName;
384 if (options.isJsFramework) {
385 SetJsFramework();
386 }
387 jsEnv->InitWorkerModule(workerInfo);
388 }
389
SetChildOptions(const Options & options)390 void JsRuntimeLite::SetChildOptions(const Options& options)
391 {
392 std::lock_guard<std::mutex> lock(childOptionsMutex_);
393 if (childOptions_ == nullptr) {
394 childOptions_ = std::make_shared<Options>();
395 }
396 childOptions_->lang = options.lang;
397 childOptions_->bundleName = options.bundleName;
398 childOptions_->moduleName = options.moduleName;
399 childOptions_->codePath = options.codePath;
400 childOptions_->bundleCodeDir = options.bundleCodeDir;
401 childOptions_->hapPath = options.hapPath;
402 childOptions_->arkNativeFilePath = options.arkNativeFilePath;
403 childOptions_->hapModulePath = options.hapModulePath;
404 childOptions_->loadAce = options.loadAce;
405 childOptions_->preload = options.preload;
406 childOptions_->isBundle = options.isBundle;
407 childOptions_->isDebugVersion = options.isDebugVersion;
408 childOptions_->isJsFramework = options.isJsFramework;
409 childOptions_->isStageModel = options.isStageModel;
410 childOptions_->isTestFramework = options.isTestFramework;
411 childOptions_->uid = options.uid;
412 childOptions_->isUnique = options.isUnique;
413 childOptions_->moduleCheckerDelegate = options.moduleCheckerDelegate;
414 childOptions_->apiTargetVersion = options.apiTargetVersion;
415 childOptions_->packagePathStr = options.packagePathStr;
416 childOptions_->assetBasePathStr = options.assetBasePathStr;
417 childOptions_->jitEnabled = options.jitEnabled;
418 childOptions_->pkgContextInfoJsonStringMap = options.pkgContextInfoJsonStringMap;
419 childOptions_->packageNameList = options.packageNameList;
420 }
421
GetChildOptions()422 std::shared_ptr<Options> JsRuntimeLite::GetChildOptions()
423 {
424 std::lock_guard<std::mutex> lock(childOptionsMutex_);
425 TAG_LOGD(AAFwkTag::JSRUNTIME, "called");
426 return childOptions_;
427 }
428
GetPkgContextInfoListMap(const std::map<std::string,std::string> & contextInfoMap,std::map<std::string,std::vector<std::vector<std::string>>> & pkgContextInfoMap,std::map<std::string,std::string> & pkgAliasMap)429 void JsRuntimeLite::GetPkgContextInfoListMap(const std::map<std::string, std::string> &contextInfoMap,
430 std::map<std::string, std::vector<std::vector<std::string>>> &pkgContextInfoMap,
431 std::map<std::string, std::string> &pkgAliasMap)
432 {
433 for (auto it = contextInfoMap.begin(); it != contextInfoMap.end(); it++) {
434 std::vector<std::vector<std::string>> pkgContextInfoList;
435 std::string filePath = it->second;
436 bool newCreate = false;
437 std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(
438 ExtractorUtil::GetLoadFilePath(filePath), newCreate, false);
439 if (!extractor) {
440 TAG_LOGE(AAFwkTag::JSRUNTIME, "moduleName: %{public}s load hapPath failed", it->first.c_str());
441 continue;
442 }
443 std::unique_ptr<uint8_t[]> data;
444 size_t dataLen = 0;
445 if (!extractor->ExtractToBufByName("pkgContextInfo.json", data, dataLen)) {
446 TAG_LOGD(AAFwkTag::JSRUNTIME, "moduleName: %{public}s get pkgContextInfo failed", it->first.c_str());
447 continue;
448 }
449 auto jsonObject = nlohmann::json::parse(data.get(), data.get() + dataLen, nullptr, false);
450 if (jsonObject.is_discarded()) {
451 TAG_LOGE(AAFwkTag::JSRUNTIME, "moduleName: %{public}s parse json error", it->first.c_str());
452 continue;
453 }
454 ParsePkgContextInfoJson(jsonObject, pkgContextInfoList, pkgAliasMap);
455 TAG_LOGI(AAFwkTag::JSRUNTIME, "moduleName: %{public}s parse json success", it->first.c_str());
456 pkgContextInfoMap[it->first] = pkgContextInfoList;
457 }
458 }
459
ParsePkgContextInfoJson(nlohmann::json & jsonObject,std::vector<std::vector<std::string>> & pkgContextInfoList,std::map<std::string,std::string> & pkgAliasMap)460 void JsRuntimeLite::ParsePkgContextInfoJson(nlohmann::json &jsonObject,
461 std::vector<std::vector<std::string>> &pkgContextInfoList, std::map<std::string, std::string> &pkgAliasMap)
462 {
463 for (nlohmann::json::iterator jsonIt = jsonObject.begin(); jsonIt != jsonObject.end(); jsonIt++) {
464 std::vector<std::string> items;
465 items.emplace_back(jsonIt.key());
466 nlohmann::json itemObject = jsonIt.value();
467 std::string pkgName = "";
468 items.emplace_back(PACKAGE_NAME);
469 if (itemObject[PACKAGE_NAME].is_null() || !itemObject[PACKAGE_NAME].is_string()) {
470 items.emplace_back(pkgName);
471 } else {
472 pkgName = itemObject[PACKAGE_NAME].get<std::string>();
473 items.emplace_back(pkgName);
474 }
475
476 ParsePkgContextInfoJsonString(itemObject, BUNDLE_NAME, items);
477 ParsePkgContextInfoJsonString(itemObject, MODULE_NAME, items);
478 ParsePkgContextInfoJsonString(itemObject, VERSION, items);
479 ParsePkgContextInfoJsonString(itemObject, ENTRY_PATH, items);
480 items.emplace_back(IS_SO);
481 if (itemObject[IS_SO].is_null() || !itemObject[IS_SO].is_boolean()) {
482 items.emplace_back("false");
483 } else {
484 bool isSo = itemObject[IS_SO].get<bool>();
485 if (isSo) {
486 items.emplace_back("true");
487 } else {
488 items.emplace_back("false");
489 }
490 }
491 if (!itemObject[DEPENDENCY_ALIAS].is_null() && itemObject[DEPENDENCY_ALIAS].is_string()) {
492 std::string pkgAlias = itemObject[DEPENDENCY_ALIAS].get<std::string>();
493 if (!pkgAlias.empty()) {
494 pkgAliasMap[pkgAlias] = pkgName;
495 }
496 }
497 pkgContextInfoList.emplace_back(items);
498 }
499 }
500
ParsePkgContextInfoJsonString(const nlohmann::json & itemObject,const std::string & key,std::vector<std::string> & items)501 void JsRuntimeLite::ParsePkgContextInfoJsonString(
502 const nlohmann::json &itemObject, const std::string &key, std::vector<std::string> &items)
503 {
504 items.emplace_back(key);
505 if (itemObject[key].is_null() || !itemObject[key].is_string()) {
506 items.emplace_back("");
507 } else {
508 items.emplace_back(itemObject[key].get<std::string>());
509 }
510 }
511 } // namespace AbilityRuntime
512 } // namespace OHOS