From 3bcf7b853a876fdf871c68784823857b1a6d8f73 Mon Sep 17 00:00:00 2001 From: ningqicheng Date: Thu, 11 Sep 2025 15:17:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ningqicheng Change-Id: I89c838119b32c657b4ada0b349cb8eb2f0f072bd --- interfaces/inner_api/napi/native_common.h | 6 +++ test/unittest/test_napi.cpp | 63 +++++++++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/interfaces/inner_api/napi/native_common.h b/interfaces/inner_api/napi/native_common.h index 1d8e9b31c..fbb128514 100644 --- a/interfaces/inner_api/napi/native_common.h +++ b/interfaces/inner_api/napi/native_common.h @@ -100,6 +100,12 @@ (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \ } +// W-writable E-enumerable C-configurable napi_writable is WEC +#define DECLARE_NAPI_WRITABLE_FUNCTION(name, func) \ + { \ + (name), nullptr, (func), nullptr, nullptr, nullptr, napi_writable, nullptr \ + } + // W-writable E-enumerable C-configurable napi_default_jsproperty is WEC #define DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION(name, func) \ { \ diff --git a/test/unittest/test_napi.cpp b/test/unittest/test_napi.cpp index 7e05d9979..6bf03dc75 100644 --- a/test/unittest/test_napi.cpp +++ b/test/unittest/test_napi.cpp @@ -2453,7 +2453,6 @@ HWTEST_F(NapiBasicTest, ObjectWrapperTest004, testing::ext::TestSize.Level1) LoggerCollector collector(LogLevel::LOG_WARN, LOG_DOMAIN); uint8_t* data1 = new uint8_t; ASSERT_EQ(napi_wrap(engine, object, data1, finalizer, nullptr, nullptr), napi_ok); - ASSERT_TRUE(collector.Includes("napi_wrap: current js_object has been wrapped.")); }); t.join(); } @@ -2481,7 +2480,6 @@ HWTEST_F(NapiBasicTest, ObjectWrapperTest005, testing::ext::TestSize.Level1) uint8_t* data1 = new uint8_t; ASSERT_EQ(napi_wrap_async_finalizer(engine, object, data1, finalizer, nullptr, nullptr, sizeof(data0)), napi_ok); - ASSERT_TRUE(collector.Includes("napi_wrap_async_finalizer: current js_object has been wrapped.")); }); t.join(); } @@ -2507,7 +2505,6 @@ HWTEST_F(NapiBasicTest, ObjectWrapperTest006, testing::ext::TestSize.Level1) LoggerCollector collector(LogLevel::LOG_WARN, LOG_DOMAIN); uint8_t* data1 = new uint8_t; ASSERT_EQ(napi_wrap_with_size(engine, object, data1, finalizer, nullptr, nullptr, sizeof(data1)), napi_ok); - ASSERT_TRUE(collector.Includes("napi_wrap_with_size: current js_object has been wrapped.")); }); t.join(); } @@ -14611,4 +14608,64 @@ HWTEST_F(NapiBasicTest, NapiQueueAsyncWorkWithQueueTest009, testing::ext::TestSi auto res = napi_queue_async_work_with_queue(env, work, napi_qos_default, reinterpret_cast(&TASKID)); ASSERT_EQ(res, napi_ok); +} + +static napi_value Func(napi_env env, napi_callback_info info) +{ + napi_value num = nullptr; + napi_create_int32(env, 666, &num); + return num; +} + +static napi_value NewFunc(napi_env env, napi_callback_info info) +{ + napi_value num = nullptr; + napi_create_int32(env, 42, &num); + return num; +} + +/** + * @tc.name: NapiFuncWritableTest001 + * @tc.desc: Test interface of napi_create_function with writable property + * @tc.type: FUNC + */ +HWTEST_F(NapiBasicTest, NapiFuncWritableTest001, testing::ext::TestSize.Level1) +{ + napi_env env = (napi_env)engine_; + napi_value excep = nullptr; + ASSERT_CHECK_CALL(napi_get_and_clear_last_exception(env, &excep)); + napi_value obj = nullptr; + napi_property_descriptor desc[] = { + DECLARE_NAPI_WRITABLE_FUNCTION("func", Func), + }; + ASSERT_CHECK_CALL(napi_create_object_with_properties(env, &obj, 1, desc)); + napi_value origFunc = nullptr; + ASSERT_CHECK_CALL(napi_get_named_property(env, obj, "func", &origFunc)); + + napi_value recv = nullptr; + ASSERT_CHECK_CALL(napi_get_undefined(env, &recv)); + + napi_value result = nullptr; + ASSERT_CHECK_CALL(napi_call_function(env, recv, origFunc, 0, nullptr, &result)); + + int32_t firstRet = 0; + ASSERT_CHECK_CALL(napi_get_value_int32(env, result, &firstRet)); + ASSERT_EQ(firstRet, 666); + + napi_value newFunc = nullptr; + ASSERT_CHECK_CALL(napi_create_function(env,"newFunc", NAPI_AUTO_LENGTH, NewFunc, nullptr, &newFunc)); + ASSERT_CHECK_CALL(napi_set_named_property(env, obj, "func", newFunc)); + + napi_value currentFunc = nullptr; + ASSERT_CHECK_CALL(napi_get_named_property(env, obj, "func", ¤tFunc)); + + bool same = true; + ASSERT_CHECK_CALL(napi_strict_equals(env, currentFunc, origFunc, &same)); + ASSERT_FALSE(same); + result = nullptr; + ASSERT_CHECK_CALL(napi_call_function(env, recv, currentFunc, 0, nullptr, &result)); + + int32_t last = 0; + ASSERT_CHECK_CALL(napi_get_value_int32(env,result,&last)); + ASSERT_EQ(last, 42); } \ No newline at end of file -- Gitee