const char *hvname;
I32 items;
+ PERL_ARGS_ASSERT_ISA_LOOKUP;
+
/* A stash/class can go by many names (ie. User == main::User), so
we compare the stash itself just in case */
if (name_stash && ((const HV *)stash == name_stash))
*/
bool
-Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
+Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
{
dVAR;
HV *stash;
+ PERL_ARGS_ASSERT_SV_DERIVED_FROM;
+
SvGETMAGIC(sv);
if (SvROK(sv)) {
#include "XSUB.h"
bool
-Perl_sv_does(pTHX_ SV *sv, const char *name)
+Perl_sv_does(pTHX_ SV *sv, const char *const name)
{
const char *classname;
bool does_it;
SV *methodname;
-
dSP;
+
+ PERL_ARGS_ASSERT_SV_DOES;
+
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv);
- XPUSHs(sv_2mortal(newSVpv(name, 0)));
+ mXPUSHs(newSVpv(name, 0));
PUTBACK;
- methodname = sv_2mortal(newSVpv("isa", 0));
+ methodname = newSVpvs_flags("isa", SVs_TEMP);
/* ugly hack: use the SvSCREAM flag so S_method_common
* can figure out we're calling DOES() and not isa(),
* and report eventual errors correctly. --rgs */
XS(XS_re_regname);
XS(XS_re_regnames);
XS(XS_re_regnames_count);
+XS(XS_re_regexp_pattern);
XS(XS_Tie_Hash_NamedCapture_FETCH);
XS(XS_Tie_Hash_NamedCapture_STORE);
XS(XS_Tie_Hash_NamedCapture_DELETE);
newXSproto("re::regname", XS_re_regname, file, ";$$");
newXSproto("re::regnames", XS_re_regnames, file, ";$");
newXSproto("re::regnames_count", XS_re_regnames_count, file, "");
+ newXSproto("re::regexp_pattern", XS_re_regexp_pattern, file, "$");
newXS("Tie::Hash::NamedCapture::FETCH", XS_Tie_Hash_NamedCapture_FETCH, file);
newXS("Tie::Hash::NamedCapture::STORE", XS_Tie_Hash_NamedCapture_STORE, file);
newXS("Tie::Hash::NamedCapture::DELETE", XS_Tie_Hash_NamedCapture_DELETE, file);
newXS("Tie::Hash::NamedCapture::flags", XS_Tie_Hash_NamedCapture_flags, file);
}
+/*
+=for apidoc croak_xs_usage
+
+A specialised variant of C<croak()> for emitting the usage message for xsubs
+
+ croak_xs_usage(cv, "eee_yow");
+
+works out the package name and subroutine name from C<cv>, and then calls
+C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
+
+ Perl_croak(aTHX_ "Usage %s::%s(%s)", "ouch" "awk", "eee_yow");
+
+=cut
+*/
+
+void
+Perl_croak_xs_usage(pTHX_ const CV *const cv, const char *const params)
+{
+ const GV *const gv = CvGV(cv);
+
+ PERL_ARGS_ASSERT_CROAK_XS_USAGE;
+
+ if (gv) {
+ const char *const gvname = GvNAME(gv);
+ const HV *const stash = GvSTASH(gv);
+ const char *const hvname = stash ? HvNAME_get(stash) : NULL;
+
+ if (hvname)
+ Perl_croak(aTHX_ "Usage: %s::%s(%s)", hvname, gvname, params);
+ else
+ Perl_croak(aTHX_ "Usage: %s(%s)", gvname, params);
+ } else {
+ /* Pants. I don't think that it should be possible to get here. */
+ Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
+ }
+}
XS(XS_UNIVERSAL_isa)
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
+ croak_xs_usage(cv, "reference, kind");
else {
SV * const sv = ST(0);
const char *name;
const char *name;
SV *rv;
HV *pkg = NULL;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
+ croak_xs_usage(cv, "object-ref, method");
sv = ST(0);
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items > 3)
- Perl_croak(aTHX_ "Usage: version::new(class, version)");
+ croak_xs_usage(cv, "class, version");
SP -= items;
{
SV *vs = ST(1);
if ( strcmp(classname,"version") != 0 ) /* inherited new() */
sv_bless(rv, gv_stashpv(classname, GV_ADD));
- PUSHs(sv_2mortal(rv));
+ mPUSHs(rv);
PUTBACK;
return;
}
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::stringify(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
SP -= items;
{
SV * lobj;
else
Perl_croak(aTHX_ "lobj is not of type version");
- PUSHs(sv_2mortal(vstringify(lobj)));
+ mPUSHs(vstringify(lobj));
PUTBACK;
return;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::numify(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
SP -= items;
{
SV * lobj;
else
Perl_croak(aTHX_ "lobj is not of type version");
- PUSHs(sv_2mortal(vnumify(lobj)));
+ mPUSHs(vnumify(lobj));
PUTBACK;
return;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::normal(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
SP -= items;
{
SV * lobj;
else
Perl_croak(aTHX_ "lobj is not of type version");
- PUSHs(sv_2mortal(vnormal(lobj)));
+ mPUSHs(vnormal(lobj));
PUTBACK;
return;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::vcmp(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
SP -= items;
{
SV * lobj;
rs = newSViv(vcmp(lobj,rvs));
}
- PUSHs(sv_2mortal(rs));
+ mPUSHs(rs);
}
PUTBACK;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::boolean(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
SP -= items;
if (sv_derived_from(ST(0), "version")) {
SV * const lobj = SvRV(ST(0));
SV * const rs = newSViv( vcmp(lobj,new_version(newSVpvs("0"))) );
- PUSHs(sv_2mortal(rs));
+ mPUSHs(rs);
PUTBACK;
return;
}
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1)
- Perl_croak(aTHX_ "Usage: version::noop(lobj, ...)");
+ croak_xs_usage(cv, "lobj, ...");
if (sv_derived_from(ST(0), "version"))
Perl_croak(aTHX_ "operation not supported with version object");
else
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: version::is_alpha(lobj)");
+ croak_xs_usage(cv, "lobj");
SP -= items;
if (sv_derived_from(ST(0), "version")) {
SV * const lobj = ST(0);
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: version::qv(ver)");
+ croak_xs_usage(cv, "ver");
SP -= items;
{
SV * ver = ST(0);
}
else
{
- PUSHs(sv_2mortal(new_version(ver)));
+ mPUSHs(new_version(ver));
}
PUTBACK;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: utf8::is_utf8(sv)");
+ croak_xs_usage(cv, "sv");
else {
const SV * const sv = ST(0);
if (SvUTF8(sv))
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: utf8::valid(sv)");
+ croak_xs_usage(cv, "sv");
else {
SV * const sv = ST(0);
STRLEN len;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: utf8::encode(sv)");
+ croak_xs_usage(cv, "sv");
sv_utf8_encode(ST(0));
XSRETURN_EMPTY;
}
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: utf8::decode(sv)");
+ croak_xs_usage(cv, "sv");
else {
SV * const sv = ST(0);
const bool RETVAL = sv_utf8_decode(sv);
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: utf8::upgrade(sv)");
+ croak_xs_usage(cv, "sv");
else {
SV * const sv = ST(0);
STRLEN RETVAL;
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1 || items > 2)
- Perl_croak(aTHX_ "Usage: utf8::downgrade(sv, failok=0)");
+ croak_xs_usage(cv, "sv, failok=0");
else {
SV * const sv = ST(0);
const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1));
dVAR;
dXSARGS;
const UV uv = SvUV(ST(0));
- PERL_UNUSED_ARG(cv);
if (items > 1)
- Perl_croak(aTHX_ "Usage: utf8::native_to_unicode(sv)");
+ croak_xs_usage(cv, "sv");
ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
XSRETURN(1);
dVAR;
dXSARGS;
const UV uv = SvUV(ST(0));
- PERL_UNUSED_ARG(cv);
if (items > 1)
- Perl_croak(aTHX_ "Usage: utf8::unicode_to_native(sv)");
+ croak_xs_usage(cv, "sv");
ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
XSRETURN(1);
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: UNIVERSAL::hv_clear_placeholders(hv)");
+ croak_xs_usage(cv, "hv");
else {
HV * const hv = (HV *) SvRV(ST(0));
hv_clear_placeholders(hv);
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items < 1 || items % 2 == 0)
- Perl_croak(aTHX_ "Usage: PerlIO_get_layers(filehandle[,args])");
+ croak_xs_usage(cv, "filehandle[,args]");
#ifdef USE_PERLIO
{
SV * sv;
}
if (gv && (io = GvIO(gv))) {
- dTARGET;
AV* const av = PerlIO_get_layers(aTHX_ input ?
IoIFP(io) : IoOFP(io));
I32 i;
const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
if (details) {
+ /* Indents of 5? Yuck. */
+ /* We know that PerlIO_get_layers creates a new SV for
+ the name and flags, so we can just take a reference
+ and "steal" it when we free the AV below. */
XPUSHs(namok
- ? newSVpvn(SvPVX_const(*namsvp), SvCUR(*namsvp))
+ ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
: &PL_sv_undef);
XPUSHs(argok
- ? newSVpvn(SvPVX_const(*argsvp), SvCUR(*argsvp))
+ ? newSVpvn_flags(SvPVX_const(*argsvp),
+ SvCUR(*argsvp),
+ (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
+ | SVs_TEMP)
+ : &PL_sv_undef);
+ XPUSHs(namok
+ ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
: &PL_sv_undef);
- if (flgok)
- XPUSHi(SvIVX(*flgsvp));
- else
- XPUSHs(&PL_sv_undef);
nitem += 3;
}
else {
if (namok && argok)
- XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
+ XPUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
SVfARG(*namsvp),
- SVfARG(*argsvp)));
+ SVfARG(*argsvp))));
else if (namok)
- XPUSHs(Perl_newSVpvf(aTHX_ "%"SVf,
- SVfARG(*namsvp)));
+ XPUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
else
XPUSHs(&PL_sv_undef);
nitem++;
const IV flags = SvIVX(*flgsvp);
if (flags & PERLIO_F_UTF8) {
- XPUSHs(newSVpvs("utf8"));
+ XPUSHs(newSVpvs_flags("utf8", SVs_TEMP));
nitem++;
}
}
PERL_UNUSED_VAR(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: %s(%s)", "re::is_regexp", "sv");
+ croak_xs_usage(cv, "sv");
SP -= items;
SV * ret;
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 0)
- Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames_count", "");
+ croak_xs_usage(cv, "");
SP -= items;
SPAGAIN;
if (ret) {
- XPUSHs(ret);
+ mXPUSHs(ret);
PUTBACK;
return;
} else {
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items < 1 || items > 2)
- Perl_croak(aTHX_ "Usage: %s(%s)", "re::regname", "name[, all ]");
+ croak_xs_usage(cv, "name[, all ]");
SP -= items;
XSRETURN_UNDEF;
if (items == 2 && SvTRUE(ST(1))) {
- flags = RXf_HASH_ALL;
+ flags = RXapif_ALL;
} else {
- flags = RXf_HASH_ONE;
+ flags = RXapif_ONE;
}
- ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXf_HASH_REGNAME));
+ ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
if (ret) {
- if (SvROK(ret))
- XPUSHs(ret);
- else
- XPUSHs(SvREFCNT_inc(ret));
+ mXPUSHs(ret);
XSRETURN(1);
}
XSRETURN_UNDEF;
I32 length;
I32 i;
SV **entry;
- PERL_UNUSED_ARG(cv);
if (items > 1)
- Perl_croak(aTHX_ "Usage: %s(%s)", "re::regnames", "[all]");
+ croak_xs_usage(cv, "[all]");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
XSRETURN_UNDEF;
if (items == 1 && SvTRUE(ST(0))) {
- flags = RXf_HASH_ALL;
+ flags = RXapif_ALL;
} else {
- flags = RXf_HASH_ONE;
+ flags = RXapif_ONE;
}
SP -= items;
- ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXf_HASH_REGNAMES));
+ ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
SPAGAIN;
if (!entry)
Perl_croak(aTHX_ "NULL array element in re::regnames()");
- XPUSHs(*entry);
+ mXPUSHs(SvREFCNT_inc_simple_NN(*entry));
}
+
+ SvREFCNT_dec(ret);
+
PUTBACK;
return;
}
+XS(XS_re_regexp_pattern)
+{
+ dVAR;
+ dXSARGS;
+ REGEXP *re;
+
+ if (items != 1)
+ croak_xs_usage(cv, "sv");
+
+ SP -= items;
+
+ /*
+ Checks if a reference is a regex or not. If the parameter is
+ not a ref, or is not the result of a qr// then returns false
+ in scalar context and an empty list in list context.
+ Otherwise in list context it returns the pattern and the
+ modifiers, in scalar context it returns the pattern just as it
+ would if the qr// was stringified normally, regardless as
+ to the class of the variable and any strigification overloads
+ on the object.
+ */
+
+ if ((re = SvRX(ST(0)))) /* assign deliberate */
+ {
+ /* Housten, we have a regex! */
+ SV *pattern;
+ STRLEN left = 0;
+ char reflags[6];
+
+ if ( GIMME_V == G_ARRAY ) {
+ /*
+ we are in list context so stringify
+ the modifiers that apply. We ignore "negative
+ modifiers" in this scenario.
+ */
+
+ const char *fptr = INT_PAT_MODS;
+ char ch;
+ U16 match_flags = (U16)((RX_EXTFLAGS(re) & PMf_COMPILETIME)
+ >> RXf_PMf_STD_PMMOD_SHIFT);
+
+ while((ch = *fptr++)) {
+ if(match_flags & 1) {
+ reflags[left++] = ch;
+ }
+ match_flags >>= 1;
+ }
+
+ pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
+ (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
+
+ /* return the pattern and the modifiers */
+ XPUSHs(pattern);
+ XPUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
+ XSRETURN(2);
+ } else {
+ /* Scalar, so use the string that Perl would return */
+ /* return the pattern in (?msix:..) format */
+#if PERL_VERSION >= 11
+ pattern = sv_2mortal(newSVsv((SV*)re));
+#else
+ pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
+ (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
+#endif
+ XPUSHs(pattern);
+ XSRETURN(1);
+ }
+ } else {
+ /* It ain't a regexp folks */
+ if ( GIMME_V == G_ARRAY ) {
+ /* return the empty list */
+ XSRETURN_UNDEF;
+ } else {
+ /* Because of the (?:..) wrapping involved in a
+ stringified pattern it is impossible to get a
+ result for a real regexp that would evaluate to
+ false. Therefore we can return PL_sv_no to signify
+ that the object is not a regex, this means that one
+ can say
+
+ if (regex($might_be_a_regex) eq '(?:foo)') { }
+
+ and not worry about undefined values.
+ */
+ XSRETURN_NO;
+ }
+ }
+ /* NOT-REACHED */
+}
+
XS(XS_Tie_Hash_NamedCapture_FETCH)
{
dVAR;
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $flags)");
+ croak_xs_usage(cv, "$key, $flags");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
SPAGAIN;
if (ret) {
- if (SvROK(ret))
- XPUSHs(ret);
- else
- XPUSHs(SvREFCNT_inc(ret));
+ mXPUSHs(ret);
PUTBACK;
return;
}
dXSARGS;
REGEXP * rx;
U32 flags;
- PERL_UNUSED_ARG(cv);
if (items != 3)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::STORE($key, $value, $flags)");
+ croak_xs_usage(cv, "$key, $value, $flags");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
dXSARGS;
REGEXP * rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
U32 flags;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::DELETE($key, $flags)");
+ croak_xs_usage(cv, "$key, $flags");
if (!rx)
Perl_croak(aTHX_ PL_no_modify);
dXSARGS;
REGEXP * rx;
U32 flags;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::CLEAR($flags)");
+ croak_xs_usage(cv, "$flags");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::EXISTS($key, $flags)");
+ croak_xs_usage(cv, "$key, $flags");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::FIRSTKEY()");
+ croak_xs_usage(cv, "");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
SPAGAIN;
if (ret) {
- XPUSHs(SvREFCNT_inc(ret));
+ mXPUSHs(ret);
PUTBACK;
} else {
XSRETURN_UNDEF;
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items != 2)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::NEXTKEY($lastkey)");
+ croak_xs_usage(cv, "$lastkey");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
SPAGAIN;
if (ret) {
- XPUSHs(ret);
+ mXPUSHs(ret);
} else {
XSRETURN_UNDEF;
}
REGEXP * rx;
U32 flags;
SV * ret;
- PERL_UNUSED_ARG(cv);
if (items != 1)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::SCALAR()");
+ croak_xs_usage(cv, "");
rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
SPAGAIN;
if (ret) {
- XPUSHs(ret);
+ mXPUSHs(ret);
PUTBACK;
return;
} else {
{
dVAR;
dXSARGS;
- PERL_UNUSED_ARG(cv);
if (items != 0)
- Perl_croak(aTHX_ "Usage: Tie::Hash::NamedCapture::flags()");
+ croak_xs_usage(cv, "");
- XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ONE)));
- XPUSHs(sv_2mortal(newSVuv(RXf_HASH_ALL)));
+ mXPUSHu(RXapif_ONE);
+ mXPUSHu(RXapif_ALL);
PUTBACK;
return;
}