- 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;
- 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;
}
-
输出结果:
Test NAPI modifyObject result is {“obj”:{“str”:“this is modified”},“hello”:“world0”,“arr”:[0,1,null],“typedArray”:{“0”:2,“1”:4,“2”:6}}