赞
踩
N-API提供一些API来创建JavaScript所有类型值。这些API用于执行以下操作:
- // let obj = {}
- napi_value obj;
- napi_create_object(env, &obj);
-
- // let world = "world"
- napi_value world;
- napi_create_string_utf8(env, "world", NAPI_AUTO_LENGTH, &world);
-
- // let arr = []
- napi_value arr, arrElement0, arrElement1, arrElement2;
- napi_create_array(env, &arr);
-
- // arr[0] = 94
- napi_create_int32(env, 94, &arrElement0);
- napi_set_element(env, arr, 0, arrElement0);
-
- // arr[1] = "123d"
- napi_create_string_utf8(env, "123d", NAPI_AUTO_LENGTH, &arrElement1);
- napi_set_element(env, arr, 1, arrElement1);
-
- // arr[2] = 12.32
- napi_create_double(env, 12.32, &arrElement2);
- napi_set_element(env, arr, 2, arrElement2);
-
- // let obj1 = {}
- napi_value obj1, objValue;
- napi_create_object(env, &obj1);
-
- // let objValue = "obj in obj"
- napi_create_string_utf8(env, "obj in obj", NAPI_AUTO_LENGTH, &objValue);
-
- // obj1["test"] = objValue
- napi_set_named_property(env, obj1, "test", objValue);
-
- // obj["hello"] = world
- // obj["arr"] = arr
- // obj["obj1"] = obj1
- napi_set_named_property(env, obj, "hello", world);
- napi_set_named_property(env, obj, "arr", arr);
- napi_set_named_property(env, obj, "obj1", obj1);
场景:js 端向 C++ 端传入 Object 类型数据,C++端对其属性就行修改后,返回给 js 端。 注:建议先学习 JS TypedArray
- Row() {
- Text("ModifyObject")
- .fontSize(50)
- .fontWeight(FontWeight.Bold)
- .onClick(() => {
- const typedArray = new Uint8Array(6);
- typedArray[0] = 127;
- typedArray[2] = 66;
- typedArray[4] = 32;
- const obj: {
- obj1: Object,
- hello: String,
- arr: Array<any>,
- typedArray: Uint8Array
- } = {
- hello: 'world',
- arr: [94, '123d', 12.32, { test: 'obj in arr'}],
- obj1: {
- test: 'obj in obj'
- },
- typedArray
- }
- hilog.info(0x0000, 'napiTestTag', 'Test NAPI modifyObject result is %{public}s', JSON.stringify(testNapi.modifyObject(obj)));
- })
- }
- static napi_value 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);
- // js端传过来的参数
- napi_value obj = args[0];
- }
- // let obj = {
- // hello: 'world',
- // arr: [94, '123d', 12.32, { test: 'obj in arr'}],
- // obj1: {
- // test: 'obj in obj'
- // }
- // }
- napi_value obj = args[0];
-
- // let world0 = "world0";
- // obj['hello'] = world0
- napi_value world0;
- napi_create_string_utf8(env, "world0", NAPI_AUTO_LENGTH, &world0);
- napi_set_named_property(env, obj, "hello", world0);
-
- // let arr = obj['arr'];
- // delete arr[2];
- napi_value arr;
- napi_get_named_property(env, obj, "arr", &arr);
- napi_delete_element(env, arr, 2, nullptr);
-
- // let arrLen;
- // arrLen = arr.length;
- uint32_t arrLen;
- napi_get_array_length(env, arr, &arrLen);
-
- // for(let i = 0; i < arrLen; i++) {
- // let tmp = i;
- // arr[i] = tmp;
- // }
- uint32_t i = 0;
- for(i = 0; i < arrLen; i++) {
- napi_value tmp;
- napi_create_uint32(env, i, &tmp);
- napi_set_element(env, arr, i, tmp);
- }
-
- // let typedArr = obj['typedArray']
- napi_value typedArr;
- napi_get_named_property(env, obj, "typedArray", &typedArr);
- bool is_typedarray;
- NODE_API_CALL(env, napi_is_typedarray(env, typedArr, &is_typedarray));
-
- // let input_buffer = typedArr.buffer
- // let byte_offset = typedArr.byteOffset
- // let length = typedArr.length
- napi_typedarray_type type;
- napi_value input_buffer;
- size_t byte_offset;
- size_t length;
- NODE_API_CALL(env, napi_get_typedarray_info(env, typedArr, &type, &length, NULL, &input_buffer, &byte_offset));
-
- // 获取 input_buffer 的基础数据缓冲区 data,和基础数据缓冲区的长度 byte_length。
- void* data;
- size_t byte_length;
- NODE_API_CALL(env, napi_get_arraybuffer_info(env, input_buffer, &data, &byte_length));
-
- // 创建新的ArrayBuffer,&output_ptr 指向 ArrayBuffer 的底层数据缓冲区的指针
- napi_value output_buffer;
- void* output_ptr = NULL;
- NODE_API_CALL(env, napi_create_arraybuffer(env, byte_length, &output_ptr, &output_buffer));
-
- // 使用 output_buffer 创建 typedarray
- napi_value output_array;
- NODE_API_CALL(env, napi_create_typedarray(env, type, length, output_buffer, byte_offset, &output_array));
-
- // data 是由连续的内存位置组成,(uint8_t*)(data) 表示其第一个元素的内存地址。
- // data 是旧的 arraybuffer 数据指针
- uint8_t* input_bytes = (uint8_t*)(data) + byte_offset;
-
- // 把 output_ptr 指针赋值给 output_bytes
- // output_ptr 是新的 arraybuffer 数据指针
- uint8_t* output_bytes = (uint8_t*)(output_ptr);
- for (i = 0; i < length; i++) {
- // 将旧 arraybuffer 数据每一个元素乘 2,赋值给新 arraybuffer 数据
- output_bytes[i] = (uint8_t)(input_bytes[i] * 2);
- }
-
- // 将新 typedArray 赋值给 obj['typedArray']
- napi_set_named_property(env, obj, "typedArray", output_array);
-
- // let obj1 = obj['obj1'];
- napi_value obj1;
- napi_get_named_property(env, obj, "obj1", &obj1);
-
- // let str = obj1['test'];
- // let buf = str;
- // buf += ",this is be modified";
- napi_value str;
- napi_get_named_property(env, obj1, "test", &str);
- char buf;
- size_t bufSize = 20;
- size_t res;
- napi_get_value_string_utf8(env, str, &buf, bufSize, &res);
- strcat(&buf, ",this is be modified");
-
- // obj1['test'] = buf;
- napi_value newNapiStr;
- napi_create_string_utf8(env, &buf, NAPI_AUTO_LENGTH, &newNapiStr);
- napi_set_named_property(env, obj1, "test", newNapiStr);
-
- napi_set_named_property(env, obj, "obj1", obj1);
Test NAPI modifyObject result is {"hello":"world0","arr":[0,1,2,3],"obj1":{"test":"obj in obj,this is be modified"}}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。