鸿蒙5.0开发常见问题【Native侧如何对ArkTS传递的Object类型的数据、属性进行修改?】

  1. ArkTS侧调用Native侧方法modifyObject,并传递参数。
import testNapi from 'libentry.so';


interface Obj1 {
  obj: Obj2,
  hello: String,
  arr: number[],
  typedArray: Uint8Array
}


interface Obj2 {
  str: string
}


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';


  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            const typedArr = new Uint8Array(3);
            typedArr[0] = 1;
            typedArr[1] = 2;
            typedArr[2] = 3;
            const obj: Obj1 = {
              obj: { str: 'obj in obj' },
              hello: 'world',
              arr: [94, 32, 43],
              typedArray: typedArr
            }
            console.info(`Test NAPI modifyObject result is ${JSON.stringify(testNapi.modifyObject(obj))}`)
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

index.d.ts声明导出接口。

export const modifyObject: (a: object) => object;
  1. Native侧解析参数并修改数据、属性
#include "RevArkTSObj.h"
napi_value RevArkTSObj::ModifyObject(napi_env env, napi_callback_info info) {
    size_t argc = 1;
    napi_value args[1] = {nullptr};
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);


    napi_value obj = args[0];


    napi_value obj1;
    napi_value hello1;
    napi_value arr1;
    napi_value typedArray1;


    napi_get_named_property(env, obj, "obj", &obj1);
    char *buf = "this is modified";
    napi_value str1;
    napi_create_string_utf8(env, buf, NAPI_AUTO_LENGTH, &str1);
    napi_set_named_property(env, obj1, "str", str1);
    napi_set_named_property(env, obj, "obj", obj1);


    napi_create_string_utf8(env, "world0", NAPI_AUTO_LENGTH, &hello1);
    napi_set_named_property(env, obj, "hello", hello1);


    napi_get_named_property(env, obj, "arr", &arr1);
    uint32_t arrLen;
    napi_get_array_length(env, arr1, &arrLen);
    for (int i = 0; i < arrLen; i++) {
        napi_value tmp;
        napi_create_uint32(env, i, &tmp);
        napi_set_element(env, arr1, i, tmp);
    }
    napi_delete_element(env, arr1, 2, nullptr);




    napi_get_named_property(env, obj, "typedArray", &typedArray1);
    bool is_typedArray;
    if (napi_ok != napi_is_typedarray(env, typedArray1, &is_typedArray)) {
        return nullptr;
    }
    napi_typedarray_type type;
    napi_value input_buffer;
    size_t length;
    size_t byte_offset;
    napi_get_typedarray_info(env, typedArray1, &type, &length, nullptr, &input_buffer, &byte_offset);
    // 获取 input_buffer 的基础数据缓冲区 data,和基础数据缓冲区的长度 byte_length。
    void *data;
    size_t byte_length;
    napi_get_arraybuffer_info(env, input_buffer, &data, &byte_length);
    // 创建新的ArrayBuffer,&output_ptr 指向 ArrayBuffer 的底层数据缓冲区的指针
    napi_value output_buffer;
    void *output_prt = nullptr;
    napi_create_arraybuffer(env, byte_length, &output_prt, &output_buffer);
    // 使用 output_buffer 创建 typedarray
    napi_value output_array;
    napi_create_typedarray(env, type, length, output_buffer, byte_offset, &output_array);
    // data 是由连续的内存位置组成,reinterpret_cast<uint8_t *>(data)  表示其第一个元素的内存地址。
    // data 是旧的 arraybuffer 数据指针
    uint8_t *input_bytes = reinterpret_cast<uint8_t *>(data) + byte_offset;
    // 把 output_ptr 指针赋值给 output_bytes
    // output_ptr 是新的 arraybuffer 数据指针
    uint8_t *output_bytes = reinterpret_cast<uint8_t *>(output_prt);
    for (int i = 0; i < length; i++) {
        // 将旧 arraybuffer 数据每一个元素乘 2,赋值给新 arraybuffer 数据
        output_bytes[i] = input_bytes[i] * 2;
    }
    // 将新 typedArray 赋值给 obj['typedArray']
    napi_set_named_property(env, obj, "typedArray", output_array);
    return obj;
}
  1. 输出结果:

    Test NAPI modifyObject result is {“obj”:{“str”:“this is modified”},“hello”:“world0”,“arr”:[0,1,null],“typedArray”:{“0”:2,“1”:4,“2”:6}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值