/* Copyright (c) 2001-2009 by SoftIntegration, Inc. All Rights Reserved */
#include <ch.h>
#include <stdio.h>
#include <limits.h>

typedef return_type2 (*FUNC)(int num, ...); /* function pointer type */
static ChInterp_t interp;
static void *func_chdl_funptr;
static return_type2 func_chdl_funarg(int num, ...) {
    return_type2 retval;
    data_type1 arg1;
    data_type2 arg2;
    /* ... other possible arguments in the variable length argument list */
    ChVaList_t ap_ch, ap;

    va_start(ap, num);
    ap_ch = Ch_VarArgsCreate(interp);
    if(num>=1) {
      arg1 = va_arg(ap, data_type1);
      Ch_VarArgsAddArg(interp, &ap_ch, CH_TYPE_1, arg1); /* CH_TYPE_1 is one of macros
                                     corresponding to data_type1 and 
                                     defined in ch.h, 
                                     such as CH_INTTYPE to int */
    }
    if(num>=2) {
      arg2 = va_arg(ap, data_type2);
      Ch_VarArgsAddArg(interp, &ap_ch, CH_TYPE_2, arg2);
    }
    /* ... for other possible number of argument */

    Ch_CallFuncByAddr(interp, func_chdl_funptr, &retval, num, ap_ch);  
    Ch_VarArgsDelete(interp, ap_ch);
    return retval;
}

EXPORTCH  return_type setFuncVNA_chdl(void *varg)
{
   ChVaList_t ap;
   return_type retval;

   Ch_VaStart(interp, ap, varg);
   func_chdl_funptr = Ch_VaArg(interp, ap, void *);
   if(func_chdl_funptr != NULL) {
      retval = setFuncVNA(func_chdl_funarg);
   }
   else {
      retval = setFuncVNA(NULL);
   }

   Ch_VaEnd(interp, ap);
   return retval;
}

