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

perlcall seems to suggest simply using call_sv(). I don't know where exactly you encounter problems, so maybe showing a bit of the relevant code maybe helps.

  • Comment on Re: 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^2: Perl calls C calls Perl CallBack: How Perl callback use the same interpreter/context as Perl caller?
by itamarat (Acolyte) on Jul 08, 2014 at 10:45 UTC

    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);

      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