Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: xs modifying passed value

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


in reply to Re^2: xs modifying passed value
in thread xs modifying passed value

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


Comment on Re^3: xs modifying passed value
Select or Download Code
Re^4: xs modifying passed value
by Festus Hagen (Novice) on Nov 18, 2012 at 00:11 UTC
    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://1004357]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-09-02 19:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls