develooper Front page | perl.perl5.porters | Postings from September 2017

Proposed new API function: sv_rvunweaken

Thread Next
From:
ilmari
Date:
September 1, 2017 10:14
Subject:
Proposed new API function: sv_rvunweaken
Message ID:
d8jmv6ehhfg.fsf@dalvik.ping.uio.no
Hi hackers,

Tom Molesworth (tm604) pointed out on freenode #perl that in-place sort
fails to strengthen weak references, even though it semantically copies
the items.  To fix this, I stole the guts of Scalar::Util::unweaken()
into a new function sv_rvunweaken(), and made the in_place sort code
call that for any weak refs.

Before merging this new API addition, I figured I'd ask for a review.
It's currently on the branch smoke-me/ilmari/sort-weak, and the function
itself is included below for convenience.  I've not added any tests, but
a subsequent commit on the branch (which will be submitted upstrem once
the function is merged) makes Scalar::Util::unweaken() call
sv_rvunweaken() when available, so it'll be tested indirectly via
Scalar::Util's t/weak.t

    /*
    =for apidoc sv_rvunweaken

    Unweaken a reference: Clear the C<SvWEAKREF> flag on this RV; remove the
    backreference to this RV from the array of backreferences associated
    with the target SV, increment the refcount of the target.

    =cut
    */

    SV *
    Perl_sv_rvunweaken(pTHX_ SV *const sv)
    {
        SV *tsv;

        PERL_ARGS_ASSERT_SV_RVUNWEAKEN;

        if (!SvOK(sv)) /* let undefs pass */
            return sv;
        if (!SvROK(sv))
            Perl_croak(aTHX_ "Can't unweaken a nonreference");
        else if (!SvWEAKREF(sv)) {
            Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "Reference is not weak");
            return sv;
        }
        else if (SvREADONLY(sv)) croak_no_modify();

        tsv = SvRV(sv);
        SvWEAKREF_off(sv);
        SvROK_on(sv);
        SvREFCNT_inc_NN(tsv);
        Perl_sv_del_backref(aTHX_ tsv, sv);
        return sv;
    }

Thanks,

- ilmari
-- 
"A disappointingly low fraction of the human race is,
 at any given time, on fire." - Stig Sandbeck Mathisen

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About