/*****************************************************************
 * File: "native-function-export.mustache"
 *
 * Native Function: {{ name }}
 *  - moduleName: {{ target.moduleName }}
 *  - exportName: {{ target.exportName }}
 *  - Parameters: {{{ nativeParamTypes }}}
 *  - Ret: {{ retType }}
 ******************************************************************/

const __sidecar__{{ name }}_Function_wrapper = (() => {
  /**
   * Huan(202107): Need to be workaround for the `getExportByName` function
   *  see: https://github.com/huan/sidecar/issues/10
   */
  const nativeFunctionAddress =
  {{# target.moduleName }}
    Module.enumerateExportsSync('{{ target.moduleName }}')
    .filter(x => x.type === 'function')
    .filter(x => x.name === '{{ target.exportName }}')
    .map(x => x.address)
    .pop()
  {{/ target.moduleName}}
  {{^ target.moduleName }}
    Module.getExportByName(
      null,
      '{{ target.exportName }}',
    )
  {{/ target.moduleName}}

  const nativeFunction = new NativeFunction(
    nativeFunctionAddress,
    {{{ nativeRetType }}},
    {{{ nativeParamTypes }}},
  )

  return function (...args) {
    log.verbose(
      'SidecarAgent',
      '{{ name }}(%s)',
      args.join(', '),
    )

    {{{ declareNativeArgs }}}

    const ret = nativeFunction(...{{{ nativeArgs }}})
    return {{{ jsRet }}}
  }

})()

