Native如何调ArkTS的方法
=================

1\. 在index.d.ts文件中提供 ArkTS 接口方法。

export const nativeCallArkTS: (a: object) => number;

2\. 实现Native侧的NativeCallArkTS接口，代码如下：

static napi\_value NativeCallArkTS(napi\_env env, napi\_callback\_info info) 
{     
    size\_t argc = 1; 
   _ // Declaring parameter array ARG_
    napi\_value args\[1\] = { nullptr }; 
 
   _ // Retrieve the passed parameters and place them in the parameter array 'args'_
    napi\_get\_cb\_info(env, info, &argc, args , nullptr, nullptr); 
 
   _ // Create int as an input parameter for ArkTS_
    napi\_value argv = nullptr;     
    napi\_create\_int32(env, 2, &argv ); 
 
  _  // Call the incoming callback and return the result_
    napi\_value result = nullptr; 
    napi\_call\_function(env, nullptr, args\[0\], 1, &argv, &result); 
    return result; 
}

3\. 在ArkTS侧，通过nativeModule.nativeCallArkTS()方法传入回调函数。

entry/src/main/ets/pages/Index.ets

_// Introduce native capabilities through import._
import nativeModule from 'libentry.so'

@Entry
@Component
struct InvokeArkTSMethod {
  @State message: string = 'Test Node-API nativeCallArkTS result: ';

  build() {
    Row() {
      Column() {
       _ // Call the nativeCallArkTS method, corresponding to the Native NativeCallArkTS, and call the ArkTS function in Native._
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.message += nativeModule.nativeCallArkTS((a: number) => {
              return a \* 2;
            });
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}

**参考链接**

[Node-API典型使用场景](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/napi-scenarios)