From db52445a875652703784131c034d534fa2431d98 Mon Sep 17 00:00:00 2001 From: zhangmenghan Date: Fri, 18 Oct 2024 14:02:36 +0800 Subject: [PATCH 1/3] jsproperties && jsabstractops Signed-off-by: zhangmenghan --- .../entry/src/main/cpp/CMakeLists.txt | 4 + .../src/main/cpp/include/javascriptapi.h | 4 + .../cpp/javascriptapi/jsabstractops/init.cpp | 4 +- .../jsabstractops/napiisarray.cpp | 55 +++++++ .../jsabstractops/napistrictequals.cpp | 55 +++++++ .../jsproperty/jsPropertyInit.cpp | 2 + .../jsproperty/napiobjectfreeze.cpp | 59 +++++++ .../jsproperty/napiobjectseal.cpp | 58 +++++++ .../src/main/cpp/types/libentry/index.d.ts | 5 +- .../ets/pages/javascript/JavascriptApi.ets | 8 +- .../javascript/jsabstractops/napiisarray.ets | 147 +++++++++++++++++ .../jsabstractops/napistrictequals.ets | 148 ++++++++++++++++++ .../jsproperties/napiobjectfreeze.ets | 128 +++++++++++++++ .../jsproperties/napiobjectseal.ets | 128 +++++++++++++++ .../resources/base/profile/main_pages.json | 4 + .../ets/test/JsProperty/JsProperty.test.ets | 38 +++++ .../test/javascriptapi/jsabstractops.test.ets | 81 ++++++++++ 17 files changed, 922 insertions(+), 6 deletions(-) create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp create mode 100644 examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napiisarray.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napistrictequals.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectfreeze.ets create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectseal.ets diff --git a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt index 695af1a8..69351324 100644 --- a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt +++ b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt @@ -50,6 +50,8 @@ add_library(entry SHARED javascriptapi/jsproperty/napigetelement.cpp javascriptapi/jsproperty/napihaselement.cpp javascriptapi/jsproperty/napideleteelement.cpp + javascriptapi/jsproperty/napiobjectfreeze.cpp + javascriptapi/jsproperty/napiobjectseal.cpp javascriptapi/jsproperty/jsPropertyInit.cpp javascriptapi/common.cpp javascriptapi/jsabstractops/napicoercetobool.cpp @@ -57,6 +59,8 @@ add_library(entry SHARED javascriptapi/jsabstractops/napicoercetoobject.cpp javascriptapi/jsabstractops/napicoercetostring.cpp javascriptapi/jsabstractops/napitypeof.cpp + javascriptapi/jsabstractops/napiisarray.cpp + javascriptapi/jsabstractops/napistrictequals.cpp javascriptapi/jsabstractops/init.cpp javascriptapi/jsvalues/jsValuesInit.cpp javascriptapi/jsvalues/napicreateint32.cpp diff --git a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h index de849edd..b9558d20 100644 --- a/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h +++ b/examples/napitutorials/entry/src/main/cpp/include/javascriptapi.h @@ -40,12 +40,16 @@ napi_value testNapiSetElement(napi_env env, napi_callback_info info); napi_value testNapiGetElement(napi_env env, napi_callback_info info); napi_value testNapiHasElement(napi_env env, napi_callback_info info); napi_value testNapiDeleteElement(napi_env env, napi_callback_info info); +napi_value testNapiObjectFreeze(napi_env env, napi_callback_info info); +napi_value testNapiObjectSeal(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 testNapiCoerceToObject(napi_env env, napi_callback_info info); napi_value testNapiCoerceToString(napi_env env, napi_callback_info info); napi_value testNapiTypeof(napi_env env, napi_callback_info info); +napi_value testNapiIsArray(napi_env env, napi_callback_info info); +napi_value testNapiStrictEquals(napi_env env, napi_callback_info info); void jsAbstractOpsInit(napi_property_descriptor **origDescPtr, size_t *len); diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/init.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/init.cpp index 846ffe10..1620d2bc 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/init.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/init.cpp @@ -23,7 +23,9 @@ void jsAbstractOpsInit(napi_property_descriptor **origDescPtr, size_t *len) {"testNapiCoerceToNumber", nullptr, testNapiCoerceToNumber, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiCoerceToObject", nullptr, testNapiCoerceToObject, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiCoerceToString", nullptr, testNapiCoerceToString, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"testNapiTypeof", nullptr, testNapiTypeof, nullptr, nullptr, nullptr, napi_default, nullptr} + {"testNapiTypeof", nullptr, testNapiTypeof, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiIsArray", nullptr, testNapiIsArray, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiStrictEquals", nullptr, testNapiStrictEquals, nullptr, nullptr, nullptr, napi_default, nullptr} }; // Allocate memory diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp new file mode 100644 index 00000000..fa073a4c --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp @@ -0,0 +1,55 @@ +/* + * 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 "common.h" + +static const char *TAG = "[jsapi_isarray]"; + +napi_value testNapiIsArray(napi_env env, napi_callback_info info) { + // pages/javascript/jsabstractops/napiisarray + size_t requireArgc = PARAM1; + size_t argc = PARAM1; + napi_status status; + bool result; + napi_value bolresult; + napi_value argv[PARAM1]; + const napi_extended_error_info *extended_error_info; + + // Obtain the parameters of the callback function + status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + // Check if the number of parameters meets expectations + if (argc != requireArgc) { + napi_throw_error(env, NULL, "Expected exactly one argument"); + return NULL; + } + + // Call napi_is_array(), any -> bool + status = napi_is_array(env, argv[0], &result); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to check if value is an array"); + return NULL; + } + + status = napi_get_boolean(env, result, &bolresult); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to convert boolean to napi_value"); + return NULL; + } + return bolresult; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp new file mode 100644 index 00000000..c1b78928 --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp @@ -0,0 +1,55 @@ +/* + * 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 "common.h" + +static const char *TAG = "[jsapi_strictequals]"; + +napi_value testNapiStrictEquals(napi_env env, napi_callback_info info) { + // pages/javascript/jsabstractops/napistrictequals + size_t requireArgc = PARAM2; + size_t argc = PARAM2; + napi_status status; + bool result; + napi_value bolresult; + napi_value argv[PARAM2]; + const napi_extended_error_info *extended_error_info; + + // Obtain the parameters of the callback function + status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "get cb info", TAG); + return NULL; + } + // Check if the number of parameters meets expectations + if (argc != requireArgc) { + napi_throw_error(env, NULL, "Expected exactly two argument"); + return NULL; + } + + // Call napi_strict_equals() + status = napi_strict_equals(env, argv[0], argv[1], &result); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to check if the two objects are equal"); + return NULL; + } + + status = napi_get_boolean(env, result, &bolresult); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to convert boolean to napi_value"); + return NULL; + } + return bolresult; +} 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 b1d6b61b..11570a62 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/jsPropertyInit.cpp @@ -34,6 +34,8 @@ napi_value jsPropertyInit(napi_env env, napi_value exports) {"testNapiGetElement", nullptr, testNapiGetElement, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiHasElement", nullptr, testNapiHasElement, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiDeleteElement", nullptr, testNapiDeleteElement, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiObjectFreeze", nullptr, testNapiObjectFreeze, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiObjectSeal", nullptr, testNapiObjectSeal, 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/napiobjectfreeze.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp new file mode 100644 index 00000000..b8affbfb --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp @@ -0,0 +1,59 @@ +/* + * 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 "common.h" +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiObjectFreeze(napi_env env, napi_callback_info info) { + // pages/javascript/jsproperty/napiobjectfreeze + // 获取参数数量 + size_t argc = PARAM1; + // 准备接收参数的变量 + napi_value argv[PARAM1]; + napi_value obj; + napi_status status; + const napi_extended_error_info *extended_error_info; + + // 获取回调函数的参数信息 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "Failed to get callback info", TAG); + return NULL; + } + + // 检查参数数量是否符合预期 + if (argc != PARAM1) { + napi_throw_error(env, NULL, "Expected exactly one argument"); + return NULL; + } + + // 检查传入参数是否为object + napi_valuetype resultType; + napi_typeof(env, argv[0], &resultType); + if (resultType != napi_object) { + napi_throw_error(env, NULL, "The incoming parameters are not as expected"); + return NULL; + } + obj = argv[PARAM0]; + status = napi_object_freeze(env, obj); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "object freeze", TAG); + return NULL; + } + + return obj; +} diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp new file mode 100644 index 00000000..834b219d --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp @@ -0,0 +1,58 @@ +/* + * 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 "common.h" +#include "javascriptapi.h" + +static const char *TAG = "[javascriptapi_property]"; + +napi_value testNapiObjectSeal(napi_env env, napi_callback_info info) { + // pages/javascript/jsproperty/napiobjectseal + // 获取参数数量 + size_t argc = PARAM1; + // 准备接收参数的变量 + napi_value argv[PARAM1]; + napi_value obj; + napi_status status; + const napi_extended_error_info *extended_error_info; + + // 获取回调函数的参数信息 + status = napi_get_cb_info(env, info, &argc, argv, NULL, NULL); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "Failed to get callback info", TAG); + return NULL; + } + + // 检查参数数量是否符合预期 + if (argc != PARAM1) { + napi_throw_error(env, NULL, "Expected exactly one argument"); + return NULL; + } + + // 检查传入参数是否为object + napi_valuetype resultType; + napi_typeof(env, argv[0], &resultType); + if (resultType != napi_object) { + napi_throw_error(env, NULL, "The incoming parameters are not as expected"); + return NULL; + } + obj = argv[PARAM0]; + status = napi_object_seal(env, obj); + if (status != napi_ok) { + getErrMsg(status, env, extended_error_info, "object freeze", TAG); + return NULL; + } + return obj; +} 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 41a7db44..371d004f 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 @@ -49,7 +49,8 @@ export const testNapiSetElement: (a: object, b: number, c: any) => string; export const testNapiGetElement: (a: object, b: number) => string; export const testNapiHasElement: (a: object, b: number) => string; export const testNapiDeleteElement: (a: object, b: number) => string; - +export const testNapiObjectFreeze: (a: object) => object; +export const testNapiObjectSeal: (a: object) => object; /* work_with_javascript_values_and_abstract_operations */ export const testNapiCoerceToBool: (a: any) => boolean; @@ -57,6 +58,8 @@ export const testNapiCoerceToNumber: (a: any) => number; export const testNapiCoerceToObject: (a: any) => object; export const testNapiCoerceToString: (a: any) => string; export const testNapiTypeof: (a: any) => string; +export const testNapiIsArray: (a: any) => boolean; +export const testNapiStrictEquals: (a: any, b: any) => boolean; /* work_with_javascript_values */ export const testNapiCreateInt32: (number) => number; 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 42753490..f3d33cbc 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets @@ -270,7 +270,7 @@ const WORK_WITH_JAVASCRIPT_VALUES_AND_ABSTRACT_OPERATIONS: ThirdLevelCategory = }, { title: $r('app.string.napi_is_array'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsabstractops/napiisarray' }, { title: $r('app.string.napi_is_arraybuffer'), @@ -298,7 +298,7 @@ const WORK_WITH_JAVASCRIPT_VALUES_AND_ABSTRACT_OPERATIONS: ThirdLevelCategory = }, { title: $r('app.string.napi_strict_equals'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsabstractops/napistrictequals' }, { title: $r('app.string.napi_detach_arraybuffer'), @@ -385,11 +385,11 @@ const WORK_WITH_JAVASCRIPT_PROPERTIES: ThirdLevelCategory = }, { title: $r('app.string.napi_object_freeze'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napiobjectfreeze' }, { title: $r('app.string.napi_object_seal'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsproperties/napiobjectseal' } ] } diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napiisarray.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napiisarray.ets new file mode 100644 index 00000000..8148bd8a --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napiisarray.ets @@ -0,0 +1,147 @@ +/* + * 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 image from '@ohos.multimedia.image'; +import testNapi from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar'; +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_is_array'; + +@Entry +@Component +struct napiisarray { + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State textcont: string = 'napi_is_array() 用于判断给定对象是否是数组' + + '如果 API 成功,则返回 napi_ok。' + + '该 API 表示调用对象上的 IsArray 操作,如 ECMAScript 语言规范的 第 7.2.2 节 中所定义。'; + @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' + + 'const testNumIsArrayResult = addon.testNapiIsArray(123); // 数字 -> false \n' + + 'const testStrIsArrayResult = addon.testNapiIsArray(\'test123\'); // 字符串 -> false \n' + + 'const testBoolIsArrayResult = addon.testNapiIsArray(true); // 布尔值 -> false \n' + + 'const testNullIsArrayResult = addon.testNapiIsArray(null); // null -> false \n' + + 'const testUndefIsArrayResult = addon.testNapiIsArray(undefined); // undefined -> false \n' + + 'const testArrIsArrayResult = addon.testNapiIsArray([1, \'2\', false, null, undefined])' + + '// 输出判断结果 \n' + + 'console.log(`123 -> ${testNumIsArrayResult}`); \n' + + 'console.log(`\'test123\' -> ${testStrIsArrayResult}`); \n' + + 'console.log(`true -> ${testBoolIsArrayResult}`); \n' + + 'console.log(`null -> ${testNullIsArrayResult}`); \n' + + 'console.log(`undefined -> ${testUndefIsArrayResult}`); \n' + + 'console.log(`[1, \'2\', false, null, undefined] -> ${testArrIsArrayResult}`); \n'; + controller: TextAreaController = new TextAreaController() + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + + build() { + Scroll() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_is_array') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#182431') + .backgroundColor('#FFFFFF') + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#ff400336') + .backgroundColor('#FFFFFF') + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_is_array'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_is_array') + .onClick(() => { + try { + // Test if number/string/boolean/null/undefined/array is array + const testNum: number = 123; + const testStr: string = 'test123' + const testBool: boolean = true; + const testNull: null = null; + const testUndef: undefined = undefined; + const testArr: Array = [1, '2', false, null, undefined]; + console.log('=======begin==========='); + const testNumIsArray = testNapi.testNapiIsArray(testNum); + const testStrIsArray = testNapi.testNapiIsArray(testStr); + const testBoolIsArray = testNapi.testNapiIsArray(testBool); + const testNullIsArray = testNapi.testNapiIsArray(testNull); + const testUndefIsArray = testNapi.testNapiIsArray(testUndef); + const testArrIsArray = testNapi.testNapiIsArray(testArr); + console.log('=========try========='); + + // Replace result in testcont + this.testcont = this.testcont.replace('${testNumIsArrayResult}', `## ${testNumIsArray} ##`); + this.testcont = this.testcont.replace('${testStrIsArrayResult}', `## ${testStrIsArray} ##`); + this.testcont = this.testcont.replace('${testBoolIsArrayResult}', `## ${testBoolIsArray} ##`); + this.testcont = this.testcont.replace('${testNullIsArrayResult}', `## ${testNullIsArray} ##`); + this.testcont = this.testcont.replace('${testUndefIsArrayResult}', `## ${testUndefIsArray} ##`); + this.testcont = this.testcont.replace('${testArrIsArrayResult}', `## ${testArrIsArray} ##`); + + // Print the results + hilog.info(0x0000, TAG, `(whether is ${testNum} array? -> ${testNumIsArray}`); + hilog.info(0x0000, TAG, `(whether is ${testStr} array? -> ${testStrIsArray}`); + hilog.info(0x0000, TAG, `(whether is ${testBool} array? -> ${testBoolIsArray}`); + hilog.info(0x0000, TAG, `(whether is ${testNull} array? -> ${testNullIsArray}`); + hilog.info(0x0000, TAG, `(whether is ${testUndef} array? -> ${testUndefIsArray}`); + hilog.info(0x0000, TAG, `(whether is ${testArr} array? -> ${testArrIsArray}`); + } catch (error) { + hilog.error(0x0000, TAG, `Catch error testNapiIsArray: ${error.message}}`) + } + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + } + .width('100%') + .height('100%') + .align(Alignment.Top) + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napistrictequals.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napistrictequals.ets new file mode 100644 index 00000000..f92600c7 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napistrictequals.ets @@ -0,0 +1,148 @@ +/* + * 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 WARRAi]kNTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import image from '@ohos.multimedia.image'; +import testNapi from 'libentry.so'; +import { TitleBar } from '../../../common/TitleBar'; +import hilog from '@ohos.hilog'; + +const TAG: string = 'napi_strict_eaquals'; + +@Entry +@Component +struct napistrictequals { + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State textcont: string = 'napi_strict_equals() 用于判断两个对象是否相等' + + '如果 API 成功,则返回 napi_ok。' + + '该 API 表示调用 ECMAScript 语言规范的 第 7.2.14 节 中定义的严格相等算法。'; + @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' + + 'const testNumStrStrictEqualsResult = addon.testNapiStrictEquals(123, \'123\'); // false \n' + + 'const testZeroNumStrStrictEqualsResult = addon.testNapiStrictEquals(0, \'0\'); // false \n' + + 'const testTrueBoolStrStrictEqualsResult = addon.testNapiStrictEquals(\'true\', true); // false \n' + + 'const testNullZeroStrictEqualsResult = addon.testNapiStrictEquals(null, 0); // -> false \n' + + 'const testNullUndefStrictEqualsResult = addon.testNapiStrictEquals(null, undefined); // false \n' + + 'const testNumStrictEqualsResult = addon.testNapiStrictEquals(123, 123) // true \n' + + '// 输出判断结果 \n' + + 'console.log(`123 = \'123\'? -> ${testNumStrStrictEqualsResult}`); \n' + + 'console.log(`0 = \'0\'? -> ${testZeroNumStrStrictEqualsResult}`); \n' + + 'console.log(`\'true\' = true? -> ${testTrueBoolStrStrictEqualsResult}`); \n' + + 'console.log(`null = 0? -> ${testNullZeroStrictEqualsResult}`); \n' + + 'console.log(`null = undefined? -> ${testNullUndefStrictEqualsResult}`); \n' + + 'console.log(`123 = 123? -> ${testNumStrictEqualsResult}`); \n'; + controller: TextAreaController = new TextAreaController() + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + + build() { + Scroll() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_strict_equals') }) + + Column() { + Column() { + TextArea({ + text: this.textcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#182431') + .backgroundColor('#FFFFFF') + .enabled(false) + + TextArea({ + text: this.testcont, + placeholder: '', + }) + .placeholderFont({ size: 16, weight: 400 }) + .width('90%') + .margin(10) + .fontSize(16) + .fontColor('#ff400336') + .backgroundColor('#FFFFFF') + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_strict_equals'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_strict_equals') + .onClick(() => { + try { + // Check if the two objects are equal + const testNum: number = 123; + const testNumEqu: number = 123; + const testZeroNum: number = 0; + const testNumStr: string = '123'; + const testZeroNumStr: string = '0'; + const testBoolStr: string = 'true'; + const testTrueBool: boolean = true; + const testNull: null = null; + const testUndef: undefined = undefined; + const testNumStrStrictEquals = testNapi.testNapiStrictEquals(testNum, testNumStr); + const testZeroNumStrStrictEquals = testNapi.testNapiStrictEquals(testZeroNum, testZeroNumStr); + const testTrueBoolStrStrictEquals = testNapi.testNapiStrictEquals(testBoolStr, testTrueBool); + const testNullZeroStrictEquals = testNapi.testNapiStrictEquals(testNull, testZeroNum); + const testNullUndefStrictEquals = testNapi.testNapiStrictEquals(testNull, testUndef); + const testNumStrictEquals = testNapi.testNapiStrictEquals(testNum, testNumEqu); + + // Replace result in testcont + this.testcont = this.testcont.replace('${testNumStrStrictEqualsResult}', `## ${testNumStrStrictEquals} ##`); + this.testcont = this.testcont.replace('${testZeroNumStrStrictEqualsResult}', `## ${testZeroNumStrStrictEquals} ##`); + this.testcont = this.testcont.replace('${testTrueBoolStrStrictEqualsResult}', `## ${testTrueBoolStrStrictEquals} ##`); + this.testcont = this.testcont.replace('${testNullZeroStrictEqualsResult}', `## ${testNullZeroStrictEquals} ##`); + this.testcont = this.testcont.replace('${testNullUndefStrictEqualsResult}', `## ${testNullUndefStrictEquals} ##`); + this.testcont = this.testcont.replace('${testNumStrictEqualsResult}', `## ${testNumStrictEquals} ##`); + + // Print the results + hilog.info(0x0000, TAG, `(123 = '123'? -> ${testNumStrStrictEquals}`); + hilog.info(0x0000, TAG, `(0 = '0'? -> ${testZeroNumStrStrictEquals}`); + hilog.info(0x0000, TAG, `('true' = true? -> ${testTrueBoolStrStrictEquals}`); + hilog.info(0x0000, TAG, `(null = 0? -> ${testNullZeroStrictEquals}`); + hilog.info(0x0000, TAG, `(null = undefined? -> ${testNullUndefStrictEquals}`); + hilog.info(0x0000, TAG, `(123 = 123? -> ${testNumStrictEquals}`); + } catch (error) { + hilog.error(0x0000, TAG, `Catch error testNapiStrictEquals: ${error.message}}`) + } + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + } + .width('100%') + .height('100%') + .align(Alignment.Top) + .backgroundColor($r('app.color.background_shallow_grey')) + } +} diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectfreeze.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectfreeze.ets new file mode 100644 index 00000000..84dbee86 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectfreeze.ets @@ -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. + */ + +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_object_freeze'; + +interface MyObj { + key1: string; + key2: number; + key3: boolean; + key4: Array +} + +@Entry +@Component +struct napiObjectFreeze { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State textcont: string = 'napi_object_freeze()用于冻结对象。' + + '此 API 可防止向其添加新属性,删除现有属性,防止更改现有属性的可枚举性、可配置性或可写性,' + + '并防止更改现有属性的值。它还可以防止对象的原型被更改。' + + '这在 ECMA-262 规范的 第 19.1.2.6 节 中进行了描述。'; + @State testcont: string = ' // 测试 N-API napi_object_freeze \n' + + ' let obj = {key1: "value", key2: 4, key3: true, key4: ["a", "b"]}; \n' + + ' const myData = testNapi.testNapiObjectFreeze(obj); \n' + + ' console.log(myData); \n' + // + ' // 改变属性 \n' + // + ' const myData1 = testNapi.testNapiSetProperty(obj, "key1", "price"); \n' + // + ' console.log(myData1); \n' + + ' // 删除其中属性 \n' + + ' const myData2 = testNapi.testNapiDeleteProperty(obj, "key1"); \n' + + ' console.log(myData2); \n' + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_object_freeze') }) + + 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.white')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_object_freeze'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_object_freeze') + .onClick(() => { + let obj: MyObj = { + key1: "value", + key2: 4, + key3: true, + key4: ["a", "b"] + } + let ret = testNapi.testNapiObjectFreeze(obj); + // let ret1 = testNapi.testNapiSetProperty(obj, 'key1', 'price'); + let ret2 = testNapi.testNapiDeleteProperty(obj, 'key1'); + this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); + // this.testcont = this.testcont.replace('log(myData1)', 'log(## '+JSON.stringify(ret1)+' ##)'); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} \ No newline at end of file diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectseal.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectseal.ets new file mode 100644 index 00000000..8497eee4 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsproperties/napiobjectseal.ets @@ -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. + */ + +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_object_seal'; + +interface MyObj { + key1: string; + key2: number; + key3: boolean; + key4: Array +} + +@Entry +@Component +struct napiObjectSeal { + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State textcont: string = 'napi_object_seal()用于冻结对象。' + + '此 API 可防止向其添加新属性,以及将所有现有属性标记为不可配置。' + + '这在 ECMA-262 规范的 第 19.1.2.20 节 中进行了描述。'; + @State testcont: string = ' // 测试 N-API napi_object_seal \n' + + ' let obj = {key1: "value", key2: 4, key3: true, key4: ["a", "b"]}; \n' + + ' const myData = testNapi.testNapiObjectSeal(obj); \n' + + ' console.log(myData); \n' + + ' // 改变属性 \n' + + ' const myData1 = testNapi.testNapiSetNamedProperty(obj, "key1", "price"); \n' + + ' console.log(myData1); \n' + + ' // 删除其中属性 \n' + + ' const myData2 = testNapi.testNapiDeleteProperty(obj, "key1"); \n' + + ' console.log(myData2); \n' + + + controller: TextAreaController = new TextAreaController() + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_object_seal') }) + + 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.white')) + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_object_seal'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_object_seal') + .onClick(() => { + let obj: MyObj = { + key1: "value", + key2: 4, + key3: true, + key4: ["a", "b"] + } + let ret = testNapi.testNapiObjectSeal(obj); + let ret1 = testNapi.testNapiSetProperty(obj, 'key1', 'price'); + let ret2 = testNapi.testNapiDeleteProperty(obj, 'key1'); + this.testcont = this.testcont.replace('log(myData)', 'log(## '+JSON.stringify(ret)+' ##)'); + this.testcont = this.testcont.replace('log(myData1)', 'log(## '+JSON.stringify(ret1)+' ##)'); + this.testcont = this.testcont.replace('log(myData2)', 'log(## '+JSON.stringify(ret2)+' ##)'); + }) + } + .width('100%') + .height(48) + .alignItems(VerticalAlign.Center) + .justifyContent(FlexAlign.SpaceBetween) + } + .width('100%') + } + .height('100%') + .width('100%') + .backgroundColor($r('app.color.background_shallow_grey')) + } +} \ No newline at end of file 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 ee53eaeb..22a86a69 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 @@ -69,6 +69,8 @@ "pages/javascript/jsproperties/napigetelement", "pages/javascript/jsproperties/napihaselement", "pages/javascript/jsproperties/napideleteelement", + "pages/javascript/jsproperties/napiobjectfreeze", + "pages/javascript/jsproperties/napiobjectseal", "pages/javascript/jsabstractops/napicoercetobool", "pages/javascript/jsabstractops/napicoercetonumber", "pages/ncpp/cjsonfuncs/cjsonfuncs", @@ -92,6 +94,8 @@ "pages/javascript/jsabstractops/napicoercetoobject", "pages/javascript/jsabstractops/napicoercetostring", "pages/javascript/jsabstractops/napitypeof", + "pages/javascript/jsabstractops/napiisarray", + "pages/javascript/jsabstractops/napistrictequals", "pages/javascript/jsvalues/napicreateint32", "pages/javascript/jsvalues/napicreateuint32", "pages/javascript/jsvalues/napicreateint64" 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 320079cb..82a026ca 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 @@ -407,6 +407,44 @@ export default function abilityTestJsProperty() { expect(result3.toString()).assertContain(',,'); }) + it('testNapiObjectFreeze', 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 testNapiObjectFreeze begin'); + let obj: myObj = { + key1: 'value', + key2: 222, + true: 333, + key3: true + }; + let result = testNapi.testNapiObjectFreeze(obj); + hilog.info(0x0000, 'testTag', `napi_object_freeze(obj) = ${JSON.stringify(result)}`); + expect(JSON.stringify(result)).assertContain('{"key1":"value","key2":222,"true":333,"key3":true}'); + let result2 = testNapi.testNapiDeleteProperty(obj, 'key1'); + hilog.info(0x0000, 'testTag', `napi_object_freeze(obj, 'key1') = ${JSON.stringify(result2)}`); + expect(JSON.stringify(result2)).assertContain('{"key1":"value","key2":222,"true":333,"key3":true}'); + }) + + it('testNapiObjectSeal', 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 testNapiObjectSeal begin'); + let obj: myObj = { + key1: 'value', + key2: 222, + true: 333, + key3: true + }; + let result = testNapi.testNapiObjectSeal(obj); + hilog.info(0x0000, 'testTag', `napi_object_seal(obj) = ${JSON.stringify(result)}`); + expect(JSON.stringify(result)).assertContain('{"key1":"value","key2":222,"true":333,"key3":true}'); + + let result2 = testNapi.testNapiDeleteProperty(obj, 'key1'); + hilog.info(0x0000, 'testTag', `napi_delete_property(obj, 'key1') = ${JSON.stringify(result2)}`); + expect(JSON.stringify(result2)).assertContain('{"key1":"value","key2":222,"true":333,"key3":true}'); + + let result3 = testNapi.testNapiSetProperty(obj, 'key1', 'flower'); + hilog.info(0x0000, 'testTag', `napi_set_property(obj, 'key1', 'flower') = ${JSON.stringify(result3)}`); + expect(JSON.stringify(result3)).assertContain('{"key1":"flower","key2":222,"true":333,"key3":true}'); + }) }) } diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets index 077032d9..21db79ab 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets @@ -267,5 +267,86 @@ export default function jsAbstractOpsTest() { expect(testFuncResult).assertEqual('function'); hilog.info(0x0000, 'testTag', '%{public}s', 'it napiTypeof end'); }) + + /** + * cpp/javascriptapi/jsabstractops/napiisarray.cpp + * pages/javascript/jsabstractops/napiisarray.ets + */ + it('napiIsArrayTest', 0, () => { + hilog.info(0x0000, 'testTag', '%{public}s', 'it napiIsArray begin'); + + // Test if number/string/boolean/null/undefined/array is array + const testNum: number = 123; + const testStr: string = 'test123' + const testBool: boolean = true; + const testNull: null = null; + const testUndef: undefined = undefined; + const testArr: Array = [1, '2', false, null, undefined]; + const testNumIsArrayResult = testNapi.testNapiIsArray(testNum); + const testStrIsArrayResult = testNapi.testNapiIsArray(testStr); + const testBoolIsArrayResult = testNapi.testNapiIsArray(testBool); + const testNullIsArrayResult = testNapi.testNapiIsArray(testNull); + const testUndefIsArrayResult = testNapi.testNapiIsArray(testUndef); + const testArrIsArrayResult = testNapi.testNapiIsArray(testArr); + + // Print the results + hilog.info(0x0000, 'testTag', `napi_is_array(${testNum}) = ${testNumIsArrayResult}`); + hilog.info(0x0000, 'testTag', `napi_is_array(${testStr}) = ${testStrIsArrayResult}`); + hilog.info(0x0000, 'testTag', `napi_is_array(${testBool}) = ${testBoolIsArrayResult}`); + hilog.info(0x0000, 'testTag', `napi_is_array(${testNull}) = ${testNullIsArrayResult}`); + hilog.info(0x0000, 'testTag', `napi_is_array(${testUndef}) = ${testUndefIsArrayResult}`); + hilog.info(0x0000, 'testTag', `napi_is_array(${testArr}) = ${testArrIsArrayResult}`); + + // Define assertion methods for type checking + expect(testNumIsArrayResult).assertEqual(false); + expect(testStrIsArrayResult).assertEqual(false); + expect(testBoolIsArrayResult).assertEqual(false); + expect(testNullIsArrayResult).assertEqual(false); + expect(testUndefIsArrayResult).assertEqual(false); + expect(testArrIsArrayResult).assertEqual(true); + hilog.info(0x0000, 'testTag', '%{public}s', 'it napiIsArray end'); + }) + + /** + * cpp/javascriptapi/jsabstractops/napistrictequals.cpp + * pages/javascript/jsabstractops/napistrictequals.ets + */ + it('napiStrictEqualsTest', 0, () => { + hilog.info(0x0000, 'testTag', '%{public}s', 'it napiStrictEquals begin'); + + // Test if the two objects are equal + const testNum: number = 123; + const testNumEqu: number = 123; + const testZeroNum: number = 0; + const testNumStr: string = '123'; + const testZeroNumStr: string = '0'; + const testBoolStr: string = 'true'; + const testTrueBool: boolean = true; + const testNull: null = null; + const testUndef: undefined = undefined; + const testNumStrStrictEqualsResult = testNapi.testNapiStrictEquals(testNum, testNumStr); + const testZeroNumStrStrictEqualsResult = testNapi.testNapiStrictEquals(testZeroNum, testZeroNumStr); + const testTrueBoolStrStrictEqualsResult = testNapi.testNapiStrictEquals(testBoolStr, testTrueBool); + const testNullZeroStrictEqualsResult = testNapi.testNapiStrictEquals(testNull, testZeroNum); + const testNullUndefStrictEqualsResult = testNapi.testNapiStrictEquals(testNull, testUndef); + const testNumStrictEqualsResult = testNapi.testNapiStrictEquals(testNum, testNumEqu); + + // Print the results + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNum}, ${testNumStr}) = ${testNumStrStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testZeroNum}, ${testZeroNumStr}) = ${testZeroNumStrStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testBoolStr}, ${testTrueBool}) = ${testTrueBoolStrStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNull}, ${testZeroNum}) = ${testNullZeroStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNull}, ${testUndef}) = ${testNullUndefStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNum}, ${testNumEqu}) = ${testNumStrictEqualsResult}`); + + // Define assertion methods for type checking + expect(testNumStrStrictEqualsResult).assertEqual(false); + expect(testZeroNumStrStrictEqualsResult).assertEqual(false); + expect(testTrueBoolStrStrictEqualsResult).assertEqual(false); + expect(testNullZeroStrictEqualsResult).assertEqual(false); + expect(testNullUndefStrictEqualsResult).assertEqual(false); + expect(testNumStrictEqualsResult).assertEqual(true); + hilog.info(0x0000, 'testTag', '%{public}s', 'it napiIsArray end'); + }) }) } -- Gitee From 27a43bf26ebd3eba3c9b57005415554134605276 Mon Sep 17 00:00:00 2001 From: zhangmenghan Date: Fri, 18 Oct 2024 14:21:34 +0800 Subject: [PATCH 2/3] codecheck Signed-off-by: zhangmenghan --- .../src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp | 3 ++- .../main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp | 3 ++- .../src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp | 3 ++- .../src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp index fa073a4c..20d74a75 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napiisarray.cpp @@ -17,7 +17,8 @@ static const char *TAG = "[jsapi_isarray]"; -napi_value testNapiIsArray(napi_env env, napi_callback_info info) { +napi_value testNapiIsArray(napi_env env, napi_callback_info info) +{ // pages/javascript/jsabstractops/napiisarray size_t requireArgc = PARAM1; size_t argc = PARAM1; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp index c1b78928..dbb8e064 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsabstractops/napistrictequals.cpp @@ -17,7 +17,8 @@ static const char *TAG = "[jsapi_strictequals]"; -napi_value testNapiStrictEquals(napi_env env, napi_callback_info info) { +napi_value testNapiStrictEquals(napi_env env, napi_callback_info info) +{ // pages/javascript/jsabstractops/napistrictequals size_t requireArgc = PARAM2; size_t argc = PARAM2; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp index b8affbfb..16f50e33 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectfreeze.cpp @@ -18,7 +18,8 @@ static const char *TAG = "[javascriptapi_property]"; -napi_value testNapiObjectFreeze(napi_env env, napi_callback_info info) { +napi_value testNapiObjectFreeze(napi_env env, napi_callback_info info) +{ // pages/javascript/jsproperty/napiobjectfreeze // 获取参数数量 size_t argc = PARAM1; diff --git a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp index 834b219d..ed13d579 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascriptapi/jsproperty/napiobjectseal.cpp @@ -18,7 +18,8 @@ static const char *TAG = "[javascriptapi_property]"; -napi_value testNapiObjectSeal(napi_env env, napi_callback_info info) { +napi_value testNapiObjectSeal(napi_env env, napi_callback_info info) +{ // pages/javascript/jsproperty/napiobjectseal // 获取参数数量 size_t argc = PARAM1; -- Gitee From 2e601036f9b1444968a040fbb95a232779f2c99f Mon Sep 17 00:00:00 2001 From: zhangmenghan Date: Mon, 21 Oct 2024 11:03:39 +0800 Subject: [PATCH 3/3] napistrictequals.test Signed-off-by: zhangmenghan --- .../test/javascriptapi/jsabstractops.test.ets | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets index 21db79ab..7bcf127b 100644 --- a/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets +++ b/examples/napitutorials/entry/src/ohosTest/ets/test/javascriptapi/jsabstractops.test.ets @@ -17,6 +17,18 @@ import hilog from '@ohos.hilog'; import testNapi from 'libentry.so'; import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +interface NumObj { + key1: number; + key2: number; + key3: number +} + +interface NumObject { + true: number; + key2: number; + key3: number +} + export default function jsAbstractOpsTest() { describe('jsAbstractOpsTest', () => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. @@ -317,19 +329,48 @@ export default function jsAbstractOpsTest() { // Test if the two objects are equal const testNum: number = 123; const testNumEqu: number = 123; + const testNumb: number = 986; const testZeroNum: number = 0; const testNumStr: string = '123'; const testZeroNumStr: string = '0'; const testBoolStr: string = 'true'; const testTrueBool: boolean = true; + const testTrueBoolean: boolean = true; + const testFalseBool: boolean = false; const testNull: null = null; + const testNullEqu: null = null; const testUndef: undefined = undefined; + const testNumArr: Array = [1, 2, 3]; + const testNumArrUnequ: Array = [1, 2, 3]; + const testNumObj: NumObj = { + key1: 1, + key2: 2, + key3: 3 + } + const testNumObjEqu: NumObj = { + key1: 1, + key2: 2, + key3: 3 + } + const testNumObject: NumObject = { + true: 1, + key2: 2, + key3: 3 + } const testNumStrStrictEqualsResult = testNapi.testNapiStrictEquals(testNum, testNumStr); const testZeroNumStrStrictEqualsResult = testNapi.testNapiStrictEquals(testZeroNum, testZeroNumStr); const testTrueBoolStrStrictEqualsResult = testNapi.testNapiStrictEquals(testBoolStr, testTrueBool); const testNullZeroStrictEqualsResult = testNapi.testNapiStrictEquals(testNull, testZeroNum); const testNullUndefStrictEqualsResult = testNapi.testNapiStrictEquals(testNull, testUndef); + const testBoolStrictEqualsResult = testNapi.testNapiStrictEquals(testTrueBool, testFalseBool); + const testArrObjStrictEqualsResult = testNapi.testNapiStrictEquals(testNumArr, testNumObj); + const testNumbStrictEqualsResult = testNapi.testNapiStrictEquals(testNum, testNumb); + const testNumbObjStrictEqualsResult = testNapi.testNapiStrictEquals(testNumObj, testNumObject); + const testNumArrUnequStrictEqualsResult = testNapi.testNapiStrictEquals(testNumArr, testNumArrUnequ); + const testNumObjEquStrictEqualsResult = testNapi.testNapiStrictEquals(testNumObj, testNumObjEqu); const testNumStrictEqualsResult = testNapi.testNapiStrictEquals(testNum, testNumEqu); + const testNullStrictEqualsResult = testNapi.testNapiStrictEquals(testNull, testNullEqu); + const testTruBoolStrictEqualsResult = testNapi.testNapiStrictEquals(testTrueBool, testTrueBoolean); // Print the results hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNum}, ${testNumStr}) = ${testNumStrStrictEqualsResult}`); @@ -337,7 +378,15 @@ export default function jsAbstractOpsTest() { hilog.info(0x0000, 'testTag', `napi_strict_equals(${testBoolStr}, ${testTrueBool}) = ${testTrueBoolStrStrictEqualsResult}`); hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNull}, ${testZeroNum}) = ${testNullZeroStrictEqualsResult}`); hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNull}, ${testUndef}) = ${testNullUndefStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testTrueBool}, ${testFalseBool}) = ${testBoolStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNumArr}, ${testNumObj}) = ${testArrObjStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNum}, ${testNumb}) = ${testNumbStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNumObj}, ${testNumObject}) = ${testNumbObjStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNumArr}, ${testNumArrUnequ}) = ${testNumArrUnequStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNumObj}, ${testNumObjEqu}) = ${testNumObjEquStrictEqualsResult}`); hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNum}, ${testNumEqu}) = ${testNumStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testNull}, ${testNullEqu}) = ${testNullStrictEqualsResult}`); + hilog.info(0x0000, 'testTag', `napi_strict_equals(${testTrueBool}, ${testTrueBoolean}) = ${testTruBoolStrictEqualsResult}`); // Define assertion methods for type checking expect(testNumStrStrictEqualsResult).assertEqual(false); @@ -345,8 +394,16 @@ export default function jsAbstractOpsTest() { expect(testTrueBoolStrStrictEqualsResult).assertEqual(false); expect(testNullZeroStrictEqualsResult).assertEqual(false); expect(testNullUndefStrictEqualsResult).assertEqual(false); + expect(testBoolStrictEqualsResult).assertEqual(false); + expect(testArrObjStrictEqualsResult).assertEqual(false); + expect(testNumbStrictEqualsResult).assertEqual(false); + expect(testNumbObjStrictEqualsResult).assertEqual(false); + expect(testNumArrUnequStrictEqualsResult).assertEqual(false); + expect(testNumObjEquStrictEqualsResult).assertEqual(false); expect(testNumStrictEqualsResult).assertEqual(true); - hilog.info(0x0000, 'testTag', '%{public}s', 'it napiIsArray end'); + expect(testNullStrictEqualsResult).assertEqual(true); + expect(testTruBoolStrictEqualsResult).assertEqual(true); + hilog.info(0x0000, 'testTag', '%{public}s', 'it napiStrictEquals end'); }) }) } -- Gitee