This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge the XS implementation of B::OP::name and B::OP::desc
[perl5.git] / ext / B / B.xs
index 28ad952..0143487 100644 (file)
@@ -235,13 +235,19 @@ cc_opclassname(pTHX_ const OP *o)
     return (char *)opclassnames[cc_opclass(aTHX_ o)];
 }
 
+/* FIXME - figure out how to get the typemap to assign this to ST(0), rather
+   than creating a new mortal for ST(0) then passing it in as the first
+   argument.  */
 static SV *
 make_sv_object(pTHX_ SV *arg, SV *sv)
 {
     const char *type = 0;
     IV iv;
     dMY_CXT;
-    
+
+    if (!arg)
+       arg = sv_newmortal();
+
     for (iv = 0; iv < sizeof(specialsv_list)/sizeof(SV*); iv++) {
        if (sv == specialsv_list[iv]) {
            type = "B::SPECIAL";
@@ -258,9 +264,10 @@ make_sv_object(pTHX_ SV *arg, SV *sv)
 
 #if PERL_VERSION >= 9
 static SV *
-make_temp_object(pTHX_ SV *arg, SV *temp)
+make_temp_object(pTHX_ SV *temp)
 {
     SV *target;
+    SV *arg = sv_newmortal();
     const char *const type = svclassnames[SvTYPE(temp)];
     const IV iv = PTR2IV(temp);
 
@@ -278,7 +285,7 @@ make_temp_object(pTHX_ SV *arg, SV *temp)
 }
 
 static SV *
-make_warnings_object(pTHX_ SV *arg, STRLEN *warnings)
+make_warnings_object(pTHX_ STRLEN *warnings)
 {
     const char *type = 0;
     dMY_CXT;
@@ -295,35 +302,36 @@ make_warnings_object(pTHX_ SV *arg, STRLEN *warnings)
        }
     }
     if (type) {
+       SV *arg = sv_newmortal();
        sv_setiv(newSVrv(arg, type), iv);
        return arg;
     } else {
        /* B assumes that warnings are a regular SV. Seems easier to keep it
           happy by making them into a regular SV.  */
-       return make_temp_object(aTHX_ arg,
-                               newSVpvn((char *)(warnings + 1), *warnings));
+       return make_temp_object(aTHX_ newSVpvn((char *)(warnings + 1), *warnings));
     }
 }
 
 static SV *
-make_cop_io_object(pTHX_ SV *arg, COP *cop)
+make_cop_io_object(pTHX_ COP *cop)
 {
     SV *const value = newSV(0);
 
     Perl_emulate_cop_io(aTHX_ cop, value);
 
     if(SvOK(value)) {
-       return make_temp_object(aTHX_ arg, newSVsv(value));
+       return make_sv_object(aTHX_ NULL, value);
     } else {
        SvREFCNT_dec(value);
-       return make_sv_object(aTHX_ arg, NULL);
+       return make_sv_object(aTHX_ NULL, NULL);
     }
 }
 #endif
 
 static SV *
-make_mg_object(pTHX_ SV *arg, MAGIC *mg)
+make_mg_object(pTHX_ MAGIC *mg)
 {
+    SV *arg = sv_newmortal();
     sv_setiv(newSVrv(arg, "B::MAGIC"), PTR2IV(mg));
     return arg;
 }
@@ -702,12 +710,12 @@ B_formfeed()
 void
 B_warnhook()
     CODE:
-       ST(0) = make_sv_object(aTHX_ sv_newmortal(), PL_warnhook);
+       ST(0) = make_sv_object(aTHX_ NULL, PL_warnhook);
 
 void
 B_diehook()
     CODE:
-       ST(0) = make_sv_object(aTHX_ sv_newmortal(), PL_diehook);
+       ST(0) = make_sv_object(aTHX_ NULL, PL_diehook);
 
 MODULE = B     PACKAGE = B
 
@@ -781,7 +789,7 @@ hash(sv)
     CODE:
        STRLEN len;
        U32 hash = 0;
-       const char *s = SvPV(sv, len);
+       const char *s = SvPVbyte(sv, len);
        PERL_HASH(hash, s, len);
        ST(0) = sv_2mortal(Perl_newSVpvf(aTHX_ "0x%"UVxf, (UV)hash));
 
@@ -825,17 +833,9 @@ threadsv_names()
 
 #define OP_next(o)     o->op_next
 #define OP_sibling(o)  o->op_sibling
-#define OP_desc(o)     (char *)PL_op_desc[o->op_type]
 #define OP_targ(o)     o->op_targ
-#define OP_type(o)     o->op_type
-#if PERL_VERSION >= 9
-#  define OP_opt(o)    o->op_opt
-#else
-#  define OP_seq(o)    o->op_seq
-#endif
 #define OP_flags(o)    o->op_flags
 #define OP_private(o)  o->op_private
-#define OP_spare(o)    o->op_spare
 
 MODULE = B     PACKAGE = B::OP         PREFIX = OP_
 
@@ -858,49 +858,69 @@ OP_sibling(o)
 char *
 OP_name(o)
        B::OP           o
+    ALIAS:
+       desc = 1
     CODE:
-       RETVAL = (char *)PL_op_name[o->op_type];
+       RETVAL = (char *)(ix ? PL_op_desc : PL_op_name)[o->op_type];
     OUTPUT:
        RETVAL
 
-
 void
 OP_ppaddr(o)
        B::OP           o
     PREINIT:
        int i;
-       SV *sv = sv_newmortal();
+       SV *sv = newSVpvs_flags("PL_ppaddr[OP_", SVs_TEMP);
     CODE:
-       sv_setpvs(sv, "PL_ppaddr[OP_");
        sv_catpv(sv, PL_op_name[o->op_type]);
        for (i=13; (STRLEN)i < SvCUR(sv); ++i)
            SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
        sv_catpvs(sv, "]");
        ST(0) = sv;
 
-char *
-OP_desc(o)
-       B::OP           o
-
 PADOFFSET
 OP_targ(o)
        B::OP           o
 
-U16
-OP_type(o)
-       B::OP           o
-
 #if PERL_VERSION >= 9
-
-U16
-OP_opt(o)
+#  These 3 are all bitfields, so we can't take their addresses.
+UV
+OP_type(o)
        B::OP           o
+    ALIAS:
+       opt = 1
+       spare = 2
+    CODE:
+       switch(ix) {
+         case 1:
+           RETVAL = o->op_opt;
+           break;
+         case 2:
+           RETVAL = o->op_spare;
+           break;
+         default:
+           RETVAL = o->op_type;
+       }
+    OUTPUT:
+       RETVAL
 
 #else
 
-U16
-OP_seq(o)
+UV
+OP_type(o)
        B::OP           o
+    ALIAS:
+       seq = 1
+    CODE:
+       switch(ix) {
+         case 1:
+           RETVAL = o->op_seq;
+           break;
+         default:
+           RETVAL = o->op_type;
+       }
+    OUTPUT:
+       RETVAL
 
 #endif
 
@@ -912,14 +932,6 @@ U8
 OP_private(o)
        B::OP           o
 
-#if PERL_VERSION >= 9
-
-U16
-OP_spare(o)
-       B::OP           o
-
-#endif
-
 void
 OP_oplist(o)
        B::OP           o
@@ -1180,10 +1192,6 @@ LOOP_lastop(o)
 #define COP_arybase(o) CopARYBASE_get(o)
 #define COP_line(o)    CopLINE(o)
 #define COP_hints(o)   CopHINTS_get(o)
-#if PERL_VERSION < 9
-#  define COP_warnings(o)  o->cop_warnings
-#  define COP_io(o)    o->cop_io
-#endif
 
 MODULE = B     PACKAGE = B::COP                PREFIX = COP_
 
@@ -1230,22 +1238,30 @@ U32
 COP_line(o)
        B::COP  o
 
-#if PERL_VERSION >= 9
-
 void
 COP_warnings(o)
        B::COP  o
        PPCODE:
-       ST(0) = make_warnings_object(aTHX_ sv_newmortal(), o->cop_warnings);
+#if PERL_VERSION >= 9
+       ST(0) = make_warnings_object(aTHX_ o->cop_warnings);
+#else
+       ST(0) = make_sv_object(aTHX_ NULL, o->cop_warnings);
+#endif
        XSRETURN(1);
 
 void
 COP_io(o)
        B::COP  o
        PPCODE:
-       ST(0) = make_cop_io_object(aTHX_ sv_newmortal(), o);
+#if PERL_VERSION >= 9
+       ST(0) = make_cop_io_object(aTHX_ o);
+#else
+       ST(0) = make_sv_object(aTHX_ NULL, o->cop_io);
+#endif
        XSRETURN(1);
 
+#if PERL_VERSION >= 9
+
 B::RHE
 COP_hints_hash(o)
        B::COP o
@@ -1254,16 +1270,6 @@ COP_hints_hash(o)
     OUTPUT:
        RETVAL
 
-#else
-
-B::SV
-COP_warnings(o)
-       B::COP  o
-
-B::SV
-COP_io(o)
-       B::COP  o
-
 #endif
 
 U32
@@ -1431,23 +1437,22 @@ void
 SvPV(sv)
        B::PV   sv
     CODE:
-        ST(0) = sv_newmortal();
         if( SvPOK(sv) ) {
+           STRLEN len = SvCUR(sv);
+           const char *p = SvPVX_const(sv);
            /* FIXME - we need a better way for B to identify PVs that are
               in the pads as variable names.  */
-           if((SvLEN(sv) && SvCUR(sv) >= SvLEN(sv))) {
+           if((SvLEN(sv) && len >= SvLEN(sv))) {
                /* It claims to be longer than the space allocated for it -
                   presuambly it's a variable name in the pad  */
-               sv_setpv(ST(0), SvPV_nolen_const(sv));
-           } else {
-               sv_setpvn(ST(0), SvPVX_const(sv), SvCUR(sv));
+               len = strlen(p);
            }
-            SvFLAGS(ST(0)) |= SvUTF8(sv);
+           ST(0) = newSVpvn_flags(p, len, SVs_TEMP | SvUTF8(sv));
         }
         else {
             /* XXX for backward compatibility, but should fail */
             /* croak( "argument is not SvPOK" ); */
-            sv_setpvn(ST(0), NULL, 0);
+            ST(0) = sv_newmortal();
         }
 
 # This used to read 257. I think that that was buggy - should have been 258.
@@ -1457,9 +1462,9 @@ void
 SvPVBM(sv)
        B::PV   sv
     CODE:
-        ST(0) = sv_newmortal();
-       sv_setpvn(ST(0), SvPVX_const(sv),
-           SvCUR(sv) + (SvVALID(sv) ? 256 + PERL_FBM_TABLE_OFFSET : 0));
+        ST(0) = newSVpvn_flags(SvPVX_const(sv),
+           SvCUR(sv) + (SvVALID(sv) ? 256 + PERL_FBM_TABLE_OFFSET : 0),
+           SVs_TEMP);
 
 
 STRLEN
@@ -1478,7 +1483,7 @@ SvMAGIC(sv)
        MAGIC * mg = NO_INIT
     PPCODE:
        for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic)
-           XPUSHs(make_mg_object(aTHX_ sv_newmortal(), mg));
+           XPUSHs(make_mg_object(aTHX_ mg));
 
 MODULE = B     PACKAGE = B::PVMG
 
@@ -1585,15 +1590,15 @@ void
 MgPTR(mg)
        B::MAGIC        mg
     CODE:
-       ST(0) = sv_newmortal();
        if (mg->mg_ptr){
                if (mg->mg_len >= 0){
-                       sv_setpvn(ST(0), mg->mg_ptr, mg->mg_len);
+                       ST(0) = newSVpvn_flags(mg->mg_ptr, mg->mg_len, SVs_TEMP);
                } else if (mg->mg_len == HEf_SVKEY) {
-                       ST(0) = make_sv_object(aTHX_
-                                   sv_newmortal(), (SV*)mg->mg_ptr);
-               }
-       }
+                       ST(0) = make_sv_object(aTHX_ NULL, (SV*)mg->mg_ptr);
+               } else
+                   ST(0) = sv_newmortal();
+       } else
+           ST(0) = sv_newmortal();
 
 MODULE = B     PACKAGE = B::PVLV       PREFIX = Lv
 
