in reply to Re: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
in thread Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?

Hi,

Appreciate your response!

In general the flow of my current code is divided to 5 stages(as seen bellow):
1. Create an interpreter (my_perl)
2. Push parameters to the Perl stack (XPUSHs)
3. Call Perl function (call_pv)
4. POP return value (POPi)
5. Destruct the interpreter

Code wise, stages 2 and 4 depends on the existance of 'my_perl' interpreter pointer (XPUSHs and POPi macros are using it).

How can I get a pointer to the interpreter of the Perl function that called the C code? Should I use other macros?


Thanks a lot for your help :)


Itamar


HV* my_hash; AV* my_arr; int retVal = 0; char* my_argv[] = { "", cb->cbFunc.perlCbFunc.fileName }; // PERL interpreter creation my_perl = perl_alloc(); perl_construct( my_perl ); // PERL interpreter initiation perl_parse( my_perl, // The interpreter NULL, // XSINIT value - Should be NULL if no + external modules are being used 2, // Number of entries in 'my_argv' my_argv, // PERL 'command line'. (char **)NULL); // ENV, not relevant for our usage // Run the PERL interpreter perl_run(my_perl); // Call a PERL function dSP; /* Ini +tialize stack pointer */ ENTER; /* Eve +rything created after here */ SAVETMPS; /* ... +is a temporary variable. */ PUSHMARK(SP); /* Rem +ember the stack pointer */ XPUSHs(sv_2mortal(newSViv((unsigned int)cbCtx))); /* Pus +h callback context onto stack */ my_hash = newHV(); my_arr = newAV(); hv_store( my_hash, "aaaaaa", 6, newSViv(1), 0 ); hv_store( my_hash, "bbb", 3, newSViv(2), 0 ); hv_store( my_hash, "cccccccccc", 10, newSViv(3), 0 ); hv_store( my_hash, "ddddddddd", 9, newSViv(4), 0 ); hv_store( my_hash, "eeeeeee", 7, newSViv(5), 0 ); hv_store( my_hash, "ffff", 4, newSViv(6), 0 ); hv_store( my_hash, "gggggg", 6, newSViv(7), 0 ); hv_store( my_hash, "hhhhhhh", 7, newSViv(8), 0 ); for ( int i = 0; i < 50; i++ ) { av_push( my_arr, newSViv((unsigned int)arr[i]) ); } // Store reference to parameters on the HASH hv_store( my_hash, "arr", 6, newRV((SV*)my_arr), 0 ); XPUSHs( sv_2mortal(newRV((SV*)my_hash)) ); PUTBACK; /* Mak +e local stack pointer global */ call_pv( funcName, G_SCALAR ); /* Cal +l the function */ retVal = POPi; /* POP + the return of the PERL function */ SPAGAIN; /* Ref +resh stack pointer */ PUTBACK; FREETMPS; /* fre +e that return value */ LEAVE; /* ... +and the XPUSHed "mortal" args */ // Destruct PERL interpreter perl_destruct(my_perl); perl_free(my_perl);
  • Comment on Re^2: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
  • Download Code

Replies are listed 'Best First'.
Re^3: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
by Corion (Patriarch) on Jul 08, 2014 at 10:47 UTC

    I think that basically you will have to make my_perl a global variable so that it is available everywhere. You could also pass the information through Perl as an integer (SvIV), but that makes for a horribly brittle design.

      Hi,

      Is there a Perl syntax for getting interpreter pointer?

      Making my_perl global will require a new compilation of the Perl LIB (perl514.lib).


      Thanks,

      Itamar

        Is there a Perl syntax for getting interpreter pointer?

        You can pass it around from XS to C using the pTHX and aTHX (and pTHX_ and pTHX_) macros. Another option is to retrieve the current Perl interpreter calling Perl_get_context(). It is more or less explained on perlguts.

        Also, search CPAN for code containing call_sv and see how others have done it!

        I thought that my_perl was a variable in your C code, but I now realize that your C code gets called from Perl. I don't find a convenient way in perlguts, perlapi or perlcall, but maybe you don't even need special things like passing the Perl interpreter around as the current Perl interpreter already is a global variable.