如何在C++调用从ArkTS传递过来的function
===========================

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; 
       _// Declare parameter array_ 
        napi\_value args\[1\] = {nullptr}; 
     
      _// Retrieve the incoming parameters and sequentially place them into the parameter array_ 
        napi\_get\_cb\_info(env, info, &argc, args , nullptr, nullptr); 
     
       _ // Create an int as a parameter for ArkTS_
        napi\_value argv = nullptr;     
        napi\_create\_int32(env, 2, &argv ); 
     
      _  // Call the incoming callback and return its 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 Index {
      @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%')
      }
    }
    

**参考链接**

[Native侧方法的实现](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/use-napi-process#native侧方法的实现)