Pike_sp

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


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].

Stack element
Pike_sp[0] First free slot on the stack.
Pike_sp[-1] Most recently pushed element.

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.

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

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