diff --git a/frameworks/ets/ani/native_constructor/context_native_constructor.cpp b/frameworks/ets/ani/native_constructor/context_native_constructor.cpp index b402dad70d58335e188783889c763d4955ab3219..df0ee9b773b51aae03296d5f5b2f48a2fd8eccee 100644 --- a/frameworks/ets/ani/native_constructor/context_native_constructor.cpp +++ b/frameworks/ets/ani/native_constructor/context_native_constructor.cpp @@ -12,9 +12,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + #include #include -#include "hilog_tag_wrapper.h" namespace OHOS { namespace AbilityRuntime { @@ -39,18 +40,15 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) { ani_env *env; if (vm == nullptr || result == nullptr) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Illegal VM or result"); return ANI_ERROR; } if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Unsupported ANI_VERSION_1"); return ANI_ERROR; } // class Context ani_class contextClass; static const char *contextClassName = "Lapplication/Context/Context;"; if (ANI_OK != env->FindClass(contextClassName, &contextClass)) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Not found class %{public}s.", contextClassName); return ANI_NOT_FOUND; } std::array classMethods_context = { @@ -58,14 +56,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) }; if (ANI_OK != env->Class_BindNativeMethods(contextClass, classMethods_context.data(), classMethods_context.size())) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Cannot bind native ctor to class %{public}s.", contextClassName); return ANI_ERROR; }; // class ExtensionContext ani_class extensionContextClass; static const char *extensionContextClassName = "Lapplication/ExtensionContext/ExtensionContext;"; if (ANI_OK != env->FindClass(extensionContextClassName, &extensionContextClass)) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Not found class %{public}s.", extensionContextClassName); return ANI_NOT_FOUND; } std::array classMethods_extensionContext = { @@ -73,14 +69,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) }; if (ANI_OK != env->Class_BindNativeMethods(extensionContextClass, classMethods_extensionContext.data(), classMethods_extensionContext.size())) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Cannot bind native ctor to class %{public}s.", extensionContextClassName); return ANI_ERROR; }; // class UIAbilityContext ani_class uiAbilityClass; static const char *uiAbilityClassName = "Lapplication/UIAbilityContext/UIAbilityContext;"; if (ANI_OK != env->FindClass(uiAbilityClassName, &uiAbilityClass)) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Not found class %{public}s.", uiAbilityClassName); return ANI_NOT_FOUND; } std::array classMethods_uiAbility = { @@ -88,14 +82,12 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) }; if (ANI_OK != env->Class_BindNativeMethods(uiAbilityClass, classMethods_uiAbility.data(), classMethods_uiAbility.size())) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Cannot bind native ctor to class %{public}s.", uiAbilityClassName); return ANI_ERROR; }; // class AbilityStageContext ani_class abilityStageContextClass; static const char *abilityStageContextClassName = "Lapplication/AbilityStageContext/AbilityStageContext;"; if (ANI_OK != env->FindClass(abilityStageContextClassName, &abilityStageContextClass)) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Not found class %{public}s.", abilityStageContextClassName); return ANI_NOT_FOUND; } std::array classMethods_abilityStage = { @@ -103,7 +95,6 @@ ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) }; if (ANI_OK != env->Class_BindNativeMethods(abilityStageContextClass, classMethods_abilityStage.data(), classMethods_abilityStage.size())) { - TAG_LOGE(AAFwkTag::STSRUNTIME, "Cannot bind native ctor to class %{public}s.", abilityStageContextClassName); return ANI_ERROR; }; diff --git a/frameworks/simulator/BUILD.gn b/frameworks/simulator/BUILD.gn index fe4510fae1e6e38952fe7676fdff55994d69f75f..ef4dc70c41b77a9269b4e59eda791f69596dcce6 100644 --- a/frameworks/simulator/BUILD.gn +++ b/frameworks/simulator/BUILD.gn @@ -16,8 +16,10 @@ import("//foundation/ability/ability_runtime/ability_runtime.gni") if (is_mac) { dylib_suffix = ".dylib" -} else { +} else if (is_mingw) { dylib_suffix = ".dll" +} else if (is_linux) { + dylib_suffix = ".so" } not_needed("*", [ "dylib_suffix" ]) diff --git a/frameworks/simulator/ability_simulator/BUILD.gn b/frameworks/simulator/ability_simulator/BUILD.gn index a4111ede3463af8d0bba32763eff0113ad82c389..f4c65908886218d5d99cac97ff1567895020369b 100644 --- a/frameworks/simulator/ability_simulator/BUILD.gn +++ b/frameworks/simulator/ability_simulator/BUILD.gn @@ -20,8 +20,33 @@ config("ability_simulator_public_config") { cflags_cc = [ "-Wno-unused-variable" ] } +ohos_shared_library("simulator_context_ani") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + sources = [ + "${ability_runtime_path}/frameworks/ets/ani/native_constructor/context_native_constructor.cpp", + ] + + include_dirs = [ + "${ability_runtime_services_path}/common/include", + ] + + external_deps = [ + "runtime_core:ani", + ] + subsystem_name = "ability" + part_name = "ability_runtime" + output_extension = "so" +} + ohos_shared_library("ability_simulator_inner") { - if (is_mingw || is_mac) { + if (is_mingw || is_mac || is_linux) { if (is_mingw) { defines = [ "WINDOWS_PLATFORM", @@ -43,8 +68,15 @@ ohos_shared_library("ability_simulator_inner") { "${windowmanager_path}/interfaces/innerkits/wm", "${windowmanager_path}/interfaces/kits/napi/window_runtime/window_stage_napi", "include/bundle_parser", + "${resource_management_path}/interfaces/inner_api/include", ] + if (is_linux) { + include_dirs += [ + "${windowmanager_path}/interfaces/kits/ani/window_runtime/window_stage_ani/include/", + ] + } + sources = [ "${ability_runtime_native_path}/ability/native/ability_business_error/ability_business_error.cpp", "${ability_runtime_native_path}/runtime/js_module_searcher.cpp", @@ -72,6 +104,12 @@ ohos_shared_library("ability_simulator_inner") { "src/simulator.cpp", ] + if (is_linux) { + sources += [ + "src/dynamic_loader_linux.cpp", + ] + } + public_configs = [ ":ability_simulator_public_config", "${simulator_path}/common:ability_simulator_common_config", @@ -101,8 +139,10 @@ ohos_shared_library("ability_simulator_inner") { if (is_mingw) { external_deps += [ "resource_management:win_resmgr" ] - } else { + } else if (is_mac) { external_deps += [ "resource_management:mac_resmgr" ] + } else if (is_linux) { + external_deps += [ "resource_management:linux_resmgr" ] } } @@ -113,7 +153,10 @@ ohos_shared_library("ability_simulator_inner") { } group("ability_simulator") { - if (is_mingw || is_mac) { + if (is_mingw || is_mac || is_linux) { public_deps = [ ":ability_simulator_inner" ] } + if (is_linux) { + deps = [ ":simulator_context_ani" ] + } } diff --git a/frameworks/simulator/ability_simulator/include/bundle_parser/inner_bundle_info.h b/frameworks/simulator/ability_simulator/include/bundle_parser/inner_bundle_info.h index 2f8dead654c209f7996d6747a3a291fe1c79001b..27382476732c4142a9ce8eab2dea4eee93c62006 100644 --- a/frameworks/simulator/ability_simulator/include/bundle_parser/inner_bundle_info.h +++ b/frameworks/simulator/ability_simulator/include/bundle_parser/inner_bundle_info.h @@ -16,6 +16,8 @@ #ifndef OHOS_ABILITY_RUNTIME_SIMULATOR_INNER_BUNDLE_INFO_H #define OHOS_ABILITY_RUNTIME_SIMULATOR_INNER_BUNDLE_INFO_H +#include + #include "nocopyable.h" #include "ability_info.h" #include "bundle_constants.h" @@ -94,6 +96,7 @@ struct InnerModuleInfo { bool compressNativeLibs = true; std::vector nativeLibraryFileNames; AOTCompileStatus aotCompileStatus = AOTCompileStatus::NOT_COMPILED; + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; class InnerBundleInfo { diff --git a/frameworks/simulator/ability_simulator/include/dynamic_loader.h b/frameworks/simulator/ability_simulator/include/dynamic_loader.h new file mode 100644 index 0000000000000000000000000000000000000000..1e25e0af937322ae97e916f4d72ab4158a8e74d0 --- /dev/null +++ b/frameworks/simulator/ability_simulator/include/dynamic_loader.h @@ -0,0 +1,31 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef OHOS_ABILITY_RUNTIME_DYNAMIC_LOADER_H +#define OHOS_ABILITY_RUNTIME_DYNAMIC_LOADER_H + +#if defined(LINUX_PLATFORM) +#include +#endif + +extern "C" { +void* DynamicLoadLibrary(const char* dlPath, unsigned int mode); +void* DynamicFindSymbol(void* so, const char* symbol); +const char* DynamicGetError(); +void DynamicFreeLibrary(void* so); +void DynamicInitNamespace(void* parent, const char* entries, const char* name); +}; + +#endif //OHOS_ABILITY_RUNTIME_DYNAMIC_LOADER_H diff --git a/frameworks/simulator/ability_simulator/include/simulator.h b/frameworks/simulator/ability_simulator/include/simulator.h index 3b33d633eb76c7fc32917af50bc0b220d5266c95..027f8137e3382be62feb03c1b8e5495940208f5e 100644 --- a/frameworks/simulator/ability_simulator/include/simulator.h +++ b/frameworks/simulator/ability_simulator/include/simulator.h @@ -21,6 +21,10 @@ #include #include +#if defined(LINUX_PLATFORM) +#include "ani.h" +#include "window_scene.h" +#endif #include "configuration.h" #include "options.h" @@ -53,6 +57,12 @@ public: virtual void UpdateConfiguration(const AppExecFwk::Configuration &config) = 0; virtual void SetMockList(const std::map &mockList) = 0; virtual void SetHostResolveBufferTracker(ResolveBufferTrackerCallback cb) = 0; + virtual std::string GetCodeLanguage() = 0; +private: +#if defined(LINUX_PLATFORM) + virtual ani_object GetAniObj() { return nullptr; } + virtual std::shared_ptr GetWindowScene() { return nullptr; } +#endif // LINUX_PLATFORM }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/ability_info.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/ability_info.cpp index 07e3bfd9177a87c881213b75daf1390f17c8bbff..880cf9edea438ef5a9b4babbf5fcc9db495bafc1 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/ability_info.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/ability_info.cpp @@ -199,6 +199,7 @@ void to_json(nlohmann::json &jsonObject, const AbilityInfo &abilityInfo) {JSON_KEY_RECOVERABLE, abilityInfo.recoverable}, {JSON_KEY_SUPPORT_EXT_NAMES, abilityInfo.supportExtNames}, {JSON_KEY_SUPPORT_MIME_TYPES, abilityInfo.supportMimeTypes}, + {Constants::CODE_LANGUAGE, abilityInfo.codeLanguage}, }; if (abilityInfo.maxWindowRatio == 0) { // maxWindowRatio in json string will be 0 instead of 0.0 @@ -849,6 +850,14 @@ void from_json(const nlohmann::json &jsonObject, AbilityInfo &abilityInfo) false, parseResult, ArrayType::STRING); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + Constants::CODE_LANGUAGE, + abilityInfo.codeLanguage, + JsonType::STRING, + false, + parseResult, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITY_SIM, "AbilityInfo from_json error:%{public}d", parseResult); } diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/application_info.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/application_info.cpp index 4cff547349f7f97d1c0d4ea3291069419084d16f..3f63b543633e4f0cf8f7395c30922f240650a509 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/application_info.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/application_info.cpp @@ -233,6 +233,7 @@ void to_json(nlohmann::json &jsonObject, const ApplicationInfo &applicationInfo) {APPLICATION_COMPILE_SDK_VERSION, applicationInfo.compileSdkVersion}, {APPLICATION_COMPILE_SDK_TYPE, applicationInfo.compileSdkType}, {APPLICATION_RESOURCES_APPLY, applicationInfo.resourcesApply}, + {Constants::CODE_LANGUAGE, applicationInfo.codeLanguage}, }; } @@ -864,6 +865,14 @@ void from_json(const nlohmann::json &jsonObject, ApplicationInfo &applicationInf false, parseResult, ArrayType::NUMBER); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + Constants::CODE_LANGUAGE, + applicationInfo.codeLanguage, + JsonType::STRING, + false, + parseResult, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITY_SIM, "from_json error:%{public}d", parseResult); } diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/extension_ability_info.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/extension_ability_info.cpp index 625aa557f21f52c1bc63b65dfc5fbebe04c0e4d7..2c63bb7c84d172206b1f139ba3dd9da5d7706851 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/extension_ability_info.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/extension_ability_info.cpp @@ -278,6 +278,14 @@ void from_json(const nlohmann::json &jsonObject, ExtensionAbilityInfo &extension false, parseResult, ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + Constants::CODE_LANGUAGE, + extensionInfo.codeLanguage, + JsonType::STRING, + false, + parseResult, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITY_SIM, "ExtensionAbilityInfo error:%{public}d", parseResult); } diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/hap_module_info.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/hap_module_info.cpp index c66cd11dcbedb6a98b746a6af98eb1d8ab8dffb1..506cdd8153f4bb89ec7703e586a5e937487e6265 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/hap_module_info.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/hap_module_info.cpp @@ -197,7 +197,8 @@ void to_json(nlohmann::json &jsonObject, const HapModuleInfo &hapModuleInfo) {HAP_MODULE_INFO_ISOLATION_MODE, hapModuleInfo.isolationMode}, {HAP_MODULE_INFO_AOT_COMPILE_STATUS, hapModuleInfo.aotCompileStatus}, {HAP_MODULE_INFO_COMPRESS_NATIVE_LIBS, hapModuleInfo.compressNativeLibs}, - {HAP_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES, hapModuleInfo.nativeLibraryFileNames} + {HAP_MODULE_INFO_NATIVE_LIBRARY_FILE_NAMES, hapModuleInfo.nativeLibraryFileNames}, + {Constants::CODE_LANGUAGE, hapModuleInfo.codeLanguage} }; } @@ -605,6 +606,14 @@ void from_json(const nlohmann::json &jsonObject, HapModuleInfo &hapModuleInfo) false, parseResult, ArrayType::STRING); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + Constants::CODE_LANGUAGE, + hapModuleInfo.codeLanguage, + JsonType::STRING, + false, + parseResult, + ArrayType::NOT_ARRAY); if (parseResult != ERR_OK) { TAG_LOGE(AAFwkTag::ABILITY_SIM, "HapModuleInfo error:%{public}d", parseResult); } diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/inner_bundle_info.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/inner_bundle_info.cpp index 02ebcba5aa084299e0101af877fa5d04db9651f2..cde912da46cd98826f49b46a7932c01b0bb0fda3 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/inner_bundle_info.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/inner_bundle_info.cpp @@ -981,6 +981,8 @@ std::optional InnerBundleInfo::FindHapModuleInfo(const std::strin hapInfo.compressNativeLibs = it->second.compressNativeLibs; hapInfo.nativeLibraryFileNames = it->second.nativeLibraryFileNames; hapInfo.aotCompileStatus = it->second.aotCompileStatus; + hapInfo.codeLanguage = it->second.codeLanguage; + TAG_LOGE(AAFwkTag::ABILITY_SIM, "[zzz] hapInfo.codeLanguage %{public}s", hapInfo.codeLanguage.c_str()); return hapInfo; } diff --git a/frameworks/simulator/ability_simulator/src/bundle_parser/module_profile.cpp b/frameworks/simulator/ability_simulator/src/bundle_parser/module_profile.cpp index 6d369d08f13c271d16ccb20a983b5b4955718839..137f8a2f2f2f7229c8d1ba235d338260cdf990b9 100644 --- a/frameworks/simulator/ability_simulator/src/bundle_parser/module_profile.cpp +++ b/frameworks/simulator/ability_simulator/src/bundle_parser/module_profile.cpp @@ -199,6 +199,7 @@ struct Ability { bool excludeFromMissions = false; bool recoverable = false; bool unclearableMission = false; + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; struct Extension { @@ -278,6 +279,7 @@ struct Module { std::string buildHash; std::string isolationMode; bool compressNativeLibs = true; + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; struct ModuleJson { @@ -588,6 +590,15 @@ void from_json(const nlohmann::json &jsonObject, Ability &ability) false, g_parseResult, ArrayType::NOT_ARRAY); + GetValueIfFindKey(jsonObject, + jsonObjectEnd, + Constants::CODE_LANGUAGE, + ability.codeLanguage, + JsonType::STRING, + false, + g_parseResult, + ArrayType::NOT_ARRAY); + } void from_json(const nlohmann::json &jsonObject, Extension &extension) @@ -1613,6 +1624,8 @@ bool ToAbilityInfo( abilityInfo.minWindowWidth = ability.minWindowWidth; abilityInfo.maxWindowHeight = ability.maxWindowHeight; abilityInfo.minWindowHeight = ability.minWindowHeight; + abilityInfo.codeLanguage = ability.codeLanguage; + TAG_LOGE(AAFwkTag::ABILITY_SIM, "[zzz] abilityInfo.codeLanguage %{public}s", abilityInfo.codeLanguage.c_str()); return true; } diff --git a/frameworks/simulator/ability_simulator/src/dynamic_loader_linux.cpp b/frameworks/simulator/ability_simulator/src/dynamic_loader_linux.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f6cb270c8aa14bd160c29708ed215d99c9cb4435 --- /dev/null +++ b/frameworks/simulator/ability_simulator/src/dynamic_loader_linux.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dynamic_loader.h" + +#if defined(LINUX_PLATFORM) +#include +#endif + +extern "C" { +void* DynamicLoadLibrary(const char* dlPath, unsigned int mode) +{ + return dlopen(dlPath, mode); +} + +void* DynamicFindSymbol(void* so, const char* symbol) +{ + return dlsym(so, symbol); +} + +void DynamicFreeLibrary(void* so) +{ + (void)dlclose(so); +} + +const char* DynamicGetError() +{ + return dlerror(); +} +} diff --git a/frameworks/simulator/ability_simulator/src/sim.diff b/frameworks/simulator/ability_simulator/src/sim.diff new file mode 100644 index 0000000000000000000000000000000000000000..50935180902d78173d361c9c54ff6c4c8d9dbeb3 --- /dev/null +++ b/frameworks/simulator/ability_simulator/src/sim.diff @@ -0,0 +1,105 @@ +--- simulator.cpp_ 2025-06-17 09:42:28.047739786 +0300 ++++ simulator.cpp 2025-06-18 10:22:19.955284856 +0300 +@@ -25,6 +25,7 @@ + + #include "ability_context.h" + #include "ability_stage_context.h" ++#include "bundle_constants.h" + #if defined(LINUX_PLATFORM) + #include "ani.h" + #include "ani_window_stage.h" +@@ -227,9 +228,11 @@ + void UpdateConfiguration(const AppExecFwk::Configuration &config) override; + void SetMockList(const std::map &mockList) override; + void SetHostResolveBufferTracker(ResolveBufferTrackerCallback cb) override; ++ std::string GetCodeLanguage() override ++ { ++ return abilityInfo_->codeLanguage; ++ } + #if defined(LINUX_PLATFORM) +- int64_t StartSTSAbility( +- const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = "") override; + ani_object GetAniObj() override + { + return entryObject; +@@ -238,12 +241,8 @@ + { + return windowScene_; + } +-#else +- int64_t StartSTSAbility( +- const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = "") override { +- return 0; +- } +-#endif ++#endif // LINUX_PLATFORM ++ + private: + bool OnInit(); + void Run(); +@@ -294,8 +293,12 @@ + std::map &pkgAliasMap); + void GetPkgContextInfoListInner(nlohmann::json &itemObject, std::vector &items, + std::map &pkgAliasMap, std::string &pkgName); +- ++ int64_t StartJSAbility( ++ const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = ""); + #if defined(LINUX_PLATFORM) ++ int64_t StartSTSAbility( ++ const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = ""); ++ + bool LoadSTSRuntimeApis(); + bool StartSTSRuntime(); + bool LoadStsModule(); +@@ -497,6 +500,22 @@ + } + + CreateStageContext(); ++ ++ if (abilityInfo_->codeLanguage == AppExecFwk::Constants::CODE_LANGUAGE_1_1) { ++ return StartJSAbility(abilitySrcPath, callback, abilityName); ++#if defined(LINUX_PLATFORM) ++ } else if (abilityInfo_->codeLanguage == AppExecFwk::Constants::CODE_LANGUAGE_1_2) { ++ return StartSTSAbility(abilitySrcPath, callback, abilityName); ++#endif // LINUX_PLATFORM ++ } else { ++ TAG_LOGE(AAFwkTag::ABILITY_SIM, "Unsuported code language %{public}s", abilityInfo_->codeLanguage.c_str()); ++ return 0; ++ } ++} ++ ++int64_t SimulatorImpl::StartJSAbility( ++ const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName) ++{ + std::ifstream stream(options_.modulePath, std::ios::ate | std::ios::binary); + if (!stream.is_open()) { + TAG_LOGE(AAFwkTag::ABILITY_SIM, "open:%{public}s failed", options_.modulePath.c_str()); +@@ -553,11 +572,6 @@ + int64_t SimulatorImpl::StartSTSAbility( + const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName) + { +- if (!ParseAbilityInfo(abilitySrcPath, abilityName)) { +- return -1; +- } +- CreateStageContext(); +- + LoadSTSRuntimeApis(); + StartSTSRuntime(); + +@@ -1165,7 +1179,7 @@ + TAG_LOGE(AAFwkTag::STSRUNTIME, "Class_FindMethod ctor failed"); + return false; + } +- static ani_object object = nullptr; ++ ani_object object = nullptr; + if (aniEnv_->Object_New(cls, method, &object, undefined_ref, refArray) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Object_New AbcRuntimeLinker failed"); + return false; +@@ -1187,7 +1201,6 @@ + return false; + } + entryClass = static_cast(entryClassRef); +- + ani_method entryMethod = nullptr; + if (aniEnv_->Class_FindMethod(entryClass, "", ":V", &entryMethod) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Class_FindMethod ctor failed"); diff --git a/frameworks/simulator/ability_simulator/src/simulator.cpp b/frameworks/simulator/ability_simulator/src/simulator.cpp index ef34549f47424c19db19aeb5da9b9a0dccf3a651..85028e624e85738247d88e3ba4359bcc83b09010 100644 --- a/frameworks/simulator/ability_simulator/src/simulator.cpp +++ b/frameworks/simulator/ability_simulator/src/simulator.cpp @@ -19,14 +19,21 @@ #include #include #include +#include #include #include #include "ability_context.h" #include "ability_stage_context.h" +#include "bundle_constants.h" +#if defined(LINUX_PLATFORM) +#include "ani.h" +#include "ani_window_stage.h" +#endif // LINUX_PLATFORM #include "bundle_container.h" #include "console.h" #include "declarative_module_preloader.h" +#include "dynamic_loader.h" #include "hilog_tag_wrapper.h" #include "js_ability_context.h" #include "js_ability_stage_context.h" @@ -49,6 +56,7 @@ namespace OHOS { namespace AbilityRuntime { namespace { + constexpr int64_t DEFAULT_GC_POOL_SIZE = 0x10000000; // 256MB constexpr int32_t DEFAULT_ARK_PROPERTIES = -1; constexpr size_t DEFAULT_GC_THREAD_NUM = 7; @@ -63,15 +71,129 @@ const std::string VERSION = "version"; const std::string ENTRY_PATH = "entryPath"; const std::string IS_SO = "isSO"; const std::string DEPENDENCY_ALIAS = "dependencyAlias"; +const std::string ENTERANCE_PATH = "entry/src/main/ets/entryability/EntryAbility/EntryAbility"; +const std::string PREVIEWER_BOOT_PATH = "module/bootpath.json"; #if defined(WINDOWS_PLATFORM) constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.dll"; #elif defined(MAC_PLATFORM) constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.dylib"; +#elif defined(LINUX_PLATFORM) +constexpr char ARK_DEBUGGER_LIB_PATH[] = "libark_inspector.so"; #else #error "Unsupported platform" #endif +#if defined(LINUX_PLATFORM) +typedef struct EtsVMOption { +// EtsOptionType option; + const void *extraInfo; +} EtsVMOption; + +typedef struct EtsVMInitArgs { + int version; + int nOptions; + EtsVMOption *options; +} EtsVMInitArgs; + +struct STSRuntimeAPI { + int (*ETS_GetDefaultVMInitArgs)(EtsVMInitArgs *vmArgs); + int (*ETS_GetCreatedVMs)(void **vmBuf, size_t bufLen, size_t *nVms); + ani_status (*ANI_GetCreatedVMs)(ani_vm **vms_buffer, ani_size vms_buffer_length, ani_size *result); + ani_status (*ANI_CreateVM)(const ani_options *options, uint32_t version, ani_vm **result); +}; + +using GetDefaultVMInitArgsSTSRuntimeType = int (*)(EtsVMInitArgs* vmArgs); +using GetCreatedVMsSTSRuntimeType = int (*)(void** vmBuf, size_t bufLen, size_t* nVms); +using CreateVMSTSRuntimeType = ani_status (*)(const ani_options *options, uint32_t version, ani_vm **result); +using ANIGetCreatedVMsType = ani_status (*)(ani_vm **vms_buffer, ani_size vms_buffer_length, ani_size *result); + +auto&& renderCallback = []( + const void*, const size_t bufferSize, const int32_t width, const int32_t height, const uint64_t timestamp) -> bool { + return true; +}; + +bool LoadBootPathFile(std::string& bootfiles) +{ + std::ifstream inFile; + inFile.open(PREVIEWER_BOOT_PATH, std::ios::in); + if (!inFile.is_open()) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "read json error"); + return false; + } + nlohmann::json jsonObject = nlohmann::json::parse(inFile); + if (jsonObject.is_discarded()) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "json discarded error"); + inFile.close(); + return false; + } + if (jsonObject.is_null() || jsonObject.empty()) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "invalid json"); + inFile.close(); + return false; + } + for (const auto &[key, value] : jsonObject.items()) { + if (!value.is_null() && value.is_string()) { + std::string jsonValue = value.get(); + if (jsonValue.empty()) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "json value of %{public}s is empty", key.c_str()); + continue; + } + if (!bootfiles.empty()) { + bootfiles += ":"; + } + bootfiles += jsonValue.c_str(); + } + } + inFile.close(); + return true; +} + +bool LoadSymbolGetDefaultVMInitArgs(void* handle, STSRuntimeAPI& apis) +{ + auto symbol = DynamicFindSymbol(handle, "ETS_GetDefaultVMInitArgs"); + if (symbol == nullptr) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "runtime api not found: %{public}s", "ETS_GetDefaultVMInitArgs"); + return false; + } + apis.ETS_GetDefaultVMInitArgs = reinterpret_cast(symbol); + return true; +} + +bool LoadSymbolGetCreatedVMs(void* handle, STSRuntimeAPI& apis) +{ + auto symbol = DynamicFindSymbol(handle, "ETS_GetCreatedVMs"); + if (symbol == nullptr) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "runtime api not found: %{public}s", "ETS_GetCreatedVMs"); + return false; + } + apis.ETS_GetCreatedVMs = reinterpret_cast(symbol); + return true; +} + +bool LoadSymbolANIGetCreatedVMs(void* handle, STSRuntimeAPI& apis) +{ + auto symbol = DynamicFindSymbol(handle, "ANI_GetCreatedVMs"); + if (symbol == nullptr) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "runtime api not found: %{public}s", "ANI_GetCreatedVMs"); + return false; + } + apis.ANI_GetCreatedVMs = reinterpret_cast(symbol); + return true; +} + +bool LoadSymbolCreateVM(void* handle, STSRuntimeAPI& apis) +{ + auto symbol = DynamicFindSymbol(handle, "ANI_CreateVM"); + if (symbol == nullptr) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "runtime api not found: %{public}s", "ANI_CreateVM"); + return false; + } + apis.ANI_CreateVM = reinterpret_cast(symbol); + return true; +} +#endif // LINUX_PLATFORM + int32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char *message) { TAG_LOGD(AAFwkTag::ABILITY_SIM, "ArkLog:%{public}s", message); @@ -106,6 +228,21 @@ public: void UpdateConfiguration(const AppExecFwk::Configuration &config) override; void SetMockList(const std::map &mockList) override; void SetHostResolveBufferTracker(ResolveBufferTrackerCallback cb) override; + std::string GetCodeLanguage() override + { + return abilityInfo_->codeLanguage; + } +#if defined(LINUX_PLATFORM) + ani_object GetAniObj() override + { + return entryObject; + } + std::shared_ptr GetWindowScene() override + { + return windowScene_; + } +#endif // LINUX_PLATFORM + private: bool OnInit(); void Run(); @@ -156,6 +293,27 @@ private: std::map &pkgAliasMap); void GetPkgContextInfoListInner(nlohmann::json &itemObject, std::vector &items, std::map &pkgAliasMap, std::string &pkgName); + int64_t StartJSAbility( + const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = ""); +#if defined(LINUX_PLATFORM) + int64_t StartSTSAbility( + const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName = ""); + + bool LoadSTSRuntimeApis(); + bool StartSTSRuntime(); + bool LoadStsModule(); + bool onWindowStageCreate(ani_class entryClass, ani_object entryObject, ...); + + ani_env *aniEnv_ = nullptr; + ani_vm *aniVM_ = nullptr; + ani_class entryClass = nullptr; + ani_object entryObject = nullptr; + ani_ref entryObjectRef = nullptr; + + STSRuntimeAPI lazyApis_ = {}; + + std::shared_ptr windowScene_; +#endif // LINUX_PLATFORM }; void DebuggerTask::HandleTask(const uv_async_t *req) @@ -342,6 +500,22 @@ int64_t SimulatorImpl::StartAbility( } CreateStageContext(); + + if (abilityInfo_->codeLanguage == AppExecFwk::Constants::CODE_LANGUAGE_1_1) { + return StartJSAbility(abilitySrcPath, callback, abilityName); +#if defined(LINUX_PLATFORM) + } else if (abilityInfo_->codeLanguage == AppExecFwk::Constants::CODE_LANGUAGE_1_2) { + return StartSTSAbility(abilitySrcPath, callback, abilityName); +#endif // LINUX_PLATFORM + } else { + TAG_LOGE(AAFwkTag::ABILITY_SIM, "Unsuported code language %{public}s", abilityInfo_->codeLanguage.c_str()); + return 0; + } +} + +int64_t SimulatorImpl::StartJSAbility( + const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName) +{ std::ifstream stream(options_.modulePath, std::ios::ate | std::ios::binary); if (!stream.is_open()) { TAG_LOGE(AAFwkTag::ABILITY_SIM, "open:%{public}s failed", options_.modulePath.c_str()); @@ -394,6 +568,45 @@ int64_t SimulatorImpl::StartAbility( return currentId_; } +#if defined(LINUX_PLATFORM) +int64_t SimulatorImpl::StartSTSAbility( + const std::string &abilitySrcPath, TerminateCallback callback, const std::string &abilityName) +{ + LoadSTSRuntimeApis(); + StartSTSRuntime(); + + if (context_ == nullptr) { + context_ = std::make_shared(); + context_->SetSimulator(static_cast(this)); + context_->SetOptions(options_); + context_->SetAbilityStageContext(stageContext_); + context_->SetResourceManager(resourceMgr_); + context_->SetAbilityInfo(abilityInfo_); + } + windowScene_ = std::make_shared(); + if (windowScene_ == nullptr) { + TAG_LOGE(AAFwkTag::ABILITY, "Could not create WindowScene"); + return 0; + } + sptr listener = nullptr; + auto ret = windowScene_->Init(-1, context_, listener); + if (ret != OHOS::Rosen::WMError::WM_OK) { + TAG_LOGE(AAFwkTag::ABILITY, "Could not init WindowScene"); + return 0; + } + auto window = windowScene_->GetMainWindow(); + window->CreateSurfaceNode("default", std::move(renderCallback)); + LoadStsModule(); + auto stsWindowStage = CreateAniWindowStage(aniEnv_, windowScene_); + onWindowStageCreate(entryClass, entryObject, stsWindowStage); + + ++currentId_; + terminateCallback_ = callback; + InitResourceMgr(); + return currentId_; +} +#endif // LINUX_PLATFORM + bool SimulatorImpl::LoadAbilityStage(uint8_t *buffer, size_t len) { if (moduleInfo_ == nullptr) { @@ -849,7 +1062,194 @@ void SimulatorImpl::Run() postTask_([this]() { Run(); }, 0); } } + +#if defined(LINUX_PLATFORM) +bool SimulatorImpl::LoadSTSRuntimeApis() +{ + auto dsoAce = DynamicLoadLibrary("libace_compatible.so", 1); + if (!dsoAce) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "load library failed: %{public}s", "libace_compatible.so"); + return false; + } + auto dso = DynamicLoadLibrary("libarkruntime.so", 1); + if (!dso) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "load library failed: %{public}s", "libarkruntime.so"); + return false; + } + auto dsoEts = DynamicLoadLibrary("libetsnative.so", 1); + if (!dso) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "load library failed: %{public}s", "libetsnative.so"); + return false; + } + if (!LoadSymbolGetDefaultVMInitArgs(dsoEts, lazyApis_) || + !LoadSymbolGetCreatedVMs(dsoEts, lazyApis_) || + !LoadSymbolCreateVM(dso, lazyApis_) || + !LoadSymbolANIGetCreatedVMs(dso, lazyApis_)) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "load library failed: %{public}s", "LoadSymbolGetDefaultVMInitArgs"); + return false; + } + return true; +} + +bool SimulatorImpl::StartSTSRuntime() +{ + std::string bootfiles; + LoadBootPathFile(bootfiles); + + // Create boot-panda-files options + std::vector options; + const std::string optionPrefix = "--ext:"; + std::string bootString = optionPrefix + "--boot-panda-files=" + bootfiles; + TAG_LOGI(AAFwkTag::STSRUNTIME, "bootString %{public}s", bootString.c_str()); + options.push_back(ani_option{bootString.c_str(), nullptr}); + std::string schedulingExternal = optionPrefix + "--coroutine-enable-external-scheduling=true"; + ani_option schedulingExternalOption = {schedulingExternal.data(), nullptr}; + options.push_back(schedulingExternalOption); + + std::string forbiddenJIT = optionPrefix + "--compiler-enable-jit=false"; + ani_option forbiddenJITOption = {forbiddenJIT.data(), nullptr}; + options.push_back(forbiddenJITOption); + + options.push_back(ani_option{"--ext:--log-level=info", nullptr}); + + options.push_back(ani_option{"--ext:--gc-type=stw", nullptr}); + +// Linux runtime does not support "interop" +// std::string interop = optionPrefix + "interop"; +// ani_option interopOption = {interop.data(), (void*)aniEnv_}; +// options.push_back(interopOption); + + ani_options optionsPtr = {options.size(), options.data()}; + auto status = lazyApis_.ANI_CreateVM(&optionsPtr, ANI_VERSION_1, &aniVM_); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "ANI_CreateVM failed %{public}d", status); + return false; + } + + ani_size nrVMs; + if (lazyApis_.ANI_GetCreatedVMs(&aniVM_, 1, &nrVMs) != ANI_OK) { + return false; + }; + + if (aniVM_->GetEnv(ANI_VERSION_1, &aniEnv_) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Failed GetEnv"); + return false; + } + return true; +} + +bool SimulatorImpl::LoadStsModule() +{ + ani_class stringCls = nullptr; + if (aniEnv_->FindClass("Lstd/core/String;", &stringCls) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "FindClass Lstd/core/String Failed"); + return false; + } + + std::regex reg("modules.abc"); + std::string modulePath = std::regex_replace(options_.modulePath, reg, "modules_static.abc"); + ani_string ani_str; + if (aniEnv_->String_NewUTF8(modulePath.c_str(), modulePath.size(), &ani_str) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "String_NewUTF8 modulePath Failed"); + return false; + } + + ani_ref undefined_ref; + if (aniEnv_->GetUndefined(&undefined_ref) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "GetUndefined failed"); + return false; + } + ani_array_ref refArray; + if (aniEnv_->Array_New_Ref(stringCls, 1, undefined_ref, &refArray) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Array_New_Ref Failed"); + return false; + } + if (aniEnv_->Array_Set_Ref(refArray, 0, ani_str) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Array_Set_Ref Failed"); + return false; + } + + ani_class cls = nullptr; + if (aniEnv_->FindClass("Lstd/core/AbcRuntimeLinker;", &cls) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "FindClass AbcRuntimeLinker failed"); + return false; + } + ani_method method = nullptr; + if (aniEnv_->Class_FindMethod(cls, "", "Lstd/core/RuntimeLinker;[Lstd/core/String;:V", &method) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Class_FindMethod ctor failed"); + return false; + } + ani_object object = nullptr; + if (aniEnv_->Object_New(cls, method, &object, undefined_ref, refArray) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Object_New AbcRuntimeLinker failed"); + return false; + } + ani_method loadClassMethod = nullptr; + if (aniEnv_->Class_FindMethod(cls, "loadClass", nullptr, &loadClassMethod) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Class_FindMethod loadClass failed"); + return false; + } + + std::string entryPath = ENTERANCE_PATH; + ani_string entryClassStr; + aniEnv_->String_NewUTF8(entryPath.c_str(), entryPath.length(), &entryClassStr); + ani_ref entryClassRef = nullptr; + ani_boolean isInit = false; + TAG_LOGI(AAFwkTag::STSRUNTIME, "load class: %{public}s", entryPath.c_str()); + if (aniEnv_->Object_CallMethod_Ref(object, loadClassMethod, &entryClassRef, entryClassStr, isInit) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Object_CallMethod_Ref loadClassMethod failed"); + return false; + } + entryClass = static_cast(entryClassRef); + ani_method entryMethod = nullptr; + if (aniEnv_->Class_FindMethod(entryClass, "", ":V", &entryMethod) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Class_FindMethod ctor failed"); + return false; + } + + auto status = aniEnv_->Object_New(entryClass, entryMethod, &entryObject); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "Object_New AbcRuntimeLinker failed, %{public}d", status); + return false; + } + + if (aniEnv_->GlobalReference_Create(entryObject, &entryObjectRef) != ANI_OK) { + TAG_LOGE(AAFwkTag::STSRUNTIME, "GlobalReference_Create failed"); + return false; + } + + if (aniEnv_->Class_FindMethod(entryClass, "onCreate", ":V", &method) != ANI_OK) { + TAG_LOGI(AAFwkTag::ABILITY, "Class_FindMethod FAILED"); + return false; + } + + status = aniEnv_->Object_CallMethod_Void(entryObject, method); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::ABILITY, "CALL Object_CallMethod FAILED, %{public}d", status); + return false; + } + return true; +} + +bool SimulatorImpl::onWindowStageCreate(ani_class entryClass, ani_object entryObject, ...) +{ + ani_method method {}; + auto status = aniEnv_->Class_FindMethod(entryClass, "onWindowStageCreate", "L@ohos/window/window/WindowStage;:V", &method); + if (status != ANI_OK) { + TAG_LOGE(AAFwkTag::UIABILITY, "status : %{public}d", status); + return false; + } + va_list args; + va_start(args, entryObject); + if ((status = aniEnv_->Object_CallMethod_Void_V(entryObject, method, args)) != ANI_OK) { + TAG_LOGE(AAFwkTag::UIABILITY, "status : %{public}d", status); + return false; + } + va_end(args); + return true; } +#endif // LINUX_PLATFORM +} // namespace std::shared_ptr Simulator::Create(const Options &options) { diff --git a/frameworks/simulator/build/ability_simulator.gni b/frameworks/simulator/build/ability_simulator.gni index e7f52af63980c84d32660103be922f8e69b0944a..1225e393d9232539c13367329879bdd08fa45ab3 100644 --- a/frameworks/simulator/build/ability_simulator.gni +++ b/frameworks/simulator/build/ability_simulator.gni @@ -29,7 +29,7 @@ template("gen_js_src_binary") { gen_obj(target_name + "_js") { input = invoker.js_source - if (is_mac || is_mingw) { + if (is_mac || is_mingw || is_linux) { output = target_out_dir + "/" + invoker.target_name + "_js.c" } else { output = target_out_dir + "/" + invoker.target_name + "_js.o" @@ -43,7 +43,7 @@ template("gen_js_src_binary") { get_label_info(":gen_abc_" + invoker.target_name, "target_out_dir") + "/" + get_path_info(invoker.js_source, "name") + ".abc" - if (is_mac || is_mingw) { + if (is_mac || is_mingw || is_linux) { output = target_out_dir + "/" + invoker.target_name + "_abc.c" } else { output = target_out_dir + "/" + invoker.target_name + "_abc.o" diff --git a/frameworks/simulator/common/include/ability_info.h b/frameworks/simulator/common/include/ability_info.h index 1956e699a638f3acc174c670b1c92cd9507fb4c1..d63a4647639f39fa7616131b62f97d8fb672bf7d 100644 --- a/frameworks/simulator/common/include/ability_info.h +++ b/frameworks/simulator/common/include/ability_info.h @@ -20,6 +20,7 @@ #include #include "application_info.h" +#include "bundle_constants.h" #include "extension_ability_info.h" namespace OHOS { @@ -185,6 +186,8 @@ struct AbilityInfo { int64_t installTime; std::vector supportExtNames; std::vector supportMimeTypes; + + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/simulator/common/include/application_info.h b/frameworks/simulator/common/include/application_info.h index a22dd9d5fdfb3e8b7aae693f25771593562a8189..3ac946f13320f36ad3f7ca41c20b4487dfe16323 100644 --- a/frameworks/simulator/common/include/application_info.h +++ b/frameworks/simulator/common/include/application_info.h @@ -19,6 +19,7 @@ #include #include #include +#include "bundle_constants.h" #include "module_info.h" namespace OHOS { @@ -192,6 +193,8 @@ struct ApplicationInfo { std::string compileSdkVersion; std::string compileSdkType = DEFAULT_COMPILE_SDK_TYPE; + + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/simulator/common/include/bindable.h b/frameworks/simulator/common/include/bindable.h new file mode 100644 index 0000000000000000000000000000000000000000..8fb38e80cdc628dd40b01c2a7766a193425bfaf8 --- /dev/null +++ b/frameworks/simulator/common/include/bindable.h @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_BINDABLE_H +#define OHOS_ABILITY_RUNTIME_BINDABLE_H + +#include +#include + +class NativeReference; +typedef class __ani_ref *ani_ref; +namespace OHOS { +namespace AbilityRuntime { +class Runtime; + +class BindingObject final { +public: + BindingObject() = default; + ~BindingObject() = default; + + template + void Bind(Runtime& runtime, T* object) + { + static_assert(IsValidType(), "T must be ani_ref or NativeReference"); + const std::string typeName = GetTypeString(); + std::lock_guard guard(objectsMutex_); + std::unique_ptr obj(object, SimpleRelease); + objects_.emplace(typeName, std::move(obj)); + } + + template + T* Get() + { + const std::string typeName = GetTypeString(); + std::lock_guard guard(objectsMutex_); + const auto& iter = objects_.find(typeName); + if (iter == objects_.end()) { + return nullptr; + } + return static_cast(iter->second.get()); + } + + void Unbind() + { + // Consistency with previous behavior + Unbind(); + } + + template + void Unbind() + { + const std::string typeName = GetTypeString(); + std::lock_guard guard(objectsMutex_); + const auto& iter = objects_.find(typeName); + if (iter == objects_.end()) { + return; + } + iter->second.release(); + } + + BindingObject(const BindingObject&) = delete; + BindingObject& operator=(const BindingObject&) = delete; + BindingObject(BindingObject&&) = delete; + BindingObject& operator=(BindingObject&&) = delete; + +private: + template + static void SimpleRelease(void* ptr) + { + delete static_cast(ptr); + } + + template + static constexpr bool IsValidType() + { + if (std::is_same_v || std::is_same_v) { + return true; + } + return false; + } + + template + static std::string GetTypeString() + { + if (std::is_same_v) { + return "ani_ref"; + } else { + return "NativeReference"; + } + } + + std::map> objects_; + std::mutex objectsMutex_; +}; + +class Bindable { +public: + virtual ~Bindable() = default; + + template + void Bind(Runtime& runtime, T* object) + { + if (object_) { + object_->Bind(runtime, object); + } + } + + void Unbind() const + { + if (object_) { + object_->Unbind(); + } + } + + template + void Unbind() const + { + if (object_) { + object_->Unbind(); + } + } + + const std::unique_ptr& GetBindingObject() const + { + return object_; + } + +protected: + Bindable() = default; + +private: + std::unique_ptr object_ = std::make_unique(); +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_BINDABLE_H diff --git a/frameworks/simulator/common/include/bundle_constants.h b/frameworks/simulator/common/include/bundle_constants.h index d47c281b74507b0b0475c85f24f2a1f665e299a1..a1818632426a56be88c81c447a3a2a62bb3a52a2 100644 --- a/frameworks/simulator/common/include/bundle_constants.h +++ b/frameworks/simulator/common/include/bundle_constants.h @@ -46,6 +46,11 @@ constexpr const char* HAP_PATH = "hapPath"; constexpr int32_t INITIAL_APP_INDEX = 0; constexpr const char* RELATIVE_PATH = "../"; constexpr const char* APP_DETAIL_ABILITY = "AppDetailAbility"; + +constexpr const char* CODE_LANGUAGE = "codeLanguage"; +constexpr const char* CODE_LANGUAGE_1_1 = "1.1"; +constexpr const char* CODE_LANGUAGE_1_2 = "1.2"; +constexpr const char* CODE_LANGUAGE_HYBRID = "hybrid"; } // namespace Constants } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/simulator/common/include/context.h b/frameworks/simulator/common/include/context.h index ac2468c16e109a3868f874eb4c4407da967fd1ef..6bfbb2f68cc9db485ebe2f2911b5d7ad9e9ab9f0 100644 --- a/frameworks/simulator/common/include/context.h +++ b/frameworks/simulator/common/include/context.h @@ -20,13 +20,14 @@ #include #include "application_info.h" +#include "bindable.h" #include "configuration.h" #include "hap_module_info.h" #include "options.h" namespace OHOS { namespace AbilityRuntime { -class Context { +class Context : public Bindable, public std::enable_shared_from_this { public: Context() = default; ~Context() = default; diff --git a/frameworks/simulator/common/include/extension_ability_info.h b/frameworks/simulator/common/include/extension_ability_info.h index b4822125678938427d63d37d592b001b650eca4f..9a578ee0434c653bb68683ff08a2d97e0ba4785f 100644 --- a/frameworks/simulator/common/include/extension_ability_info.h +++ b/frameworks/simulator/common/include/extension_ability_info.h @@ -110,6 +110,7 @@ struct ExtensionAbilityInfo { CompileMode compileMode = CompileMode::JS_BUNDLE; int32_t uid = -1; std::vector skillUri; + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/simulator/common/include/hap_module_info.h b/frameworks/simulator/common/include/hap_module_info.h index 2f39d4d8e95edd976af42ea71eda57cfd98b713f..c0621ac5c181e76f119ed96117ed320c52b45e51 100644 --- a/frameworks/simulator/common/include/hap_module_info.h +++ b/frameworks/simulator/common/include/hap_module_info.h @@ -20,6 +20,7 @@ #include "ability_info.h" #include "extension_ability_info.h" +#include "bundle_constants.h" namespace OHOS { namespace AppExecFwk { @@ -128,6 +129,7 @@ struct HapModuleInfo { std::string buildHash; IsolationMode isolationMode = IsolationMode::NONISOLATION_FIRST; AOTCompileStatus aotCompileStatus = AOTCompileStatus::NOT_COMPILED; + std::string codeLanguage = Constants::CODE_LANGUAGE_1_1; }; } // namespace AppExecFwk } // namespace OHOS