Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Embedding perl and exposing xs functions

by BUU (Prior)
on Dec 19, 2005 at 08:17 UTC ( [id://517674]=perlquestion: print w/replies, xml ) Need Help??

BUU has asked for the wisdom of the Perl Monks concerning the following question:

Basically I'm trying to create a file that creates an embedded perl interpreter and exposes various XS functions to the created interpreter. I wish to do this in the same file mainly for simplicity reasons, I actually need to run this code inside of a much larger project and I think it will simplify things a great deal to have easy access to the functions I'm wrapping. Here's the code:
#include <EXTERN.h> /* from the Perl distribution */ #include <perl.h> /* from the Perl distribution */ #include <XSUB.h> #include <stdlib.h> static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ void say( char *m ) { printf("I am say: %s", m ); } XS(XS_Module_say); /* prototype to pass -Wmissing-prototypes */ XS(XS_Module_say) { dXSARGS; if (items != 1) Perl_croak(aTHX_ "Usage: Module::say(message)"); { char * message = (char *)SvPV_nolen(ST(0)); say(message); } XSRETURN_EMPTY; } #ifdef __cplusplus extern "C" #endif XS(boot_Module); /* prototype to pass -Wmissing-prototypes */ XS(boot_Module) { dXSARGS; char* file = __FILE__; XS_VERSION_BOOTCHECK ; newXS("Module::say", XS_Module_say, file); XSRETURN_YES; } int main(int argc, char **argv, char **env) { char *embedding = { "", "-e", "0" }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; perl_parse(my_perl, NULL, argc, argv, (char **)NULL); perl_run(my_perl); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
The problem is when I run it, I see no sign of my XS function ("say") in either main:: or Module::. It occurred to me that perhaps I need to call "boot_Module", which apparently takes two arguments: a perl_interpreter, which I have, and a CV*, which I don't. I've tried various combinations of arguments to boot_Module, but it constantly segfaults. What is the right thing to pass?

Replies are listed 'Best First'.
Re: Embedding perl and exposing xs functions
by PodMaster (Abbot) on Dec 19, 2005 at 09:37 UTC
    Add
    static void xs_init (pTHX); EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); EXTERN_C void boot_Module (pTHX_ CV* cv); EXTERN_C void xs_init(pTHX) { char *file = __FILE__; dXSUB_SYS; /* DynaLoader is a special case */ newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); newXS("Module::bootstrap", boot_Module, file); }
    Change
    perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
    to
    perl_parse(my_perl, xs_init, argc, argv, (char **)NULL);
    And then try
    C:\>interp -e "Module::bootstrap();Module::say()"
    and it should work :)(the dynaloader stuff is just there to show where I got it from, perlembed of course)

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

      Oh god, that's so obvious now that you've told me. I must have read that section a dozen times. Much thanks!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://517674]
Approved by Tomte
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-25 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found