X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/2c6c1df5c2ddebe97be50ffbfbe1f5a2cf113eb1..80dd201b1d988a0f948dec8b25f624b4007aca59:/universal.c?ds=sidebyside diff --git a/universal.c b/universal.c index b217c14..906f74c 100644 --- a/universal.c +++ b/universal.c @@ -29,7 +29,7 @@ #define PERL_IN_UNIVERSAL_C #include "perl.h" -#ifdef USE_PERLIO +#if defined(USE_PERLIO) #include "perliol.h" /* For the PERLIO_F_XXX */ #endif @@ -41,7 +41,6 @@ STATIC bool S_isa_lookup(pTHX_ HV *stash, const char * const name, STRLEN len, U32 flags) { - dVAR; const struct mro_meta *const meta = HvMROMETA(stash); HV *isa = meta->isa; const HV *our_stash; @@ -67,7 +66,7 @@ S_isa_lookup(pTHX_ HV *stash, const char * const name, STRLEN len, U32 flags) if (our_stash) { HEK *canon_name = HvENAME_HEK(our_stash); if (!canon_name) canon_name = HvNAME_HEK(our_stash); - + assert(canon_name); if (hv_common(isa, NULL, HEK_KEY(canon_name), HEK_LEN(canon_name), HEK_FLAGS(canon_name), HV_FETCH_ISEXISTS, NULL, HEK_HASH(canon_name))) { @@ -147,7 +146,6 @@ Perl_sv_derived_from_pv(pTHX_ SV *sv, const char *const name, U32 flags) bool Perl_sv_derived_from_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags) { - dVAR; HV *stash; PERL_ARGS_ASSERT_SV_DERIVED_FROM_PVN; @@ -294,7 +292,7 @@ A specialised variant of C for emitting the usage message for xsubs croak_xs_usage(cv, "eee_yow"); works out the package name and subroutine name from C, and then calls -C. Hence if C is C<&ouch::awk>, it would call C as: +C. Hence if C is C<&ouch::awk>, it would call C as: Perl_croak(aTHX_ "Usage: %"SVf"::%"SVf"(%s)", "ouch" "awk", "eee_yow"); @@ -304,11 +302,12 @@ C. Hence if C is C<&ouch::awk>, it would call C as: void Perl_croak_xs_usage(const CV *const cv, const char *const params) { - const GV *const gv = CvGV(cv); + /* Avoid CvGV as it requires aTHX. */ + const GV *gv = CvNAMED(cv) ? NULL : cv->sv_any->xcv_gv_u.xcv_gv; PERL_ARGS_ASSERT_CROAK_XS_USAGE; - if (gv) { + if (gv) got_gv: { const HV *const stash = GvSTASH(gv); if (HvNAME_get(stash)) @@ -322,15 +321,18 @@ Perl_croak_xs_usage(const CV *const cv, const char *const params) Perl_croak_nocontext("Usage: %"HEKf"(%s)", HEKfARG(GvNAME_HEK(gv)), params); } else { + dTHX; + if ((gv = CvGV(cv))) goto got_gv; + /* Pants. I don't think that it should be possible to get here. */ /* diag_listed_as: SKIPME */ - Perl_croak_nocontext("Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params); + Perl_croak(aTHX_ "Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params); } } +XS(XS_UNIVERSAL_isa); /* prototype to pass -Wmissing-prototypes */ XS(XS_UNIVERSAL_isa) { - dVAR; dXSARGS; if (items != 2) @@ -348,9 +350,9 @@ XS(XS_UNIVERSAL_isa) } } +XS(XS_UNIVERSAL_can); /* prototype to pass -Wmissing-prototypes */ XS(XS_UNIVERSAL_can) { - dVAR; dXSARGS; SV *sv; SV *rv; @@ -386,7 +388,7 @@ XS(XS_UNIVERSAL_can) else { pkg = gv_stashsv(sv, 0); if (!pkg) - pkg = gv_stashpv("UNIVERSAL", 0); + pkg = gv_stashpvs("UNIVERSAL", 0); } if (pkg) { @@ -399,9 +401,9 @@ XS(XS_UNIVERSAL_can) XSRETURN(1); } +XS(XS_UNIVERSAL_DOES); /* prototype to pass -Wmissing-prototypes */ XS(XS_UNIVERSAL_DOES) { - dVAR; dXSARGS; PERL_UNUSED_ARG(cv); @@ -416,385 +418,9 @@ XS(XS_UNIVERSAL_DOES) } } -XS(XS_UNIVERSAL_VERSION) -{ - dVAR; - dXSARGS; - HV *pkg; - GV **gvp; - GV *gv; - SV *sv; - const char *undef; - PERL_UNUSED_ARG(cv); - - if (SvROK(ST(0))) { - sv = MUTABLE_SV(SvRV(ST(0))); - if (!SvOBJECT(sv)) - Perl_croak(aTHX_ "Cannot find version of an unblessed reference"); - pkg = SvSTASH(sv); - } - else { - pkg = gv_stashsv(ST(0), 0); - } - - gvp = pkg ? (GV**)hv_fetchs(pkg, "VERSION", FALSE) : NULL; - - if (gvp && isGV(gv = *gvp) && (sv = GvSV(gv)) && SvOK(sv)) { - SV * const nsv = sv_newmortal(); - sv_setsv(nsv, sv); - sv = nsv; - if ( !sv_isobject(sv) || !sv_derived_from(sv, "version")) - upg_version(sv, FALSE); - - undef = NULL; - } - else { - sv = &PL_sv_undef; - undef = "(undef)"; - } - - if (items > 1) { - SV *req = ST(1); - - if (undef) { - if (pkg) { - const HEK * const name = HvNAME_HEK(pkg); - Perl_croak(aTHX_ - "%"HEKf" does not define $%"HEKf - "::VERSION--version check failed", - HEKfARG(name), HEKfARG(name)); - } else { - Perl_croak(aTHX_ - "%"SVf" defines neither package nor VERSION--version check failed", - SVfARG(ST(0)) ); - } - } - - if ( !sv_isobject(req) || !sv_derived_from(req, "version")) { - /* req may very well be R/O, so create a new object */ - req = sv_2mortal( new_version(req) ); - } - - if ( vcmp( req, sv ) > 0 ) { - if ( hv_exists(MUTABLE_HV(SvRV(req)), "qv", 2 ) ) { - Perl_croak(aTHX_ "%"HEKf" version %"SVf" required--" - "this is only version %"SVf"", - HEKfARG(HvNAME_HEK(pkg)), - SVfARG(sv_2mortal(vnormal(req))), - SVfARG(sv_2mortal(vnormal(sv)))); - } else { - Perl_croak(aTHX_ "%"HEKf" version %"SVf" required--" - "this is only version %"SVf, - HEKfARG(HvNAME_HEK(pkg)), - SVfARG(sv_2mortal(vstringify(req))), - SVfARG(sv_2mortal(vstringify(sv)))); - } - } - - } - - if ( SvOK(sv) && sv_derived_from(sv, "version") ) { - ST(0) = sv_2mortal(vstringify(sv)); - } else { - ST(0) = sv; - } - - XSRETURN(1); -} - -XS(XS_version_new) -{ - dVAR; - dXSARGS; - if (items > 3 || items < 1) - croak_xs_usage(cv, "class, version"); - SP -= items; - { - SV *vs = ST(1); - SV *rv; - STRLEN len; - const char *classname; - U32 flags; - - /* Just in case this is something like a tied hash */ - SvGETMAGIC(vs); - - if ( sv_isobject(ST(0)) ) { /* get the class if called as an object method */ - const HV * stash = SvSTASH(SvRV(ST(0))); - classname = HvNAME(stash); - len = HvNAMELEN(stash); - flags = HvNAMEUTF8(stash) ? SVf_UTF8 : 0; - } - else { - classname = SvPV(ST(0), len); - flags = SvUTF8(ST(0)); - } - - if ( items == 1 || ! SvOK(vs) ) { /* no param or explicit undef */ - /* create empty object */ - vs = sv_newmortal(); - sv_setpvs(vs, "0"); - } - else if ( items == 3 ) { - vs = sv_newmortal(); - Perl_sv_setpvf(aTHX_ vs,"v%s",SvPV_nolen_const(ST(2))); - } - - rv = new_version(vs); - if ( strnNE(classname,"version", len) ) /* inherited new() */ - sv_bless(rv, gv_stashpvn(classname, len, GV_ADD | flags)); - - mPUSHs(rv); - PUTBACK; - return; - } -} - -XS(XS_version_stringify) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - SP -= items; - { - SV * lobj = ST(0); - - if (sv_isobject(lobj) && sv_derived_from(lobj, "version")) { - lobj = SvRV(lobj); - } - else - Perl_croak(aTHX_ "lobj is not of type version"); - - mPUSHs(vstringify(lobj)); - - PUTBACK; - return; - } -} - -XS(XS_version_numify) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - SP -= items; - { - SV * lobj = ST(0); - - if (sv_isobject(lobj) && sv_derived_from(lobj, "version")) { - lobj = SvRV(lobj); - } - else - Perl_croak(aTHX_ "lobj is not of type version"); - - mPUSHs(vnumify(lobj)); - - PUTBACK; - return; - } -} - -XS(XS_version_normal) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - SP -= items; - { - SV * lobj = ST(0); - - if (sv_isobject(lobj) && sv_derived_from(lobj, "version")) { - lobj = SvRV(lobj); - } - else - Perl_croak(aTHX_ "lobj is not of type version"); - - mPUSHs(vnormal(lobj)); - - PUTBACK; - return; - } -} - -XS(XS_version_vcmp) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - SP -= items; - { - SV * lobj = ST(0); - - if (sv_isobject(lobj) && sv_derived_from(lobj, "version")) { - lobj = SvRV(lobj); - } - else - Perl_croak(aTHX_ "lobj is not of type version"); - - { - SV *rs; - SV *rvs; - SV * robj = ST(1); - const IV swap = (IV)SvIV(ST(2)); - - if ( !sv_isobject(robj) || !sv_derived_from(robj, "version") ) - { - robj = new_version(SvOK(robj) ? robj : newSVpvs_flags("0", SVs_TEMP)); - sv_2mortal(robj); - } - rvs = SvRV(robj); - - if ( swap ) - { - rs = newSViv(vcmp(rvs,lobj)); - } - else - { - rs = newSViv(vcmp(lobj,rvs)); - } - - mPUSHs(rs); - } - - PUTBACK; - return; - } -} - -XS(XS_version_boolean) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - SP -= items; - if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "version")) { - SV * const lobj = SvRV(ST(0)); - SV * const rs = - newSViv( vcmp(lobj, - sv_2mortal(new_version( - sv_2mortal(newSVpvs("0")) - )) - ) - ); - mPUSHs(rs); - PUTBACK; - return; - } - else - Perl_croak(aTHX_ "lobj is not of type version"); -} - -XS(XS_version_noop) -{ - dVAR; - dXSARGS; - if (items < 1) - croak_xs_usage(cv, "lobj, ..."); - if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "version")) - Perl_croak(aTHX_ "operation not supported with version object"); - else - Perl_croak(aTHX_ "lobj is not of type version"); -#ifndef HASATTRIBUTE_NORETURN - XSRETURN_EMPTY; -#endif -} - -XS(XS_version_is_alpha) -{ - dVAR; - dXSARGS; - if (items != 1) - croak_xs_usage(cv, "lobj"); - SP -= items; - if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "version")) { - SV * const lobj = ST(0); - if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "alpha", 5 ) ) - XSRETURN_YES; - else - XSRETURN_NO; - PUTBACK; - return; - } - else - Perl_croak(aTHX_ "lobj is not of type version"); -} - -XS(XS_version_qv) -{ - dVAR; - dXSARGS; - PERL_UNUSED_ARG(cv); - SP -= items; - { - SV * ver = ST(0); - SV * rv; - STRLEN len = 0; - const char * classname = ""; - U32 flags = 0; - if ( items == 2 ) { - SvGETMAGIC(ST(1)); - if (SvOK(ST(1))) { - ver = ST(1); - } - else { - Perl_croak(aTHX_ "Invalid version format (version required)"); - } - if ( sv_isobject(ST(0)) ) { /* class called as an object method */ - const HV * stash = SvSTASH(SvRV(ST(0))); - classname = HvNAME(stash); - len = HvNAMELEN(stash); - flags = HvNAMEUTF8(stash) ? SVf_UTF8 : 0; - } - else { - classname = SvPV(ST(0), len); - flags = SvUTF8(ST(0)); - } - } - if ( !SvVOK(ver) ) { /* not already a v-string */ - rv = sv_newmortal(); - sv_setsv(rv,ver); /* make a duplicate */ - upg_version(rv, TRUE); - } else { - rv = sv_2mortal(new_version(ver)); - } - if ( items == 2 - && strnNE(classname,"version", len) ) { /* inherited new() */ - sv_bless(rv, gv_stashpvn(classname, len, GV_ADD | flags)); - } - PUSHs(rv); - } - PUTBACK; - return; -} - -XS(XS_version_is_qv) -{ - dVAR; - dXSARGS; - if (items != 1) - croak_xs_usage(cv, "lobj"); - SP -= items; - if (sv_isobject(ST(0)) && sv_derived_from(ST(0), "version")) { - SV * const lobj = ST(0); - if ( hv_exists(MUTABLE_HV(SvRV(lobj)), "qv", 2 ) ) - XSRETURN_YES; - else - XSRETURN_NO; - PUTBACK; - return; - } - else - Perl_croak(aTHX_ "lobj is not of type version"); -} - +XS(XS_utf8_is_utf8); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_is_utf8) { - dVAR; dXSARGS; if (items != 1) croak_xs_usage(cv, "sv"); @@ -809,9 +435,9 @@ XS(XS_utf8_is_utf8) XSRETURN_EMPTY; } +XS(XS_utf8_valid); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_valid) { - dVAR; dXSARGS; if (items != 1) croak_xs_usage(cv, "sv"); @@ -827,9 +453,9 @@ XS(XS_utf8_valid) XSRETURN_EMPTY; } +XS(XS_utf8_encode); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_encode) { - dVAR; dXSARGS; if (items != 1) croak_xs_usage(cv, "sv"); @@ -838,9 +464,9 @@ XS(XS_utf8_encode) XSRETURN_EMPTY; } +XS(XS_utf8_decode); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_decode) { - dVAR; dXSARGS; if (items != 1) croak_xs_usage(cv, "sv"); @@ -855,9 +481,9 @@ XS(XS_utf8_decode) XSRETURN(1); } +XS(XS_utf8_upgrade); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_upgrade) { - dVAR; dXSARGS; if (items != 1) croak_xs_usage(cv, "sv"); @@ -872,15 +498,15 @@ XS(XS_utf8_upgrade) XSRETURN(1); } +XS(XS_utf8_downgrade); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_downgrade) { - dVAR; dXSARGS; if (items < 1 || items > 2) croak_xs_usage(cv, "sv, failok=0"); else { SV * const sv = ST(0); - const bool failok = (items < 2) ? 0 : (int)SvIV(ST(1)); + const bool failok = (items < 2) ? 0 : SvTRUE(ST(1)) ? 1 : 0; const bool RETVAL = sv_utf8_downgrade(sv, failok); ST(0) = boolSV(RETVAL); @@ -888,9 +514,9 @@ XS(XS_utf8_downgrade) XSRETURN(1); } +XS(XS_utf8_native_to_unicode); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_native_to_unicode) { - dVAR; dXSARGS; const UV uv = SvUV(ST(0)); @@ -901,9 +527,9 @@ XS(XS_utf8_native_to_unicode) XSRETURN(1); } +XS(XS_utf8_unicode_to_native); /* prototype to pass -Wmissing-prototypes */ XS(XS_utf8_unicode_to_native) { - dVAR; dXSARGS; const UV uv = SvUV(ST(0)); @@ -914,9 +540,9 @@ XS(XS_utf8_unicode_to_native) XSRETURN(1); } +XS(XS_Internals_SvREADONLY); /* prototype to pass -Wmissing-prototypes */ XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */ { - dVAR; dXSARGS; SV * const svz = ST(0); SV * sv; @@ -951,9 +577,9 @@ XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */ XSRETURN_UNDEF; /* Can't happen. */ } +XS(XS_constant__make_const); /* prototype to pass -Wmissing-prototypes */ XS(XS_constant__make_const) /* This is dangerous stuff. */ { - dVAR; dXSARGS; SV * const svz = ST(0); SV * sv; @@ -981,9 +607,9 @@ XS(XS_constant__make_const) /* This is dangerous stuff. */ XSRETURN(0); } +XS(XS_Internals_SvREFCNT); /* prototype to pass -Wmissing-prototypes */ XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */ { - dVAR; dXSARGS; SV * const svz = ST(0); SV * sv; @@ -1006,9 +632,9 @@ XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */ } +XS(XS_Internals_hv_clear_placehold); /* prototype to pass -Wmissing-prototypes */ XS(XS_Internals_hv_clear_placehold) { - dVAR; dXSARGS; if (items != 1 || !SvROK(ST(0))) @@ -1020,13 +646,13 @@ XS(XS_Internals_hv_clear_placehold) } } +XS(XS_PerlIO_get_layers); /* prototype to pass -Wmissing-prototypes */ XS(XS_PerlIO_get_layers) { - dVAR; dXSARGS; if (items < 1 || items % 2 == 0) croak_xs_usage(cv, "filehandle[,args]"); -#ifdef USE_PERLIO +#if defined(USE_PERLIO) { SV * sv; GV * gv; @@ -1082,7 +708,7 @@ XS(XS_PerlIO_get_layers) AV* const av = PerlIO_get_layers(aTHX_ input ? IoIFP(io) : IoOFP(io)); SSize_t i; - const SSize_t last = av_len(av); + const SSize_t last = av_tindex(av); SSize_t nitem = 0; for (i = last; i >= 0; i -= 3) { @@ -1146,9 +772,9 @@ XS(XS_PerlIO_get_layers) } +XS(XS_re_is_regexp); /* prototype to pass -Wmissing-prototypes */ XS(XS_re_is_regexp) { - dVAR; dXSARGS; PERL_UNUSED_VAR(cv); @@ -1162,11 +788,11 @@ XS(XS_re_is_regexp) } } +XS(XS_re_regnames_count); /* prototype to pass -Wmissing-prototypes */ XS(XS_re_regnames_count) { REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL; SV * ret; - dVAR; dXSARGS; if (items != 0) @@ -1185,9 +811,9 @@ XS(XS_re_regnames_count) XSRETURN(1); } +XS(XS_re_regname); /* prototype to pass -Wmissing-prototypes */ XS(XS_re_regname) { - dVAR; dXSARGS; REGEXP * rx; U32 flags; @@ -1217,9 +843,9 @@ XS(XS_re_regname) } +XS(XS_re_regnames); /* prototype to pass -Wmissing-prototypes */ XS(XS_re_regnames) { - dVAR; dXSARGS; REGEXP * rx; U32 flags; @@ -1254,7 +880,7 @@ XS(XS_re_regnames) XSRETURN_UNDEF; av = MUTABLE_AV(SvRV(ret)); - length = av_len(av); + length = av_tindex(av); EXTEND(SP, length+1); /* better extend stack just once */ for (i = 0; i <= length; i++) { @@ -1272,9 +898,9 @@ XS(XS_re_regnames) return; } +XS(XS_re_regexp_pattern); /* prototype to pass -Wmissing-prototypes */ XS(XS_re_regexp_pattern) { - dVAR; dXSARGS; REGEXP *re; @@ -1372,6 +998,9 @@ XS(XS_re_regexp_pattern) /* NOT-REACHED */ } +#include "vutil.h" +#include "vxs.inc" + struct xsub_details { const char *name; XSUBADDR_t xsub; @@ -1382,35 +1011,9 @@ static const struct xsub_details details[] = { {"UNIVERSAL::isa", XS_UNIVERSAL_isa, NULL}, {"UNIVERSAL::can", XS_UNIVERSAL_can, NULL}, {"UNIVERSAL::DOES", XS_UNIVERSAL_DOES, NULL}, - {"UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, NULL}, - {"version::()", XS_version_noop, NULL}, - {"version::new", XS_version_new, NULL}, - {"version::parse", XS_version_new, NULL}, - {"version::(\"\"", XS_version_stringify, NULL}, - {"version::stringify", XS_version_stringify, NULL}, - {"version::(0+", XS_version_numify, NULL}, - {"version::numify", XS_version_numify, NULL}, - {"version::normal", XS_version_normal, NULL}, - {"version::(cmp", XS_version_vcmp, NULL}, - {"version::(<=>", XS_version_vcmp, NULL}, - {"version::vcmp", XS_version_vcmp, NULL}, - {"version::(bool", XS_version_boolean, NULL}, - {"version::boolean", XS_version_boolean, NULL}, - {"version::(+", XS_version_noop, NULL}, - {"version::(-", XS_version_noop, NULL}, - {"version::(*", XS_version_noop, NULL}, - {"version::(/", XS_version_noop, NULL}, - {"version::(+=", XS_version_noop, NULL}, - {"version::(-=", XS_version_noop, NULL}, - {"version::(*=", XS_version_noop, NULL}, - {"version::(/=", XS_version_noop, NULL}, - {"version::(abs", XS_version_noop, NULL}, - {"version::(nomethod", XS_version_noop, NULL}, - {"version::noop", XS_version_noop, NULL}, - {"version::is_alpha", XS_version_is_alpha, NULL}, - {"version::qv", XS_version_qv, NULL}, - {"version::declare", XS_version_qv, NULL}, - {"version::is_qv", XS_version_is_qv, NULL}, +#define VXS_XSUB_DETAILS +#include "vxs.inc" +#undef VXS_XSUB_DETAILS {"utf8::is_utf8", XS_utf8_is_utf8, NULL}, {"utf8::valid", XS_utf8_valid, NULL}, {"utf8::encode", XS_utf8_encode, NULL}, @@ -1434,11 +1037,9 @@ static const struct xsub_details details[] = { void Perl_boot_core_UNIVERSAL(pTHX) { - dVAR; static const char file[] = __FILE__; const struct xsub_details *xsub = details; - const struct xsub_details *end - = details + sizeof(details) / sizeof(details[0]); + const struct xsub_details *end = C_ARRAY_END(details); do { newXS_flags(xsub->name, xsub->xsub, file, xsub->proto, 0);