1/* 2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import image from '@ohos.multimedia.image'; 17import testNapi from 'libentry.so'; 18import { TitleBar } from '../../../common/TitleBar'; 19import hilog from '@ohos.hilog'; 20 21const TAG: string = 'napi_coerce_to_string'; 22 23@Entry 24@Component 25struct napicoercetostring { 26 @State isSetInstance: Boolean = false; 27 @State imagePixelMap: PixelMap | undefined = undefined; 28 @State textcont: string = 'napi_coerce_to_string() 用于将任意类型的 JavaScript 值' 29 + '(例如 boolean 或 number)强制转换为 string。' 30 + '如果 API 成功,则返回 napi_ok。' 31 + '该 API 实现了 ECMAScript 语言规范的 第 7.1.13 节 中定义的抽象操作 ToString()。' 32 + '如果传入的值是对象,此函数可能会运行 JS 代码。'; 33 @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n' 34 + 'const testBool2StrResult = addon.addonCoerceToString(true); // true -> \'true\'\n' 35 + 'const testNum2StrResult = addon.testNapiCoerceToString(-123.456); // -123.456 -> \'-123.456\'\n' 36 + 'const testNull2StrResult = addon.testNapiCoerceToString(null); // null -> \'null\'\n' 37 + 'const testUndef2StrResult = addon.testNapiCoerceToString(undefined); // undefined -> \'undefined\'\n' 38 + '// 输出强制转换结果 \n' 39 + 'console.log(`true -> ${testBool2StrResult}`) \n' 40 + 'console.log(`-123.456 -> ${testNum2StrResult}`) \n' 41 + 'console.log(`null -> ${testNull2StrResult}`) \n' 42 + 'console.log(`undefined -> ${testUndef2StrResult}`) \n'; 43 controller: TextAreaController = new TextAreaController() 44 private btnFontColor: Resource = $r('app.color.white'); 45 private pixelMapFormat: image.PixelMapFormat = 3; 46 47 build() { 48 Scroll() { 49 Column() { 50 // 标题 51 TitleBar({ title: $r('app.string.napi_coerce_to_string') }) 52 53 Column() { 54 Column() { 55 TextArea({ 56 text: this.textcont, 57 placeholder: '', 58 }) 59 .placeholderFont({ size: 16, weight: 400 }) 60 .width('90%') 61 .margin(10) 62 .fontSize(16) 63 .fontColor('#182431') 64 .backgroundColor('#FFFFFF') 65 .enabled(false) 66 67 TextArea({ 68 text: this.testcont, 69 placeholder: '', 70 }) 71 .placeholderFont({ size: 16, weight: 400 }) 72 .width('90%') 73 .margin(10) 74 .fontSize(16) 75 .fontColor('#ff400336') 76 .backgroundColor('#ff985307') 77 .enabled(false) 78 } 79 .width('100%') 80 .alignItems(HorizontalAlign.Center) 81 .justifyContent(FlexAlign.Start) 82 83 Row() { 84 85 Button($r('app.string.napi_coerce_to_string'), { type: ButtonType.Capsule }) 86 .backgroundColor(Color.Blue) 87 .width('80%') 88 .height(48) 89 .fontSize(16) 90 .fontWeight(500) 91 .fontColor(this.btnFontColor) 92 .margin({ left: 24 }) 93 .id('napi_coerce_to_string') 94 .onClick(() => { 95 try { 96 // Test coercing boolean/number/null/undefined values to string 97 const testBool: boolean = true; 98 const testNum: number = -123.456; 99 const testNull: null = null; 100 const testUndef: undefined = undefined; 101 const testBool2StrResult = testNapi.testNapiCoerceToString(testBool); 102 const testNum2StrResult = testNapi.testNapiCoerceToString(testNum); 103 const testNull2StrResult = testNapi.testNapiCoerceToString(testNull); 104 const testUndef2StrResult = testNapi.testNapiCoerceToString(testUndef); 105 106 // Replace result in testcont 107 this.testcont = this.testcont.replace('${testBool2StrResult}', `## ${testBool2StrResult} ##`); 108 this.testcont = this.testcont.replace('${testNum2StrResult}', `## ${testNum2StrResult} ##`); 109 this.testcont = this.testcont.replace('${testNull2StrResult}', `## ${testNull2StrResult} ##`); 110 this.testcont = this.testcont.replace('${testUndef2StrResult}', `## ${testUndef2StrResult} ##`); 111 112 // Print the results 113 hilog.info(0x0000, TAG, `(${typeof testBool})${testBool} -> ` 114 + `(${typeof testBool2StrResult})${testBool2StrResult}`); 115 hilog.info(0x0000, TAG, `(${typeof testNum})${testNum} -> ` 116 + `(${typeof testNum2StrResult})${testNum2StrResult}`); 117 hilog.info(0x0000, TAG, `(${typeof testNull})${testNull} -> ` 118 + `(${typeof testNull2StrResult})${testNull2StrResult}`); 119 hilog.info(0x0000, TAG, `(${typeof testUndef})${testUndef} -> ` 120 + `(${typeof testUndef2StrResult})${testUndef2StrResult}`); 121 } catch (error) { 122 hilog.error(0x0000, TAG, `Catch error testNapiCoerceToString: ${error.message}}`) 123 } 124 }) 125 } 126 .width('100%') 127 .height(48) 128 .alignItems(VerticalAlign.Center) 129 .justifyContent(FlexAlign.SpaceBetween) 130 } 131 .width('100%') 132 } 133 } 134 .width('100%') 135 .height('100%') 136 .align(Alignment.Top) 137 .backgroundColor($r('app.color.background_shallow_grey')) 138 } 139}