4 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 * 2005, 2006, 2007, 2008 by Larry Wall and others
7 * You may distribute under the terms of either the GNU General Public
8 * License or the Artistic License, as specified in the README file.
13 * '"The roots of those mountains must be roots indeed; there must be
14 * great secrets buried there which have not been discovered since the
15 * beginning."' --Gandalf, relating Gollum's history
17 * [p.54 of _The Lord of the Rings_, I/ii: "The Shadow of the Past"]
20 /* This file contains the code that implements the functions in Perl's
21 * UNIVERSAL package, such as UNIVERSAL->can().
23 * It is also used to store XS functions that need to be present in
24 * miniperl for a lack of a better place to put them. It might be
25 * clever to move them to separate XS files which would then be pulled
26 * in by some to-be-written build process.
30 #define PERL_IN_UNIVERSAL_C
33 #if defined(USE_PERLIO)
34 #include "perliol.h" /* For the PERLIO_F_XXX */
39 * The main guts of traverse_isa was actually copied from gv_fetchmeth
42 #define PERL_ARGS_ASSERT_ISA_LOOKUP \
44 assert(namesv || name)
48 S_isa_lookup(pTHX_ HV *stash, SV *namesv, const char * name, STRLEN len, U32 flags)
50 const struct mro_meta *const meta = HvMROMETA(stash);
54 PERL_ARGS_ASSERT_ISA_LOOKUP;
57 (void)mro_get_linear_isa(stash);
61 if (hv_common(isa, namesv, name, len, ( flags & SVf_UTF8 ? HVhek_UTF8 : 0),
62 HV_FETCH_ISEXISTS, NULL, 0)) {
63 /* Direct name lookup worked. */
67 /* A stash/class can go by many names (ie. User == main::User), so
68 we use the HvENAME in the stash itself, which is canonical, falling
69 back to HvNAME if necessary. */
70 our_stash = gv_stashsvpvn_cached(namesv, name, len, flags);
73 HEK *canon_name = HvENAME_HEK(our_stash);
74 if (!canon_name) canon_name = HvNAME_HEK(our_stash);
76 if (hv_common(isa, NULL, HEK_KEY(canon_name), HEK_LEN(canon_name),
77 HEK_FLAGS(canon_name),
78 HV_FETCH_ISEXISTS, NULL, HEK_HASH(canon_name))) {
86 #define PERL_ARGS_ASSERT_SV_DERIVED_FROM_SVPVN \
88 assert(namesv || name)
91 S_sv_derived_from_svpvn(pTHX_ SV *sv, SV *namesv, const char * name, const STRLEN len, U32 flags)
95 PERL_ARGS_ASSERT_SV_DERIVED_FROM_SVPVN;
101 type = sv_reftype(sv,0);
104 name = SvPV_nolen(namesv);
105 if (strEQ(name, type))
113 stash = gv_stashsv(sv, 0);
116 if (stash && isa_lookup(stash, namesv, name, len, flags))
119 stash = gv_stashpvs("UNIVERSAL", 0);
120 return stash && isa_lookup(stash, namesv, name, len, flags);
124 =for apidoc_section $SV
126 =for apidoc sv_derived_from
127 =for apidoc_item sv_derived_from_hv
128 =for apidoc_item sv_derived_from_pv
129 =for apidoc_item sv_derived_from_pvn
130 =for apidoc_item sv_derived_from_sv
132 These each return a boolean indicating whether C<sv> is derived from the
133 specified class I<at the C level>. To check derivation at the Perl level, call
134 C<isa()> as a normal Perl method.
136 In C<sv_derived_from_hv>, the class name is C<HvNAME(hv)> (which would
137 presumably represent a stash). Its UTF8ness is C<HvNAMEUTF8(hv)>.
139 In C<sv_derived_from> and C<sv_derived_from_pv>, the class name is given by
140 C<name>, which is a NUL-terminated C string. In C<sv_derived_from>, the name
141 is never considered to be encoded as UTF-8.
143 The remaining forms differ only in how the class name is specified;
144 they all have a C<flags> parameter. Currently, the only significant value for
145 which is C<SVf_UTF8> to indicate that the class name is encoded as such.
147 In C<sv_derived_from_sv>, the class name is extracted from C<namesv>.
148 This is the preferred form. The class name is considered to be in UTF-8 if
149 C<namesv> is marked as such.
151 In C<sv_derived_from_pvn>, C<len> gives the length of C<name>, so the latter
152 may contain embedded NUL characters.
159 Perl_sv_derived_from_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
161 PERL_ARGS_ASSERT_SV_DERIVED_FROM_SV;
162 return sv_derived_from_svpvn(sv, namesv, NULL, 0, flags);
166 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
168 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
169 return sv_derived_from_svpvn(sv, NULL, name, strlen(name), 0);
174 Perl_sv_derived_from_pv(pTHX_ SV *sv, const char *const name, U32 flags)
176 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PV;
177 return sv_derived_from_svpvn(sv, NULL, name, strlen(name), flags);
181 Perl_sv_derived_from_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
183 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PVN;
184 return sv_derived_from_svpvn(sv, NULL, name, len, flags);
188 Perl_sv_derived_from_hv(pTHX_ SV *sv, HV *hv)
190 PERL_ARGS_ASSERT_SV_DERIVED_FROM_HV;
192 const char *hvname = HvNAME(hv);
196 return sv_derived_from_svpvn(sv, NULL, hvname, HvNAMELEN(hv), HvNAMEUTF8(hv) ? SVf_UTF8 : 0);
200 =for apidoc sv_isa_sv
202 Returns a boolean indicating whether the SV is an object reference and is
203 derived from the specified class, respecting any C<isa()> method overloading
204 it may have. Returns false if C<sv> is not a reference to an object, or is
205 not derived from the specified class.
207 This is the function used to implement the behaviour of the C<isa> operator.
209 Does not invoke magic on C<sv>.
211 Not to be confused with the older C<sv_isa> function, which does not use an
212 overloaded C<isa()> method, nor will check subclassing.
219 Perl_sv_isa_sv(pTHX_ SV *sv, SV *namesv)
223 PERL_ARGS_ASSERT_SV_ISA_SV;
225 if(!SvROK(sv) || !SvOBJECT(SvRV(sv)))
228 isagv = gv_fetchmeth_pvn(SvSTASH(SvRV(sv)), "isa", 3, -1, GV_NOUNIVERSAL);
231 CV *isacv = isGV(isagv) ? GvCV(isagv) : (CV *)isagv;
246 call_sv((SV *)isacv, G_SCALAR);
259 /* TODO: Support namesv being an HV ref to the stash directly? */
261 return sv_derived_from_sv(sv, namesv, 0);
265 =for apidoc sv_does_sv
267 Returns a boolean indicating whether the SV performs a specific, named role.
268 The SV can be a Perl object or the name of a Perl class.
276 Perl_sv_does_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
283 PERL_ARGS_ASSERT_SV_DOES_SV;
284 PERL_UNUSED_ARG(flags);
291 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) {
296 if (SvROK(sv) && SvOBJECT(SvRV(sv))) {
297 classname = sv_ref(NULL,SvRV(sv),TRUE);
302 if (sv_eq(classname, namesv)) {
313 /* create a PV with value "isa", but with a special address
314 * so that perl knows we're really doing "DOES" instead */
315 methodname = newSV_type_mortal(SVt_PV);
316 SvLEN_set(methodname, 0);
317 SvCUR_set(methodname, strlen(PL_isa_DOES));
318 SvPVX(methodname) = (char *)PL_isa_DOES; /* discard 'const' qualifier */
319 SvPOK_on(methodname);
320 call_sv(methodname, G_SCALAR | G_METHOD);
323 does_it = SvTRUE_NN( TOPs );
333 Like L</sv_does_pv>, but doesn't take a C<flags> parameter.
339 Perl_sv_does(pTHX_ SV *sv, const char *const name)
341 PERL_ARGS_ASSERT_SV_DOES;
342 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP), 0);
346 =for apidoc sv_does_pv
348 Like L</sv_does_sv>, but takes a nul-terminated string instead of an SV.
355 Perl_sv_does_pv(pTHX_ SV *sv, const char *const name, U32 flags)
357 PERL_ARGS_ASSERT_SV_DOES_PV;
358 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP | flags), flags);
362 =for apidoc sv_does_pvn
364 Like L</sv_does_sv>, but takes a string/length pair instead of an SV.
370 Perl_sv_does_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
372 PERL_ARGS_ASSERT_SV_DOES_PVN;
374 return sv_does_sv(sv, newSVpvn_flags(name, len, flags | SVs_TEMP), flags);
378 =for apidoc croak_xs_usage
380 A specialised variant of C<croak()> for emitting the usage message for xsubs
382 croak_xs_usage(cv, "eee_yow");
384 works out the package name and subroutine name from C<cv>, and then calls
385 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
387 diag_listed_as: SKIPME
388 croak("Usage: %" SVf "::%" SVf "(%s)", "ouch" "awk",
395 Perl_croak_xs_usage(const CV *const cv, const char *const params)
397 /* Avoid CvGV as it requires aTHX. */
398 const GV *gv = CvNAMED(cv) ? NULL : cv->sv_any->xcv_gv_u.xcv_gv;
400 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
403 const HV *const stash = GvSTASH(gv);
405 if (HvNAME_get(stash))
406 /* diag_listed_as: SKIPME */
407 Perl_croak_nocontext("Usage: %" HEKf "::%" HEKf "(%s)",
408 HEKfARG(HvNAME_HEK(stash)),
409 HEKfARG(GvNAME_HEK(gv)),
412 /* diag_listed_as: SKIPME */
413 Perl_croak_nocontext("Usage: %" HEKf "(%s)",
414 HEKfARG(GvNAME_HEK(gv)), params);
417 if ((gv = CvGV(cv))) goto got_gv;
419 /* Pants. I don't think that it should be possible to get here. */
420 /* diag_listed_as: SKIPME */
421 Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
425 XS(XS_UNIVERSAL_isa); /* prototype to pass -Wmissing-prototypes */
431 croak_xs_usage(cv, "reference, kind");
433 SV * const sv = ST(0);
437 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
440 ST(0) = boolSV(sv_derived_from_sv(sv, ST(1), 0));
445 XS(XS_UNIVERSAL_import_unimport); /* prototype to pass -Wmissing-prototypes */
446 XS(XS_UNIVERSAL_import_unimport)
452 char *class_pv= SvPV_nolen(ST(0));
453 if (strEQ(class_pv,"UNIVERSAL"))
454 croak("UNIVERSAL does not export anything");
455 /* _charnames is special - ignore it for now as the code that
456 * depends on it has its own "no import" logic that produces better
457 * warnings than this does. */
458 if (strNE(class_pv,"_charnames"))
459 ck_warner_d(packWARN(WARN_DEPRECATED__MISSING_IMPORT_CALLED_WITH_ARGS),
460 "Attempt to call undefined %s method with arguments "
461 "(%" SVf_QUOTEDPREFIX "%s) via package "
462 "%" SVf_QUOTEDPREFIX " (Perhaps you forgot to load the package?)",
463 ix ? "unimport" : "import",
465 (items > 2 ? " ..." : ""),
472 XS(XS_UNIVERSAL_can); /* prototype to pass -Wmissing-prototypes */
482 croak_xs_usage(cv, "object-ref, method");
488 /* Reject undef and empty string. Note that the string form takes
489 precedence here over the numeric form, as (!1)->foo treats the
490 invocant as the empty string, though it is a dualvar. */
491 if (!SvOK(sv) || (SvPOK(sv) && !SvCUR(sv)))
497 sv = MUTABLE_SV(SvRV(sv));
500 else if (isGV_with_GP(sv) && GvIO(sv))
501 pkg = SvSTASH(GvIO(sv));
503 else if (isGV_with_GP(sv) && GvIO(sv))
504 pkg = SvSTASH(GvIO(sv));
505 else if ((iogv = gv_fetchsv_nomg(sv, 0, SVt_PVIO)) && GvIO(iogv))
506 pkg = SvSTASH(GvIO(iogv));
508 pkg = gv_stashsv(sv, 0);
510 pkg = gv_stashpvs("UNIVERSAL", 0);
514 GV * const gv = gv_fetchmethod_sv_flags(pkg, ST(1), 0);
516 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
523 XS(XS_UNIVERSAL_DOES); /* prototype to pass -Wmissing-prototypes */
524 XS(XS_UNIVERSAL_DOES)
530 croak("Usage: invocant->DOES(kind)");
532 SV * const sv = ST(0);
533 if (sv_does_sv( sv, ST(1), 0 ))
540 XS(XS_utf8_is_utf8); /* prototype to pass -Wmissing-prototypes */
545 croak_xs_usage(cv, "sv");
547 SV * const sv = ST(0);
557 XS(XS_utf8_valid); /* prototype to pass -Wmissing-prototypes */
562 croak_xs_usage(cv, "sv");
564 SV * const sv = ST(0);
566 const char * const s = SvPV_const(sv,len);
567 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
575 XS(XS_utf8_encode); /* prototype to pass -Wmissing-prototypes */
580 croak_xs_usage(cv, "sv");
581 sv_utf8_encode(ST(0));
586 XS(XS_utf8_decode); /* prototype to pass -Wmissing-prototypes */
591 croak_xs_usage(cv, "sv");
593 SV * const sv = ST(0);
595 SvPV_force_nolen(sv);
596 RETVAL = sv_utf8_decode(sv);
598 ST(0) = boolSV(RETVAL);
603 XS(XS_utf8_upgrade); /* prototype to pass -Wmissing-prototypes */
608 croak_xs_usage(cv, "sv");
610 SV * const sv = ST(0);
615 if (UNLIKELY(! sv)) {
620 if (UNLIKELY(! SvOK(sv))) {
624 RETVAL = sv_utf8_upgrade_nomg(sv);
630 XS(XS_utf8_downgrade); /* prototype to pass -Wmissing-prototypes */
631 XS(XS_utf8_downgrade)
634 if (items < 1 || items > 2)
635 croak_xs_usage(cv, "sv, failok=0");
637 SV * const sv0 = ST(0);
638 SV * const sv1 = ST(1);
639 const bool failok = (items < 2) ? 0 : SvTRUE_NN(sv1) ? 1 : 0;
640 const bool RETVAL = sv_utf8_downgrade(sv0, failok);
642 ST(0) = boolSV(RETVAL);
647 XS(XS_utf8_native_to_unicode); /* prototype to pass -Wmissing-prototypes */
648 XS(XS_utf8_native_to_unicode)
651 const UV uv = SvUV(ST(0));
654 croak_xs_usage(cv, "sv");
656 ST(0) = sv_2mortal(newSVuv(NATIVE_TO_UNI(uv)));
660 XS(XS_utf8_unicode_to_native); /* prototype to pass -Wmissing-prototypes */
661 XS(XS_utf8_unicode_to_native)
664 const UV uv = SvUV(ST(0));
667 croak_xs_usage(cv, "sv");
669 ST(0) = sv_2mortal(newSVuv(UNI_TO_NATIVE(uv)));
673 XS(XS_Internals_SvREADONLY); /* prototype to pass -Wmissing-prototypes */
674 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
677 SV * const svz = ST(0);
680 /* [perl #77776] - called as &foo() not foo() */
682 croak_xs_usage(cv, "SCALAR[, ON]");
692 else if (items == 2) {
694 if (SvTRUE_NN(sv1)) {
695 SvFLAGS(sv) |= SVf_READONLY;
699 /* I hope you really know what you are doing. */
700 SvFLAGS(sv) &=~ SVf_READONLY;
704 XSRETURN_UNDEF; /* Can't happen. */
707 XS(XS_constant__make_const); /* prototype to pass -Wmissing-prototypes */
708 XS(XS_constant__make_const) /* This is dangerous stuff. */
711 SV * const svz = ST(0);
714 /* [perl #77776] - called as &foo() not foo() */
715 if (!SvROK(svz) || items != 1)
716 croak_xs_usage(cv, "SCALAR");
721 if (SvTYPE(sv) == SVt_PVAV && AvFILLp(sv) != -1) {
722 /* for constant.pm; nobody else should be calling this
725 for (svp = AvARRAY(sv) + AvFILLp(sv)
728 if (*svp) SvPADTMP_on(*svp);
733 XS(XS_Internals_SvREFCNT); /* prototype to pass -Wmissing-prototypes */
734 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
737 SV * const svz = ST(0);
741 /* [perl #77776] - called as &foo() not foo() */
742 if ((items != 1 && items != 2) || !SvROK(svz))
743 croak_xs_usage(cv, "SCALAR[, REFCOUNT]");
747 /* I hope you really know what you are doing. */
748 /* idea is for SvREFCNT(sv) to be accessed only once */
749 refcnt = items == 2 ?
750 /* we free one ref on exit */
751 (SvREFCNT(sv) = SvUV(ST(1)) + 1)
753 XSRETURN_UV(refcnt - 1); /* Minus the ref created for us. */
757 XS(XS_Internals_hv_clear_placehold); /* prototype to pass -Wmissing-prototypes */
758 XS(XS_Internals_hv_clear_placehold)
762 if (items != 1 || !SvROK(ST(0)))
763 croak_xs_usage(cv, "hv");
765 HV * const hv = HV_FROM_REF(ST(0));
766 hv_clear_placeholders(hv);
771 XS(XS_Internals_stack_refcounted); /* prototype to pass -Wmissing-prototypes */
772 XS(XS_Internals_stack_refcounted)
778 croak_xs_usage(cv, "");
785 XS(XS_PerlIO_get_layers); /* prototype to pass -Wmissing-prototypes */
786 XS(XS_PerlIO_get_layers)
789 if (items < 1 || items % 2 == 0)
790 croak_xs_usage(cv, "filehandle[,args]");
791 #if defined(USE_PERLIO)
797 bool details = FALSE;
801 for (svp = MARK + 2; svp <= SP; svp += 2) {
802 SV * const * const varp = svp;
803 SV * const * const valp = svp + 1;
805 const char * const key = SvPV_const(*varp, klen);
809 if (memEQs(key, klen, "input")) {
810 input = SvTRUE(*valp);
815 if (memEQs(key, klen, "output")) {
816 input = !SvTRUE(*valp);
821 if (memEQs(key, klen, "details")) {
822 details = SvTRUE(*valp);
829 "get_layers: unknown argument '%s'",
839 /* MAYBE_DEREF_GV will call get magic */
840 if ((gv = MAYBE_DEREF_GV(sv)))
842 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO)
844 else if (!SvROK(sv) && (gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO)))
848 AV* const av = PerlIO_get_layers(aTHX_ input ?
849 IoIFP(io) : IoOFP(io));
851 const SSize_t last = av_top_index(av);
854 for (i = last; i >= 0; i -= 3) {
855 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
856 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
857 SV * const * const flgsvp = av_fetch(av, i, FALSE);
859 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
860 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
861 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
863 EXTEND(SP, 3); /* Three is the max in all branches: better check just once */
865 /* Indents of 5? Yuck. */
866 /* We know that PerlIO_get_layers creates a new SV for
867 the name and flags, so we can just take a reference
868 and "steal" it when we free the AV below. */
870 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
873 ? newSVpvn_flags(SvPVX_const(*argsvp),
875 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
879 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
885 PUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%" SVf "(%" SVf ")",
889 PUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
894 const IV flags = SvIVX(*flgsvp);
896 if (flags & PERLIO_F_UTF8) {
897 PUSHs(newSVpvs_flags("utf8", SVs_TEMP));
914 XS(XS_re_is_regexp); /* prototype to pass -Wmissing-prototypes */
920 croak_xs_usage(cv, "sv");
929 XS(XS_re_regnames_count); /* prototype to pass -Wmissing-prototypes */
930 XS(XS_re_regnames_count)
932 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
937 croak_xs_usage(cv, "");
942 ret = CALLREG_NAMED_BUFF_COUNT(rx);
945 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
949 XS(XS_re_regname); /* prototype to pass -Wmissing-prototypes */
957 if (items < 1 || items > 2)
958 croak_xs_usage(cv, "name[, all ]");
963 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
968 if (items == 2 && SvTRUE_NN(ST(1))) {
973 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
976 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
981 XS(XS_re_regnames); /* prototype to pass -Wmissing-prototypes */
994 croak_xs_usage(cv, "[all]");
996 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1001 if (items == 1 && SvTRUE_NN(ST(0))) {
1010 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
1017 av = AV_FROM_REF(ret);
1018 length = av_count(av);
1020 EXTEND(SP, length); /* better extend stack just once */
1021 for (i = 0; i < length; i++) {
1022 entry = av_fetch(av, i, FALSE);
1025 /* diag_listed_as: SKIPME */
1026 croak("NULL array element in re::regnames()");
1028 mPUSHs(SvREFCNT_inc_simple_NN(*entry));
1037 XS(XS_re_regexp_pattern); /* prototype to pass -Wmissing-prototypes */
1038 XS(XS_re_regexp_pattern)
1042 U8 const gimme = GIMME_V;
1047 croak_xs_usage(cv, "sv");
1050 Checks if a reference is a regex or not. If the parameter is
1051 not a ref, or is not the result of a qr// then returns false
1052 in scalar context and an empty list in list context.
1053 Otherwise in list context it returns the pattern and the
1054 modifiers, in scalar context it returns the pattern just as it
1055 would if the qr// was stringified normally, regardless as
1056 to the class of the variable and any stringification overloads
1060 if ((re = SvRX(ST(0)))) /* assign deliberate */
1062 /* Houston, we have a regex! */
1065 if ( gimme == G_LIST ) {
1067 char reflags[sizeof(INT_PAT_MODS) + MAX_CHARSET_NAME_LENGTH];
1073 we are in list context so stringify
1074 the modifiers that apply. We ignore "negative
1075 modifiers" in this scenario, and the default character set
1078 if (get_regex_charset(RX_EXTFLAGS(re)) != REGEX_DEPENDS_CHARSET) {
1080 const char* const name = get_regex_charset_name(RX_EXTFLAGS(re),
1082 Copy(name, reflags + left, len, char);
1085 fptr = INT_PAT_MODS;
1086 match_flags = (U16)((RX_EXTFLAGS(re) & RXf_PMf_COMPILETIME)
1087 >> RXf_PMf_STD_PMMOD_SHIFT);
1089 while((ch = *fptr++)) {
1090 if(match_flags & 1) {
1091 reflags[left++] = ch;
1096 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1097 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1099 /* return the pattern and the modifiers */
1101 PUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1104 /* Scalar, so use the string that Perl would return */
1105 /* return the pattern in (?msixn:..) format */
1106 pattern = sv_mortalcopy_flags(MUTABLE_SV(re), SV_GMAGIC|SV_NOSTEAL);
1111 /* It ain't a regexp folks */
1112 if ( gimme == G_LIST ) {
1113 /* return the empty list */
1116 /* Because of the (?:..) wrapping involved in a
1117 stringified pattern it is impossible to get a
1118 result for a real regexp that would evaluate to
1119 false. Therefore we can return PL_sv_no to signify
1120 that the object is not a regex, this means that one
1123 if (regex($might_be_a_regex) eq '(?:foo)') { }
1125 and not worry about undefined values.
1130 NOT_REACHED; /* NOTREACHED */
1133 #if defined(HAS_GETCWD) && defined(PERL_IS_MINIPERL)
1135 XS(XS_Internals_getcwd)
1138 SV *sv = sv_newmortal();
1141 croak_xs_usage(cv, "");
1143 (void)getcwd_sv(sv);
1152 XS(XS_NamedCapture_tie_it)
1157 croak_xs_usage(cv, "sv");
1160 GV * const gv = (GV *)sv;
1161 HV * const hv = GvHVn(gv);
1162 SV *rv = newSV_type(SVt_IV);
1163 const char *gv_name = GvNAME(gv);
1165 sv_setrv_noinc(rv, newSVuv(
1166 strEQ(gv_name, "-") || strEQ(gv_name, "\003APTURE_ALL")
1167 ? RXapif_ALL : RXapif_ONE));
1168 sv_bless(rv, GvSTASH(CvGV(cv)));
1170 sv_unmagic((SV *)hv, PERL_MAGIC_tied);
1171 sv_magic((SV *)hv, rv, PERL_MAGIC_tied, NULL, 0);
1172 SvREFCNT_dec(rv); /* As sv_magic increased it by one. */
1177 XS(XS_NamedCapture_TIEHASH)
1181 croak_xs_usage(cv, "package, ...");
1183 const char * package = (const char *)SvPV_nolen(ST(0));
1184 UV flag = RXapif_ONE;
1188 const char *p = SvPV_const(*mark, len);
1189 if(memEQs(p, len, "all"))
1190 flag = SvTRUE(mark[1]) ? RXapif_ALL : RXapif_ONE;
1193 ST(0) = newSV_type_mortal(SVt_IV);
1194 sv_setuv(newSVrv(ST(0), package), flag);
1199 /* These are tightly coupled to the RXapif_* flags defined in regexp.h */
1200 #define UNDEF_FATAL 0x80000
1201 #define DISCARD 0x40000
1202 #define EXPECT_SHIFT 24
1203 #define ACTION_MASK 0x000FF
1205 #define FETCH_ALIAS (RXapif_FETCH | (2 << EXPECT_SHIFT))
1206 #define STORE_ALIAS (RXapif_STORE | (3 << EXPECT_SHIFT) | UNDEF_FATAL | DISCARD)
1207 #define DELETE_ALIAS (RXapif_DELETE | (2 << EXPECT_SHIFT) | UNDEF_FATAL)
1208 #define CLEAR_ALIAS (RXapif_CLEAR | (1 << EXPECT_SHIFT) | UNDEF_FATAL | DISCARD)
1209 #define EXISTS_ALIAS (RXapif_EXISTS | (2 << EXPECT_SHIFT))
1210 #define SCALAR_ALIAS (RXapif_SCALAR | (1 << EXPECT_SHIFT))
1212 XS(XS_NamedCapture_FETCH)
1216 PERL_UNUSED_VAR(cv); /* -W */
1217 PERL_UNUSED_VAR(ax); /* -Wall */
1220 REGEXP *const rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1223 const U32 action = ix & ACTION_MASK;
1224 const int expect = ix >> EXPECT_SHIFT;
1225 if (items != expect)
1226 croak_xs_usage(cv, expect == 2 ? "$key"
1227 : (expect == 3 ? "$key, $value"
1230 if (!rx || !SvROK(ST(0))) {
1231 if (ix & UNDEF_FATAL)
1237 flags = (U32)SvUV(SvRV(MUTABLE_SV(ST(0))));
1240 ret = RX_ENGINE(rx)->named_buff(aTHX_ (rx), expect >= 2 ? ST(1) : NULL,
1241 expect >= 3 ? ST(2) : NULL, flags | action);
1245 /* Called with G_DISCARD, so our return stack state is thrown away.
1246 Hence if we were returned anything, free it immediately. */
1249 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
1257 XS(XS_NamedCapture_FIRSTKEY)
1261 PERL_UNUSED_VAR(cv); /* -W */
1262 PERL_UNUSED_VAR(ax); /* -Wall */
1265 REGEXP *const rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1268 const int expect = ix ? 2 : 1;
1269 const U32 action = ix ? RXapif_NEXTKEY : RXapif_FIRSTKEY;
1270 if (items != expect)
1271 croak_xs_usage(cv, expect == 2 ? "$lastkey" : "");
1273 if (!rx || !SvROK(ST(0)))
1276 flags = (U32)SvUV(SvRV(MUTABLE_SV(ST(0))));
1279 ret = RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx),
1280 expect >= 2 ? ST(1) : NULL,
1284 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
1290 /* is this still needed? */
1291 XS(XS_NamedCapture_flags)
1294 PERL_UNUSED_VAR(cv); /* -W */
1295 PERL_UNUSED_VAR(ax); /* -Wall */
1309 struct xsub_details {
1316 static const struct xsub_details these_details[] = {
1317 {"UNIVERSAL::isa", XS_UNIVERSAL_isa, NULL, 0 },
1318 {"UNIVERSAL::can", XS_UNIVERSAL_can, NULL, 0 },
1319 {"UNIVERSAL::DOES", XS_UNIVERSAL_DOES, NULL, 0 },
1320 {"UNIVERSAL::import", XS_UNIVERSAL_import_unimport, NULL, 0},
1321 {"UNIVERSAL::unimport", XS_UNIVERSAL_import_unimport, NULL, 1},
1322 #define VXS_XSUB_DETAILS
1324 #undef VXS_XSUB_DETAILS
1325 {"utf8::is_utf8", XS_utf8_is_utf8, NULL, 0 },
1326 {"utf8::valid", XS_utf8_valid, NULL, 0 },
1327 {"utf8::encode", XS_utf8_encode, NULL, 0 },
1328 {"utf8::decode", XS_utf8_decode, NULL, 0 },
1329 {"utf8::upgrade", XS_utf8_upgrade, NULL, 0 },
1330 {"utf8::downgrade", XS_utf8_downgrade, NULL, 0 },
1331 {"utf8::native_to_unicode", XS_utf8_native_to_unicode, NULL, 0 },
1332 {"utf8::unicode_to_native", XS_utf8_unicode_to_native, NULL, 0 },
1333 {"Internals::SvREADONLY", XS_Internals_SvREADONLY, "\\[$%@];$", 0 },
1334 {"Internals::SvREFCNT", XS_Internals_SvREFCNT, "\\[$%@];$", 0 },
1335 {"Internals::hv_clear_placeholders", XS_Internals_hv_clear_placehold, "\\%", 0 },
1336 {"Internals::stack_refcounted", XS_Internals_stack_refcounted, NULL, 0 },
1337 {"constant::_make_const", XS_constant__make_const, "\\[$@]", 0 },
1338 {"PerlIO::get_layers", XS_PerlIO_get_layers, "*;@", 0 },
1339 {"re::is_regexp", XS_re_is_regexp, "$", 0 },
1340 {"re::regname", XS_re_regname, ";$$", 0 },
1341 {"re::regnames", XS_re_regnames, ";$", 0 },
1342 {"re::regnames_count", XS_re_regnames_count, "", 0 },
1343 {"re::regexp_pattern", XS_re_regexp_pattern, "$", 0 },
1344 #if defined(HAS_GETCWD) && defined(PERL_IS_MINIPERL)
1345 {"Internals::getcwd", XS_Internals_getcwd, "", 0 },
1347 {"Tie::Hash::NamedCapture::_tie_it", XS_NamedCapture_tie_it, NULL, 0 },
1348 {"Tie::Hash::NamedCapture::TIEHASH", XS_NamedCapture_TIEHASH, NULL, 0 },
1349 {"Tie::Hash::NamedCapture::FETCH", XS_NamedCapture_FETCH, NULL, FETCH_ALIAS },
1350 {"Tie::Hash::NamedCapture::STORE", XS_NamedCapture_FETCH, NULL, STORE_ALIAS },
1351 {"Tie::Hash::NamedCapture::DELETE", XS_NamedCapture_FETCH, NULL, DELETE_ALIAS },
1352 {"Tie::Hash::NamedCapture::CLEAR", XS_NamedCapture_FETCH, NULL, CLEAR_ALIAS },
1353 {"Tie::Hash::NamedCapture::EXISTS", XS_NamedCapture_FETCH, NULL, EXISTS_ALIAS },
1354 {"Tie::Hash::NamedCapture::SCALAR", XS_NamedCapture_FETCH, NULL, SCALAR_ALIAS },
1355 {"Tie::Hash::NamedCapture::FIRSTKEY", XS_NamedCapture_FIRSTKEY, NULL, 0 },
1356 {"Tie::Hash::NamedCapture::NEXTKEY", XS_NamedCapture_FIRSTKEY, NULL, 1 },
1357 {"Tie::Hash::NamedCapture::flags", XS_NamedCapture_flags, NULL, 0 },
1361 optimize_out_native_convert_function(pTHX_ OP* entersubop,
1365 /* Optimizes out an identity function, i.e., one that just returns its
1366 * argument. The passed in function is assumed to be an identity function,
1367 * with no checking. This is designed to be called for utf8_to_native()
1368 * and native_to_utf8() on ASCII platforms, as they just return their
1369 * arguments, but it could work on any such function.
1371 * The code is mostly just cargo-culted from Memoize::Lift */
1375 SV* prototype = newSVpvs("$");
1377 PERL_UNUSED_ARG(protosv);
1379 assert(entersubop->op_type == OP_ENTERSUB);
1381 entersubop = ck_entersub_args_proto(entersubop, namegv, prototype);
1382 parent = entersubop;
1384 SvREFCNT_dec(prototype);
1386 pushop = cUNOPx(entersubop)->op_first;
1387 if (! OpHAS_SIBLING(pushop)) {
1389 pushop = cUNOPx(pushop)->op_first;
1391 argop = OpSIBLING(pushop);
1393 /* Carry on without doing the optimization if it is not something we're
1394 * expecting, so continues to work */
1396 || ! OpHAS_SIBLING(argop)
1397 || OpHAS_SIBLING(OpSIBLING(argop))
1402 /* cut argop from the subtree */
1403 (void)op_sibling_splice(parent, pushop, 1, NULL);
1405 op_free(entersubop);
1410 Perl_boot_core_UNIVERSAL(pTHX)
1412 static const char file[] = __FILE__;
1413 const struct xsub_details *xsub = these_details;
1414 const struct xsub_details *end = C_ARRAY_END(these_details);
1417 CV *cv = newXS_flags(xsub->name, xsub->xsub, file, xsub->proto, 0);
1418 XSANY.any_i32 = xsub->ix;
1419 } while (++xsub < end);
1422 { /* On ASCII platforms these functions just return their argument, so can
1423 be optimized away */
1425 CV* to_native_cv = get_cv("utf8::unicode_to_native", 0);
1426 CV* to_unicode_cv = get_cv("utf8::native_to_unicode", 0);
1428 cv_set_call_checker_flags(to_native_cv,
1429 optimize_out_native_convert_function,
1430 (SV*) to_native_cv, 0);
1431 cv_set_call_checker_flags(to_unicode_cv,
1432 optimize_out_native_convert_function,
1433 (SV*) to_unicode_cv, 0);
1437 /* Providing a Regexp::DESTROY fixes #21347. See test in t/op/ref.t */
1440 newCONSTSUB(get_hv("Regexp::", GV_ADD), "DESTROY", NULL);
1441 char ** cvfile = &CvFILE(cv);
1442 char * oldfile = *cvfile;
1444 *cvfile = (char *)file;
1450 * ex: set ts=8 sts=4 sw=4 et: