diff --git a/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt b/examples/napitutorials/entry/src/main/cpp/CMakeLists.txt index 6a59243d99f0214e78419f814fcf13a0759b3e24..8e192812ee50185469a794662ac3080b31aafc33 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 d267dff95eca7dac5671c4365a69456fcb0b7fdb..b4b0fcfe6bb48e467aba5fc632c746a02fa6e9fb 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 574de21019c7831dfc629f978c28f98214fed830..f47d3a72e257b24ca8d58bf447827b27c84e09af 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 0000000000000000000000000000000000000000..8753fdea800280f08a1ce3cb9fe34ccbfa053abb --- /dev/null +++ b/examples/napitutorials/entry/src/main/cpp/javascript/jsabstractops/napicoercetobool.cpp @@ -0,0 +1,51 @@ +/* + * 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 + size_t requireArgc = 1; + size_t argc = 1; + napi_status status; + napi_value result; + napi_value args[1] = {nullptr}; + + // Get args + napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); + + // 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 + napi_valuetype resultType; + napi_typeof(env, result, &resultType); + if (resultType != napi_boolean) { + char errmsg[64]; + snprintf_s(errmsg, sizeof(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 7d1ef6f7e24677bd330e8c1f6a9c7f9c63e3e2cb..94f58be878c32e940780016f2f6f34bf57abcd68 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 343b9e55c5a77f2df86967fd555d0d4c00c34b6c..0c397417b735cf00b89eae56bc4e3930dc24880b 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 0000000000000000000000000000000000000000..0f25bd51ef98b944e49c0dd48516477bba23f869 --- /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 3e04f1af1a895604a114cf8331c21715308a22d1..35538f855a22f5a89f95ac3c6a4e97b29b8ba6ee 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" ] }