From 28d8d7b2adfe41fc10f147a1c647ca62c4027e50 Mon Sep 17 00:00:00 2001 From: quyawei Date: Tue, 13 Sep 2022 12:05:46 +0800 Subject: [PATCH 1/3] napi fix Signed-off-by: quyawei --- frameworks/js/napi/src/napi_mac.cpp | 67 ++++++++++++++-------------- frameworks/js/napi/src/napi_md.cpp | 67 ++++++++++++++-------------- frameworks/js/napi/src/napi_rand.cpp | 51 ++++++++++----------- 3 files changed, 92 insertions(+), 93 deletions(-) diff --git a/frameworks/js/napi/src/napi_mac.cpp b/frameworks/js/napi/src/napi_mac.cpp index 4525b8f..dc2202c 100644 --- a/frameworks/js/napi/src/napi_mac.cpp +++ b/frameworks/js/napi/src/napi_mac.cpp @@ -410,55 +410,54 @@ static napi_value NapiGetMacLength(napi_env env, napi_callback_info info) napi_value NapiMac::MacConstructor(napi_env env, napi_callback_info info) { - printf("enter MacConstructor..."); napi_value thisVar = nullptr; - size_t exceptedArgc = ARGS_SIZE_ONE; - size_t argc = exceptedArgc; - napi_value argv[ARGS_SIZE_ONE] = { 0 }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - std::string algoName; - if (!GetStringFromJSParams(env, argv[0], algoName)) { - return NapiGetNull(env); - } - HcfMac *macObj = nullptr; - int32_t res = HcfMacCreate(algoName.c_str(), &macObj); - if (res != HCF_SUCCESS) { - LOGE("create c macObj failed."); - return NapiGetNull(env); - } - NapiMac *macNapiObj = new NapiMac(macObj); - napi_wrap( - env, thisVar, macNapiObj, - [](napi_env env, void *data, void *hint) { - NapiMac *mac = (NapiMac *)(data); - delete mac; - return; - }, - nullptr, - nullptr); - napi_value napiAlgName = nullptr; - napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); - napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); - printf("out MacConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) { - printf("enter CreateMac..."); size_t exceptedArgc = ARGS_SIZE_ONE; size_t argc; napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + std::string algoName; + if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + LOGE("Failed to get algorithm."); + return nullptr; + } + HcfMac *macObj = nullptr; + HcfResult res = HcfMacCreate(algoName.c_str(), &macObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c macObj failed."); + return nullptr; + } + napi_value napiAlgName = nullptr; + napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, argc, argv, &instance); - printf("out CreateMac..."); + napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); + NapiMac *macNapiObj = new (std:: nothrow) NapiMac(macObj); + if (macNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, macNapiObj, + [](napi_env env, void *data, void *hint) { + NapiMac *mac = (NapiMac *)(data); + delete mac; + return; + }, + nullptr, + nullptr); return instance; } diff --git a/frameworks/js/napi/src/napi_md.cpp b/frameworks/js/napi/src/napi_md.cpp index 25463f4..562627f 100644 --- a/frameworks/js/napi/src/napi_md.cpp +++ b/frameworks/js/napi/src/napi_md.cpp @@ -317,55 +317,54 @@ static napi_value NapiGetMdLength(napi_env env, napi_callback_info info) napi_value NapiMd::MdConstructor(napi_env env, napi_callback_info info) { - printf("enter MdConstructor..."); napi_value thisVar = nullptr; - size_t exceptedArgc = ARGS_SIZE_ONE; - size_t argc = exceptedArgc; - napi_value argv[ARGS_SIZE_ONE] = { 0 }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - std::string algoName; - if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { - return NapiGetNull(env); - } - HcfMd *mdObj = nullptr; - int32_t res = HcfMdCreate(algoName.c_str(), &mdObj); - if (res != HCF_SUCCESS) { - LOGE("create c mdObj failed."); - return NapiGetNull(env); - } - NapiMd *mdNapiObj = new NapiMd(mdObj); - napi_wrap( - env, thisVar, mdNapiObj, - [](napi_env env, void *data, void *hint) { - NapiMd *md = (NapiMd *)(data); - delete md; - return; - }, - nullptr, - nullptr); - napi_value napiAlgName = nullptr; - napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); - napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); - printf("out MdConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiMd::CreateMd(napi_env env, napi_callback_info info) { - printf("enter CreateMd..."); size_t exceptedArgc = ARGS_SIZE_ONE; size_t argc; napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + std::string algoName; + if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + LOGE("Failed to get algorithm."); + return nullptr; + } + HcfMd *mdObj = nullptr; + HcfResult res = HcfMdCreate(algoName.c_str(), &mdObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c mdObj failed."); + return nullptr; + } + napi_value napiAlgName = nullptr; + napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, argc, argv, &instance); - printf("out CreateMd..."); + napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); + NapiMd *mdNapiObj = new (std:: nothrow) NapiMd(mdObj); + if (mdNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, mdNapiObj, + [](napi_env env, void *data, void *hint) { + NapiMd *md = (NapiMd *)(data); + delete md; + return; + }, + nullptr, + nullptr); return instance; } diff --git a/frameworks/js/napi/src/napi_rand.cpp b/frameworks/js/napi/src/napi_rand.cpp index e45fdf4..1dcd842 100644 --- a/frameworks/js/napi/src/napi_rand.cpp +++ b/frameworks/js/napi/src/napi_rand.cpp @@ -304,44 +304,45 @@ static napi_value NapiSetSeed(napi_env env, napi_callback_info info) napi_value NapiRand::RandConstructor(napi_env env, napi_callback_info info) { - printf("enter RandConstructor..."); napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - HcfRand *randObj = nullptr; - HcfResult res = HcfRandCreate(&randObj); - if (res != HCF_SUCCESS) { - LOGE("create c rand fail."); - return NapiGetNull(env); - } - NapiRand *randNapiObj = new NapiRand(randObj); - napi_wrap( - env, thisVar, randNapiObj, - [](napi_env env, void *data, void *hint) { - NapiRand *rand = (NapiRand *)(data); - delete rand; - return; - }, - nullptr, - nullptr); - printf("out RandConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info) { - printf("enter CreateRand..."); size_t exceptedArgc = ARGS_SIZE_ZERO; size_t argc; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + HcfRand *randObj = nullptr; + HcfResult res = HcfRandCreate(&randObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c randObj failed."); + return nullptr; + } + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); - napi_new_instance(env, constructor, ARGS_SIZE_ZERO, nullptr, &instance); - printf("out CreateRand..."); + napi_new_instance(env, constructor, argc, nullptr, &instance); + NapiRand *randNapiObj = new (std:: nothrow) NapiRand(randObj); + if (macNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, randNapiObj, + [](napi_env env, void *data, void *hint) { + NapiRand *rand = (NapiRand *)(data); + delete rand; + return; + }, + nullptr, + nullptr); return instance; } -- Gitee From 1e8165c61d99f48a004d23836800d63d1f945562 Mon Sep 17 00:00:00 2001 From: quyawei Date: Tue, 13 Sep 2022 12:05:46 +0800 Subject: [PATCH 2/3] napi fix Signed-off-by: quyawei --- .vscode/c_cpp_properties.json | 19 ++++++++ frameworks/js/napi/src/napi_mac.cpp | 67 ++++++++++++++-------------- frameworks/js/napi/src/napi_md.cpp | 67 ++++++++++++++-------------- frameworks/js/napi/src/napi_rand.cpp | 51 ++++++++++----------- 4 files changed, 111 insertions(+), 93 deletions(-) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..46cf7f4 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "windows-msvc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/frameworks/js/napi/src/napi_mac.cpp b/frameworks/js/napi/src/napi_mac.cpp index 4525b8f..3a2482a 100644 --- a/frameworks/js/napi/src/napi_mac.cpp +++ b/frameworks/js/napi/src/napi_mac.cpp @@ -410,55 +410,54 @@ static napi_value NapiGetMacLength(napi_env env, napi_callback_info info) napi_value NapiMac::MacConstructor(napi_env env, napi_callback_info info) { - printf("enter MacConstructor..."); napi_value thisVar = nullptr; - size_t exceptedArgc = ARGS_SIZE_ONE; - size_t argc = exceptedArgc; - napi_value argv[ARGS_SIZE_ONE] = { 0 }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - std::string algoName; - if (!GetStringFromJSParams(env, argv[0], algoName)) { - return NapiGetNull(env); - } - HcfMac *macObj = nullptr; - int32_t res = HcfMacCreate(algoName.c_str(), &macObj); - if (res != HCF_SUCCESS) { - LOGE("create c macObj failed."); - return NapiGetNull(env); - } - NapiMac *macNapiObj = new NapiMac(macObj); - napi_wrap( - env, thisVar, macNapiObj, - [](napi_env env, void *data, void *hint) { - NapiMac *mac = (NapiMac *)(data); - delete mac; - return; - }, - nullptr, - nullptr); - napi_value napiAlgName = nullptr; - napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); - napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); - printf("out MacConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiMac::CreateMac(napi_env env, napi_callback_info info) { - printf("enter CreateMac..."); size_t exceptedArgc = ARGS_SIZE_ONE; size_t argc; napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + std::string algoName; + if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + LOGE("Failed to get algorithm."); + return nullptr; + } + HcfMac *macObj = nullptr; + HcfResult res = HcfMacCreate(algoName.c_str(), &macObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c macObj failed."); + return nullptr; + } + napi_value napiAlgName = nullptr; + napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, argc, argv, &instance); - printf("out CreateMac..."); + napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); + NapiMac *macNapiObj = new (std::nothrow) NapiMac(macObj); + if (macNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, macNapiObj, + [](napi_env env, void *data, void *hint) { + NapiMac *mac = (NapiMac *)(data); + delete mac; + return; + }, + nullptr, + nullptr); return instance; } diff --git a/frameworks/js/napi/src/napi_md.cpp b/frameworks/js/napi/src/napi_md.cpp index 25463f4..abf7a26 100644 --- a/frameworks/js/napi/src/napi_md.cpp +++ b/frameworks/js/napi/src/napi_md.cpp @@ -317,55 +317,54 @@ static napi_value NapiGetMdLength(napi_env env, napi_callback_info info) napi_value NapiMd::MdConstructor(napi_env env, napi_callback_info info) { - printf("enter MdConstructor..."); napi_value thisVar = nullptr; - size_t exceptedArgc = ARGS_SIZE_ONE; - size_t argc = exceptedArgc; - napi_value argv[ARGS_SIZE_ONE] = { 0 }; - napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); - std::string algoName; - if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { - return NapiGetNull(env); - } - HcfMd *mdObj = nullptr; - int32_t res = HcfMdCreate(algoName.c_str(), &mdObj); - if (res != HCF_SUCCESS) { - LOGE("create c mdObj failed."); - return NapiGetNull(env); - } - NapiMd *mdNapiObj = new NapiMd(mdObj); - napi_wrap( - env, thisVar, mdNapiObj, - [](napi_env env, void *data, void *hint) { - NapiMd *md = (NapiMd *)(data); - delete md; - return; - }, - nullptr, - nullptr); - napi_value napiAlgName = nullptr; - napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); - napi_set_named_property(env, thisVar, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); - printf("out MdConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiMd::CreateMd(napi_env env, napi_callback_info info) { - printf("enter CreateMd..."); size_t exceptedArgc = ARGS_SIZE_ONE; size_t argc; napi_value argv[ARGS_SIZE_ONE] = { 0 }; napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + std::string algoName; + if (!GetStringFromJSParams(env, argv[PARAM0], algoName)) { + LOGE("Failed to get algorithm."); + return nullptr; + } + HcfMd *mdObj = nullptr; + HcfResult res = HcfMdCreate(algoName.c_str(), &mdObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c mdObj failed."); + return nullptr; + } + napi_value napiAlgName = nullptr; + napi_create_string_utf8(env, algoName.c_str(), NAPI_AUTO_LENGTH, &napiAlgName); + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); napi_new_instance(env, constructor, argc, argv, &instance); - printf("out CreateMd..."); + napi_set_named_property(env, instance, CRYPTO_TAG_ALG_NAME.c_str(), napiAlgName); + NapiMd *mdNapiObj = new (std::nothrow) NapiMd(mdObj); + if (mdNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, mdNapiObj, + [](napi_env env, void *data, void *hint) { + NapiMd *md = (NapiMd *)(data); + delete md; + return; + }, + nullptr, + nullptr); return instance; } diff --git a/frameworks/js/napi/src/napi_rand.cpp b/frameworks/js/napi/src/napi_rand.cpp index e45fdf4..bef8fdd 100644 --- a/frameworks/js/napi/src/napi_rand.cpp +++ b/frameworks/js/napi/src/napi_rand.cpp @@ -304,44 +304,45 @@ static napi_value NapiSetSeed(napi_env env, napi_callback_info info) napi_value NapiRand::RandConstructor(napi_env env, napi_callback_info info) { - printf("enter RandConstructor..."); napi_value thisVar = nullptr; - napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); - HcfRand *randObj = nullptr; - HcfResult res = HcfRandCreate(&randObj); - if (res != HCF_SUCCESS) { - LOGE("create c rand fail."); - return NapiGetNull(env); - } - NapiRand *randNapiObj = new NapiRand(randObj); - napi_wrap( - env, thisVar, randNapiObj, - [](napi_env env, void *data, void *hint) { - NapiRand *rand = (NapiRand *)(data); - delete rand; - return; - }, - nullptr, - nullptr); - printf("out RandConstructor..."); - return thisVar; + napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr); + return thisVar; } napi_value NapiRand::CreateRand(napi_env env, napi_callback_info info) { - printf("enter CreateRand..."); size_t exceptedArgc = ARGS_SIZE_ZERO; size_t argc; napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr); if (argc != exceptedArgc) { LOGE("The input args num is invalid."); - return NapiGetNull(env); + return nullptr; } - napi_value instance; + HcfRand *randObj = nullptr; + HcfResult res = HcfRandCreate(&randObj); + if (res != HCF_SUCCESS) { + napi_throw(env, GenerateBusinessError(env, res, "create C obj failed.")); + LOGE("create c randObj failed."); + return nullptr; + } + napi_value instance = nullptr; napi_value constructor = nullptr; napi_get_reference_value(env, classRef_, &constructor); - napi_new_instance(env, constructor, ARGS_SIZE_ZERO, nullptr, &instance); - printf("out CreateRand..."); + napi_new_instance(env, constructor, argc, nullptr, &instance); + NapiRand *randNapiObj = new (std::nothrow) NapiRand(randObj); + if (macNapiObj == nullptr) { + LOGE("create napi obj failed"); + return nullptr; + } + napi_wrap( + env, instance, randNapiObj, + [](napi_env env, void *data, void *hint) { + NapiRand *rand = (NapiRand *)(data); + delete rand; + return; + }, + nullptr, + nullptr); return instance; } -- Gitee From 0a1ad52aa83ff87a5ec3c4d1005bbf421b336d63 Mon Sep 17 00:00:00 2001 From: WillQu Date: Tue, 13 Sep 2022 05:17:51 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20.vsc?= =?UTF-8?q?ode?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 46cf7f4..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "configurations": [ - { - "name": "Win32", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [ - "_DEBUG", - "UNICODE", - "_UNICODE" - ], - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "windows-msvc-x64" - } - ], - "version": 4 -} \ No newline at end of file -- Gitee