View Single Post
  #1  
Old 12-31-2012, 03:30 AM
c0ncrete's Avatar
c0ncrete
Dragon
 
Join Date: Dec 2009
Posts: 719
Default perlxs and hash refs

this seems to work (i get a hashref with the correct data), but since i'm having a hard time making sense of the perlxs documentation and there doesn't seem to be any code in the server source that returns a reference to a hash in this manner (using eval doesn't count!), i was wondering if i've made any grievous errors here that would cause memory leaks or any other undesirable issues.

Code:
XS(XS_Mob_Loc); /* prototype to pass -Wmissing-prototypes */
XS(XS_Mob_Loc)
{
    dXSARGS;
    if (items != 1)
        Perl_croak(aTHX_ "Usage: Mob::Loc(THIS)");
    {
        Mob * THIS;
        dXSTARG;

        if (sv_derived_from(ST(0), "Mob")) {
            IV tmp = SvIV((SV*)SvRV(ST(0)));
            THIS = INT2PTR(Mob *,tmp);
        }
        else
            Perl_croak(aTHX_ "THIS is not of type Mob");
        if(THIS == NULL)
            Perl_croak(aTHX_ "THIS is NULL, avoiding crash.");

        HV * loc_hash = (HV *)sv_2mortal((SV *)newHV());

        hv_store(loc_hash, "x", 1, newSVnv(THIS->GetX()), 0);
        hv_store(loc_hash, "y", 1, newSVnv(THIS->GetY()), 0);
        hv_store(loc_hash, "z", 1, newSVnv(THIS->GetZ()), 0);
        hv_store(loc_hash, "h", 1, newSVnv(THIS->GetHeading()), 0);

        ST(0) = newRV_noinc((SV *)loc_hash);
    }
    XSRETURN(1);
}
Reply With Quote