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

Proposed new API function: sv_rvunweaken

Thread Next
September 1, 2017 10:14
Proposed new API function: sv_rvunweaken
Message ID:
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.


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


        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);
        Perl_sv_del_backref(aTHX_ tsv, sv);
        return sv;


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

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About