Re: [ PATCH ] module test fest
[p5sagit/p5-mst-13.2.git] / ext / B / B.xs
index f18efce..83c9c4a 100644 (file)
@@ -224,7 +224,7 @@ make_mg_object(pTHX_ SV *arg, MAGIC *mg)
 }
 
 static SV *
-cstring(pTHX_ SV *sv)
+cstring(pTHX_ SV *sv, bool perlstyle)
 {
     SV *sstr = newSVpvn("", 0);
     STRLEN len;
@@ -233,6 +233,34 @@ cstring(pTHX_ SV *sv)
 
     if (!SvOK(sv))
        sv_setpvn(sstr, "0", 1);
+    else if (perlstyle && SvUTF8(sv))
+    {
+       SV *tmpsv = sv_newmortal(); /* Temporary SV to feed sv_uni_display */
+       len = SvCUR(sv);
+       s = sv_uni_display(tmpsv, sv, 8*len, UNI_DISPLAY_QQ);
+       sv_setpv(sstr,"\"");
+       while (*s)
+       {
+           if (*s == '"')
+               sv_catpv(sstr, "\\\"");
+           else if (*s == '$')
+               sv_catpv(sstr, "\\$");
+           else if (*s == '@')
+               sv_catpv(sstr, "\\@");
+           else if (*s == '\\')
+           {
+               if (strchr("nrftax\\",*(s+1)))
+                   sv_catpvn(sstr, s++, 2);
+               else
+                   sv_catpv(sstr, "\\\\");
+           }
+           else /* should always be printable */
+               sv_catpvn(sstr, s, 1);
+           ++s;
+       }
+       sv_catpv(sstr, "\"");
+       return sstr;
+    }
     else
     {
        /* XXX Optimise? */
@@ -246,12 +274,20 @@ cstring(pTHX_ SV *sv)
            else if (*s == '\\')
                sv_catpv(sstr, "\\\\");
             /* trigraphs - bleagh */
-            else if (*s == '?' && len>=3 && s[1] == '?')
+            else if (!perlstyle && *s == '?' && len>=3 && s[1] == '?')
             {
                 sprintf(escbuff, "\\%03o", '?');
                 sv_catpv(sstr, escbuff);
             }
-           else if (*s >= ' ' && *s < 127) /* XXX not portable */
+           else if (perlstyle && *s == '$')
+               sv_catpv(sstr, "\\$");
+           else if (perlstyle && *s == '@')
+               sv_catpv(sstr, "\\@");
+#ifdef EBCDIC
+           else if (isPRINT(*s))
+#else
+           else if (*s >= ' ' && *s < 127)
+#endif /* EBCDIC */
                sv_catpvn(sstr, s, 1);
            else if (*s == '\n')
                sv_catpv(sstr, "\\n");
@@ -265,7 +301,7 @@ cstring(pTHX_ SV *sv)
                sv_catpv(sstr, "\\b");
            else if (*s == '\f')
                sv_catpv(sstr, "\\f");
-           else if (*s == '\v')
+           else if (!perlstyle && *s == '\v')
                sv_catpv(sstr, "\\v");
            else
            {
@@ -292,7 +328,11 @@ cchar(pTHX_ SV *sv)
        sv_catpv(sstr, "\\'");
     else if (*s == '\\')
        sv_catpv(sstr, "\\\\");
-    else if (*s >= ' ' && *s < 127) /* XXX not portable */
+#ifdef EBCDIC
+    else if (isPRINT(*s))
+#else
+    else if (*s >= ' ' && *s < 127)
+#endif /* EBCDIC */
        sv_catpvn(sstr, s, 1);
     else if (*s == '\n')
        sv_catpv(sstr, "\\n");
@@ -325,7 +365,7 @@ void
 walkoptree(pTHX_ SV *opsv, char *method)
 {
     dSP;
-    OP *o;
+    OP *o, *kid;
     dMY_CXT;
 
     if (!SvROK(opsv))
@@ -343,13 +383,18 @@ walkoptree(pTHX_ SV *opsv, char *method)
     PUTBACK;
     perl_call_method(method, G_DISCARD);
     if (o && (o->op_flags & OPf_KIDS)) {
-       OP *kid;
        for (kid = ((UNOP*)o)->op_first; kid; kid = kid->op_sibling) {
            /* Use the same opsv. Rely on methods not to mess it up. */
            sv_setiv(newSVrv(opsv, cc_opclassname(aTHX_ kid)), PTR2IV(kid));
            walkoptree(aTHX_ opsv, method);
        }
     }
+    if (o && (cc_opclass(aTHX_ o) == OPc_PMOP)
+           && (kid = cPMOPo->op_pmreplroot))
+    {
+       sv_setiv(newSVrv(opsv, opclassnames[OPc_PMOP]), PTR2IV(kid));
+       walkoptree(aTHX_ opsv, method);
+    }
 }
 
 typedef OP     *B__OP;
@@ -410,6 +455,9 @@ BOOT:
 #define B_sv_undef()   &PL_sv_undef
 #define B_sv_yes()     &PL_sv_yes
 #define B_sv_no()      &PL_sv_no
+#ifdef USE_ITHREADS
+#define B_regex_padav()        PL_regex_padav
+#endif
 
 B::AV
 B_init_av()
@@ -420,6 +468,13 @@ B_begin_av()
 B::AV
 B_end_av()
 
+#ifdef USE_ITHREADS
+
+B::AV
+B_regex_padav()
+
+#endif
+
 B::CV
 B_main_cv()
 
@@ -543,7 +598,15 @@ SV *
 cstring(sv)
        SV *    sv
     CODE:
-       RETVAL = cstring(aTHX_ sv);
+       RETVAL = cstring(aTHX_ sv, 0);
+    OUTPUT:
+       RETVAL
+
+SV *
+perlstring(sv)
+       SV *    sv
+    CODE:
+       RETVAL = cstring(aTHX_ sv, 1);
     OUTPUT:
        RETVAL
 
@@ -605,7 +668,7 @@ OP_ppaddr(o)
     CODE:
        sv_setpvn(sv, "PL_ppaddr[OP_", 13);
        sv_catpv(sv, PL_op_name[o->op_type]);
-       for (i=13; i<SvCUR(sv); ++i)
+       for (i=13; (STRLEN)i < SvCUR(sv); ++i)
            SvPVX(sv)[i] = toUPPER(SvPVX(sv)[i]);
        sv_catpv(sv, "]");
        ST(0) = sv;
@@ -677,8 +740,12 @@ LISTOP_children(o)
 #define PMOP_pmreplstart(o)    o->op_pmreplstart
 #define PMOP_pmnext(o)         o->op_pmnext
 #define PMOP_pmregexp(o)       PM_GETRE(o)
+#ifdef USE_ITHREADS
+#define PMOP_pmoffset(o)       o->op_pmoffset
+#endif
 #define PMOP_pmflags(o)                o->op_pmflags
 #define PMOP_pmpermflags(o)    o->op_pmpermflags
+#define PMOP_pmdynflags(o)      o->op_pmdynflags
 
 MODULE = B     PACKAGE = B::PMOP               PREFIX = PMOP_
 
@@ -691,9 +758,13 @@ PMOP_pmreplroot(o)
        root = o->op_pmreplroot;
        /* OP_PUSHRE stores an SV* instead of an OP* in op_pmreplroot */
        if (o->op_type == OP_PUSHRE) {
+#ifdef USE_ITHREADS
+            sv_setiv(ST(0), INT2PTR(PADOFFSET,root) );
+#else
            sv_setiv(newSVrv(ST(0), root ?
                             svclassnames[SvTYPE((SV*)root)] : "B::SV"),
                     PTR2IV(root));
+#endif
        }
        else {
            sv_setiv(newSVrv(ST(0), cc_opclassname(aTHX_ root)), PTR2IV(root));
@@ -707,14 +778,26 @@ B::PMOP
 PMOP_pmnext(o)
        B::PMOP         o
 
-U16
+#ifdef USE_ITHREADS
+
+IV
+PMOP_pmoffset(o)
+       B::PMOP         o
+
+#endif
+
+U32
 PMOP_pmflags(o)
        B::PMOP         o
 
-U16
+U32
 PMOP_pmpermflags(o)
        B::PMOP         o
 
+U8
+PMOP_pmdynflags(o)
+        B::PMOP         o
+
 void
 PMOP_precomp(o)
        B::PMOP         o
@@ -943,7 +1026,7 @@ SvPV(sv)
        B::PV   sv
     CODE:
         ST(0) = sv_newmortal();
-        if( SvPOK(sv) ) {
+        if( SvPOK(sv) ) { 
             sv_setpvn(ST(0), SvPVX(sv), SvCUR(sv));
             SvFLAGS(ST(0)) |= SvUTF8(sv);
         }
@@ -983,6 +1066,7 @@ SvSTASH(sv)
 #define MgFLAGS(mg) mg->mg_flags
 #define MgOBJ(mg) mg->mg_obj
 #define MgLENGTH(mg) mg->mg_len
+#define MgREGEX(mg) PTR2IV(mg->mg_obj)
 
 MODULE = B     PACKAGE = B::MAGIC      PREFIX = Mg     
 
@@ -1015,6 +1099,19 @@ MgOBJ(mg)
     OUTPUT:
         RETVAL
 
+IV
+MgREGEX(mg)
+       B::MAGIC        mg
+    CODE:
+        if( mg->mg_type == 'r' ) {
+            RETVAL = MgREGEX(mg);
+        }
+        else {
+            croak( "REGEX is only meaningful on r-magic" );
+        }
+    OUTPUT:
+        RETVAL
+
 SV*
 precomp(mg)
         B::MAGIC        mg