From 5c8ac1203ab00e47091a94a26eee60a76365ea7e Mon Sep 17 00:00:00 2001 From: zhuhan Date: Sat, 27 Jan 2024 09:49:19 +0800 Subject: [PATCH 1/2] run -env Signed-off-by: zhuhan Change-Id: I265e306f555adafca8da4a059fa0ed6554992337 --- bundle.json | 3 +- frameworks/native/runtime/js_runtime.cpp | 39 +- .../inner_api/runtime/include/js_runtime.h | 4 + interfaces/ndk/ability_runtime/BUILD.gn | 68 ++++ .../ability_runtime/include/native_err_code.h | 31 ++ .../ability_runtime/include/native_rumtime.h | 74 ++++ .../include/native_runtime_impl.h | 62 ++++ .../ndk/ability_runtime/libruntime.ndk.json | 10 + .../ability_runtime/src/native_rumtime.cpp | 54 +++ .../src/native_runtime_impl.cpp | 338 ++++++++++++++++++ test/unittest/BUILD.gn | 1 + test/unittest/native_runtime_test/BUILD.gn | 55 +++ .../native_runtime_test.cpp | 95 +++++ 13 files changed, 830 insertions(+), 4 deletions(-) create mode 100644 interfaces/ndk/ability_runtime/BUILD.gn create mode 100644 interfaces/ndk/ability_runtime/include/native_err_code.h create mode 100644 interfaces/ndk/ability_runtime/include/native_rumtime.h create mode 100644 interfaces/ndk/ability_runtime/include/native_runtime_impl.h create mode 100644 interfaces/ndk/ability_runtime/libruntime.ndk.json create mode 100644 interfaces/ndk/ability_runtime/src/native_rumtime.cpp create mode 100644 interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp create mode 100644 test/unittest/native_runtime_test/BUILD.gn create mode 100644 test/unittest/native_runtime_test/native_runtime_test.cpp diff --git a/bundle.json b/bundle.json index e1d33b0347..de9617d4da 100644 --- a/bundle.json +++ b/bundle.json @@ -107,7 +107,8 @@ "//foundation/ability/ability_runtime/js_environment/frameworks/js_environment:js_environment", "//foundation/ability/ability_runtime/services/abilitymgr/etc:appfwk_etc", "//foundation/ability/ability_runtime/service_router_framework:srms_target", - "//foundation/ability/ability_runtime/service_router_framework:jsapi_target" + "//foundation/ability/ability_runtime/service_router_framework:jsapi_target", + "//foundation/ability/ability_runtime/interfaces/ndk/ability_runtime:runtime_ndk" ], "inner_api": [ { diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index d41eaf7d2d..77f8f40b27 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -146,7 +146,7 @@ int32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char* messa } // namespace std::atomic JsRuntime::hasInstance(false); - +std::shared_ptr JsRuntime::childOptions_ = nullptr; JsRuntime::JsRuntime() { HILOG_DEBUG("JsRuntime costructor."); @@ -163,7 +163,7 @@ std::unique_ptr JsRuntime::Create(const Options& options) { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); std::unique_ptr instance; - + SetChildOptions(options); if (!options.preload && options.isStageModel) { auto preloadedInstance = Runtime::GetPreloaded(); #ifdef SUPPORT_GRAPHICS @@ -658,7 +658,6 @@ bool JsRuntime::Initialize(const Options& options) codePath_ = options.codePath; ReInitJsEnvImpl(options); LoadAotFile(options); - panda::JSNApi::SetBundle(vm, options.isBundle); panda::JSNApi::SetBundleName(vm, options.bundleName); panda::JSNApi::SetHostResolveBufferTracker( @@ -1455,5 +1454,39 @@ std::vector JsRuntime::GetSystemKitsMap(uint32_t version) HILOG_DEBUG("The size of the map is %{public}zu", systemKitsMap.size()); return systemKitsMap; } + +void JsRuntime::SetChildOptions(const Options& options) +{ + if (childOptions_ == nullptr) { + childOptions_ = std::make_shared(); + } + childOptions_->lang = options.lang; + childOptions_->bundleName = options.bundleName; + childOptions_->moduleName = options.moduleName; + childOptions_->codePath = options.codePath; + childOptions_->bundleCodeDir = options.bundleCodeDir; + childOptions_->hapPath = options.hapPath; + childOptions_->arkNativeFilePath = options.arkNativeFilePath; + childOptions_->hapModulePath = options.hapModulePath; + childOptions_->loadAce = options.loadAce; + childOptions_->preload = options.preload; + childOptions_->isBundle = options.isBundle; + childOptions_->isDebugVersion = options.isDebugVersion; + childOptions_->isJsFramework = options.isJsFramework; + childOptions_->isStageModel = options.isStageModel; + childOptions_->isTestFramework = options.isTestFramework; + childOptions_->uid = options.uid; + childOptions_->isUnique = options.isUnique; + childOptions_->moduleCheckerDelegate = options.moduleCheckerDelegate; + childOptions_->apiTargetVersion = options.apiTargetVersion; + childOptions_->packagePathStr = options.packagePathStr; + childOptions_->assetBasePathStr = options.assetBasePathStr; +} + +std::shared_ptr JsRuntime::GetChildOptions() +{ + HILOG_DEBUG("called"); + return childOptions_; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index e9ee36665f..5c51bc97ee 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -65,6 +65,7 @@ public: static bool ReadSourceMapData(const std::string& hapPath, const std::string& sourceMapPath, std::string& content); + static std::shared_ptr GetChildOptions(); JsRuntime(); ~JsRuntime() override; @@ -135,6 +136,7 @@ private: bool Initialize(const Options& options); void Deinitialize(); + static void SetChildOptions(const Options& options); int32_t JsperfProfilerCommandParse(const std::string &command, int32_t defaultValue); @@ -154,6 +156,8 @@ private: static std::atomic hasInstance; + static std::shared_ptr childOptions_; + private: bool CreateJsEnv(const Options& options); void PreloadAce(const Options& options); diff --git a/interfaces/ndk/ability_runtime/BUILD.gn b/interfaces/ndk/ability_runtime/BUILD.gn new file mode 100644 index 0000000000..7b04f2e2e0 --- /dev/null +++ b/interfaces/ndk/ability_runtime/BUILD.gn @@ -0,0 +1,68 @@ +# 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. + +import("//build/ohos.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +config("runtime_ndk_config") { + defines = [ "AMS_LOG_TAG = \"Runtime\"" ] + defines += [ "AMS_LOG_DOMAIN = 0xD001309" ] + + include_dirs = [ "include" ] +} + +ohos_ndk_headers("runtime_header") { + dest_dir = "$ndk_headers_out_dir/ability_runtime/" + sources = [ "./include/native_rumtime.h" ] +} + +ohos_ndk_library("libruntime_ndk") { + ndk_description_file = "./libruntime.ndk.json" + min_compact_version = "12" + output_name = "runtime_ndk" +} + +ohos_shared_library("runtime_ndk") { + include_dirs = [ + "${ability_runtime_native_path}/runtime", + "include", + ] + + sources = [ + "${ability_runtime_path}/interfaces/ndk/ability_runtime/src/native_rumtime.cpp", + "${ability_runtime_path}/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp" + ] + + configs = [ ":runtime_ndk_config" ] + + deps = [ + "${ability_runtime_innerkits_path}/runtime:runtime", + "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", + ] + + external_deps = [ + "ability_base:extractortool", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "ets_runtime:libark_jsruntime", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] + + innerapi_tags = [ "ndk" ] + subsystem_name = "ability" + part_name = "ability_runtime" +} \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/include/native_err_code.h b/interfaces/ndk/ability_runtime/include/native_err_code.h new file mode 100644 index 0000000000..6305b03fd0 --- /dev/null +++ b/interfaces/ndk/ability_runtime/include/native_err_code.h @@ -0,0 +1,31 @@ +/* + * 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. + */ + +#ifndef ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H +#define ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H + +namespace OHOS { +namespace AbilityRuntime { +enum { + NATIVE_RUNTIME_ERR_OK, + NATIVE_RUNTIME_THREAD_COUNT_OVERLOAD, + NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV, + NATIVE_RUNTIME_DESTROY_FAILED, + NATIVE_RUNTIME_INNER_ERROR +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H + diff --git a/interfaces/ndk/ability_runtime/include/native_rumtime.h b/interfaces/ndk/ability_runtime/include/native_rumtime.h new file mode 100644 index 0000000000..df32633706 --- /dev/null +++ b/interfaces/ndk/ability_runtime/include/native_rumtime.h @@ -0,0 +1,74 @@ +/* + * 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. + */ + +/** + * @addtogroup Ability_Runtime + * @{ + * + * @brief Provides runtime environment. + + * + * @since 12 + * @version 1.0 + */ + +/** + * @file native_rumtime.h + * + * @brief Declare interfaces for creating and destroying runtime environments. + * + * @library libruntime_ndk.z.so + * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore + * @since 12 + * @version 1.0 + */ +#ifndef ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H +#define ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H + +#include +#include "napi/native_api.h" +#ifdef __cplusplus +extern "C" { +#endif +/** + * @brief Obtains the application info based on the The current bundle. + * + * @param env: The environment that the API is invoked under. + * @return 0 - Success. + * 1 - The maximum number of runtime environments exceeded. + * 2 - One thread is allowed to create only one runtime environment. + * 4 - Internal error. + * @since 12 + * @version 1.0 + */ +int32_t OH_NativeAbility_Create_NapiEnv(napi_env *env); + + +/** + * @brief Obtains the application info based on the The current bundle. + * + * @param env: The environment that the API is invoked under. + * @return 0 - Success. + * 3 - Destroy failed. + * @since 12 + * @version 1.0 + */ +int32_t OH_NativeAbility_Destroy_NapiEnv(napi_env *env); + +#ifdef __cplusplus +}; +#endif +/** @} */ +#endif // ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/include/native_runtime_impl.h b/interfaces/ndk/ability_runtime/include/native_runtime_impl.h new file mode 100644 index 0000000000..bec67c5edc --- /dev/null +++ b/interfaces/ndk/ability_runtime/include/native_runtime_impl.h @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#ifndef ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H +#define ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H + +#include "js_runtime.h" + +#include +#include +#include +#include + +#include "native_err_code.h" +using Options = OHOS::AbilityRuntime::Runtime::Options; +namespace OHOS { +namespace AbilityRuntime { +class NativeRuntimeImpl { +public: + NativeRuntimeImpl(const NativeRuntimeImpl&) = delete; + NativeRuntimeImpl& operator=(const NativeRuntimeImpl&) = delete; + static NativeRuntimeImpl& GetNativeRuntimeImpl(); + int32_t CreateJsEnv(const Options& options, std::shared_ptr& jsEnv); + int32_t RemoveJsEnv(napi_env env); + int32_t Init(const Options& options, napi_env env); + +private: + NativeRuntimeImpl(); + ~NativeRuntimeImpl(); + int32_t AddEnv(napi_env env, std::shared_ptr jsEnv); + panda::ecmascript::EcmaVM* GetEcmaVm(const std::shared_ptr& jsEnv) const; + std::shared_ptr GetJsEnv(napi_env env); + void LoadAotFile(const Options& options, const std::shared_ptr& jsEnv); + void InitConsoleModule(const std::shared_ptr& jsEnv); + void InitSourceMap(const std::shared_ptr operatorObj, + const std::shared_ptr& jsEnv); + void InitTimerModule(const std::shared_ptr& jsEnv); + void SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate, + const std::shared_ptr& jsEnv); + void SetRequestAotCallback(const std::shared_ptr& jsEnv); + bool InitLoop(const std::shared_ptr& jsEnv); + void InitWorkerModule(const Options& options, const std::shared_ptr& jsEnv); + std::unordered_map> envMap_; + std::unordered_set threadIds_; + bool preloaded_ = false; + std::mutex envMutex_; +}; +} +} +#endif // ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/libruntime.ndk.json b/interfaces/ndk/ability_runtime/libruntime.ndk.json new file mode 100644 index 0000000000..e6d4b55737 --- /dev/null +++ b/interfaces/ndk/ability_runtime/libruntime.ndk.json @@ -0,0 +1,10 @@ +[ + { + "first_introduced": "12", + "name": "OH_NativeAbility_Create_NapiEnv" + }, + { + "first_introduced": "12", + "name": "OH_NativeAbility_Destroy_NapiEnv" + } +] \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/src/native_rumtime.cpp b/interfaces/ndk/ability_runtime/src/native_rumtime.cpp new file mode 100644 index 0000000000..8a7a690d65 --- /dev/null +++ b/interfaces/ndk/ability_runtime/src/native_rumtime.cpp @@ -0,0 +1,54 @@ +/* + * 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 "native_rumtime.h" + +#include "js_environment.h" +#include "hilog_wrapper.h" +#include "native_runtime_impl.h" + +using NativeRuntimeImpl = OHOS::AbilityRuntime::NativeRuntimeImpl; + +int32_t OH_NativeAbility_Create_NapiEnv(napi_env *env) +{ + HILOG_INFO("call OH_NativeAbility_Create_NapiEnv"); + auto options = OHOS::AbilityRuntime::JsRuntime::GetChildOptions(); + if (options == nullptr) { + HILOG_ERROR("options is null, it maybe application startup failed!"); + return OHOS::AbilityRuntime::NATIVE_RUNTIME_INNER_ERROR; + } + std::shared_ptr jsEnv = nullptr; + auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(*options, jsEnv); + if (errCode != OHOS::AbilityRuntime::NATIVE_RUNTIME_ERR_OK) { + return errCode; + } + HILOG_INFO("CreateJsEnv ok"); + *env = reinterpret_cast(jsEnv->GetNativeEngine()); + if (env == nullptr) { + HILOG_ERROR("CreateJsEnv failed"); + return OHOS::AbilityRuntime::NATIVE_RUNTIME_INNER_ERROR; + } + return NativeRuntimeImpl::GetNativeRuntimeImpl().Init(*options, *env); +} + +int32_t OH_NativeAbility_Destroy_NapiEnv(napi_env *env) +{ + HILOG_INFO("call OH_NativeAbility_Destroy_NapiEnv"); + auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(*env); + if (errCode == OHOS::AbilityRuntime::NATIVE_RUNTIME_ERR_OK) { + *env = nullptr; + } + return errCode; +} \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp b/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp new file mode 100644 index 0000000000..855b06b90d --- /dev/null +++ b/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp @@ -0,0 +1,338 @@ +/* + * 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 "native_runtime_impl.h" + +#include "bundle_mgr_interface.h" +#include "hilog_wrapper.h" +#include "iservice_registry.h" +#include "js_environment.h" +#include "js_module_reader.h" +#include "js_worker.h" +#include "ohos_js_env_logger.h" +#include "ohos_js_environment_impl.h" +#include "parameters.h" +#include "system_ability_definition.h" + +using Extractor = OHOS::AbilityBase::Extractor; +using ExtractorUtil = OHOS::AbilityBase::ExtractorUtil; + +namespace OHOS { +namespace AbilityRuntime { +namespace { +constexpr int64_t DEFAULT_GC_POOL_SIZE = 0x10000000; // 256MB +constexpr size_t MAX_ENV_COUNT = 16; +const std::string SANDBOX_ARK_PROIFILE_PATH = "/data/storage/ark-profile"; +int32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char* message) +{ + HILOG_INFO("ArkLog: %{public}s", message); + return 0; +} +} +NativeRuntimeImpl::NativeRuntimeImpl() +{} + +NativeRuntimeImpl::~NativeRuntimeImpl() +{ + std::lock_guard lock(envMutex_); + for (auto it : envMap_) { + it.second.reset(); + it.second = nullptr; + } + envMap_.clear(); + threadIds_.clear(); +} + +NativeRuntimeImpl& NativeRuntimeImpl::GetNativeRuntimeImpl() +{ + static NativeRuntimeImpl nativeRuntimeImpl; + return nativeRuntimeImpl; +} + +int32_t NativeRuntimeImpl::CreateJsEnv(const Options& options, std::shared_ptr& jsEnv) +{ + HILOG_DEBUG("called"); + panda::RuntimeOption pandaOption; + int arkProperties = OHOS::system::GetIntParameter("persist.ark.properties", -1); + std::string bundleName = OHOS::system::GetParameter("persist.ark.arkbundlename", ""); + size_t gcThreadNum = OHOS::system::GetUintParameter("persist.ark.gcthreads", 7); + size_t longPauseTime = OHOS::system::GetUintParameter("persist.ark.longpausetime", 40); + pandaOption.SetArkProperties(arkProperties); + pandaOption.SetArkBundleName(bundleName); + pandaOption.SetGcThreadNum(gcThreadNum); + pandaOption.SetLongPauseTime(longPauseTime); + HILOG_INFO("NativeRuntimeImpl::Initialize ark properties = %{public}d bundlename = %{public}s", + arkProperties, bundleName.c_str()); + pandaOption.SetGcType(panda::RuntimeOption::GC_TYPE::GEN_GC); + pandaOption.SetGcPoolSize(DEFAULT_GC_POOL_SIZE); + pandaOption.SetLogLevel(panda::RuntimeOption::LOG_LEVEL::FOLLOW); + pandaOption.SetLogBufPrint(PrintVmLog); + + bool asmInterpreterEnabled = OHOS::system::GetBoolParameter("persist.ark.asminterpreter", true); + std::string asmOpcodeDisableRange = OHOS::system::GetParameter("persist.ark.asmopcodedisablerange", ""); + pandaOption.SetEnableAsmInterpreter(asmInterpreterEnabled); + pandaOption.SetAsmOpcodeDisableRange(asmOpcodeDisableRange); + + bool useAbilityRuntime = (options.isStageModel) || (options.isTestFramework); + if (useAbilityRuntime) { + bool aotEnabled = OHOS::system::GetBoolParameter("persist.ark.aot", true); + pandaOption.SetEnableAOT(aotEnabled); + pandaOption.SetProfileDir(SANDBOX_ARK_PROIFILE_PATH); + } + + OHOSJsEnvLogger::RegisterJsEnvLogger(); + // options eventRunner is nullptr + jsEnv = std::make_shared(std::make_unique(options.eventRunner)); + if (jsEnv == nullptr || !jsEnv->Initialize(pandaOption, static_cast(this))) { + HILOG_ERROR("Initialize js environment failed."); + return NATIVE_RUNTIME_INNER_ERROR; + } + + return AddEnv(reinterpret_cast(jsEnv->GetNativeEngine()), jsEnv); +} + +int32_t NativeRuntimeImpl::Init(const Options& options, napi_env env) +{ + auto jsEnv = GetJsEnv(env); + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return NATIVE_RUNTIME_INNER_ERROR; + } + + auto vm = GetEcmaVm(jsEnv); + if (!vm) { + HILOG_ERROR("vm is nullptr."); + return NATIVE_RUNTIME_INNER_ERROR; + } + + bool isModular = false; + if (!options.preload) { + LoadAotFile(options, jsEnv); + panda::JSNApi::SetBundle(vm, options.isBundle); + panda::JSNApi::SetBundleName(vm, options.bundleName); + panda::JSNApi::SetHostResolveBufferTracker( + vm, JsModuleReader(options.bundleName, options.hapPath, options.isUnique)); + isModular = !panda::JSNApi::IsBundle(vm); + panda::JSNApi::SetSearchHapPathTracker( + vm, [options](const std::string moduleName, std::string &hapPath) -> bool { + if (options.hapModulePath.find(moduleName) == options.hapModulePath.end()) { + return false; + } + hapPath = options.hapModulePath.find(moduleName)->second; + return true; + }); + } + + if (!preloaded_) { + InitConsoleModule(jsEnv); + } + + if (!options.preload) { + auto operatorObj = std::make_shared(options.bundleName, isModular); + InitSourceMap(operatorObj, jsEnv); + if (!options.isUnique) { + InitTimerModule(jsEnv); + } + InitWorkerModule(options, jsEnv); + SetModuleLoadChecker(options.moduleCheckerDelegate, jsEnv); + SetRequestAotCallback(jsEnv); + + if (!InitLoop(jsEnv)) { + HILOG_ERROR("Initialize loop failed."); + return NATIVE_RUNTIME_INNER_ERROR; + } + } + + preloaded_ = options.preload; + return NATIVE_RUNTIME_ERR_OK; +} + +int32_t NativeRuntimeImpl::AddEnv(napi_env env, std::shared_ptr jsEnv) +{ + std::lock_guard lock(envMutex_); + pid_t threadId = gettid(); + if (threadIds_.find(threadId) != threadIds_.end()) { + HILOG_ERROR("already created!"); + return NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV; + } + if (envMap_.size() >= MAX_ENV_COUNT) { + HILOG_ERROR("the maximum number of runtime environments that can be created is 16!"); + return NATIVE_RUNTIME_THREAD_COUNT_OVERLOAD; + } + threadIds_.insert(threadId); + HILOG_DEBUG("add threadId %{public}zu", threadId); + auto it = envMap_.find(env); + if (it == envMap_.end()) { + envMap_[env] = jsEnv; + return NATIVE_RUNTIME_ERR_OK; + } + return NATIVE_RUNTIME_INNER_ERROR; +} + +int32_t NativeRuntimeImpl::RemoveJsEnv(napi_env env) +{ + std::lock_guard lock(envMutex_); + pid_t threadId = gettid(); + HILOG_DEBUG("remove threadId %{public}zu", threadId); + threadIds_.erase(threadId); + auto it = envMap_.find(env); + if (it != envMap_.end()) { + it->second.reset(); + it->second = nullptr; + envMap_.erase(env); + return NATIVE_RUNTIME_ERR_OK; + } + return NATIVE_RUNTIME_DESTROY_FAILED; +} + +panda::ecmascript::EcmaVM* NativeRuntimeImpl::GetEcmaVm(const std::shared_ptr& jsEnv) const +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return nullptr; + } + return jsEnv->GetVM(); +} + +std::shared_ptr NativeRuntimeImpl::GetJsEnv(napi_env env) +{ + std::lock_guard lock(envMutex_); + auto jsEnv = envMap_.find(env); + if (jsEnv != envMap_.end()) { + return jsEnv->second; + } + return nullptr; +} + +void NativeRuntimeImpl::LoadAotFile(const Options& options, const std::shared_ptr& jsEnv) +{ + auto vm = GetEcmaVm(jsEnv); + if (!vm || options.hapPath.empty()) { + return; + } + + bool newCreate = false; + std::string loadPath = ExtractorUtil::GetLoadFilePath(options.hapPath); + std::shared_ptr extractor = ExtractorUtil::GetExtractor(loadPath, newCreate, true); + if (extractor != nullptr && newCreate) { + panda::JSNApi::LoadAotFile(vm, options.moduleName); + } +} + +void NativeRuntimeImpl::InitConsoleModule(const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + jsEnv->InitConsoleModule(); +} + +void NativeRuntimeImpl::InitSourceMap(const std::shared_ptr operatorObj, + const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + jsEnv->InitSourceMap(operatorObj); + JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData); + JsEnv::SourceMap::RegisterGetHapPathCallback(JsModuleReader::GetHapPathList); +} + +void NativeRuntimeImpl::InitTimerModule(const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + jsEnv->InitTimerModule(); +} + +void NativeRuntimeImpl::SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate, + const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + jsEnv->SetModuleLoadChecker(moduleCheckerDelegate); +} + +void NativeRuntimeImpl::SetRequestAotCallback(const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + auto callback = [](const std::string& bundleName, const std::string& moduleName, int32_t triggerMode) -> int32_t { + auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityMgr == nullptr) { + HILOG_ERROR("Failed to get system ability manager."); + return ERR_INVALID_VALUE; + } + + auto remoteObj = systemAbilityMgr->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObj == nullptr) { + HILOG_ERROR("Remote object is nullptr."); + return ERR_INVALID_VALUE; + } + + auto bundleMgr = iface_cast(remoteObj); + if (bundleMgr == nullptr) { + HILOG_ERROR("Failed to get bundle manager."); + return ERR_INVALID_VALUE; + } + + HILOG_DEBUG("Reset compile status, bundleName: %{public}s, moduleName: %{public}s, triggerMode: %{public}d.", + bundleName.c_str(), moduleName.c_str(), triggerMode); + return bundleMgr->ResetAOTCompileStatus(bundleName, moduleName, triggerMode); + }; + + jsEnv->SetRequestAotCallback(callback); +} + +bool NativeRuntimeImpl::InitLoop(const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return false; + } + return jsEnv->InitLoop(); +} + +void NativeRuntimeImpl::InitWorkerModule(const Options& options, const std::shared_ptr& jsEnv) +{ + if (jsEnv == nullptr) { + HILOG_ERROR("jsEnv is nullptr."); + return; + } + + std::shared_ptr workerInfo = std::make_shared(); + workerInfo->codePath = options.codePath; + workerInfo->isDebugVersion = options.isDebugVersion; + workerInfo->isBundle = options.isBundle; + workerInfo->packagePathStr = options.packagePathStr; + workerInfo->assetBasePathStr = options.assetBasePathStr; + workerInfo->hapPath = options.hapPath; + workerInfo->isStageModel = options.isStageModel; + workerInfo->moduleName = options.moduleName; + if (options.isJsFramework) { + SetJsFramework(); + } + jsEnv->InitWorkerModule(workerInfo); +} +} +} \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index fc7b430dea..2859d08d62 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -445,6 +445,7 @@ group("unittest") { "mission_listener_stub_test:unittest", "mission_listener_test:unittest", "napi_base_context_test:unittest", + "native_runtime_test:unittest", "os_account_manager_wrapper_test:unittest", "page_state_data_test:unittest", "pending_want_key_test:unittest", diff --git a/test/unittest/native_runtime_test/BUILD.gn b/test/unittest/native_runtime_test/BUILD.gn new file mode 100644 index 0000000000..06f6bbb37d --- /dev/null +++ b/test/unittest/native_runtime_test/BUILD.gn @@ -0,0 +1,55 @@ +# 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +module_output_path = "ability_runtime/native_runtime" + +ohos_unittest("native_runtime_test") { + module_out_path = module_output_path + include_dirs = [ + "${ability_runtime_path}/services/common/include", + "${ability_runtime_path}/interfaces/ndk/ability_runtime/include", + ] + + sources = [ + "native_runtime_test.cpp", + ] + + + deps = [ + "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", + "${ability_runtime_path}/interfaces/ndk/ability_runtime:runtime_ndk", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:extractortool", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "ets_runtime:libark_jsruntime", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "napi:ace_napi", + "samgr:samgr_proxy", + ] +} + +group("unittest") { + testonly = true + deps = [ ":native_runtime_test" ] +} diff --git a/test/unittest/native_runtime_test/native_runtime_test.cpp b/test/unittest/native_runtime_test/native_runtime_test.cpp new file mode 100644 index 0000000000..4f425f61b7 --- /dev/null +++ b/test/unittest/native_runtime_test/native_runtime_test.cpp @@ -0,0 +1,95 @@ +/* + * 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 + +#include "hilog_wrapper.h" +#include "js_runtime.h" +#include "js_environment.h" +#include "native_runtime_impl.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AbilityRuntime { + +class NativeRuntimeImplTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +}; + +void NativeRuntimeImplTest::SetUpTestCase() {} + +void NativeRuntimeImplTest::TearDownTestCase() {} + +void NativeRuntimeImplTest::SetUp() {} + +void NativeRuntimeImplTest::TearDown() {} + +HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_001, TestSize.Level1) +{ + HILOG_INFO("NativeRuntimeImplTest_001 start"); + Options options; + std::shared_ptr jsEnv = nullptr; + auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + + err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv->GetNativeEngine())); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + HILOG_INFO("NativeRuntimeImplTest_001 end"); +} + +HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_002, TestSize.Level1) +{ + HILOG_INFO("NativeRuntimeImplTest_002 start"); + Options options; + std::shared_ptr jsEnv = nullptr; + auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + + std::shared_ptr jsEnv2 = nullptr; + err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv2); + EXPECT_EQ(err, NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV); + + err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv->GetNativeEngine())); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + + err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv2->GetNativeEngine())); + EXPECT_EQ(err, NATIVE_RUNTIME_DESTROY_FAILED); + HILOG_INFO("NativeRuntimeImplTest_002 end"); +} + +HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_003, TestSize.Level1) +{ + HILOG_INFO("NativeRuntimeImplTest_003 start"); + Options options; + std::shared_ptr jsEnv = nullptr; + auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + + napi_env env = reinterpret_cast(jsEnv->GetNativeEngine()); + err = NativeRuntimeImpl::GetNativeRuntimeImpl().Init(options, env); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + + err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(env); + EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + HILOG_INFO("NativeRuntimeImplTest_003 end"); +} +} // namespace AAFwk +} // namespace OHOS \ No newline at end of file From ccbb307d9a7ec3e93d8b46d68200e7b99ebd3ec3 Mon Sep 17 00:00:00 2001 From: zhuhan Date: Tue, 5 Mar 2024 10:11:33 +0800 Subject: [PATCH 2/2] napi create env Signed-off-by: zhuhan Change-Id: I1d0d6e48e1a0fed1beddb5021c2c458e78e00aee --- bundle.json | 3 +- frameworks/native/runtime/js_runtime.cpp | 45 +++++++++++ .../native/runtime}/native_runtime_impl.cpp | 43 +++++------ interfaces/inner_api/runtime/BUILD.gn | 1 + .../runtime}/include/native_runtime_impl.h | 19 ++--- interfaces/ndk/ability_runtime/BUILD.gn | 68 ----------------- .../ability_runtime/include/native_err_code.h | 31 -------- .../ability_runtime/include/native_rumtime.h | 74 ------------------- .../ndk/ability_runtime/libruntime.ndk.json | 10 --- .../ability_runtime/src/native_rumtime.cpp | 54 -------------- test/unittest/native_runtime_test/BUILD.gn | 12 +-- .../native_runtime_test.cpp | 35 ++++++--- 12 files changed, 107 insertions(+), 288 deletions(-) rename {interfaces/ndk/ability_runtime/src => frameworks/native/runtime}/native_runtime_impl.cpp (90%) rename interfaces/{ndk/ability_runtime => inner_api/runtime}/include/native_runtime_impl.h (84%) delete mode 100644 interfaces/ndk/ability_runtime/BUILD.gn delete mode 100644 interfaces/ndk/ability_runtime/include/native_err_code.h delete mode 100644 interfaces/ndk/ability_runtime/include/native_rumtime.h delete mode 100644 interfaces/ndk/ability_runtime/libruntime.ndk.json delete mode 100644 interfaces/ndk/ability_runtime/src/native_rumtime.cpp diff --git a/bundle.json b/bundle.json index de9617d4da..e1d33b0347 100644 --- a/bundle.json +++ b/bundle.json @@ -107,8 +107,7 @@ "//foundation/ability/ability_runtime/js_environment/frameworks/js_environment:js_environment", "//foundation/ability/ability_runtime/services/abilitymgr/etc:appfwk_etc", "//foundation/ability/ability_runtime/service_router_framework:srms_target", - "//foundation/ability/ability_runtime/service_router_framework:jsapi_target", - "//foundation/ability/ability_runtime/interfaces/ndk/ability_runtime:runtime_ndk" + "//foundation/ability/ability_runtime/service_router_framework:jsapi_target" ], "inner_api": [ { diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 77f8f40b27..96c5b4d779 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -47,7 +47,9 @@ #include "module_checker_delegate.h" #include "napi/native_api.h" #include "native_engine/impl/ark/ark_native_engine.h" +#include "native_engine/native_create_env.h" #include "native_engine/native_engine.h" +#include "native_runtime_impl.h" #include "ohos_js_env_logger.h" #include "ohos_js_environment_impl.h" #include "parameters.h" @@ -143,6 +145,47 @@ int32_t PrintVmLog(int32_t, int32_t, const char*, const char*, const char* messa HILOG_INFO("ArkLog: %{public}s", message); return 0; } + +napi_status CreateNapiEnv(napi_env *env) +{ + HILOG_DEBUG("Called"); + if (env == nullptr) { + HILOG_ERROR("Invalid arg"); + return napi_status::napi_invalid_arg; + } + auto options = JsRuntime::GetChildOptions(); + if (options == nullptr) { + HILOG_ERROR("options is null, it maybe application startup failed!"); + return napi_status::napi_generic_failure; + } + std::shared_ptr jsEnv = nullptr; + auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(*options, jsEnv); + if (errCode != napi_status::napi_ok) { + HILOG_ERROR("CreateJsEnv failed"); + return errCode; + } + *env = reinterpret_cast(jsEnv->GetNativeEngine()); + if (env == nullptr) { + HILOG_ERROR("CreateJsEnv failed"); + return napi_status::napi_generic_failure; + } + return NativeRuntimeImpl::GetNativeRuntimeImpl().Init(*options, *env); +} + +napi_status DestroyNapiEnv(napi_env *env) +{ + HILOG_DEBUG("Called"); + if (env == nullptr) { + HILOG_ERROR("Invalid arg"); + return napi_status::napi_invalid_arg; + } + auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(*env); + if (errCode == napi_status::napi_ok) { + *env = nullptr; + } + return errCode; +} + } // namespace std::atomic JsRuntime::hasInstance(false); @@ -605,6 +648,8 @@ bool JsRuntime::Initialize(const Options& options) HILOG_ERROR("Create js environment failed."); return false; } + NativeCreateEnv::RegCreateNapiEnvCallback(CreateNapiEnv); + NativeCreateEnv::RegDestroyNapiEnvCallback(DestroyNapiEnv); } else { jsEnv_->StartMonitorJSHeapUsage(); } diff --git a/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp b/frameworks/native/runtime/native_runtime_impl.cpp similarity index 90% rename from interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp rename to frameworks/native/runtime/native_runtime_impl.cpp index 855b06b90d..b9acee5100 100644 --- a/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp +++ b/frameworks/native/runtime/native_runtime_impl.cpp @@ -15,6 +15,8 @@ #include "native_runtime_impl.h" +#include + #include "bundle_mgr_interface.h" #include "hilog_wrapper.h" #include "iservice_registry.h" @@ -61,7 +63,7 @@ NativeRuntimeImpl& NativeRuntimeImpl::GetNativeRuntimeImpl() return nativeRuntimeImpl; } -int32_t NativeRuntimeImpl::CreateJsEnv(const Options& options, std::shared_ptr& jsEnv) +napi_status NativeRuntimeImpl::CreateJsEnv(const Options& options, std::shared_ptr& jsEnv) { HILOG_DEBUG("called"); panda::RuntimeOption pandaOption; @@ -95,26 +97,27 @@ int32_t NativeRuntimeImpl::CreateJsEnv(const Options& options, std::shared_ptr(std::make_unique(options.eventRunner)); - if (jsEnv == nullptr || !jsEnv->Initialize(pandaOption, static_cast(this))) { + if (jsEnv == nullptr || !jsEnv->Initialize(pandaOption, static_cast(this)) + || jsEnv->GetNativeEngine() == nullptr) { HILOG_ERROR("Initialize js environment failed."); - return NATIVE_RUNTIME_INNER_ERROR; + return napi_status::napi_ok; } - + jsEnv->GetNativeEngine()->MarkNativeThread(); return AddEnv(reinterpret_cast(jsEnv->GetNativeEngine()), jsEnv); } -int32_t NativeRuntimeImpl::Init(const Options& options, napi_env env) +napi_status NativeRuntimeImpl::Init(const Options& options, napi_env env) { auto jsEnv = GetJsEnv(env); if (jsEnv == nullptr) { HILOG_ERROR("jsEnv is nullptr."); - return NATIVE_RUNTIME_INNER_ERROR; + return napi_status::napi_generic_failure; } auto vm = GetEcmaVm(jsEnv); if (!vm) { HILOG_ERROR("vm is nullptr."); - return NATIVE_RUNTIME_INNER_ERROR; + return napi_status::napi_generic_failure; } bool isModular = false; @@ -151,37 +154,37 @@ int32_t NativeRuntimeImpl::Init(const Options& options, napi_env env) if (!InitLoop(jsEnv)) { HILOG_ERROR("Initialize loop failed."); - return NATIVE_RUNTIME_INNER_ERROR; + return napi_status::napi_generic_failure; } } preloaded_ = options.preload; - return NATIVE_RUNTIME_ERR_OK; + return napi_status::napi_ok; } -int32_t NativeRuntimeImpl::AddEnv(napi_env env, std::shared_ptr jsEnv) +napi_status NativeRuntimeImpl::AddEnv(napi_env env, std::shared_ptr jsEnv) { std::lock_guard lock(envMutex_); pid_t threadId = gettid(); if (threadIds_.find(threadId) != threadIds_.end()) { HILOG_ERROR("already created!"); - return NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV; + return napi_status::napi_create_ark_runtime_only_one_env_per_thread; } if (envMap_.size() >= MAX_ENV_COUNT) { HILOG_ERROR("the maximum number of runtime environments that can be created is 16!"); - return NATIVE_RUNTIME_THREAD_COUNT_OVERLOAD; + return napi_status::napi_create_ark_runtime_too_many_envs; } threadIds_.insert(threadId); HILOG_DEBUG("add threadId %{public}zu", threadId); auto it = envMap_.find(env); if (it == envMap_.end()) { envMap_[env] = jsEnv; - return NATIVE_RUNTIME_ERR_OK; + return napi_status::napi_ok; } - return NATIVE_RUNTIME_INNER_ERROR; + return napi_status::napi_generic_failure; } -int32_t NativeRuntimeImpl::RemoveJsEnv(napi_env env) +napi_status NativeRuntimeImpl::RemoveJsEnv(napi_env env) { std::lock_guard lock(envMutex_); pid_t threadId = gettid(); @@ -192,9 +195,9 @@ int32_t NativeRuntimeImpl::RemoveJsEnv(napi_env env) it->second.reset(); it->second = nullptr; envMap_.erase(env); - return NATIVE_RUNTIME_ERR_OK; + return napi_status::napi_ok; } - return NATIVE_RUNTIME_DESTROY_FAILED; + return napi_status::napi_destroy_ark_runtime_env_not_exist; } panda::ecmascript::EcmaVM* NativeRuntimeImpl::GetEcmaVm(const std::shared_ptr& jsEnv) const @@ -248,8 +251,6 @@ void NativeRuntimeImpl::InitSourceMap(const std::shared_ptrInitSourceMap(operatorObj); - JsEnv::SourceMap::RegisterReadSourceMapCallback(JsRuntime::ReadSourceMapData); - JsEnv::SourceMap::RegisterGetHapPathCallback(JsModuleReader::GetHapPathList); } void NativeRuntimeImpl::InitTimerModule(const std::shared_ptr& jsEnv) @@ -334,5 +335,5 @@ void NativeRuntimeImpl::InitWorkerModule(const Options& options, const std::shar } jsEnv->InitWorkerModule(workerInfo); } -} -} \ No newline at end of file +} // namespace AbilityRuntime +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index 30073696b1..726682be36 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -67,6 +67,7 @@ ohos_shared_library("runtime") { "${ability_runtime_native_path}/runtime/js_runtime.cpp", "${ability_runtime_native_path}/runtime/js_runtime_utils.cpp", "${ability_runtime_native_path}/runtime/js_worker.cpp", + "${ability_runtime_native_path}/runtime/native_runtime_impl.cpp", "${ability_runtime_native_path}/runtime/ohos_js_env_logger.cpp", "${ability_runtime_native_path}/runtime/ohos_js_environment_impl.cpp", "${ability_runtime_native_path}/runtime/ohos_loop_handler.cpp", diff --git a/interfaces/ndk/ability_runtime/include/native_runtime_impl.h b/interfaces/inner_api/runtime/include/native_runtime_impl.h similarity index 84% rename from interfaces/ndk/ability_runtime/include/native_runtime_impl.h rename to interfaces/inner_api/runtime/include/native_runtime_impl.h index bec67c5edc..c1ecb6703f 100644 --- a/interfaces/ndk/ability_runtime/include/native_runtime_impl.h +++ b/interfaces/inner_api/runtime/include/native_runtime_impl.h @@ -16,14 +16,15 @@ #ifndef ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H #define ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H -#include "js_runtime.h" - #include #include #include #include -#include "native_err_code.h" +#include "js_environment.h" +#include "native_engine/native_engine.h" +#include "runtime.h" + using Options = OHOS::AbilityRuntime::Runtime::Options; namespace OHOS { namespace AbilityRuntime { @@ -32,14 +33,14 @@ public: NativeRuntimeImpl(const NativeRuntimeImpl&) = delete; NativeRuntimeImpl& operator=(const NativeRuntimeImpl&) = delete; static NativeRuntimeImpl& GetNativeRuntimeImpl(); - int32_t CreateJsEnv(const Options& options, std::shared_ptr& jsEnv); - int32_t RemoveJsEnv(napi_env env); - int32_t Init(const Options& options, napi_env env); + napi_status CreateJsEnv(const Options& options, std::shared_ptr& jsEnv); + napi_status RemoveJsEnv(napi_env env); + napi_status Init(const Options& options, napi_env env); private: NativeRuntimeImpl(); ~NativeRuntimeImpl(); - int32_t AddEnv(napi_env env, std::shared_ptr jsEnv); + napi_status AddEnv(napi_env env, std::shared_ptr jsEnv); panda::ecmascript::EcmaVM* GetEcmaVm(const std::shared_ptr& jsEnv) const; std::shared_ptr GetJsEnv(napi_env env); void LoadAotFile(const Options& options, const std::shared_ptr& jsEnv); @@ -57,6 +58,6 @@ private: bool preloaded_ = false; std::mutex envMutex_; }; -} -} +} // namespace AbilityRuntime +} // namespace OHOS #endif // ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_IMPL_H \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/BUILD.gn b/interfaces/ndk/ability_runtime/BUILD.gn deleted file mode 100644 index 7b04f2e2e0..0000000000 --- a/interfaces/ndk/ability_runtime/BUILD.gn +++ /dev/null @@ -1,68 +0,0 @@ -# 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. - -import("//build/ohos.gni") -import("//foundation/ability/ability_runtime/ability_runtime.gni") - -config("runtime_ndk_config") { - defines = [ "AMS_LOG_TAG = \"Runtime\"" ] - defines += [ "AMS_LOG_DOMAIN = 0xD001309" ] - - include_dirs = [ "include" ] -} - -ohos_ndk_headers("runtime_header") { - dest_dir = "$ndk_headers_out_dir/ability_runtime/" - sources = [ "./include/native_rumtime.h" ] -} - -ohos_ndk_library("libruntime_ndk") { - ndk_description_file = "./libruntime.ndk.json" - min_compact_version = "12" - output_name = "runtime_ndk" -} - -ohos_shared_library("runtime_ndk") { - include_dirs = [ - "${ability_runtime_native_path}/runtime", - "include", - ] - - sources = [ - "${ability_runtime_path}/interfaces/ndk/ability_runtime/src/native_rumtime.cpp", - "${ability_runtime_path}/interfaces/ndk/ability_runtime/src/native_runtime_impl.cpp" - ] - - configs = [ ":runtime_ndk_config" ] - - deps = [ - "${ability_runtime_innerkits_path}/runtime:runtime", - "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", - ] - - external_deps = [ - "ability_base:extractortool", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "ets_runtime:libark_jsruntime", - "hilog:libhilog", - "init:libbegetutil", - "ipc:ipc_core", - "napi:ace_napi", - "samgr:samgr_proxy", - ] - - innerapi_tags = [ "ndk" ] - subsystem_name = "ability" - part_name = "ability_runtime" -} \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/include/native_err_code.h b/interfaces/ndk/ability_runtime/include/native_err_code.h deleted file mode 100644 index 6305b03fd0..0000000000 --- a/interfaces/ndk/ability_runtime/include/native_err_code.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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. - */ - -#ifndef ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H -#define ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H - -namespace OHOS { -namespace AbilityRuntime { -enum { - NATIVE_RUNTIME_ERR_OK, - NATIVE_RUNTIME_THREAD_COUNT_OVERLOAD, - NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV, - NATIVE_RUNTIME_DESTROY_FAILED, - NATIVE_RUNTIME_INNER_ERROR -}; -} // namespace AbilityRuntime -} // namespace OHOS -#endif // ABILITY_ABILITY_RUNTIME_NATIVE_ERR_CODE_H - diff --git a/interfaces/ndk/ability_runtime/include/native_rumtime.h b/interfaces/ndk/ability_runtime/include/native_rumtime.h deleted file mode 100644 index df32633706..0000000000 --- a/interfaces/ndk/ability_runtime/include/native_rumtime.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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. - */ - -/** - * @addtogroup Ability_Runtime - * @{ - * - * @brief Provides runtime environment. - - * - * @since 12 - * @version 1.0 - */ - -/** - * @file native_rumtime.h - * - * @brief Declare interfaces for creating and destroying runtime environments. - * - * @library libruntime_ndk.z.so - * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore - * @since 12 - * @version 1.0 - */ -#ifndef ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H -#define ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H - -#include -#include "napi/native_api.h" -#ifdef __cplusplus -extern "C" { -#endif -/** - * @brief Obtains the application info based on the The current bundle. - * - * @param env: The environment that the API is invoked under. - * @return 0 - Success. - * 1 - The maximum number of runtime environments exceeded. - * 2 - One thread is allowed to create only one runtime environment. - * 4 - Internal error. - * @since 12 - * @version 1.0 - */ -int32_t OH_NativeAbility_Create_NapiEnv(napi_env *env); - - -/** - * @brief Obtains the application info based on the The current bundle. - * - * @param env: The environment that the API is invoked under. - * @return 0 - Success. - * 3 - Destroy failed. - * @since 12 - * @version 1.0 - */ -int32_t OH_NativeAbility_Destroy_NapiEnv(napi_env *env); - -#ifdef __cplusplus -}; -#endif -/** @} */ -#endif // ABILITY_ABILITY_RUNTIME_NATIVE_RUNTIME_H \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/libruntime.ndk.json b/interfaces/ndk/ability_runtime/libruntime.ndk.json deleted file mode 100644 index e6d4b55737..0000000000 --- a/interfaces/ndk/ability_runtime/libruntime.ndk.json +++ /dev/null @@ -1,10 +0,0 @@ -[ - { - "first_introduced": "12", - "name": "OH_NativeAbility_Create_NapiEnv" - }, - { - "first_introduced": "12", - "name": "OH_NativeAbility_Destroy_NapiEnv" - } -] \ No newline at end of file diff --git a/interfaces/ndk/ability_runtime/src/native_rumtime.cpp b/interfaces/ndk/ability_runtime/src/native_rumtime.cpp deleted file mode 100644 index 8a7a690d65..0000000000 --- a/interfaces/ndk/ability_runtime/src/native_rumtime.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * 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 "native_rumtime.h" - -#include "js_environment.h" -#include "hilog_wrapper.h" -#include "native_runtime_impl.h" - -using NativeRuntimeImpl = OHOS::AbilityRuntime::NativeRuntimeImpl; - -int32_t OH_NativeAbility_Create_NapiEnv(napi_env *env) -{ - HILOG_INFO("call OH_NativeAbility_Create_NapiEnv"); - auto options = OHOS::AbilityRuntime::JsRuntime::GetChildOptions(); - if (options == nullptr) { - HILOG_ERROR("options is null, it maybe application startup failed!"); - return OHOS::AbilityRuntime::NATIVE_RUNTIME_INNER_ERROR; - } - std::shared_ptr jsEnv = nullptr; - auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(*options, jsEnv); - if (errCode != OHOS::AbilityRuntime::NATIVE_RUNTIME_ERR_OK) { - return errCode; - } - HILOG_INFO("CreateJsEnv ok"); - *env = reinterpret_cast(jsEnv->GetNativeEngine()); - if (env == nullptr) { - HILOG_ERROR("CreateJsEnv failed"); - return OHOS::AbilityRuntime::NATIVE_RUNTIME_INNER_ERROR; - } - return NativeRuntimeImpl::GetNativeRuntimeImpl().Init(*options, *env); -} - -int32_t OH_NativeAbility_Destroy_NapiEnv(napi_env *env) -{ - HILOG_INFO("call OH_NativeAbility_Destroy_NapiEnv"); - auto errCode = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(*env); - if (errCode == OHOS::AbilityRuntime::NATIVE_RUNTIME_ERR_OK) { - *env = nullptr; - } - return errCode; -} \ No newline at end of file diff --git a/test/unittest/native_runtime_test/BUILD.gn b/test/unittest/native_runtime_test/BUILD.gn index 06f6bbb37d..c6bf4918b1 100644 --- a/test/unittest/native_runtime_test/BUILD.gn +++ b/test/unittest/native_runtime_test/BUILD.gn @@ -19,19 +19,13 @@ module_output_path = "ability_runtime/native_runtime" ohos_unittest("native_runtime_test") { module_out_path = module_output_path - include_dirs = [ - "${ability_runtime_path}/services/common/include", - "${ability_runtime_path}/interfaces/ndk/ability_runtime/include", - ] - - sources = [ - "native_runtime_test.cpp", - ] + include_dirs = [ "${ability_runtime_innerkits_path}/runtime/include" ] + sources = [ "native_runtime_test.cpp" ] deps = [ + "${ability_runtime_innerkits_path}/runtime:runtime", "${ability_runtime_path}/js_environment/frameworks/js_environment:js_environment", - "${ability_runtime_path}/interfaces/ndk/ability_runtime:runtime_ndk", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] diff --git a/test/unittest/native_runtime_test/native_runtime_test.cpp b/test/unittest/native_runtime_test/native_runtime_test.cpp index 4f425f61b7..bebf6b5463 100644 --- a/test/unittest/native_runtime_test/native_runtime_test.cpp +++ b/test/unittest/native_runtime_test/native_runtime_test.cpp @@ -42,54 +42,69 @@ void NativeRuntimeImplTest::SetUp() {} void NativeRuntimeImplTest::TearDown() {} +/** + * @tc.name: NativeRuntimeImplTest_001 + * @tc.desc: basic function test. + * @tc.type: FUNC + */ HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_001, TestSize.Level1) { HILOG_INFO("NativeRuntimeImplTest_001 start"); Options options; std::shared_ptr jsEnv = nullptr; auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv->GetNativeEngine())); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); HILOG_INFO("NativeRuntimeImplTest_001 end"); } +/** + * @tc.name: NativeRuntimeImplTest_002 + * @tc.desc: basic function test. + * @tc.type: FUNC + */ HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_002, TestSize.Level1) { HILOG_INFO("NativeRuntimeImplTest_002 start"); Options options; std::shared_ptr jsEnv = nullptr; auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); std::shared_ptr jsEnv2 = nullptr; err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv2); - EXPECT_EQ(err, NATIVE_RUNTIME_THREAD_ONLY_ONE_RUNENV); + EXPECT_EQ(err, napi_status::napi_create_ark_runtime_only_one_env_per_thread); err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv->GetNativeEngine())); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(reinterpret_cast(jsEnv2->GetNativeEngine())); - EXPECT_EQ(err, NATIVE_RUNTIME_DESTROY_FAILED); + EXPECT_EQ(err, napi_status::napi_destroy_ark_runtime_env_not_exist); HILOG_INFO("NativeRuntimeImplTest_002 end"); } +/** + * @tc.name: NativeRuntimeImplTest_003 + * @tc.desc: basic function test. + * @tc.type: FUNC + */ HWTEST_F(NativeRuntimeImplTest, NativeRuntimeImplTest_003, TestSize.Level1) { HILOG_INFO("NativeRuntimeImplTest_003 start"); Options options; std::shared_ptr jsEnv = nullptr; auto err = NativeRuntimeImpl::GetNativeRuntimeImpl().CreateJsEnv(options, jsEnv); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); napi_env env = reinterpret_cast(jsEnv->GetNativeEngine()); err = NativeRuntimeImpl::GetNativeRuntimeImpl().Init(options, env); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); err = NativeRuntimeImpl::GetNativeRuntimeImpl().RemoveJsEnv(env); - EXPECT_EQ(err, NATIVE_RUNTIME_ERR_OK); + EXPECT_EQ(err, napi_status::napi_ok); HILOG_INFO("NativeRuntimeImplTest_003 end"); } -} // namespace AAFwk +} // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file