@@ -1847,7 +1852,7 @@ AvARRAY(av)
            SV **svp = AvARRAY(av);
            I32 i;
            for (i = 0; i <= AvFILL(av); i++)
-               XPUSHs(make_sv_object(aTHX_ sv_newmortal(), svp[i]));
+               XPUSHs(make_sv_object(aTHX_ NULL, svp[i]));
        }
 
 void
@@ -1856,9 +1861,9 @@ AvARRAYelt(av, idx)
        int     idx
     PPCODE:
        if (idx >= 0 && AvFILL(av) >= 0 && idx <= AvFILL(av))
-           XPUSHs(make_sv_object(aTHX_ sv_newmortal(), (AvARRAY(av)[idx])));
+           XPUSHs(make_sv_object(aTHX_ NULL, (AvARRAY(av)[idx])));
        else
-           XPUSHs(make_sv_object(aTHX_ sv_newmortal(), NULL));
+           XPUSHs(make_sv_object(aTHX_ NULL, NULL));
 
 #if PERL_VERSION < 9
                                   
@@ -1931,9 +1936,9 @@ void
 CvXSUBANY(cv)
        B::CV   cv
     CODE:
-       ST(0) = CvCONST(cv) ?
-           make_sv_object(aTHX_ sv_newmortal(),(SV *)CvXSUBANY(cv).any_ptr) :
-           sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
+       ST(0) = CvCONST(cv)
+           ? make_sv_object(aTHX_ NULL, (SV *)CvXSUBANY(cv).any_ptr)
+           sv_2mortal(newSViv(CvISXSUB(cv) ? CvXSUBANY(cv).any_iv : 0));
 
 MODULE = B    PACKAGE = B::CV
 
@@ -1990,7 +1995,7 @@ HvARRAY(hv)
            EXTEND(sp, HvKEYS(hv) * 2);
            while ((sv = hv_iternextsv(hv, &key, &len))) {
                mPUSHp(key, len);
-               PUSHs(make_sv_object(aTHX_ sv_newmortal(), sv));
+               PUSHs(make_sv_object(aTHX_ NULL, sv));
            }
        }