register_function line

H. Peter Anvin (hpa@trantor.zytor.com)
Wed, 29 May 1996 19:32:52 PDT


Followup to:  <Pine.LNX.3.91.960529192149.494A-100000@ralin.mistral.co.uk=
>
By author:    bartmoss@mistral.co.uk
In newsgroup: local.moo
>
> > float bf_distance(float stop[3], float start[3])
>
> The register line looked fine, did you notice that=20
> arglist.v.list[i].v.fnum was a pointer?
>=20

More importantly, the calling convention for a builtin function is:

static package
bf_functionname(Var arglist, Byte next, void *vdata, Objid progr)
{
	...
}

In your case you have not only to unroll the arglist (which is a
variable which is always a list) but also check the constitution of
the component lists (since that is not done automatically).

Completely untested top-of-my-head code:

static package
bf_distance(Var arglist, Byte next, void *vdata, Objid progr)
{
  double stop[3], start[3], *dp, dist;
  Var r;
  int i, j;

  for ( i =3D 1 ; i <=3D 2 ; i++ )
    {
      dp =3D (i=3D=3D1) ? stop : start;

      r =3D arglist.v.list[i];
      if (r.v.list[0].num !=3D 3)
	{
	  free_var(arglist);
	  return make_error_pack(E_INVARG);
	}
     =20
      for ( j =3D 1 ; i <=3D 3 ; i++ )
	{
	  if ( r.v.list[j].type !=3D TYPE_FLOAT )
	    {
	      free_var(arglist);
	      return make_error_pack(E_INVARG);
	    }
	  dp[j-1] =3D *r.v.list[j].v.fnum;
	}
    }

  /* Do your thing, for example... */

  dist =3D 0.0;
  for ( i =3D 0 ; i < 3 ; i++ )
    dist +=3D (start[i]-stop[i])*(start[i]-stop[i]);
  dist =3D sqrt(dist);

  return make_var_pack(new_float(dist));
}

--=20
PGP public key available - finger hpa@zytor.com
"The earth is but one country, and mankind its citizens."  --  Bah=E1'u'l=
l=E1h
Just Say No to Morden * Save Babylon 5: http://www.babylon5.com/cmp/suppo=
rt/