Calling Pike Code

[ Start > PikeDevel > C Modules > Calling Pike Code ] [ Edit this Page | Show Page Versions | Show Raw Source ]


Calling pike functions from within C/CMOD code

Here's some code for calling (whether they're written in c or pike) functions from your C language module code. Remember that you can create objects this way because create is called implicitly, just ask for the class name you want to instantiate:

// this code works for a function that takes one argument.
// you'll have to modify it to get it to understand multiple args.
// args are at top of stack

// look up the function push_text( "Sql.sql_result"); SAFE_APPLY_MASTER("resolv", 1 );

// method is at top of the stack, we want the arg on top stack_swap();

// arg is at top, function is 1 down from the top apply_svalue( Pike_sp-2, 1 );

// result is at top of the stack, function is still one down // and we want to pop it. stack_swap(); pop_stack();

To run the function "query" in the current object with one arg, already on the stack:

// get the identifier for the member we want to call
int i;
i=find_identifier("query", Pike_fp->current_object->prog);

// now call the method with the context of the current object. apply_low(Pike_fp->current_object, i, 1);

Another option:

/* push arguments on stack */ apply(Pike_fp->current_object, "the method", number_of_args);

The return value will be on the stack, don't forget to pop it after you are done with it.


Powered by PikeWiki2

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