Native侧如何获取ArkTS侧的Uint8Array实例
==============================

ArkTS Uint8Array的传递方式与其他类型相同。

_// ArkTS passes Uint8Array parameter_
import testNapi from 'libentry.so';

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

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            let temp = new Uint8Array(2);
            temp\[0\] = 1;
            temp\[1\] = 2;
            console.info(\`Pure inputBuffer length: ${temp.length}\`);
            let res = testNapi.uintArr(temp);
            console.info(\`Pure outputBuffer: ${res}\`);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

Native侧使用napi\_get\_typedarray\_info方法获取Uint8Array的详细信息。

_// Native side obtains Uint8Array parameter and returns it to ArkTS side_ 
#include "UintArr.h" 
napi\_value Demo1::UintArr(napi\_env env, napi\_callback\_info info) { 
    size\_t requireArgc = 1; 
    size\_t argc = 1; 
    napi\_value args\[1\] = {nullptr}; 
 
    napi\_get\_cb\_info(env, info, &argc, args, nullptr, nullptr); 
 
    napi\_value inputArray = args\[0\]; 
 
   _// Get the ArrayBuffer type_ 
    napi\_typedarray\_type type; 
    napi\_value inArrayBuffer; 
    size\_t byteOffset; 
    size\_t length; 
    napi\_get\_typedarray\_info(env, inputArray, &type, &length, nullptr, &inArrayBuffer, &byteOffset); 
    if (type != napi\_uint8\_array) { 
        return nullptr; 
    } 
     
  _// Retrieve information from the ArrayBuffer_ 
    void \*data = nullptr; 
    size\_t byte\_length; 
    napi\_get\_arraybuffer\_info(env, inArrayBuffer, &data, &byte\_length); 
 
  _// Construct an ArrayBuffer and assign a value_ 
    napi\_value output\_buffer; 
    void \*output\_ptr = nullptr; 
    napi\_create\_arraybuffer(env, byte\_length, &output\_ptr, &output\_buffer); 
    napi\_value outputArray; 
    napi\_create\_typedarray(env, type, length, output\_buffer, byteOffset, &outputArray); 
    uint8\_t \*input\_bytes = (uint8\_t \*)(data) + byteOffset; 
    uint8\_t \*array = (uint8\_t \*)(output\_ptr); 
    for (size\_t idx = 0; idx < length; idx++) { 
        array\[idx\] = 3; 
    } 
 
    return outputArray; 
}

index.d.ts声明接口。

export const uintArr: (a: Uint8Array) => object;