From f70e8f60192fddfc3c9c143807f1d6cd635c62a6 Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Fri, 22 Apr 2022 16:16:30 +0800 Subject: [PATCH 1/3] modify napi code Signed-off-by: hellohyh001 --- .../plugin/include/async_callback_info.h | 1 - interfaces/plugin/src/sensor_napi_utils.cpp | 57 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/interfaces/plugin/include/async_callback_info.h b/interfaces/plugin/include/async_callback_info.h index 17d9c2a7..fc2922bf 100644 --- a/interfaces/plugin/include/async_callback_info.h +++ b/interfaces/plugin/include/async_callback_info.h @@ -106,7 +106,6 @@ public: BusinessError error; CallbackDataType type; vector sensorInfos; - sptr callbackInfo = nullptr; AsyncCallbackInfo(napi_env env, CallbackDataType type) : env(env), type(type) {} ~AsyncCallbackInfo() diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 54960163..559d6885 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -450,6 +450,16 @@ bool CreateNapiArray(const napi_env &env, float data[], int32_t dataLength, napi return true; } +sptr GetCallbackInfo(void *data) +{ + CHKPP(data); + sptr asyncCallbackInfo(static_cast(data)); + CHKPP(asyncCallbackInfo); + // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor + asyncCallbackInfo->DecStrongRef(nullptr); + return asyncCallbackInfo; +} + void EmitAsyncCallbackWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -458,16 +468,13 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) CHKNRV(asyncCallbackInfo->env, napi_create_string_utf8(asyncCallbackInfo->env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_utf8"); // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; + asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - CHKPV(data); - sptr asyncCallbackInfo = reinterpret_cast(data)->callbackInfo; + sptr asyncCallbackInfo = GetCallbackInfo(data); CHKPV(asyncCallbackInfo); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; napi_value callback = nullptr; napi_value callResult = nullptr; napi_value result[2] = {0}; @@ -495,13 +502,20 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + asyncCallbackInfo->DecStrongRef(nullptr); } } -void EmitUvEventLoop(sptr asyncCallbackInfo) +void freeWork(uv_work_t *work) { CALL_LOG_ENTER; + CHKPV(work); + delete work; + work = nullptr; +} + +void EmitUvEventLoop(sptr asyncCallbackInfo) +{ CHKPV(asyncCallbackInfo); uv_loop_s *loop(nullptr); CHKNRV(asyncCallbackInfo->env, napi_get_uv_event_loop(asyncCallbackInfo->env, &loop), @@ -509,17 +523,14 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(loop); uv_work_t *work = new(std::nothrow) uv_work_t; CHKPV(work); - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; + // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor + asyncCallbackInfo->IncStrongRef(nullptr); asyncCallbackInfo->work = work; work->data = asyncCallbackInfo.GetRefPtr(); int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { - CALL_LOG_ENTER; CHKPV(work); - CHKPV(work->data); - sptr asyncCallbackInfo = - reinterpret_cast(work->data)->callbackInfo; + sptr asyncCallbackInfo = GetCallbackInfo(work->data); CHKPV(asyncCallbackInfo); - asyncCallbackInfo->callbackInfo = nullptr; napi_env env = asyncCallbackInfo->env; napi_value callback = nullptr; CHKNRV(env, napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback), @@ -531,17 +542,14 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) g_convertfuncList[asyncCallbackInfo->type](env, asyncCallbackInfo, result); CHKNRV(env, napi_call_function(env, nullptr, callback, 1, &result[1], &callResult), "napi_call_function"); asyncCallbackInfo->work = nullptr; - CHKPV(work); - delete work; - work = nullptr; + freeWork(work); }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); - asyncCallbackInfo->callbackInfo = nullptr; + // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor + asyncCallbackInfo->DecStrongRef(nullptr); asyncCallbackInfo->work = nullptr; - CHKPV(work); - delete work; - work = nullptr; + freeWork(work); } } @@ -553,16 +561,13 @@ void EmitPromiseWork(sptr asyncCallbackInfo) CHKNRV(asyncCallbackInfo->env, napi_create_string_latin1(asyncCallbackInfo->env, "Promise", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_latin1"); // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor - asyncCallbackInfo->callbackInfo = asyncCallbackInfo; + asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - CHKPV(data); - sptr asyncCallbackInfo = reinterpret_cast(data)->callbackInfo; + sptr asyncCallbackInfo = GetCallbackInfo(data); CHKPV(asyncCallbackInfo); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; napi_value result[2] = {0}; CHKNCV(env, (g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), "Callback type invalid in promise"); @@ -581,7 +586,7 @@ void EmitPromiseWork(sptr asyncCallbackInfo) || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->callbackInfo = nullptr; + asyncCallbackInfo->DecStrongRef(nullptr); } } } // namespace Sensors -- Gitee From 4974f9640322396ffb4739106875b7f272d96d7b Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Sun, 24 Apr 2022 16:14:42 +0800 Subject: [PATCH 2/3] modify napi code Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_napi_utils.cpp | 48 +++++++++++---------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 559d6885..4fb24955 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -450,16 +450,6 @@ bool CreateNapiArray(const napi_env &env, float data[], int32_t dataLength, napi return true; } -sptr GetCallbackInfo(void *data) -{ - CHKPP(data); - sptr asyncCallbackInfo(static_cast(data)); - CHKPP(asyncCallbackInfo); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor - asyncCallbackInfo->DecStrongRef(nullptr); - return asyncCallbackInfo; -} - void EmitAsyncCallbackWork(sptr asyncCallbackInfo) { CALL_LOG_ENTER; @@ -467,14 +457,19 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) napi_value resourceName = nullptr; CHKNRV(asyncCallbackInfo->env, napi_create_string_utf8(asyncCallbackInfo->env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_utf8"); - // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - sptr asyncCallbackInfo = GetCallbackInfo(data); - CHKPV(asyncCallbackInfo); + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, + * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and + * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous + * task is executed, the reference count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_value callback = nullptr; napi_value callResult = nullptr; napi_value result[2] = {0}; @@ -501,7 +496,6 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) if (status != napi_ok || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor asyncCallbackInfo->DecStrongRef(nullptr); } } @@ -523,14 +517,19 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(loop); uv_work_t *work = new(std::nothrow) uv_work_t; CHKPV(work); - // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor - asyncCallbackInfo->IncStrongRef(nullptr); asyncCallbackInfo->work = work; + asyncCallbackInfo->IncStrongRef(nullptr); work->data = asyncCallbackInfo.GetRefPtr(); int32_t ret = uv_queue_work(loop, work, [] (uv_work_t *work) { }, [] (uv_work_t *work, int status) { CHKPV(work); - sptr asyncCallbackInfo = GetCallbackInfo(work->data); - CHKPV(asyncCallbackInfo); + sptr asyncCallbackInfo(static_cast(work->data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, + * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and + * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous + * task is executed, the reference count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_env env = asyncCallbackInfo->env; napi_value callback = nullptr; CHKNRV(env, napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback), @@ -546,7 +545,6 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) }); if (ret != 0) { SEN_HILOGE("uv_queue_work fail"); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor asyncCallbackInfo->DecStrongRef(nullptr); asyncCallbackInfo->work = nullptr; freeWork(work); @@ -560,14 +558,19 @@ void EmitPromiseWork(sptr asyncCallbackInfo) napi_value resourceName = nullptr; CHKNRV(asyncCallbackInfo->env, napi_create_string_latin1(asyncCallbackInfo->env, "Promise", NAPI_AUTO_LENGTH, &resourceName), "napi_create_string_latin1"); - // Make the reference count of asyncCallbackInfo add 1, and the function exits the non-destructor asyncCallbackInfo->IncStrongRef(nullptr); napi_status status = napi_create_async_work(asyncCallbackInfo->env, nullptr, resourceName, [](napi_env env, void* data) {}, [](napi_env env, napi_status status, void* data) { CALL_LOG_ENTER; - sptr asyncCallbackInfo = GetCallbackInfo(data); - CHKPV(asyncCallbackInfo); + sptr asyncCallbackInfo(static_cast(data)); + /** + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, + * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and + * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous + * task is executed, the reference count of the smart pointer is guaranteed to be 1. + */ + asyncCallbackInfo->DecStrongRef(nullptr); napi_value result[2] = {0}; CHKNCV(env, (g_convertfuncList.find(asyncCallbackInfo->type) != g_convertfuncList.end()), "Callback type invalid in promise"); @@ -585,7 +588,6 @@ void EmitPromiseWork(sptr asyncCallbackInfo) if (status != napi_ok || napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork) != napi_ok) { SEN_HILOGE("Create async work fail"); - // The reference count of asyncCallbackInfo is subtracted by 1, and the function exits the destructor asyncCallbackInfo->DecStrongRef(nullptr); } } -- Gitee From 9167cd9c97b9c9d1ecf48629c819e8afae4047ce Mon Sep 17 00:00:00 2001 From: hellohyh001 Date: Sun, 24 Apr 2022 16:56:52 +0800 Subject: [PATCH 3/3] modify napi code Signed-off-by: hellohyh001 --- interfaces/plugin/src/sensor_napi_utils.cpp | 27 ++++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/interfaces/plugin/src/sensor_napi_utils.cpp b/interfaces/plugin/src/sensor_napi_utils.cpp index 4fb24955..bbf2f46b 100644 --- a/interfaces/plugin/src/sensor_napi_utils.cpp +++ b/interfaces/plugin/src/sensor_napi_utils.cpp @@ -464,10 +464,11 @@ void EmitAsyncCallbackWork(sptr asyncCallbackInfo) CALL_LOG_ENTER; sptr asyncCallbackInfo(static_cast(data)); /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, - * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and - * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous - * task is executed, the reference count of the smart pointer is guaranteed to be 1. + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. */ asyncCallbackInfo->DecStrongRef(nullptr); napi_value callback = nullptr; @@ -524,10 +525,11 @@ void EmitUvEventLoop(sptr asyncCallbackInfo) CHKPV(work); sptr asyncCallbackInfo(static_cast(work->data)); /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, - * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and - * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous - * task is executed, the reference count of the smart pointer is guaranteed to be 1. + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. */ asyncCallbackInfo->DecStrongRef(nullptr); napi_env env = asyncCallbackInfo->env; @@ -565,10 +567,11 @@ void EmitPromiseWork(sptr asyncCallbackInfo) CALL_LOG_ENTER; sptr asyncCallbackInfo(static_cast(data)); /** - * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced to 0 destructions, - * so you need to add 1 to the asyncCallbackInfo reference count when the asynchronous task is created, and - * subtract 1 from the reference count after the naked pointer is converted to a pointer when the asynchronous - * task is executed, the reference count of the smart pointer is guaranteed to be 1. + * After the asynchronous task is created, the asyncCallbackInfo reference count is reduced + * to 0 destructions, so you need to add 1 to the asyncCallbackInfo reference count when the + * asynchronous task is created, and subtract 1 from the reference count after the naked + * pointer is converted to a pointer when the asynchronous task is executed, the reference + * count of the smart pointer is guaranteed to be 1. */ asyncCallbackInfo->DecStrongRef(nullptr); napi_value result[2] = {0}; -- Gitee