Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: xs modifying passed value

by bulk88 (Priest)
on Nov 17, 2012 at 18:02 UTC ( #1004332=note: print w/ replies, xml ) Need Help??


in reply to xs modifying passed value

my $t = $tmp->strToInt(\$passfail);
wrong. do "$passfail" not \$passfail. "\$passfail" is an anonymous scalar that you will never access again. "$passfail" you can access again. You passed a SV RV BTW http://cpansearch.perl.org/src/RURBAN/illguts-0.42/index-14.html#svrv and the outer SV was anonymous as I said.

If you really want to accept in the in params a scalar with a reference, it will be a bit more complicated. The XS parameters processing is done from a file called lib/ExtUtils/typemap in perl.git. That contains what the XSUB interface can automate for you. For more complicated stuff I recommend you directly manipulate the SV*s using sv_set*v().

If you want help, come back. Also use a C debugger with a watch window. printf debugging very slow for a programmer to work with.


Comment on Re: xs modifying passed value
Download Code
Re^2: xs modifying passed value
by Festus Hagen (Novice) on Nov 17, 2012 at 22:22 UTC
    I originally had it as "$passfail" (and just tried again), then reading up led me to passing by reference and led to that.
    I've been hacking/reading on this for a few hours now ... So I've tried a few different angles, Apparently all wrong!

    I'm swimming in all this Sv SV PV Pv Rv RV blah blah ... :(

    Simple is my pleasure, I'll complicate it as my mind expands, hopefully before it explodes!

    Just noticed I forgot the proto for mylib strToInt in my OP, Here it is:
    int strToInt(bool *ok) const;

    I do have stepping capabilities on each side of the fence, Perl and C, Just not across.
    Qt Creator w/dbg on the C/C++ side and of course Perldbg on the Perl side.
    I have many times ran them together to see both sides of the fence, nothing jumps out at me other then the passed value between Perl and XS doesn't get modified, Between XS and my Lib does as shown by the printf(...)'s in the XS.

    I do have many other functions that work fine, though none of them require a passed argument modification and were for the most part simple. (looking back :))

    Shoot, I figured out without asking for help how to do the new() that has variable arguments as well as several different argument types and got it all to work, so far perf!, but this stupid modified passed argument has me stumped. Arrrrgggg!

    Thanks

    -Enjoy
    fh : )_~

      I don't really know C++, that is C++ code. Can you post the function from the .c file that xsubpp generates from your XS syntax func?

      I suggest you use Devel::Peek's Dump function. You can also call it from XS/C as dump.c#l2139 in perl.git as "void sv_dump(SV *sv)" It will need a SV *, I think that ST(1) is the passfail param as a SV *, since ST(0) will be your THIS pointer in a SV * I think from Script land. Using sv_dump with your code unmodified is impossible as written since the setting of SV * that matches passfail is after your CODE: section (again look at the .c file produced), so you would need to
      int MyLib::strToInt(passfailSV) SV* passfailSV PREINIT: bool passfail; CODE: foo(&passfail); SvSetMagicSV(passfailSV, passfail ? &PL_sv_yes : &PL_sv_no); OUTPUT: RETVAL
      I think there is a bug in the typemap,
      T_BOOL $arg = boolSV($var);
      That is from the default typemap. But look at boolSV.
      /* =for apidoc Am|SV *|boolSV|bool b Returns a true SV if C<b> is a true value, or a false SV if C<b> is 0. See also C<PL_sv_yes> and C<PL_sv_no>. =cut */ #define boolSV(b) ((b) ? &PL_sv_yes : &PL_sv_no)
      We assigned a SV * to a SV *, but if this is a in parameter and not an out parameter, you can't just change the SV * to another SV *, you have to change the SV * you got from the caller (return/result's of XSUBs are *usually* brand new SV *s you created and mortalized, then boolSV would work, it won't work to change the inside value of an existing scalar). BTW, you never need to mortal in SV *s you get through the param list (AKA ST(123456) macros), your caller owns the param list SV *s. Also never mortal &PL_sv_yes, &PL_sv_no, &PL_sv_undef, they are process globals/statics.

      I probably should go file a bug report with ExtUtils::ParseXS but I dont have the time.

      update: bug filed
        Well blow my doors off ...

        Bulk88 snaps thy fingers and poof it works!

        Thank you!

        following your lead (pretty near exact cut and paste), Impressive!.

        int MyLib::strToInt(passfailSV) SV* passfailSV PREINIT: bool passfail; CODE: RETVAL = THIS->strToInt(&passfail); SvSetMagicSV(passfailSV, passfail ? &PL_sv_yes : &PL_sv_no); OUTPUT: RETVAL

        I have been so over thinking this ...

        Much Thanks

        -Enjoy
        fh : )_~

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-27 15:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls