• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_object';
22
23@Entry
24@Component
25struct napicoercetoobject {
26  @State isSetInstance: Boolean = false;
27  @State imagePixelMap: PixelMap | undefined = undefined;
28  @State textcont: string = 'napi_coerce_to_object() 用于将任意类型的 JavaScript 值'
29    + '(例如 boolean、number 或 string)强制转换为 object。'
30    + '如果 API 成功,则返回 napi_ok。'
31    + '该 API 实现了 ECMAScript 语言规范的 第 7.1.13 节 中定义的抽象操作 ToObject()。';
32  @State testcont: string = '// 调用 API 对不同类型的输入进行测试 \n'
33    + 'const testBool2ObjResult = addon.addonCoerceToObject(true);\n'
34    + 'const testNum2ObjResult = addon.testNapiCoerceToObject(-123.456);\n'
35    + 'const testStr2ObjResult = addon.testNapiCoerceToObject(\'test\');\n'
36    + 'const testNull2ObjResult = addon.testNapiCoerceToObject(null); // null -> undefined\n'
37    + 'const testUndef2ObjResult = addon.testNapiCoerceToObject(undefined); // undefined -> undefined\n'
38    + '// 输出强制转换结果 \n'
39    + 'console.log(`true -> ${testBool2ObjResult}`) \n'
40    + 'console.log(`-123.456 -> ${testNum2ObjResult}`) \n'
41    + 'console.log(`\'test\' -> ${testStr2ObjResult}`) \n'
42    + 'console.log(`null -> ${testNull2ObjResult}`) \n'
43    + 'console.log(`undefined -> ${testUndef2ObjResult}`) \n';
44  controller: TextAreaController = new TextAreaController()
45  private btnFontColor: Resource = $r('app.color.white');
46  private pixelMapFormat: image.PixelMapFormat = 3;
47
48  build() {
49    Scroll() {
50      Column() {
51        // 标题
52        TitleBar({ title: $r('app.string.napi_coerce_to_object') })
53
54        Column() {
55          Column() {
56            TextArea({
57              text: this.textcont,
58              placeholder: '',
59            })
60              .placeholderFont({ size: 16, weight: 400 })
61              .width('90%')
62              .margin(10)
63              .fontSize(16)
64              .fontColor('#182431')
65              .backgroundColor('#FFFFFF')
66              .enabled(false)
67
68            TextArea({
69              text: this.testcont,
70              placeholder: '',
71            })
72              .placeholderFont({ size: 16, weight: 400 })
73              .width('90%')
74              .margin(10)
75              .fontSize(16)
76              .fontColor('#ff400336')
77              .backgroundColor('#ff985307')
78              .enabled(false)
79          }
80          .width('100%')
81          .alignItems(HorizontalAlign.Center)
82          .justifyContent(FlexAlign.Start)
83
84          Row() {
85
86            Button($r('app.string.napi_coerce_to_object'), { type: ButtonType.Capsule })
87              .backgroundColor(Color.Blue)
88              .width('80%')
89              .height(48)
90              .fontSize(16)
91              .fontWeight(500)
92              .fontColor(this.btnFontColor)
93              .margin({ left: 24 })
94              .id('napi_coerce_to_object')
95              .onClick(() => {
96                try {
97                  // Test coercing boolean/number/string/null/undefined values to object
98                  const testBool: boolean = true;
99                  const testNum: number = -123.456;
100                  const testStr: string = 'test'
101                  const testNull: null = null;
102                  const testUndef: undefined = undefined;
103                  const testBool2ObjResult = testNapi.testNapiCoerceToObject(testBool);
104                  const testNum2ObjResult = testNapi.testNapiCoerceToObject(testNum);
105                  const testStr2ObjResult = testNapi.testNapiCoerceToObject(testStr);
106                  const testNull2ObjResult = testNapi.testNapiCoerceToObject(testNull);
107                  const testUndef2ObjResult = testNapi.testNapiCoerceToObject(testUndef);
108
109                  // Replace result in testcont
110                  this.testcont = this.testcont.replace('${testBool2ObjResult}',
111                    `## (${typeof testBool2ObjResult})${testBool2ObjResult} ##`);
112                  this.testcont = this.testcont.replace('${testNum2ObjResult}',
113                    `## (${typeof testNum2ObjResult})${testNum2ObjResult} ##`);
114                  this.testcont = this.testcont.replace('${testStr2ObjResult}',
115                    `## (${typeof testStr2ObjResult})${testStr2ObjResult} ##`);
116                  this.testcont = this.testcont.replace('${testNull2ObjResult}',
117                    `## (${typeof testNull2ObjResult})${testNull2ObjResult} ##`);
118                  this.testcont = this.testcont.replace('${testUndef2ObjResult}',
119                    `## (${typeof testUndef2ObjResult})${testUndef2ObjResult} ##`);
120
121                  // Print the results
122                  hilog.info(0x0000, TAG, `(${typeof testBool})${testBool} -> `
123                    + `(${typeof testBool2ObjResult})${testBool2ObjResult}`);
124                  hilog.info(0x0000, TAG, `(${typeof testNum})${testNum} -> `
125                    + `(${typeof testNum2ObjResult})${testNum2ObjResult}`);
126                  hilog.info(0x0000, TAG, `(${typeof testStr})${testStr} -> `
127                    + `(${typeof testStr2ObjResult})${testStr2ObjResult}`);
128                  hilog.info(0x0000, TAG, `(${typeof testNull})${testNull} -> `
129                    + `(${typeof testNull2ObjResult})${testNull2ObjResult}`);
130                  hilog.info(0x0000, TAG, `(${typeof testUndef})${testUndef} -> `
131                    + `(${typeof testUndef2ObjResult})${testUndef2ObjResult}`);
132                } catch (error) {
133                  hilog.error(0x0000, TAG, `Catch error testNapiCoerceToObject: ${error.message}}`)
134                }
135              })
136          }
137          .width('100%')
138          .height(48)
139          .alignItems(VerticalAlign.Center)
140          .justifyContent(FlexAlign.SpaceBetween)
141        }
142        .width('100%')
143      }
144    }
145    .width('100%')
146    .height('100%')
147    .align(Alignment.Top)
148    .backgroundColor($r('app.color.background_shallow_grey'))
149  }
150}