where is my magic (XS related)

Discussion in 'Perl Misc' started by Pavel Hlavnicka, Mar 1, 2005.

  1. Hi all,

    Abstract: If I assign some magic to a value returned from FIRSTKEY (tie
    method) this magic is not present as this value is passed to NEXTKEY.

    My XS provides functions for hash 'tie' magic (TIEHASH, FETCH...), all
    went fine until I started with FIRSTKEY and NEXTKEY. My extension works
    with a complex C++ library and for a hash iteration I need to keep some
    library specific iteration key. My idea was to use `~` or `U` magic and
    associate it with a key returned from FIRSTKEY and use it later in
    NEXTKEY. Like this:

    SV *
    FIRSTKEY(obj)
    SV * obj
    PREINIT:
    AG_SLOT_ENTRY entry;
    char * name;
    CODE:
    // -------- following really does not matter -------
    frame = SV2FRAME(obj);
    entry = frame -> GetStartSlot(); //C++ API I use
    if (entry) {
    name = frame -> getName(entry);
    RETVAL = newSVpv((const char*)name, 0);
    // ------- here I assign some magic to RETVAL --------
    sv_magic(RETVAL, sv_2mortal(newSVuv(PTR2UV(entry))),
    PERL_MAGIC_ext, NULL, 0);
    } else {
    RETVAL = &PL_sv_undef;
    }
    OUTPUT:
    RETVAL


    SV*
    NEXTKEY(obj, last)
    SV * obj;
    SV * last;
    PREINIT:
    AG_SLOT_ENTRY entry;
    char * name;
    CODE:
    frame = SV2FRAME(obj);
    MAGIC * magic = mg_find(last, PERL_MAGIC_ext);
    // --------- OOPS magic is always null ------------


    What's really confusing is that SvTYPE(last) still claims some magic is
    present, but there is no magic assigned to this SV at all
    (SvMAGIC(last) is NULL)

    Using Perl 5.8.5, Fedora 3 vanilla.

    Any answer to this long message would be really appreciated!!
    Thx

    Pavel

    P.S.:
    I have a XS wrapper round C++ library, my XS is compiled with g++ as
    described e.g. here <http://www.johnkeiser.com/perl-xs-c++.html>
    Actually I do not suppose this affects my problem.
     
    Pavel Hlavnicka, Mar 1, 2005
    #1
    1. Advertising

  2. Pavel Hlavnicka wrote:
    > Abstract: If I assign some magic to a value returned from FIRSTKEY (tie
    > method) this magic is not present as this value is passed to NEXTKEY.


    Abstract: AFAIK, magic applies to lvalues not to rvalues. When the the
    lvalue is resolved to and rvalue, such as when value of the magic SV is
    copied to another SV it looses it's magic.

    I don't know what would happen if you try to return an value with magic
    from a function that's expected to return an rvalue. The behaviour is
    probably undefined.

    If you want something like magic but which is preserved as the value is
    copied from SV to SV you need to go for an object with overload. I've
    never done this from XS.
     
    Brian McCauley, Mar 1, 2005
    #2
    1. Advertising

  3. Pavel Hlavnicka

    Joe Schaefer Guest

    Pavel Hlavnicka <> writes:

    > Abstract: If I assign some magic to a value returned from FIRSTKEY
    > (tie method) this magic is not present as this value is passed to
    > NEXTKEY.
    >
    > My XS provides functions for hash 'tie' magic (TIEHASH, FETCH...), all
    > went fine until I started with FIRSTKEY and NEXTKEY. My extension
    > works with a complex C++ library and for a hash iteration I need to
    > keep some library specific iteration key. My idea was to use `~` or
    > `U` magic and associate it with a key returned from FIRSTKEY and use
    > it later in NEXTKEY. Like this:


    Try adding it to the object instead of the key. MAGIC isn't
    preserved across an assignment, which is what perl must do here:
    it makes an internal copy of FIRSTKEY's (mortal) RETVAL, to
    eventually pass to NEXTKEY as "last".

    --
    Joe Schaefer
     
    Joe Schaefer, Mar 1, 2005
    #3
  4. Thanks,

    you're right it seems. Actually meanwhile I've noticed formerly
    overlooked fact that perl allows only single concurrent iteration of the
    hash so storing my internal key with an object (not with key) is
    sufficient (or even conforming if you want).

    What's strange (but understandable) is that my RETVAL with magic is
    copied with its type (SvMAGIC returns true) but no magic is retained.
    Keeping SV upgraded to PLMG seems to be obsolete. But it really doesn't
    hurt much - it was just a bit confusing.

    Thank you for your help, I really appreciate it.

    Pavel

    >
    > Try adding it to the object instead of the key. MAGIC isn't
    > preserved across an assignment, which is what perl must do here:
    > it makes an internal copy of FIRSTKEY's (mortal) RETVAL, to
    > eventually pass to NEXTKEY as "last".
    >
     
    Pavel Hlavnicka, Mar 2, 2005
    #4
  5. Pavel Hlavnicka

    Joe Schaefer Guest

    Pavel Hlavnicka <> writes:

    > What's strange (but understandable) is that my RETVAL with magic is
    > copied with its type (SvMAGIC returns true) but no magic is retained.

    ^^^^^^^

    Do you mean SvMAGICAL(sv), or are you referring to SvTYPE(sv, SVt_PVMG)?
    SvMAGICAL probably shouldn't be true, but SvTYPE certainly should be.

    > Keeping SV upgraded to PLMG seems to be obsolete.


    Some internal magics are preserved (taint, v-string), but that's not
    really the point. SvTYPE just tells how much room is available in
    the sv_any slot (which is tiered via SvUPGRADE). It doesn't say
    anything about the sv's actual semantics.

    --
    Joe Schaefer
     
    Joe Schaefer, Mar 2, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nilsson Mats

    Wrapper on magic line?

    Nilsson Mats, Dec 9, 2003, in forum: Perl
    Replies:
    0
    Views:
    624
    Nilsson Mats
    Dec 9, 2003
  2. Martin

    Application name magic

    Martin, Jan 28, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    318
    Martin
    Jan 28, 2004
  3. =?Utf-8?B?U2hhd24=?=

    Magic Trick Datagrid Delete Row

    =?Utf-8?B?U2hhd24=?=, Apr 6, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,465
    =?Utf-8?B?U2hhd24=?=
    Apr 6, 2005
  4. Maxwell Hammer
    Replies:
    7
    Views:
    650
    Peter Hansen
    Jun 18, 2005
  5. Giles Bowkett
    Replies:
    9
    Views:
    414
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page