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

int chcallfuncbyaddr(double x[], double y[], double (*func)(double), int n) {
  int i;
  double retval;
  for (i = 0; i < n; i++)
    y[i] = func(x[i]);
  return 0;
}

static ChInterp_t interp;
static void *func_chdl_funptr;
static double func_chdl_funarg(double xx) {
  double retval;
  Ch_CallFuncByAddr(interp, func_chdl_funptr, &retval, xx);
  return retval;
}

EXPORTCH int chcallfuncbyaddr_chdl(void *varg) {
  ChVaList_t ap;
  double *x, *y;
  int i, n, retval;

  Ch_VaStart(interp, ap, varg);
  n = Ch_VaArrayExtent(interp, ap, 0);
  x = Ch_VaArg(interp, ap, double*);   
  y = Ch_VaArg(interp, ap, double*);   
  func_chdl_funptr = Ch_VaArg(interp, ap, void *);
  retval = chcallfuncbyaddr(x, y, func_chdl_funarg, n);
  
  Ch_VaEnd(interp, ap);
  return retval;
}

