Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

XS: returning a 64-bit unsigned int?

by BrowserUk (Patriarch)
on Sep 27, 2011 at 04:55 UTC ( [id://928011]=perlquestion: print w/replies, xml ) Need Help??

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

What do I need to do in order that the Perl code will see the value set into the SV as unsigned?

#! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'modSV', CLEAN_AFTER_BUILD => 0; void modSV( SV *ref ) { SV *sv = SvRV( ref ); unsigned __int64 uv = 9223372036854775809ull; SvUV_set( sv, uv ); SvIOK_only_UV( sv ); return; } END_C use Devel::Peek; my $sv = 123; Dump $sv; print modSV( \$sv ); Dump $sv; __END__ SV = IV(0x2c7490) at 0x2c7498 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 123 SV = IV(0x2c7490) at 0x2c7498 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = -9223372036854775807

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re: XS: returning a 64-bit unsigned int?
by syphilis (Archbishop) on Sep 27, 2011 at 06:40 UTC
    This works as desired for me:
    void modSV( SV *ref ) { SV *sv = SvRV( ref ); unsigned __int64 uv = 9223372036854775809ull; sv_setuv(sv, uv); }
    Outputs:
    SV = IV(0x381af90) at 0x381afa0 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK) IV = 123 SV = IV(0x381af90) at 0x381afa0 REFCNT = 1 FLAGS = (PADMY,IOK,pIOK,IsUV) UV = 9223372036854775809
    Haven't yet investigated why/where your approach falls down.

    Cheers,
    Rob

      Perfect, thank you.

      ( Why on earth is that documented under SV-Body-Allocation and not SV-Manipulation-Functions? )


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        sv_setuv_mg(sv, uv); is actually what you want unless you follow the sv_setuv(sv, uv); with SvSETMAGIC(sv);.

Re: XS: returning a 64-bit unsigned int?
by ikegami (Patriarch) on Sep 27, 2011 at 07:13 UTC

    Which one of these is not like the other...

    #define SvNOK_only(sv) \ (SvOK_off(sv), SvFLAGS(sv) |= (SVf_NOK|SVp_NOK)) #define SvIOK_only(sv) \ (SvOK_off(sv), SvFLAGS(sv) |= (SVf_IOK|SVp_IOK)) #define SvIOK_only_UV(sv) \ (assert_not_glob(sv) \ SvOK_off_exc_UV(sv), SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))

      I can see the difference, but what it means I have no idea?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        The UV version turns on IOK and *preserves* IsUV instead of turning on IOK and IsUV.

        The other two don't preserve anything.

        I don't know why the difference.

        There is a SvIsUV_on(sv) that would have done the trick. Keep in mind that your original approach didn't converting the scalar to one that supports IVs (if possible) and you didn't handle magic. If it's just an internal scalar, you could get away with not doing those, though.

Re: XS: returning a 64-bit unsigned int?
by Anonymous Monk on Sep 27, 2011 at 06:49 UTC

    Petition p5p?

    It seems you can't get an IV no matter what you do

    SV* bob() PREINIT: SV* temp; //~ unsigned int uncle = 42; unsigned long long uncle = 42; CODE: //~ temp = newSVuv( 42 ); temp = newSVuv( uncle ); SvIOK_only_UV( temp ); warn("we got em %p SvIOK_UV(%b)SvUVX(%u)", temp, SvIOK_UV(temp +), SvUVX(temp) ); RETVAL = temp; OUTPUT: RETVAL

    Always gives

    we got em 3f8d14 SvIOK_UV(0)SvUVX(42) at -e line 1. SV = IV(0x3f8d10) at 0x3f8d14 REFCNT = 1 FLAGS = (TEMP,IOK,pIOK) IV = 42

      Seems you can, provided you look in the wrong part of the manual for the api. :(


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Right, but shouldn't newSVuv make a UV and not an IV? Talk about false advertising :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-03-28 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found