• 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 router from '@ohos.router';
17import image from '@ohos.multimedia.image';
18import Logger from '../../../util/Logger';
19import testNapi from 'libentry.so';
20import { TitleBar } from '../../../common/TitleBar'
21import { TcBaseSample } from '../../../interface/tctsample'
22import hilog from '@ohos.hilog';
23import { TcNADataType } from '../../../interface/tctnadatatype';
24import { TCTYPE } from '../../../interface/tctbase';
25import { TcCJsonSample } from '../../../interface/tctcjsonsample';
26// import Benchmark from '../../../../js/benchmark/index';
27
28const TAG: string = 'napi_roi';
29
30@Entry
31@Component
32struct napisetinstancedata {
33  private btnFontColor: Resource = $r('app.color.white');
34  private pixelMapFormat: image.PixelMapFormat = 3;
35  @State isSetInstance: Boolean = false;
36  @State imagePixelMap: PixelMap | undefined = undefined;
37  @State textcont: string = '一个 Node.js 环境对应一个 ECMAScript Agent。在主进程中,启动时创建一个环境,'
38    + '可以在单独的线程上创建额外的环境作为 工作线程。当 Node.js 嵌入到另一个应用中时,应用的主线程也可能在应用的生命周期'
39    + '中多次构造和销毁 Node.js 环境,使得应用创建的每个 Node.js 环境可能,在 反过来,在其生命周期中创建和销毁额外的环境作为工作线程。'
40    + '从原生插件的角度来看,这意味着它提供的绑定可以从多个上下文中多次调用,甚至可以同时从多个线程中调用。'
41    + '原生插件可能需要分配它们在 Node.js 环境的生命周期中使用的全局状态,以便该状态对于插件的每个实例都是唯一的。'
42    + '为此,Node-API 提供了一种关联数据的方法,使其生命周期与 Node.js 环境的生命周期相关联。'
43  @State testcont: string = ' // 获取导出的 N-API 实例对象 \n'
44    + ' const instance = addon.instance; \n'
45    + ' // 使用实例对象进行操作 \n'
46    + ' console.log(instance); // 输出 N-API 实例对象 \n'
47    + ' // 获取与实例对象关联的自定义数据 \n'
48    + '  const myData = addon.getInstanceData(instance); \n'
49    + ' // 使用获取的自定义数据 \n'
50    + ' console.log(myData); // 输出自定义数据 \n';
51
52  controller: TextAreaController = new TextAreaController()
53
54  build() {
55    Column() {
56      // 标题
57      TitleBar({ title: $r('app.string.napi_set_instance_data') })
58
59      Column() {
60        Column() {
61          TextArea({
62            text: this.textcont,
63            placeholder: '',
64          })
65          .placeholderFont({ size: 16, weight: 400 })
66          .width('90%')
67          .margin(10)
68          .fontSize(16)
69          .fontColor('#182431')
70          .backgroundColor('#FFFFFF')
71          .enabled(false)
72
73          TextArea({
74            text: this.testcont,
75            placeholder: '',
76          })
77            .placeholderFont({ size: 16, weight: 400 })
78            .width('90%')
79            .margin(10)
80            .fontSize(16)
81            .fontColor('#ff400336')
82            .backgroundColor('#ff985307')
83            .enabled(false)
84        }
85        .width('100%')
86        .alignItems(HorizontalAlign.Center)
87        .justifyContent(FlexAlign.Start)
88
89        Row() {
90            Button($r('app.string.napi_set_instance_data'), { type: ButtonType.Capsule })
91              .backgroundColor(Color.Blue)
92              .width('80%')
93              .height(48)
94              .fontSize(16)
95              .fontWeight(500)
96              .fontColor(this.btnFontColor)
97              .margin({ left: 24 })
98              .id('napi_set_instance_data')
99              .onClick(() => {
100                let addres = testNapi.add(5, 8);
101                hilog.info(0x0000, 'testTag', `addres : ${addres}`);
102                let baseobj = testNapi.getTestCase(1);
103                hilog.info(0x0000, 'testTag', `baseobj : ${baseobj.name}`);
104                let tcbaseobj = baseobj as TcBaseSample;
105                hilog.info(0x0000, 'testTag', `baseobj.add : ${tcbaseobj.add(1, 2)}`);
106                // let type : TCTYPE = TCTYPE.TCT_NADATATYPE;
107                let naobj = testNapi.getTestCase(2);
108                hilog.info(0x0000, 'testTag', `naobj : ${naobj.name}`);
109                let tcnadatatypeobj = naobj as TcNADataType;
110                hilog.info(0x0000, 'testTag', `tcnadatatypeobj.testNapiStatus : ${tcnadatatypeobj.testNapiStatus(1, 2)}`);
111                let cjobj = testNapi.getTestCase(7);
112                hilog.info(0x0000, 'testTag', `cjobj : ${cjobj.name}`);
113                let tccjsonobj = cjobj as TcCJsonSample;
114                hilog.info(0x0000, 'testTag', `tccjsonobj.cjson_version : ${tccjsonobj.cjson_version()}`);
115                let instanceobj: Object = testNapi.instance;
116                let ret1 = `instance ${instanceobj} `
117                let ret2 = ' ld.lld: error: undefined symbol: napi_set_instance_data '
118                hilog.info(0x0000, 'testTag', ret1);
119
120                // let benversion = Benchmark.version;
121                // hilog.info(0x0000, 'testTag', `benchmark version : ${benversion}`);
122                // hilog.info(0x0000, 'testTag', `instance ${instanceobj.testint32} `);
123                this.testcont = this.testcont.replace('log(instance)', 'log(## '+ret1+' ##)');
124                this.testcont = this.testcont.replace('log(myData)', 'log(## '+ret2+' ##)');
125              })
126          }
127        .width('100%')
128        .height(48)
129        .alignItems(VerticalAlign.Center)
130        .justifyContent(FlexAlign.SpaceBetween)
131      }
132      .width('100%')
133    }
134    .height('100%')
135    .width('100%')
136    .backgroundColor($r('app.color.background_shallow_grey'))
137  }
138}
139