MOO-cows Mailing List Archive

[Prev][Next][Index][Thread]

Re: register_function line



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 
> arglist.v.list[i].v.fnum was a pointer?
> 

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 = 1 ; i <= 2 ; i++ )
    {
      dp = (i==1) ? stop : start;

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

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

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

  return make_var_pack(new_float(dist));
}

-- 
PGP public key available - finger hpa@zytor.com
"The earth is but one country, and mankind its citizens."  --  Bahá'u'lláh
Just Say No to Morden * Save Babylon 5: http://www.babylon5.com/cmp/support/


Follow-Ups: References:

Home | Subject Index | Thread Index