Pike_sp

[ Start > PikeDevel > C Modules > Pike_sp ] [ Edit this Page | Show Page Versions | Show Formatted ]


__Working with arguments__

__Pike_sp__ is your key to working with the Pike stack. The top of the stack (ie the last argument passed to your function) is Pike_sp\[-1\], and the bottom of the stack (or the first argument) is Pike_sp\[-args\]. Therefore, to get the ~~n~~th argument, where 1 is the first argument, is __Pike_sp\[-args + n -1\]__.

{table}
Stack element | 
Pike_sp\[0\] |     First free slot on the stack.
Pike_sp\[-1\] |     Most recently pushed element.
{table} 

Alternately, you can use __[get_all_args()]__ to check to make sure you have the proper number and type of arguments, and place them into variables. If your needs are not terribly complex (I'm pretty sure that it doesn't handle variable argument types or varargs), this might be a good option.  If you need to handle optional arguments, you can use get_all_args() also.  Just put a . in the format string before the optional arguments.  In the below code, z will only be set if there are enough arguments.  In that case, you should set a default value.

{code}
int x;
float y;
int z=12;
get_all_args("func",args,"%i%f.%i",&x,&y,&z);
{code}

If your function receives incorrect arguments, instead of using Pike_error, use the following:

for too few arguments, use:

   __SIMPLE_TOO_FEW_ARGS_ERROR__("myfunction",1);

for bad arguments, use:

   __SIMPLE_BAD_ARG_ERROR__("myfunction", 1, "array|mapping|whatever");

Powered by PikeWiki2

 
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University