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

static ChInterp_t interp;
static void *func_chdl_funptr;
static int func3_tmp() {
    int retval;
    int j = 10;
    float f=20;
    double d=40;
    ChVaList_t ap_ch;

    ap_ch = Ch_VarArgsCreate(interp);
    Ch_VarArgsAddArg(interp, &ap_ch, CH_INTTYPE, j);
    Ch_VarArgsAddArg(interp, &ap_ch, CH_FLOATTYPE, f);
    Ch_VarArgsAddArg(interp, &ap_ch, CH_DOUBLETYPE, d);
    Ch_CallFuncByAddr(interp, func_chdl_funptr, &retval, ap_ch);  
    /* Ch_CallFuncByName(interp, "func", &retval, ap_ch);   */
    Ch_VarArgsDelete(interp, ap_ch);
    return retval;
}

EXPORTCH int func3_chdl(void *varg)
{
   ChVaList_t ap;
   int i;
   int j;
   int count, num;
   int retval;

   Ch_VaStart(interp, ap, varg);
   num = Ch_VaFuncArgNum(interp, ap);
   if(num == INT_MAX)
     printf("Passed pointer to func in func2_chdl has varaible number arg\n");
   else 
     printf("Passed pointer to func in func2_chdl has %d arg\n", num);
   func_chdl_funptr = Ch_VaArg(interp, ap, void *);

   if(func_chdl_funptr != NULL) {
      retval = func3_tmp();
   }
   else {
      printf("passed fun argument to func3_chdl() is NULL\n");
   }

   Ch_VaEnd(interp, ap);
   return retval;
}

