From 6314429dfc76d1ca10271820d7a02924025eb69f Mon Sep 17 00:00:00 2001 From: GuangweiLi <213203228@seu.edu.cn> Date: Sun, 7 Apr 2024 14:16:56 +0800 Subject: [PATCH 1/3] feat: Add a new page: javascript/jsabstractops/napicoercetobool Signed-off-by: GuangweiLi <213203228@seu.edu.cn> --- .../entry/src/main/cpp/CMakeLists.txt | 1 + .../entry/src/main/cpp/include/nodeapi.h | 3 + .../napitutorials/entry/src/main/cpp/init.cpp | 1 + .../jsabstractops/napicoercetobool.cpp | 50 +++++++ .../src/main/cpp/types/libentry/index.d.ts | 5 +- .../ets/pages/javascript/JavascriptApi.ets | 2 +- .../jsabstractops/napicoercetobool.ets | 129 ++++++++++++++++++ .../resources/base/profile/main_pages.json | 3 +- 8 files changed, 191 insertions(+), 3 deletions(-) create mode 100644 examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp create mode 100644 examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets diff --git a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt index 6a59243d..8e192812 100644 --- a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt +++ b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt @@ -19,6 +19,7 @@ add_library(entry SHARED nodeapi/datatypes/napithreadsafefuncall.cpp nodeapi/datatypes/napiextendederrorinfo.cpp ncpp/cjsoncase/cjsoncase.cpp + javascript/jsabstractops/napicoercetobool.cpp init.cpp) target_link_libraries(entry PUBLIC diff --git a/examples/napitutorials/entry/src/main/cpp/include/nodeapi.h b/examples/napitutorials/entry/src/main/cpp/include/nodeapi.h index d267dff9..b4b0fcfe 100644 --- a/examples/napitutorials/entry/src/main/cpp/include/nodeapi.h +++ b/examples/napitutorials/entry/src/main/cpp/include/nodeapi.h @@ -33,4 +33,7 @@ napi_value setThreadsafefuncall(napi_env env, napi_callback_info info); napi_value cJSONVersion(napi_env env, napi_callback_info info); +/* work_with_javascript_values_and_abstract_operations */ +napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info); + #endif //NAPITUTORIALS_NODEAPI_H diff --git a/examples/napitutorials/entry/src/main/cpp/init.cpp b/examples/napitutorials/entry/src/main/cpp/init.cpp index 574de210..f47d3a72 100644 --- a/examples/napitutorials/entry/src/main/cpp/init.cpp +++ b/examples/napitutorials/entry/src/main/cpp/init.cpp @@ -61,6 +61,7 @@ static napi_value Init(napi_env env, napi_value exports) {"testNapiThreadsafefuncrel", nullptr, setThreadsafefuncrel, nullptr, nullptr, nullptr, napi_default, nullptr}, {"testNapiThreadsafefuncall", nullptr, setThreadsafefuncall, nullptr, nullptr, nullptr, napi_default, nullptr}, {"cjson_version", nullptr, cJSONVersion, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"testNapiCoerceToBool", nullptr, testNapiCoerceToBool, nullptr, nullptr, nullptr, napi_default, nullptr}, }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); return exports; diff --git a/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp new file mode 100644 index 00000000..1f28c404 --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp @@ -0,0 +1,50 @@ +/* + * 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 "nodeapi.h" + +static const char *TAG = "[jsapi_coercetobool]"; + +napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) { + // pages/javascript/jsabstractops/napicoercetobool + // Get args + size_t requireArgc = 1; + size_t argc = 1; + napi_value args[1] = {nullptr}; + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // Call napi_coerce_to_bool(), any -> bool + napi_value result; + napi_status status; + status = napi_coerce_to_bool(env, args[0], &result); + + if (status != napi_ok) { + napi_throw_error(env, NULL, "Failed to coerce to bool"); + return NULL; + } + + // Check if the result is a boolean + napi_valuetype resultType; + napi_typeof(env, result, &resultType); + if (resultType != napi_boolean) { + char errmsg[64]; + snprintf(errmsg, sizeof(errmsg), "Expected a boolean, got %d", resultType); + napi_throw_error(env, NULL, errmsg); + return NULL; + } + + return result; +} 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 7d1ef6f7..94f58be8 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 @@ -31,4 +31,7 @@ export const testNapiThreadsafefuncrel: (callback: Callback) => number; export const testNapiThreadsafefuncall: (callback: Callback) => number; export const instance; -export const cjson_version: () => string; \ No newline at end of file +export const cjson_version: () => string; + +/* work_with_javascript_values_and_abstract_operations */ +export const testNapiCoerceToBool: (a: any) => boolean; \ No newline at end of file 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 343b9e55..0c397417 100644 --- a/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/JavascriptApi.ets @@ -246,7 +246,7 @@ const WORK_WITH_JAVASCRIPT_VALUES_AND_ABSTRACT_OPERATIONS: ThirdLevelCategory = childNodes: [ { title: $r('app.string.napi_coerce_to_bool'), - url: 'pages/image/basicSample/image2Gray' + url: 'pages/javascript/jsabstractops/napicoercetobool' }, { title: $r('app.string.napi_coerce_to_number'), diff --git a/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets new file mode 100644 index 00000000..0f25bd51 --- /dev/null +++ b/examples/napitutorials/entry/src/main/ets/pages/javascript/jsabstractops/napicoercetobool.ets @@ -0,0 +1,129 @@ +/* + * 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_coerce_to_bool'; + +@Entry +@Component +struct napicoercetobool { + @State isSetInstance: Boolean = false; + @State imagePixelMap: PixelMap | undefined = undefined; + @State textcont: string = 'napi_coerce_to_bool() 用于将任意类型的 JavaScript 值' + + '(例如 number 或 string)强制转换为 Boolean。' + + '如果 API 成功,则返回 napi_ok。' + + '该 API 实现了 ECMAScript 语言规范的 第 7.1.2 节 中定义的抽象操作 ToBoolean()。'; + @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' + + 'const result1 = addon.testNapiCoerceToBool(\'test\'); // 非空字符串 -> true \n' + + 'const result2 = addon.testNapiCoerceToBool(\'\'); // 空字符串 -> false \n' + + 'const result3 = addon.testNapiCoerceToBool(123); // 非零值 -> true \n' + + 'const result4 = addon.testNapiCoerceToBool(0); // 零值 -> false \n' + + '// 输出强制转换结果 \n' + + 'console.log(`\'test\' -> ${result1}`); \n' + + 'console.log(`\'\' -> ${result2}`); \n' + + 'console.log(`123 -> ${result3}`); \n' + + 'console.log(`0 -> ${result4}`); \n'; + controller: TextAreaController = new TextAreaController() + private btnFontColor: Resource = $r('app.color.white'); + private pixelMapFormat: image.PixelMapFormat = 3; + + build() { + Column() { + // 标题 + TitleBar({ title: $r('app.string.napi_coerce_to_bool') }) + + 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('#ff985307') + .enabled(false) + } + .width('100%') + .alignItems(HorizontalAlign.Center) + .justifyContent(FlexAlign.Start) + + Row() { + + Button($r('app.string.napi_coerce_to_bool'), { type: ButtonType.Capsule }) + .backgroundColor(Color.Blue) + .width('80%') + .height(48) + .fontSize(16) + .fontWeight(500) + .fontColor(this.btnFontColor) + .margin({ left: 24 }) + .id('napi_coerce_to_bool') + .onClick(() => { + try { + // Test coercing string/number values to boolean + let testValue1: string = 'test'; + let testValue2: string = ''; + let testValue3: number = 123; + let testValue4: number = 0; + let result1 = testNapi.testNapiCoerceToBool(testValue1); + let result2 = testNapi.testNapiCoerceToBool(testValue2); + let result3 = testNapi.testNapiCoerceToBool(testValue3); + let result4 = testNapi.testNapiCoerceToBool(testValue4); + // Replace result in testcont + this.testcont = this.testcont.replace('${result1}', `## ${result1} ##`); + this.testcont = this.testcont.replace('${result2}', `## ${result2} ##`); + this.testcont = this.testcont.replace('${result3}', `## ${result3} ##`); + this.testcont = this.testcont.replace('${result4}', `## ${result4} ##`); + // Print the results + hilog.info(0x0000, TAG, `(${typeof (testValue1)})${testValue1} -> ${result1}`); + hilog.info(0x0000, TAG, `(${typeof (testValue2)})${testValue2} -> ${result2}`); + hilog.info(0x0000, TAG, `(${typeof (testValue3)})${testValue3} -> ${result3}`); + hilog.info(0x0000, TAG, `(${typeof (testValue4)})${testValue4} -> ${result4}`); + } catch (error) { + hilog.error(0x0000, TAG, `Catch error testNapiCoerceToBool: ${error.message}}`) + } + }) + } + .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/profile/main_pages.json b/examples/napitutorials/entry/src/main/resources/base/profile/main_pages.json index 3e04f1af..35538f85 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 @@ -57,6 +57,7 @@ "pages/nodeapi/objlifetimemgr/napiremoveenvcleanuphook", "pages/nodeapi/objlifetimemgr/napiaddasynccleanuphook", "pages/nodeapi/objlifetimemgr/napiremoveasynccleanuphook", - "pages/ncpp/cjsonfuncs/cjsonfuncs" + "pages/ncpp/cjsonfuncs/cjsonfuncs", + "pages/javascript/jsabstractops/napicoercetobool" ] } -- Gitee From d2f1e2fc1039254ccba9658b401f36c808b2699a Mon Sep 17 00:00:00 2001 From: GuangweiLi <213203228@seu.edu.cn> Date: Sun, 7 Apr 2024 15:36:53 +0800 Subject: [PATCH 2/3] fix: Code check --- .../jsabstractops/napicoercetobool.cpp | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp index 1f28c404..8753fdea 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp @@ -18,22 +18,23 @@ static const char *TAG = "[jsapi_coercetobool]"; -napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) { +napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) +{ // pages/javascript/jsabstractops/napicoercetobool - // Get args size_t requireArgc = 1; size_t argc = 1; + napi_status status; + napi_value result; napi_value args[1] = {nullptr}; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - // Call napi_coerce_to_bool(), any -> bool - napi_value result; - napi_status status; - status = napi_coerce_to_bool(env, args[0], &result); + // Get args + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Failed to coerce to bool"); - return NULL; + // Call napi_coerce_to_bool(), any -> bool + status = napi_coerce_to_bool(env, args[0], &result); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Failed to coerce to bool"); + return NULL; } // Check if the result is a boolean @@ -41,7 +42,7 @@ napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) { napi_typeof(env, result, &resultType); if (resultType != napi_boolean) { char errmsg[64]; - snprintf(errmsg, sizeof(errmsg), "Expected a boolean, got %d", resultType); + snprintf_s(errmsg, sizeof(errmsg), sizeof(errmsg), "Expected a boolean, got %d", resultType); napi_throw_error(env, NULL, errmsg); return NULL; } -- Gitee From 5c837715d34dde3c8956d7dd8a11d8fe68f09ba4 Mon Sep 17 00:00:00 2001 From: GuangweiLi <213203228@seu.edu.cn> Date: Sun, 7 Apr 2024 15:36:53 +0800 Subject: [PATCH 3/3] fix: Code check Signed-off-by: GuangweiLi <213203228@seu.edu.cn> --- .../jsabstractops/napicoercetobool.cpp | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp index 1f28c404..8753fdea 100644 --- a/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp +++ b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp @@ -18,22 +18,23 @@ static const char *TAG = "[jsapi_coercetobool]"; -napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) { +napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) +{ // pages/javascript/jsabstractops/napicoercetobool - // Get args size_t requireArgc = 1; size_t argc = 1; + napi_status status; + napi_value result; napi_value args[1] = {nullptr}; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - // Call napi_coerce_to_bool(), any -> bool - napi_value result; - napi_status status; - status = napi_coerce_to_bool(env, args[0], &result); + // Get args + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - if (status != napi_ok) { - napi_throw_error(env, NULL, "Failed to coerce to bool"); - return NULL; + // Call napi_coerce_to_bool(), any -> bool + status = napi_coerce_to_bool(env, args[0], &result); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Failed to coerce to bool"); + return NULL; } // Check if the result is a boolean @@ -41,7 +42,7 @@ napi_value testNapiCoerceToBool(napi_env env, napi_callback_info info) { napi_typeof(env, result, &resultType); if (resultType != napi_boolean) { char errmsg[64]; - snprintf(errmsg, sizeof(errmsg), "Expected a boolean, got %d", resultType); + snprintf_s(errmsg, sizeof(errmsg), sizeof(errmsg), "Expected a boolean, got %d", resultType); napi_throw_error(env, NULL, errmsg); return NULL; } -- Gitee