From c0679fc8071b2be0a59ee9033be8a53eda5056a6 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 10 Apr 2024 17:13:04 +0800 Subject: [PATCH 1/4] feature: add jsProperty demo: napi_get_named_property,napi_set_named_property,napi_has_named_property Signed-off-by: gou-jingjing --- .../entry/src/main/cpp/CMakeLists.txt | 3 + .../src/main/cpp/include/javascriptapi.h | 7 +- .../src/main/cpp/javascriptapi/common.cpp | 32 ++++ .../jsproperty/jsPropertyInit.cpp | 6 + .../jsproperty/napigetnamedproperty.cpp | 128 ++++++++++++++++ .../jsproperty/napihasnamedproperty.cpp | 89 +++++++++++ .../jsproperty/napisetnamedproperty.cpp | 95 ++++++++++++ .../src/main/cpp/types/libentry/index.d.ts | 4 +- .../ets/pages/javascript/JavascriptApi.ets | 6 +- .../jsproperties/napigetnamedproperty.ets | 138 +++++++++++++++++ .../jsproperties/napihasnamedproperty.ets | 140 +++++++++++++++++ .../jsproperties/napisetnamedproperty.ets | 136 +++++++++++++++++ .../main/resources/base/element/string.json | 12 ++ .../resources/base/profile/main_pages.json | 3 + .../main/resources/en_US/element/string.json | 12 ++ .../main/resources/zh_CN/element/string.json | 12 ++ .../ets/test/JsProperty/JsProperty.test.ets | 141 ++++++++++-------- 17 files changed, 897 insertions(+), 67 deletions(-) create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets diff --git a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt index 0962de1d..f52e10b7 100644 --- a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt +++ b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt @@ -24,6 +24,9 @@ add_library(entry SHARED javascriptapi/jsproperty/napisetproperty.cpp javascriptapi/jsproperty/napihasproperty.cpp javascriptapi/jsproperty/napideleteproperty.cpp + javascriptapi/jsproperty/napigetnamedproperty.cpp + javascriptapi/jsproperty/napisetnamedproperty.cpp + javascriptapi/jsproperty/napihasnamedproperty.cpp javascriptapi/jsproperty/jsPropertyInit.cpp javascriptapi/common.cpp javascriptapi/jsabstractops/napicoercetobool.cpp diff --git a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h index a2ae52f0..c54e657f 100644 --- a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h +++ b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h @@ -33,8 +33,9 @@ napi_value testNapiHasProperty(napi_env env, napi_callback_info info); napi_value testNapiDeleteProperty(napi_env env, napi_callback_info info); napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info); -napi_value testNapiCoerceToNumber(napi_env env, napi_callback_info info); - -napi_value jsAbstractOpsInit(napi_env env, napi_value exports); +bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag); +napi_value testNapiSetNamedProperty(napi_env env, napi_callback_info info); +napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info); +napi_value testNapiHasNamedProperty(napi_env env, napi_callback_info info); #endif //NAPITUTORIALS_JAVASCRIPTAPI_H diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp index 7540af04..f987d79a 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp @@ -14,6 +14,7 @@ */ #include "common.h" +#include "javascriptapi.h" void getErrMsg(napi_status &status, napi_env &env, const napi_extended_error_info *&extended_error_info, const char *info, const char *tag) @@ -31,3 +32,34 @@ void getErrMsg(napi_status &status, napi_env &env, const napi_extended_error_inf napi_throw_error(env, NULL, res.c_str()); } } + +bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName, const char *tag) +{ + napi_status status; + napi_valuetype valuetype0; + napi_valuetype valuetype1; + const napi_extended_error_info *extended_error_info; + + // 确认第一个参数是个对象 + status = napi_typeof(env, obj, &valuetype0); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get obj type", tag); + return false; + } + if (valuetype0 != napi_object) { + napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); + return false; + } + + // 确认第二个参数是个字符串 + status = napi_typeof(env, propName, &valuetype1); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get propName type", tag); + return false; + } + if (valuetype1 != napi_string) { + napi_throw_type_error(env, NULL, "Wrong argument type, expected a string"); + return false; + } + return true; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp index 6c6b9f2c..1d521314 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp @@ -24,6 +24,12 @@ napi_value jsPropertyInit(napi_env env, napi_value exports) {"testNapiGetProperty", nullptr, testNapiGetProperty, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiHasProperty", nullptr, testNapiHasProperty, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiDeleteProperty", nullptr, testNapiDeleteProperty, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiGetNamedProperty", nullptr, testNapiGetNamedProperty, nullptr, nullptr, nullptr, napi_default, + nullptr}, + {"testNapiSetNamedProperty", nullptr, testNapiSetNamedProperty, nullptr, nullptr, nullptr, napi_default, + nullptr}, + {"testNapiHasNamedProperty", nullptr, testNapiHasNamedProperty, nullptr, nullptr, nullptr, napi_default, + nullptr}, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc); return exports; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp new file mode 100644 index 00000000..03744787 --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +//bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName) +//{ +// napi_status status; +// napi_valuetype valuetype0; +// napi_valuetype valuetype1; +// const napi_extended_error_info *extended_error_info; +// +// // 纭绗竴涓弬鏁版槸涓璞 +// status = napi_typeof(env, obj, &valuetype0); +// if (status != napi_ok) { +// getErrMsg(status, env, extended_error_info, "get obj type", TAG); +// return false; +// } +// if (valuetype0 != napi_object) { +// napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); +// return false; +// } +// +// // 纭绗簩涓弬鏁版槸涓瓧绗︿覆 +// status = napi_typeof(env, propName, &valuetype1); +// if (status != napi_ok) { +// getErrMsg(status, env, extended_error_info, "get propName type", TAG); +// return false; +// } +// if (valuetype1 != napi_string) { +// napi_throw_type_error(env, NULL, "Wrong argument type, expected a string"); +// return false; +// } +// return true; +//} + +napi_value callFunctionIfPropertyTypeIsFunction(napi_env &env, napi_value &obj, napi_value &propValue) +{ + napi_status status; + const napi_extended_error_info *extended_error_info; + napi_valuetype valuetype; + + status = napi_typeof(env, propValue, &valuetype); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get type", TAG); + return NULL; + } + + // propValue 鏄竴涓嚱鏁帮紝鎴戜滑鍙互灏濊瘯璋冪敤瀹 + if (valuetype == napi_function) { + napi_value result; + status = napi_call_function(env, obj, propValue, 0, NULL, &result); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "call function", TAG); + return NULL; + } + // 鍑芥暟琚皟鐢紝鍏剁粨鏋滃瓨鍌ㄥ湪 result 涓,杩斿洖鍑芥暟璋冪敤缁撴灉 + return result; + } + + return propValue; +} + +napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info) { + // pages/javascript/jsproperties/napigetnamedproperty + size_t argc = PARAM2; + napi_value argv[PARAM2]; + napi_status status; + napi_value obj; + napi_value propName; + const napi_extended_error_info *extended_error_info; + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); // 瑙f瀽浼犲叆鐨勫弬鏁 + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + if (argc < PARAM2) { // 妫鏌ュ弬鏁版暟閲 + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + obj = argv[PARAM0]; + propName = argv[PARAM1]; + // 鍒ゆ柇鍙傛暟鏈夋晥鎬 + bool resValid = validateObjectProperty(env, obj, propName, TAG); + if (resValid == false) { + return NULL; + } + // 灏嗙浜屼釜鍙傛暟浠巒api_value杞崲涓篊瀛楃涓 + size_t str_size = 0; + status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + return NULL; + } + char *propertyName = (char *)malloc(str_size + 1); + status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + return NULL; + } + napi_value propValue; + status = napi_get_named_property(env, obj, propertyName, &propValue); // 璇诲彇灞炴 + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get named property", TAG); + return NULL; + } + free(propertyName); + // 妫鏌 propValue鏄惁鏄竴涓嚱鏁 + napi_value result = callFunctionIfPropertyTypeIsFunction(env, obj, propValue); + if (result != NULL) { + return result; + } + return propValue; // 杩斿洖灞炴у +} \ No newline at end of file diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp new file mode 100644 index 00000000..c896fd46 --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +char * getSecondParamStr(napi_env &env, napi_value &propName) +{ + napi_status status; + const napi_extended_error_info *extended_error_info; + size_t str_size = 0; + status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + return NULL; + } + + char *propertyName = new char[str_size + 1]; + status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + delete[] propertyName; + return NULL; + } + return propertyName; +} + +napi_value testNapiHasNamedProperty(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napihasnamedproperty + size_t argc = PARAM2; + napi_value argv[PARAM2]; + napi_status status; + napi_value obj; + napi_value propName; + const napi_extended_error_info *extended_error_info; + // 瑙f瀽浼犲叆鐨勫弬鏁 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + // 妫鏌ュ弬鏁版暟閲 + if (argc < PARAM2) { + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + obj = argv[PARAM0]; + propName = argv[PARAM1]; + // 鍒ゆ柇鍙傛暟鏈夋晥鎬 + bool resValid = validateObjectProperty(env, obj, propName, TAG); + if (resValid == false) { + return NULL; + } + // 灏嗙浜屼釜鍙傛暟浠巒api_value杞崲涓篊瀛楃涓 + char *propertyName = getSecondParamStr(env, propName); + // 鑾峰彇瀵硅薄涓婃寚瀹氬悕绉扮殑灞炴 + bool hasProperty = false; + status = napi_has_named_property(env, obj, propertyName, &hasProperty); + free(propertyName); + if (status != napi_ok) { + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "has named property", TAG); + return NULL; + } + return NULL; + } + // 杩斿洖灞炴ф槸鍚﹀瓨鍦ㄧ殑甯冨皵鍊 + napi_value result; + status = napi_get_boolean(env, hasProperty, &result); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get boolean", TAG); + return NULL; + } + return result; +} \ No newline at end of file diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp new file mode 100644 index 00000000..46ab059c --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiSetNamedProperty(napi_env env, napi_callback_info info) +{ + // pages/javascript/jsproperties/napisetnamedproperty + size_t argc = PARAM3; + napi_value argv[PARAM3]; + napi_status status; + napi_value obj; + napi_value propName; + napi_value propValue; + const napi_extended_error_info *extended_error_info; + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); // 瑙f瀽浼犲叆鐨勫弬鏁 + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + if (argc < PARAM3) { // 妫鏌ュ弬鏁版暟閲 + napi_throw_error(env, NULL, "Expected 2 arguments"); + return NULL; + } + obj = argv[PARAM0]; + propName = argv[PARAM1]; + propValue = argv[PARAM2]; + // 鍒ゆ柇鍙傛暟鏈夋晥鎬 + bool resValid = validateObjectProperty(env, obj, propName, TAG); + if (resValid == false) { + return NULL; + } + // 灏嗙浜屼釜鍙傛暟浠巒api_value杞崲涓篊瀛楃涓 + size_t str_size = 0; + status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + return NULL; + } + char *propertyName = (char *)malloc(str_size + 1); + status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get value string", TAG); + return NULL; + } + // 璁剧疆瀵硅薄鐨勫睘鎬 + status = napi_set_named_property(env, obj, propertyName, propValue); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "set named property", TAG); + return NULL; + } + free(propertyName); + // 杩斿洖鏂拌缃璞 + return obj; +} + + +// napi_valuetype valuetype0; +// napi_valuetype valuetype1; +// +// // 纭绗竴涓弬鏁版槸涓璞 +// status = napi_typeof(env, obj, &valuetype0); +// if (status != napi_ok) { +// getErrMsg(status, env, extended_error_info, "get obj type", TAG); +// return NULL; +// } +// if (valuetype0 != napi_object) { +// napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); +// return NULL; +// } +// +// // 纭绗簩涓弬鏁版槸涓瓧绗︿覆 +// status = napi_typeof(env, propName, &valuetype1); +// if (status != napi_ok) { +// getErrMsg(status, env, extended_error_info, "get propName type", TAG); +// return NULL; +// } +// if (valuetype1 != napi_string) { +// napi_throw_type_error(env, NULL, "Wrong argument type, expected a string"); +// return NULL; +// } diff --git a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts index 30ea9757..a0b1eb1f 100644 --- a/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts +++ b/examples/napitutorials/entry/src/main/cpp/types/libentry/index.d.ts @@ -39,4 +39,6 @@ export const testNapiHasProperty: (a: object, b: any) => boolean; export const testNapiDeleteProperty: (a: object, b: any) => string; /* work_with_javascript_values_and_abstract_operations */ export const testNapiCoerceToBool: (a: any) => boolean; -export const testNapiCoerceToNumber: (a: any) => number; +export const testNapiGetNamedProperty: (a: object, b: string) => string; +export const testNapiSetNamedProperty: (a: object, b: string, c: any) => string; +export const testNapiHasNamedProperty: (a: object, b: string) => string; diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets index 734b001a..1cb3ec8c 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets @@ -353,15 +353,15 @@ const WORK_WITH_JAVASCRIPT_PROPERTIES: ThirdLevelCategory = }, { title: $r('app.string.napi_set_named_property'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napisetnamedproperty' }, { title: $r('app.string.napi_get_named_property'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napigetnamedproperty' }, { title: $r('app.string.napi_has_named_property'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napihasnamedproperty' }, { title: $r('app.string.napi_set_element'), diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets new file mode 100644 index 00000000..8f9d1abc --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_get_named_property'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_get_named_property_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 娴嬭瘯 N-API napi_get_named_property \n' + // + ' const myData = testNapi.testNapiGetNamedProperty(obj, "myProperty"); \n' + // + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' + // + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + + 'let obj = {key1: "value",key2: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + + ' const myData = testNapi.testNapiGetNamedProperty(obj, "key1"); \n' + + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' + + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData2 = testNapi.testNapiGetNamedProperty(obj, "key2"); \n' + + ' console.log(myData2); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData3 = testNapi.testNapiGetNamedProperty(obj, "key3"); \n' + + ' console.log(myData3); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData4 = testNapi.testNapiGetNamedProperty(obj, "key4"); \n' + + ' console.log(myData4); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData5 = testNapi.testNapiGetNamedProperty(obj, "key5"); \n' + + ' console.log(myData5); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData6 = testNapi.testNapiGetNamedProperty(obj, "key6"); \n' + + ' console.log(myData6); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 鏍囬 + TitleBar({ title: $r('app.string.napi_get_named_property') }) + + Scroll() { + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_get_named_property'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_get_named_property') + .onClick(() => { + let obj = { + key1: "value", + key2: 1, + key3: false, + key4: ["a", "b", "c"], + key5: function () { + return "function" + }, + key6: { key: "value" } + } + let ret = testNapi.testNapiGetNamedProperty(obj, "key1"); + this.testcont = this.testcont.replace('log(myData)', 'log(## ' + ret + ' ##)'); + let ret2 = testNapi.testNapiGetNamedProperty(obj, "key2"); + this.testcont = this.testcont.replace('log(myData2)', 'log(## ' + ret2 + ' ##)'); + let ret3 = testNapi.testNapiGetNamedProperty(obj, "key3"); + this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + ret3 + ' ##)'); + let ret4 = testNapi.testNapiGetNamedProperty(obj, "key4"); + this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + ret4 + ' ##)'); + let ret5 = testNapi.testNapiGetNamedProperty(obj, "key5"); + this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + ret5 + ' ##)'); + let ret6 = testNapi.testNapiGetNamedProperty(obj, "key6"); + this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + JSON.stringify(ret6) + ' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets new file mode 100644 index 00000000..8c7e36af --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napihasnamedproperty.ets @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_has_named_property'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_has_named_property_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 娴嬭瘯 N-API napi_has_named_property \n' + + 'let obj = {key1: "value","key2": 1,"key3": false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + + '鍖呭惈key鐨勬儏鍐垫祴璇曪細鍖呭惈璇ュ睘鎬э紝杩斿洖true \n' + + ' const myData = testNapi.testNapiHasNamedProperty(obj, "key1"); \n' + + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData2 = testNapi.testNapiHasNamedProperty(obj, "key2"); \n' + + ' console.log(myData2); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData3 = testNapi.testNapiHasNamedProperty(obj, "key3"); \n' + + ' console.log(myData3); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData4 = testNapi.testNapiHasNamedProperty(obj, "key4"); \n' + + ' console.log(myData4); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData5 = testNapi.testNapiHasNamedProperty(obj, "key5"); \n' + + ' console.log(myData5); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData6 = testNapi.testNapiHasNamedProperty(obj, "key6"); \n' + + ' console.log(myData6); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + '涓嶅寘鍚玨ey鐨勬儏鍐垫祴璇曪細涓嶅寘鍚灞炴э紝杩斿洖false \n' + + ' const myExcData = testNapi.testNapiHasNamedProperty(obj, "key7"); \n' + + ' console.log(myExcData); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 鏍囬 + TitleBar({ title: $r('app.string.napi_has_named_property') }) + + Scroll() { + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_has_named_property'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_has_named_property') + .onClick(() => { + let obj = { + key1: "value", + key2: 1, + key3: false, + key4: ["a", "b", "c"], + key5: function () { + return "function" + }, + key6: { key: "value" } + } + let ret = testNapi.testNapiHasNamedProperty(obj, "key1"); + this.testcont = this.testcont.replace('log(myData)', 'log(## ' + ret + ' ##)'); + let ret2 = testNapi.testNapiHasNamedProperty(obj, "key2"); + this.testcont = this.testcont.replace('log(myData2)', 'log(## ' + ret2 + ' ##)'); + let ret3 = testNapi.testNapiHasNamedProperty(obj, "key3"); + this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + ret3 + ' ##)'); + let ret4 = testNapi.testNapiHasNamedProperty(obj, "key4"); + this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + ret4 + ' ##)'); + let ret5 = testNapi.testNapiHasNamedProperty(obj, "key5"); + this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + ret5 + ' ##)'); + let ret6 = testNapi.testNapiHasNamedProperty(obj, "key6"); + this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + ret6 + ' ##)'); + let ret7 = testNapi.testNapiHasNamedProperty(obj, "key7"); + this.testcont = this.testcont.replace('log(myExcData)', 'log(## ' + ret7 + ' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets new file mode 100644 index 00000000..40ec885b --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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 router from '@ohos.router'; +import image from '@ohos.multimedia.image'; +import Logger from '../../../util/Logger'; +import testNapi, { testNapiValue } from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar' +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_set_named_property'; + +@Entry +@Component +struct napiGetPropertyNames { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + private textcont: Resource = $r('app.string.napi_set_named_property_description'); + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State testcont: string = ' // 娴嬭瘯 N-API napi_set_named_property \n' + // + ' const myData = testNapi.testNapiSetNamedProperty(); \n' + // + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' + // + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + + ' let obj = {}; \n' + + ' const myData = testNapi.testNapiSetNamedProperty(obj, "key1", "value"); \n' + + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' + + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData2 = testNapi.testNapiSetNamedProperty(obj, "key2", false); \n' + + ' console.log(myData2); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData3 = testNapi.testNapiSetNamedProperty(obj, "key3", 1); \n' + + ' console.log(myData3); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' const myData4 = testNapi.testNapiSetNamedProperty(obj, "key4", ["a","b","c"]); \n' + + ' console.log(myData4); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' let func = function () {return "this is a function"} \n' + + ' const myData5 = testNapi.testNapiSetNamedProperty(obj, "key5", func); \n' + + ' console.log(myData5); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + ' let objVal = {key: "value"}; \n' + + ' const myData6 = testNapi.testNapiSetNamedProperty(obj, "key6", objVal); \n' + + ' console.log(myData6); // 杈撳嚭鑷畾涔夋暟鎹 \n' + + controller: TextAreaController = new TextAreaController() + + build() { + + Column() { + // 鏍囬 + TitleBar({ title: $r('app.string.napi_set_named_property') }) + + Scroll() { + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.sub_title_color')) + .backgroundColor($r('app.color.white')) + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor($r('app.color.textarea_font_color')) + .backgroundColor($r('app.color.textarea_background_color')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_set_named_property'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_set_named_property') + .onClick(() => { + let obj = {}; + let ret = testNapi.testNapiSetNamedProperty(obj, "key1", "value"); + this.testcont = this.testcont.replace('log(myData)', 'log(## ' + JSON.stringify(ret) + ' ##)'); + let ret2 = testNapi.testNapiSetNamedProperty(obj, "key2", false); + this.testcont = this.testcont.replace('log(myData2)', 'log(## ' + JSON.stringify(ret2) + ' ##)'); + let ret3 = testNapi.testNapiSetNamedProperty(obj, "key3", 1); + this.testcont = this.testcont.replace('log(myData3)', 'log(## ' + JSON.stringify(ret3) + ' ##)'); + let ret4 = testNapi.testNapiSetNamedProperty(obj, "key4", ["a", "b", "c"]); + this.testcont = this.testcont.replace('log(myData4)', 'log(## ' + JSON.stringify(ret4) + ' ##)'); + let func = function () { + return "this is a function" + }; + let ret5 = testNapi.testNapiSetNamedProperty(obj, "key5", func); + this.testcont = this.testcont.replace('log(myData5)', 'log(## ' + JSON.stringify(typeof ret5.key5) + ' ##)'); + let objVal = { key: "value" }; + let ret6 = testNapi.testNapiSetNamedProperty(obj, "key6", objVal); + this.testcont = this.testcont.replace('log(myData6)', 'log(## ' + JSON.stringify(ret6) + ' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/resources/base/element/string.json b/examples/napitutorials/entry/src/main/resources/base/element/string.json index bf09955a..d3a793a1 100644 --- a/examples/napitutorials/entry/src/main/resources/base/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/base/element/string.json @@ -6583,6 +6583,18 @@ { "name": "napi_delete_property_description", "value": "napi_delete_property is an N-API function used to delete a specified property from a JavaScript object." + }, + { + "name": "napi_get_named_property_description", + "value": "The napi_get_named_property function is used to retrieve the value of a named property from a JavaScript object. It allows you to access the value associated with a specified property name on the given object." + }, + { + "name": "napi_set_named_property_description", + "value": "The napi_set_named_property function is used to set a named property on a JavaScript object. It associates a value with a specified property name on the given object. This function allows you to add or modify properties on an object dynamically during runtime." + }, + { + "name": "napi_has_named_property_description", + "value": "The napi_has_named_property function is used to check if a JavaScript object has a named property. It allows you to determine whether the given object has a specified property name. " } ] } diff --git a/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json b/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json index c30bbba4..9fa3646b 100644 --- a/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json +++ b/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json @@ -62,6 +62,9 @@ "pages/javascript/jsproperties/napigetproperty", "pages/javascript/jsproperties/napihasproperty", "pages/javascript/jsproperties/napideleteproperty", + "pages/javascript/jsproperties/napisetnamedproperty", + "pages/javascript/jsproperties/napigetnamedproperty", + "pages/javascript/jsproperties/napihasnamedproperty", "pages/ncpp/cjsonfuncs/cjsonfuncs", "pages/javascript/jsabstractops/napicoercetobool", "pages/javascript/jsabstractops/napicoercetonumber" diff --git a/examples/napitutorials/entry/src/main/resources/en_US/element/string.json b/examples/napitutorials/entry/src/main/resources/en_US/element/string.json index 447eba48..26e14416 100644 --- a/examples/napitutorials/entry/src/main/resources/en_US/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/en_US/element/string.json @@ -6582,6 +6582,18 @@ { "name": "napi_delete_property_description", "value": "napi_delete_property is an N-API function used to delete a specified property from a JavaScript object." + }, + { + "name": "napi_get_named_property_description", + "value": "The napi_get_named_property function is used to retrieve the value of a named property from a JavaScript object. It allows you to access the value associated with a specified property name on the given object." + }, + { + "name": "napi_set_named_property_description", + "value": "The napi_set_named_property function is used to set a named property on a JavaScript object. It associates a value with a specified property name on the given object. This function allows you to add or modify properties on an object dynamically during runtime." + }, + { + "name": "napi_has_named_property_description", + "value": "The napi_has_named_property function is used to check if a JavaScript object has a named property. It allows you to determine whether the given object has a specified property name. " } ] } \ No newline at end of file diff --git a/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json b/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json index 2573fc40..a39bd682 100644 --- a/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json +++ b/examples/napitutorials/entry/src/main/resources/zh_CN/element/string.json @@ -5551,6 +5551,18 @@ { "name": "napi_delete_property_description", "value": "napi_delete_property 鏄竴涓 N-API 鍑芥暟锛岀敤浜庡垹闄 JavaScript 瀵硅薄鐨勬寚瀹氬睘鎬с" + }, + { + "name": "napi_get_named_property_description", + "value": "napi_get_named_property 鍑芥暟鐢ㄤ簬浠 JavaScript 瀵硅薄涓绱㈠懡鍚嶅睘鎬х殑鍊笺傚畠鍏佽璁块棶缁欏畾瀵硅薄涓婁笌鎸囧畾灞炴у悕绉板叧鑱旂殑鍊笺" + }, + { + "name": "napi_set_named_property_description", + "value": "napi_set_named_property 鍑芥暟鐢ㄤ簬鍦 JavaScript 瀵硅薄涓婅缃竴涓懡鍚嶅睘鎬с傚畠灏嗙粰瀹氬璞′笂鐨勬寚瀹氬睘鎬у悕绉颁笌涓涓煎叧鑱旇捣鏉ャ傝鍑芥暟鍏佽鍦ㄨ繍琛屾椂鍔ㄦ佸湴娣诲姞鎴栦慨鏀瑰璞$殑灞炴с", + }, + { + "name": "napi_has_named_property_description", + "value": "napi_has_named_property 鍑芥暟鐢ㄤ簬妫鏌 JavaScript 瀵硅薄鏄惁鍏锋湁鍛藉悕灞炴с傚畠鍏佽纭畾缁欏畾瀵硅薄鏄惁鍏锋湁鎸囧畾鐨勫睘鎬у悕绉般" } ] } diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets index aa9e6a3e..1e25a5c5 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets @@ -162,66 +162,87 @@ export default function abilityTestJsProperty() { expect(JSON.stringify(result6)).assertContain('{}'); }) - // it('testNapiHasOwnProperty', 0, () => { - // // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - // hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - // let myObj = { - // ownKey: "value" - // } - // let result = testNapi.testNapiHasOwnProperty(myObj, "ownKey"); - // let parentObj = { - // inheritedKey: 'value' - // }; - // Object.setPrototypeOf(myObj, parentObj); - // let result2 = testNapi.testNapiHasOwnProperty(myObj, "inheritedKey"); - // let result3 = testNapi.testNapiHasOwnProperty(myObj, "noPropName"); - // console.info("AbilityJsTest Has inheritedKey (JS):", myObj.hasOwnProperty('inheritedKey')); - // // 预期值为false, 实际为true 暂时找不到原因 - // console.info("AbilityJsTest Has inheritedKey result2 :", result2); - // // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - // expect(result.toString()).assertContain('true'); - // // expect(result2.toString()).assertContain('false'); - // expect(result3.toString()).assertContain('false'); - // }) - // - // it('testNapiPropertyAttributes', 0, () => { - // // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - // hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - // let result = testNapi.testNapiPropertyAttributes(); - // // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - // expect(result).assertContain('napi_default'); - // }) - // - // it('testNapiPropertyDescriptor', 0, () => { - // // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - // hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - // let result = testNapi.testNapiPropertyDescriptorFunc(); - // let result2 = testNapi.testNapiPropertyDescriptorMyReadOnlyProperty; - // // // @ts-ignore - // // testNapi.testNapiPropertyDescriptorMyProperty = 'newValue'; // 打印:Setter Called - // // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - // expect(result).assertContain('Function Called'); - // expect(result2).assertContain('Getter Called'); - // }) - // - // it('testNapiSetNamedProperty', 0, () => { - // // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - // hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - // let result = testNapi.testNapiSetNamedProperty(); - // // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - // expect(result).assertContain('Hello World!'); - // }) - // - // it('testNapiGetNamedProperty', 0, () => { - // // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - // hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); - // let obj = { - // myProperty: 'value' - // } - // let result = testNapi.testNapiGetNamedProperty(obj, 'myProperty'); - // // Defines a variety of assertion methods, which are used to declare expected boolean conditions. - // expect(result).assertContain('value'); - // }) + it('testNapiSetNamedProperty', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let obj1 = {}; + let result = testNapi.testNapiSetNamedProperty(obj1, "key1", "value"); + let obj2 = {}; + let result2 = testNapi.testNapiSetNamedProperty(obj2, "key2", false); + let obj3 = {}; + let result3 = testNapi.testNapiSetNamedProperty(obj3, "key3", 1); + let obj4 = {}; + let result4 = testNapi.testNapiSetNamedProperty(obj4, "key4", ["a","b","c"]); + let obj5 = {}; + let func = function () {return "this is a function"}; + let result5 = testNapi.testNapiSetNamedProperty(obj5, "key5", func); + let obj6 = {}; + let objVal = {key: "value"}; + let result6 = testNapi.testNapiSetNamedProperty(obj6, "key6", objVal); + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(JSON.stringify(result)).assertContain('"key1":"value"'); + expect(JSON.stringify(result2)).assertContain('"key2":false'); + expect(JSON.stringify(result3)).assertContain('"key3":1'); + expect(JSON.stringify(result4)).assertContain('"key4":["a","b","c"]'); + expect(JSON.stringify(typeof result5.key5)).assertContain('function'); + expect(JSON.stringify(result6)).assertContain('"key6":{"key":"value"}'); + }) + + it('testNapiGetNamedProperty', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let obj = { + key1: "value", + key2: 1, + key3: false, + key4: ["a", "b", "c"], + key5: function () { return "function" }, + key6: {key: "value"} + } + let result = testNapi.testNapiGetNamedProperty(obj, "key1"); + let result2 = testNapi.testNapiGetNamedProperty(obj, "key2"); + let result3 = testNapi.testNapiGetNamedProperty(obj, "key3"); + let result4 = testNapi.testNapiGetNamedProperty(obj, "key4"); + let result5 = testNapi.testNapiGetNamedProperty(obj, "key5"); + let result6 = testNapi.testNapiGetNamedProperty(obj, "key6"); + + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(JSON.stringify(result)).assertContain('value'); + expect(JSON.stringify(result2)).assertContain('1'); + expect(JSON.stringify(result3)).assertContain('false'); + expect(JSON.stringify(result4)).assertContain('["a","b","c"]'); + expect(JSON.stringify(result5)).assertContain('function'); + expect(JSON.stringify(result6)).assertContain('{"key":"value"}'); + }) + + it('testNapiHasNamedProperty', 0, () => { + // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. + hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + let obj = { + key1: "value", + key2: 1, + key3: false, + key4: ["a", "b", "c"], + key5: function () { return "function" }, + key6: {key: "value"} + } + let result = testNapi.testNapiHasNamedProperty(obj, "key1"); + let result2 = testNapi.testNapiHasNamedProperty(obj, "key2"); + let result3 = testNapi.testNapiHasNamedProperty(obj, "key3"); + let result4 = testNapi.testNapiHasNamedProperty(obj, "key4"); + let result5 = testNapi.testNapiHasNamedProperty(obj, "key5"); + let result6 = testNapi.testNapiHasNamedProperty(obj, "key6"); + let result7 = testNapi.testNapiHasNamedProperty(obj, "key7"); + + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. + expect(result.toString()).assertContain('true'); + expect(result2.toString()).assertContain('true'); + expect(result3.toString()).assertContain('true'); + expect(result4.toString()).assertContain('true'); + expect(result5.toString()).assertContain('true'); + expect(result6.toString()).assertContain('true'); + expect(result7.toString()).assertContain('false'); + }) }) } -- Gitee From ebaf4000b1f9c01509b44999d7fcb324fde77ec0 Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 10 Apr 2024 17:21:50 +0800 Subject: [PATCH 2/4] fix: remove redundant comments Signed-off-by: gou-jingjing --- .../jsproperty/napigetnamedproperty.cpp | 31 ------------------- .../jsproperty/napisetnamedproperty.cpp | 26 ---------------- .../jsproperties/napigetnamedproperty.ets | 3 -- .../jsproperties/napisetnamedproperty.ets | 3 -- 4 files changed, 63 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp index 03744787..ea91caef 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp @@ -17,37 +17,6 @@ static const char *TAG = "[javascriptapi_property]"; -//bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName) -//{ -// napi_status status; -// napi_valuetype valuetype0; -// napi_valuetype valuetype1; -// const napi_extended_error_info *extended_error_info; -// -// // 纭绗竴涓弬鏁版槸涓璞 -// status = napi_typeof(env, obj, &valuetype0); -// if (status != napi_ok) { -// getErrMsg(status, env, extended_error_info, "get obj type", TAG); -// return false; -// } -// if (valuetype0 != napi_object) { -// napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); -// return false; -// } -// -// // 纭绗簩涓弬鏁版槸涓瓧绗︿覆 -// status = napi_typeof(env, propName, &valuetype1); -// if (status != napi_ok) { -// getErrMsg(status, env, extended_error_info, "get propName type", TAG); -// return false; -// } -// if (valuetype1 != napi_string) { -// napi_throw_type_error(env, NULL, "Wrong argument type, expected a string"); -// return false; -// } -// return true; -//} - napi_value callFunctionIfPropertyTypeIsFunction(napi_env &env, napi_value &obj, napi_value &propValue) { napi_status status; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp index 46ab059c..2c1779f4 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp @@ -67,29 +67,3 @@ napi_value testNapiSetNamedProperty(napi_env env, napi_callback_info info) // 杩斿洖鏂拌缃璞 return obj; } - - -// napi_valuetype valuetype0; -// napi_valuetype valuetype1; -// -// // 纭绗竴涓弬鏁版槸涓璞 -// status = napi_typeof(env, obj, &valuetype0); -// if (status != napi_ok) { -// getErrMsg(status, env, extended_error_info, "get obj type", TAG); -// return NULL; -// } -// if (valuetype0 != napi_object) { -// napi_throw_type_error(env, NULL, "Wrong argument type, expected an object"); -// return NULL; -// } -// -// // 纭绗簩涓弬鏁版槸涓瓧绗︿覆 -// status = napi_typeof(env, propName, &valuetype1); -// if (status != napi_ok) { -// getErrMsg(status, env, extended_error_info, "get propName type", TAG); -// return NULL; -// } -// if (valuetype1 != napi_string) { -// napi_throw_type_error(env, NULL, "Wrong argument type, expected a string"); -// return NULL; -// } diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets index 8f9d1abc..a2f91615 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napigetnamedproperty.ets @@ -31,9 +31,6 @@ struct napiGetPropertyNames { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State testcont: string = ' // 娴嬭瘯 N-API napi_get_named_property \n' - // + ' const myData = testNapi.testNapiGetNamedProperty(obj, "myProperty"); \n' - // + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' - // + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + 'let obj = {key1: "value",key2: 1,key3: false,key4: ["a", "b", "c"],key5: function () { return "function" },key6: {key: "value"}} \n' + ' const myData = testNapi.testNapiGetNamedProperty(obj, "key1"); \n' + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets index 40ec885b..b15d7656 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napisetnamedproperty.ets @@ -31,9 +31,6 @@ struct napiGetPropertyNames { @State isSetInstance: Boolean = false; @State imagePixelMap: PixelMap | undefined = undefined; @State testcont: string = ' // 娴嬭瘯 N-API napi_set_named_property \n' - // + ' const myData = testNapi.testNapiSetNamedProperty(); \n' - // + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' - // + ' console.log(myData); // 杈撳嚭鑷畾涔夋暟鎹 \n'; + ' let obj = {}; \n' + ' const myData = testNapi.testNapiSetNamedProperty(obj, "key1", "value"); \n' + ' // 浣跨敤鑾峰彇鐨勮嚜瀹氫箟鏁版嵁 \n' -- Gitee From baf28f2b2f54e9291f1a528ec4299b8b8381cb6b Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 10 Apr 2024 18:02:41 +0800 Subject: [PATCH 3/4] fix: add abilityTest case hilog Signed-off-by: gou-jingjing --- .../ets/test/JsProperty/JsProperty.test.ets | 73 ++++++++++++++++--- 1 file changed, 64 insertions(+), 9 deletions(-) diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets index 1e25a5c5..26337f56 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/JsProperty/JsProperty.test.ets @@ -41,7 +41,7 @@ export default function abilityTestJsProperty() { it('testNapiGetPropertyNames', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetPropertyNames begin'); let obj = { key1: "aa", key2: 1, @@ -50,13 +50,14 @@ export default function abilityTestJsProperty() { key5: function () { console.info("This is a function") } } let result = testNapi.testNapiGetPropertyNames(obj); + hilog.info(0x0000, 'testTag', `napi_get_property_names(${obj}) = ${result}`); // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(JSON.stringify(result)).assertContain('["key1","key2","key3","key4","key5"]'); }) it('testNapiGetProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetProperty begin'); let obj = { key1: "value", true: 1, @@ -72,6 +73,13 @@ export default function abilityTestJsProperty() { let result5 = testNapi.testNapiGetProperty(obj, "key5"); let result6 = testNapi.testNapiGetProperty(obj, "key6"); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.key1) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.true) = ${result2}`); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.0) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.key4) = ${result4}`); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.key5) = ${result5}`); + hilog.info(0x0000, 'testTag', `napi_get_property(obj.key6) = ${result6}`); + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(JSON.stringify(result)).assertContain('value'); expect(JSON.stringify(result2)).assertContain('1'); @@ -83,7 +91,7 @@ export default function abilityTestJsProperty() { it('testNapiSetProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiSetProperty begin'); let obj1 = {}; let result = testNapi.testNapiSetProperty(obj1, "key1", "value"); let obj2 = {}; @@ -98,6 +106,14 @@ export default function abilityTestJsProperty() { let obj6 = {}; let objVal = {key: "value"}; let result6 = testNapi.testNapiSetProperty(obj6, "key6", objVal); + + hilog.info(0x0000, 'testTag', `napi_set_property(obj.key1) = ${JSON.stringify(result)}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.0) = ${JSON.stringify(result2)}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.true) = ${JSON.stringify(result3}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.key4) = ${JSON.stringify(result4)}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.key5) = ${JSON.stringify(typeof result5.key5)}`); + hilog.info(0x0000, 'testTag', `napi_set_property(obj.key6) = ${JSON.stringify(result6)}`); + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(JSON.stringify(result)).assertContain('"key1":"value"'); expect(JSON.stringify(result2)).assertContain('"0":false'); @@ -109,7 +125,7 @@ export default function abilityTestJsProperty() { it('testNapiHasProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiHasProperty begin'); let obj = { key1: "value", true: 1, @@ -126,6 +142,13 @@ export default function abilityTestJsProperty() { let result6 = testNapi.testNapiHasProperty(obj, "key6"); let result7 = testNapi.testNapiHasProperty(obj, "key7"); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key1) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.true) = ${result2}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.0) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key4) = ${result4}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key5) = ${result5}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key6) = ${result6}`); + hilog.info(0x0000, 'testTag', `napi_has_property(obj.key7) = ${result7}`); // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(result.toString()).assertContain('true'); expect(result2.toString()).assertContain('true'); @@ -138,7 +161,7 @@ export default function abilityTestJsProperty() { it('testNapiDeleteProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiDeleteProperty begin'); let obj = { key1: "value", true: 1, @@ -148,24 +171,34 @@ export default function abilityTestJsProperty() { key6: {key: "value"} } let result = testNapi.testNapiDeleteProperty(obj, "key1"); - console.info("testNapiDeletePropertyTest result = " + JSON.stringify(result)) + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key1) = ${JSON.stringify(result)}`); expect(JSON.stringify(result)).assertContain('{"0":false,"true":1,"key4":["a","b","c"],"key6":{"key":"value"}}'); + let result2 = testNapi.testNapiDeleteProperty(obj, true); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.0) = ${JSON.stringify(result2)}`); expect(JSON.stringify(result2)).assertContain('{"0":false,"key4":["a","b","c"],"key6":{"key":"value"}}'); + let result3 = testNapi.testNapiDeleteProperty(obj, 0); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.true) = ${JSON.stringify(result3}`); expect(JSON.stringify(result3)).assertContain('{"key4":["a","b","c"],"key6":{"key":"value"}}'); + let result4 = testNapi.testNapiDeleteProperty(obj, "key4"); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key4) = ${JSON.stringify(result4)}`); expect(JSON.stringify(result4)).assertContain('{"key6":{"key":"value"}}'); + let result5 = testNapi.testNapiDeleteProperty(obj, "key5"); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key5) = ${JSON.stringify(result5)}`); expect(JSON.stringify(result5)).assertContain('{"key6":{"key":"value"}}'); + let result6 = testNapi.testNapiDeleteProperty(obj, "key6"); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj.key6) = ${JSON.stringify(result6)}`); expect(JSON.stringify(result6)).assertContain('{}'); }) it('testNapiSetNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiSetNamedProperty begin'); let obj1 = {}; let result = testNapi.testNapiSetNamedProperty(obj1, "key1", "value"); let obj2 = {}; @@ -180,6 +213,13 @@ export default function abilityTestJsProperty() { let obj6 = {}; let objVal = {key: "value"}; let result6 = testNapi.testNapiSetNamedProperty(obj6, "key6", objVal); + + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key1) = ${JSON.stringify(result)}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key2) = ${JSON.stringify(result2)}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key3) = ${JSON.stringify(result3}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key4) = ${JSON.stringify(result4)}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key5) = ${JSON.stringify(typeof result5.key5)}`); + hilog.info(0x0000, 'testTag', `napi_set_named_property(obj.key6) = ${JSON.stringify(result6)}`); // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(JSON.stringify(result)).assertContain('"key1":"value"'); expect(JSON.stringify(result2)).assertContain('"key2":false'); @@ -191,7 +231,7 @@ export default function abilityTestJsProperty() { it('testNapiGetNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiGetNamedProperty begin'); let obj = { key1: "value", key2: 1, @@ -207,6 +247,13 @@ export default function abilityTestJsProperty() { let result5 = testNapi.testNapiGetNamedProperty(obj, "key5"); let result6 = testNapi.testNapiGetNamedProperty(obj, "key6"); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key1) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key2) = ${result2}`); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key3) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key4) = ${result4}`); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key5) = ${result5}`); + hilog.info(0x0000, 'testTag', `napi_get_named_property(obj.key6) = ${result6}`); + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(JSON.stringify(result)).assertContain('value'); expect(JSON.stringify(result2)).assertContain('1'); @@ -218,7 +265,7 @@ export default function abilityTestJsProperty() { it('testNapiHasNamedProperty', 0, () => { // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function. - hilog.info(0x0000, 'testTag', '%{public}s', 'it begin'); + hilog.info(0x0000, 'testTag', '%{public}s', 'it testNapiHasNamedProperty begin'); let obj = { key1: "value", key2: 1, @@ -235,6 +282,14 @@ export default function abilityTestJsProperty() { let result6 = testNapi.testNapiHasNamedProperty(obj, "key6"); let result7 = testNapi.testNapiHasNamedProperty(obj, "key7"); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key1) = ${result}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key2) = ${result2}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key3) = ${result3}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key4) = ${result4}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key5) = ${result5}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key6) = ${result6}`); + hilog.info(0x0000, 'testTag', `napi_has_named_property(obj.key7) = ${result7}`); + // Defines a variety of assertion methods, which are used to declare expected boolean conditions. expect(result.toString()).assertContain('true'); expect(result2.toString()).assertContain('true'); -- Gitee From 57ed116ea79029496c183319d590da11e5d4664f Mon Sep 17 00:00:00 2001 From: gou-jingjing Date: Wed, 10 Apr 2024 18:10:16 +0800 Subject: [PATCH 4/4] fix: fix codeCheck Signed-off-by: gou-jingjing --- .../entry/src/main/cpp/javascriptapi/common.cpp | 4 ++-- .../jsproperty/napigetnamedproperty.cpp | 12 +++++++----- .../jsproperty/napihasnamedproperty.cpp | 2 +- .../jsproperty/napisetnamedproperty.cpp | 9 +++++---- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp index f987d79a..0263165d 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/common.cpp @@ -40,7 +40,7 @@ bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName napi_valuetype valuetype1; const napi_extended_error_info *extended_error_info; - // 确认第一个参数是个对象 + // 纭绗竴涓弬鏁版槸涓璞 status = napi_typeof(env, obj, &valuetype0); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get obj type", tag); @@ -51,7 +51,7 @@ bool validateObjectProperty(napi_env &env, napi_value &obj, napi_value &propName return false; } - // 确认第二个参数是个字符串 + // 纭绗簩涓弬鏁版槸涓瓧绗︿覆 status = napi_typeof(env, propName, &valuetype1); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get propName type", tag); diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp index ea91caef..4bcfa0ab 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napigetnamedproperty.cpp @@ -44,7 +44,8 @@ napi_value callFunctionIfPropertyTypeIsFunction(napi_env &env, napi_value &obj, return propValue; } -napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info) { +napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info) +{ // pages/javascript/jsproperties/napigetnamedproperty size_t argc = PARAM2; napi_value argv[PARAM2]; @@ -68,26 +69,27 @@ napi_value testNapiGetNamedProperty(napi_env env, napi_callback_info info) { if (resValid == false) { return NULL; } - // 灏嗙浜屼釜鍙傛暟浠巒api_value杞崲涓篊瀛楃涓 - size_t str_size = 0; + size_t str_size = 0; // 灏嗙浜屼釜鍙傛暟浠巒api_value杞崲涓篊瀛楃涓 status = napi_get_value_string_utf8(env, propName, NULL, 0, &str_size); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get value string", TAG); return NULL; } - char *propertyName = (char *)malloc(str_size + 1); + char *propertyName = new char[str_size + 1]; status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get value string", TAG); + delete[] propertyName; return NULL; } napi_value propValue; status = napi_get_named_property(env, obj, propertyName, &propValue); // 璇诲彇灞炴 if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get named property", TAG); + delete[] propertyName; return NULL; } - free(propertyName); + delete[] propertyName; // 妫鏌 propValue鏄惁鏄竴涓嚱鏁 napi_value result = callFunctionIfPropertyTypeIsFunction(env, obj, propValue); if (result != NULL) { diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp index c896fd46..303d2352 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napihasnamedproperty.cpp @@ -17,7 +17,7 @@ static const char *TAG = "[javascriptapi_property]"; -char * getSecondParamStr(napi_env &env, napi_value &propName) +char *getSecondParamStr(napi_env &env, napi_value &propName) { napi_status status; const napi_extended_error_info *extended_error_info; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp index 2c1779f4..54072fbe 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napisetnamedproperty.cpp @@ -51,19 +51,20 @@ napi_value testNapiSetNamedProperty(napi_env env, napi_callback_info info) getErrMsg(status, env, extended_error_info, "get value string", TAG); return NULL; } - char *propertyName = (char *)malloc(str_size + 1); + char *propertyName = new char[str_size + 1]; status = napi_get_value_string_utf8(env, propName, propertyName, str_size + 1, &str_size); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "get value string", TAG); + delete[] propertyName; return NULL; } // 璁剧疆瀵硅薄鐨勫睘鎬 status = napi_set_named_property(env, obj, propertyName, propValue); if (status != napi_ok) { getErrMsg(status, env, extended_error_info, "set named property", TAG); + delete[] propertyName; return NULL; } - free(propertyName); - // 杩斿洖鏂拌缃璞 - return obj; + delete[] propertyName; + return obj; // 杩斿洖鏂拌缃璞 } -- Gitee