diff --git a/interfaces/inner_api/napi/native_node_api.h b/interfaces/inner_api/napi/native_node_api.h index 18fd603f315e61130f9f7a6726c3eeafedf5e05c..0b92f670348823daf594e15b94592c3dec985130 100644 --- a/interfaces/inner_api/napi/native_node_api.h +++ b/interfaces/inner_api/napi/native_node_api.h @@ -234,6 +234,11 @@ NAPI_EXTERN napi_status napi_wrap_with_xref(napi_env env, void* native_object, napi_finalize finalize_cb, napi_ref* result); +NAPI_EXTERN napi_status napi_is_alive_object(napi_env env, napi_ref ref, bool* result); +NAPI_EXTERN napi_status napi_is_contain_object(napi_env env, napi_ref ref, bool* result); +NAPI_EXTERN napi_status napi_is_xref_type(napi_env env, napi_value js_object, bool* result); +NAPI_EXTERN napi_status napi_get_ets_implements(napi_env env, napi_value value, napi_value* result); +NAPI_EXTERN napi_status napi_setup_hybrid_environment(napi_env env); #endif // PANDA_JS_ETS_HYBRID_MODE NAPI_EXTERN napi_status napi_is_alive_object(napi_env env, napi_ref ref, bool* result); NAPI_EXTERN napi_status napi_is_contain_object(napi_env env, napi_ref ref, bool* result); diff --git a/module_manager/native_module_manager.cpp b/module_manager/native_module_manager.cpp index 0b43b0b34cc992a23ce2f335e9bb256de6ecd096..911602ecb19057790c74b72c464d05ccba8fb0d1 100644 --- a/module_manager/native_module_manager.cpp +++ b/module_manager/native_module_manager.cpp @@ -43,6 +43,10 @@ enum ModuleLoadFailedReason : uint32_t { MODULE_LOAD_SUCCESS = 0, MODULE_NOT_EXIST = 1, }; +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \ + !defined(LINUX_PLATFORM) +constexpr char MODULE_NS[] = "moduleNs_"; +#endif } // namespace NativeModuleManager* NativeModuleManager::instance_ = NULL; @@ -454,7 +458,7 @@ void NativeModuleManager::CreateLdNamespace(const std::string moduleName, const Dl_namespace ns; // Create module ns. - std::string nsName = "moduleNs_" + moduleName; + std::string nsName = MODULE_NS + moduleName; dlns_init(&ns, nsName.c_str()); dlns_get(nullptr, ¤t_ns); @@ -502,6 +506,21 @@ void NativeModuleManager::CreateLdNamespace(const std::string moduleName, const #endif } +bool NativeModuleManager::GetLdNamespaceName(const std::string &moduleName, std::string &nsName) +{ +#if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(__BIONIC__) && !defined(IOS_PLATFORM) && \ + !defined(LINUX_PLATFORM) + if (nsMap_.find(moduleName) == nsMap_.end()) { + HILOG_ERROR("not found ns: %{public}s", moduleName.c_str()); + return false; + } + nsName = MODULE_NS + moduleName; + return true; +#else + return false; +#endif +} + void NativeModuleManager::SetAppLibPath(const std::string& moduleName, const std::vector& appLibPath, const bool& isSystemApp) { diff --git a/module_manager/native_module_manager.h b/module_manager/native_module_manager.h index 73563033b5095c29ff5412097d2bc3d583782f9c..6ac3f8819967476d16062426f198911c55236eab 100644 --- a/module_manager/native_module_manager.h +++ b/module_manager/native_module_manager.h @@ -86,6 +86,7 @@ public: void Register(NativeModule* nativeModule); void SetAppLibPath(const std::string& moduleName, const std::vector& appLibPath, const bool& isSystemApp = false); + bool GetLdNamespaceName(const std::string &moduleName, std::string &nsName); NativeModule* LoadNativeModule(const char* moduleName, const char* path, bool isAppModule, std::string& errInfo, bool internal = false, const char* relativePath = ""); void SetNativeEngine(std::string moduleName, NativeEngine* nativeEngine); diff --git a/module_manager/test/unittest/module_manager_test/module_manager_test.cpp b/module_manager/test/unittest/module_manager_test/module_manager_test.cpp index 9722ade09b9cf5c9c4a2e55aadf16a4659e58e0c..61bc45214a0db89e8e4a77914a195b8dd3cc3659 100644 --- a/module_manager/test/unittest/module_manager_test/module_manager_test.cpp +++ b/module_manager/test/unittest/module_manager_test/module_manager_test.cpp @@ -49,6 +49,8 @@ void ModuleManagerTest::TearDown() MockResetModuleManagerState(); } +constexpr char MODULE_NS[] = "moduleNs_"; + /* * @tc.name: LoadNativeModuleTest_001 * @tc.desc: test NativeModule's LoadNativeModule function @@ -227,7 +229,13 @@ HWTEST_F(ModuleManagerTest, LoadNativeModuleTest_008, TestSize.Level1) moduleManager->Register(nullptr); moduleManager->CreateSharedLibsSonames(); + std::string nsName; + bool res = moduleManager->GetLdNamespaceName(moduleName, nsName); + EXPECT_EQ(res, false); moduleManager->CreateLdNamespace(moduleName, libPath, true); + res = moduleManager->GetLdNamespaceName(moduleName, nsName); + EXPECT_EQ(res, true); + EXPECT_STREQ(nsName.c_str(), (MODULE_NS + moduleName).c_str()); GTEST_LOG_(INFO) << "ModuleManagerTest, LoadNativeModuleTest_008 end"; } @@ -245,7 +253,13 @@ HWTEST_F(ModuleManagerTest, LoadNativeModuleTest_009, TestSize.Level1) std::shared_ptr moduleManager = std::make_shared(); ASSERT_NE(nullptr, moduleManager); + std::string nsName; + bool res = moduleManager->GetLdNamespaceName(moduleName, nsName); + EXPECT_EQ(res, false); moduleManager->CreateLdNamespace(moduleName, libPath, false); + res = moduleManager->GetLdNamespaceName(moduleName, nsName); + EXPECT_EQ(res, true); + EXPECT_STREQ(nsName.c_str(), (MODULE_NS + moduleName).c_str()); std::vector appLibPath; moduleManager->SetAppLibPath(moduleName, appLibPath, false); GTEST_LOG_(INFO) << "ModuleManagerTest, LoadNativeModuleTest_009 end"; diff --git a/native_engine/native_api.cpp b/native_engine/native_api.cpp index baa7a45656f1466ce08e381d056e0972c242681c..7fcaa3d5809737da6b881c52e62b26ac658c5154 100644 --- a/native_engine/native_api.cpp +++ b/native_engine/native_api.cpp @@ -4743,7 +4743,7 @@ NAPI_EXTERN napi_status napi_mark_from_object(napi_env env, napi_ref ref) return napi_clear_last_error(env); } -NAPI_EXTERN napi_status napi_is_alive_object(napi_env env, napi_ref ref, bool* result) +NAPI_EXTERN napi_status napi_is_alive_object(napi_env env, napi_ref ref, bool *result) { NAPI_PREAMBLE(env); CHECK_ARG(env, ref); @@ -4752,7 +4752,7 @@ NAPI_EXTERN napi_status napi_is_alive_object(napi_env env, napi_ref ref, bool* r return napi_clear_last_error(env); } -NAPI_EXTERN napi_status napi_is_contain_object(napi_env env, napi_ref ref, bool* result) +NAPI_EXTERN napi_status napi_is_contain_object(napi_env env, napi_ref ref, bool *result) { NAPI_PREAMBLE(env); CHECK_ARG(env, ref); diff --git a/native_engine/native_node_api.cpp b/native_engine/native_node_api.cpp index 6f865f898c0c11fa685726fce6d5f1db12c8e9d1..f28ad3214eb2b9aec6cebea14028b5ab6ad06d4f 100644 --- a/native_engine/native_node_api.cpp +++ b/native_engine/native_node_api.cpp @@ -643,3 +643,27 @@ NAPI_EXTERN napi_status napi_set_module_validate_callback(napi_module_validate_c } return napi_generic_failure; } + +NAPI_EXTERN napi_status napi_get_ets_implements(napi_env env, napi_value value, napi_value* result) +{ + NAPI_PREAMBLE(env); + CHECK_ARG(env, value); + CHECK_ARG(env, result); + + auto nativeValue = LocalValueFromJsValue(value); + auto engine = reinterpret_cast(env); + auto vm = engine->GetEcmaVm(); + Local implementsValue = panda::JSNApi::GetImplements(vm, nativeValue); + *result = JsValueFromLocalValue(implementsValue); + return GET_RETURN_STATUS(env); +} + +NAPI_EXTERN napi_status napi_setup_hybrid_environment(napi_env env) +{ + NAPI_PREAMBLE(env); + + auto engine = reinterpret_cast(env); + auto vm = engine->GetEcmaVm(); + panda::JSNApi::InitHybridVMEnv(vm); + return GET_RETURN_STATUS(env); +} diff --git a/test/unittest/test_napi.cpp b/test/unittest/test_napi.cpp index fc40f24b84d32254bbd2b9ed7ba8a9ac51eb0ce6..51869f0e8b81e82c4c0cda8d264f271dedb28581 100644 --- a/test/unittest/test_napi.cpp +++ b/test/unittest/test_napi.cpp @@ -8308,6 +8308,21 @@ HWTEST_F(NapiBasicTest, NapiMakeCallbackTest002, testing::ext::TestSize.Level1) ASSERT_EQ(status, napi_invalid_arg); } +/** + * @tc.name: NapiGetEtsImplementsTest + * @tc.desc: Test interface of napi_get_ets_implements + * @tc.type: FUNC + */ +HWTEST_F(NapiBasicTest, NapiGetEtsImplementsTest, testing::ext::TestSize.Level1) +{ + napi_env env = reinterpret_cast(engine_); + napi_value value = nullptr; + napi_value result = nullptr; + + napi_status status = napi_get_ets_implements(env, value, &result); + ASSERT_EQ(status, napi_invalid_arg); +} + HWTEST_F(NapiBasicTest, NapiAsyncDestroyTest001, testing::ext::TestSize.Level1) { napi_env env = reinterpret_cast(engine_); @@ -9082,6 +9097,47 @@ HWTEST_F(NapiBasicTest, NapiGetValueBigintInt64Test004, testing::ext::TestSize.L ASSERT_EQ(status, napi_bigint_expected); } +HWTEST_F(NapiBasicTest, NapiIsAliveObjectTest001, testing::ext::TestSize.Level1) +{ + napi_env env = reinterpret_cast(engine_); + napi_value obj; + napi_ref result = nullptr; + bool res = false; + + napi_create_object(env, &obj); + napi_status status = + napi_wrap_with_xref(env, obj, (void*)TEST_STRING, [](napi_env, void* data, void* hint) {}, &result); + ASSERT_EQ(status, napi_ok); + status = napi_is_alive_object(env, result, &res); + ASSERT_EQ(status, napi_ok); +} + +HWTEST_F(NapiBasicTest, NapiIsValidHeapObjectTest001, testing::ext::TestSize.Level1) +{ + napi_env env = reinterpret_cast(engine_); + napi_value obj; + napi_ref result = nullptr; + bool res = false; + + napi_create_object(env, &obj); + napi_status status = + napi_wrap_with_xref(env, obj, (void*)TEST_STRING, [](napi_env, void* data, void* hint) {}, &result); + ASSERT_EQ(status, napi_ok); + status = napi_is_contain_object(env, result, &res); + ASSERT_EQ(status, napi_ok); +} + +HWTEST_F(NapiBasicTest, NapiIsXrefTypeTest001, testing::ext::TestSize.Level1) +{ + napi_env env = reinterpret_cast(engine_); + napi_value obj; + bool res = false; + + napi_create_object(env, &obj); + napi_status status = napi_is_xref_type(env, obj, &res); + ASSERT_EQ(status, napi_ok); +} + HWTEST_F(NapiBasicTest, NapiGetValueBigintInt64Test005, testing::ext::TestSize.Level1) { napi_env env = reinterpret_cast(engine_);