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 */
38 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
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_pvn
128 Returns a boolean indicating whether the SV is derived from the specified class
129 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
132 Currently, the only significant value for C<flags> is SVf_UTF8.
136 =for apidoc sv_derived_from_sv
138 Exactly like L</sv_derived_from_pvn>, but takes the name string in the form
139 of an SV instead of a string/length pair. This is the advised form.
146 Perl_sv_derived_from_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
148 PERL_ARGS_ASSERT_SV_DERIVED_FROM_SV;
149 return sv_derived_from_svpvn(sv, namesv, NULL, 0, flags);
153 =for apidoc sv_derived_from
155 Exactly like L</sv_derived_from_pv>, but doesn't take a C<flags> parameter.
161 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
163 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
164 return sv_derived_from_svpvn(sv, NULL, name, strlen(name), 0);
168 =for apidoc sv_derived_from_pv
170 Exactly like L</sv_derived_from_pvn>, but takes a nul-terminated string
171 instead of a string/length pair.
178 Perl_sv_derived_from_pv(pTHX_ SV *sv, const char *const name, U32 flags)
180 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PV;
181 return sv_derived_from_svpvn(sv, NULL, name, strlen(name), flags);
185 Perl_sv_derived_from_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
187 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PVN;
188 return sv_derived_from_svpvn(sv, NULL, name, len, flags);
192 =for apidoc sv_derived_from_hv
194 Exactly like L</sv_derived_from_pvn>, but takes the name string as the
195 C<HvNAME> of the given HV (which would presumably represent a stash).
201 Perl_sv_derived_from_hv(pTHX_ SV *sv, HV *hv)
203 PERL_ARGS_ASSERT_SV_DERIVED_FROM_HV;
205 const char *hvname = HvNAME(hv);
209 return sv_derived_from_svpvn(sv, NULL, hvname, HvNAMELEN(hv), HvNAMEUTF8(hv) ? SVf_UTF8 : 0);
213 =for apidoc sv_isa_sv
215 Returns a boolean indicating whether the SV is an object reference and is
216 derived from the specified class, respecting any C<isa()> method overloading
217 it may have. Returns false if C<sv> is not a reference to an object, or is
218 not derived from the specified class.
220 This is the function used to implement the behaviour of the C<isa> operator.
222 Does not invoke magic on C<sv>.
224 Not to be confused with the older C<sv_isa> function, which does not use an
225 overloaded C<isa()> method, nor will check subclassing.
232 Perl_sv_isa_sv(pTHX_ SV *sv, SV *namesv)
236 PERL_ARGS_ASSERT_SV_ISA_SV;
238 if(!SvROK(sv) || !SvOBJECT(SvRV(sv)))
241 isagv = gv_fetchmeth_pvn(SvSTASH(SvRV(sv)), "isa", 3, -1, GV_NOUNIVERSAL);
244 CV *isacv = isGV(isagv) ? GvCV(isagv) : (CV *)isagv;
259 call_sv((SV *)isacv, G_SCALAR);
272 /* TODO: Support namesv being an HV ref to the stash directly? */
274 return sv_derived_from_sv(sv, namesv, 0);
278 =for apidoc sv_does_sv
280 Returns a boolean indicating whether the SV performs a specific, named role.
281 The SV can be a Perl object or the name of a Perl class.
289 Perl_sv_does_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
296 PERL_ARGS_ASSERT_SV_DOES_SV;
297 PERL_UNUSED_ARG(flags);
304 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) {
309 if (SvROK(sv) && SvOBJECT(SvRV(sv))) {
310 classname = sv_ref(NULL,SvRV(sv),TRUE);
315 if (sv_eq(classname, namesv)) {
326 /* create a PV with value "isa", but with a special address
327 * so that perl knows we're really doing "DOES" instead */
328 methodname = newSV_type_mortal(SVt_PV);
329 SvLEN_set(methodname, 0);
330 SvCUR_set(methodname, strlen(PL_isa_DOES));
331 SvPVX(methodname) = (char *)PL_isa_DOES; /* discard 'const' qualifier */
332 SvPOK_on(methodname);
333 call_sv(methodname, G_SCALAR | G_METHOD);
336 does_it = SvTRUE_NN( TOPs );
346 Like L</sv_does_pv>, but doesn't take a C<flags> parameter.
352 Perl_sv_does(pTHX_ SV *sv, const char *const name)
354 PERL_ARGS_ASSERT_SV_DOES;
355 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP), 0);
359 =for apidoc sv_does_pv
361 Like L</sv_does_sv>, but takes a nul-terminated string instead of an SV.
368 Perl_sv_does_pv(pTHX_ SV *sv, const char *const name, U32 flags)
370 PERL_ARGS_ASSERT_SV_DOES_PV;
371 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP | flags), flags);
375 =for apidoc sv_does_pvn
377 Like L</sv_does_sv>, but takes a string/length pair instead of an SV.
383 Perl_sv_does_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
385 PERL_ARGS_ASSERT_SV_DOES_PVN;
387 return sv_does_sv(sv, newSVpvn_flags(name, len, flags | SVs_TEMP), flags);
391 =for apidoc croak_xs_usage
393 A specialised variant of C<croak()> for emitting the usage message for xsubs
395 croak_xs_usage(cv, "eee_yow");
397 works out the package name and subroutine name from C<cv>, and then calls
398 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
400 Perl_croak(aTHX_ "Usage: %" SVf "::%" SVf "(%s)", "ouch" "awk",
407 Perl_croak_xs_usage(const CV *const cv, const char *const params)
409 /* Avoid CvGV as it requires aTHX. */
410 const GV *gv = CvNAMED(cv) ? NULL : cv->sv_any->xcv_gv_u.xcv_gv;
412 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
415 const HV *const stash = GvSTASH(gv);
417 if (HvNAME_get(stash))
418 /* diag_listed_as: SKIPME */
419 Perl_croak_nocontext("Usage: %" HEKf "::%" HEKf "(%s)",
420 HEKfARG(HvNAME_HEK(stash)),
421 HEKfARG(GvNAME_HEK(gv)),
424 /* diag_listed_as: SKIPME */
425 Perl_croak_nocontext("Usage: %" HEKf "(%s)",
426 HEKfARG(GvNAME_HEK(gv)), params);
429 if ((gv = CvGV(cv))) goto got_gv;
431 /* Pants. I don't think that it should be possible to get here. */
432 /* diag_listed_as: SKIPME */
433 Perl_croak(aTHX_ "Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
437 XS(XS_UNIVERSAL_isa); /* prototype to pass -Wmissing-prototypes */
443 croak_xs_usage(cv, "reference, kind");
445 SV * const sv = ST(0);
449 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
452 ST(0) = boolSV(sv_derived_from_sv(sv, ST(1), 0));
457 XS(XS_UNIVERSAL_can); /* prototype to pass -Wmissing-prototypes */
467 croak_xs_usage(cv, "object-ref, method");
473 /* Reject undef and empty string. Note that the string form takes
474 precedence here over the numeric form, as (!1)->foo treats the
475 invocant as the empty string, though it is a dualvar. */
476 if (!SvOK(sv) || (SvPOK(sv) && !SvCUR(sv)))
482 sv = MUTABLE_SV(SvRV(sv));
485 else if (isGV_with_GP(sv) && GvIO(sv))
486 pkg = SvSTASH(GvIO(sv));
488 else if (isGV_with_GP(sv) && GvIO(sv))
489 pkg = SvSTASH(GvIO(sv));
490 else if ((iogv = gv_fetchsv_nomg(sv, 0, SVt_PVIO)) && GvIO(iogv))
491 pkg = SvSTASH(GvIO(iogv));
493 pkg = gv_stashsv(sv, 0);
495 pkg = gv_stashpvs("UNIVERSAL", 0);
499 GV * const gv = gv_fetchmethod_sv_flags(pkg, ST(1), 0);
501 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
508 XS(XS_UNIVERSAL_DOES); /* prototype to pass -Wmissing-prototypes */
509 XS(XS_UNIVERSAL_DOES)
515 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
517 SV * const sv = ST(0);
518 if (sv_does_sv( sv, ST(1), 0 ))
525 XS(XS_utf8_is_utf8); /* prototype to pass -Wmissing-prototypes */
530 croak_xs_usage(cv, "sv");
532 SV * const sv = ST(0);
542 XS(XS_utf8_valid); /* prototype to pass -Wmissing-prototypes */
547 croak_xs_usage(cv, "sv");
549 SV * const sv = ST(0);
551 const char * const s = SvPV_const(sv,len);
552 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
560 XS(XS_utf8_encode); /* prototype to pass -Wmissing-prototypes */
565 croak_xs_usage(cv, "sv");
566 sv_utf8_encode(ST(0));
571 XS(XS_utf8_decode); /* prototype to pass -Wmissing-prototypes */
576 croak_xs_usage(cv, "sv");
578 SV * const sv = ST(0);
580 SvPV_force_nolen(sv);
581 RETVAL = sv_utf8_decode(sv);
583 ST(0) = boolSV(RETVAL);
588 XS(XS_utf8_upgrade); /* prototype to pass -Wmissing-prototypes */
593 croak_xs_usage(cv, "sv");
595 SV * const sv = ST(0);
600 if (UNLIKELY(! sv)) {
605 if (UNLIKELY(! SvOK(sv))) {
609 RETVAL = sv_utf8_upgrade_nomg(sv);
615 XS(XS_utf8_downgrade); /* prototype to pass -Wmissing-prototypes */
616 XS(XS_utf8_downgrade)
619 if (items < 1 || items > 2)
620 croak_xs_usage(cv, "sv, failok=0");
622 SV * const sv0 = ST(0);
623 SV * const sv1 = ST(1);
624 const bool failok = (items < 2) ? 0 : SvTRUE_NN(sv1) ? 1 : 0;
625 const bool RETVAL = sv_utf8_downgrade(sv0, failok);
627 ST(0) = boolSV(RETVAL);
632 XS(XS_utf8_native_to_unicode); /* prototype to pass -Wmissing-prototypes */
633 XS(XS_utf8_native_to_unicode)
636 const UV uv = SvUV(ST(0));
639 croak_xs_usage(cv, "sv");
641 ST(0) = sv_2mortal(newSVuv(NATIVE_TO_UNI(uv)));
645 XS(XS_utf8_unicode_to_native); /* prototype to pass -Wmissing-prototypes */
646 XS(XS_utf8_unicode_to_native)
649 const UV uv = SvUV(ST(0));
652 croak_xs_usage(cv, "sv");
654 ST(0) = sv_2mortal(newSVuv(UNI_TO_NATIVE(uv)));
658 XS(XS_Internals_SvREADONLY); /* prototype to pass -Wmissing-prototypes */
659 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
662 SV * const svz = ST(0);
665 /* [perl #77776] - called as &foo() not foo() */
667 croak_xs_usage(cv, "SCALAR[, ON]");
677 else if (items == 2) {
679 if (SvTRUE_NN(sv1)) {
680 SvFLAGS(sv) |= SVf_READONLY;
684 /* I hope you really know what you are doing. */
685 SvFLAGS(sv) &=~ SVf_READONLY;
689 XSRETURN_UNDEF; /* Can't happen. */
692 XS(XS_constant__make_const); /* prototype to pass -Wmissing-prototypes */
693 XS(XS_constant__make_const) /* This is dangerous stuff. */
696 SV * const svz = ST(0);
699 /* [perl #77776] - called as &foo() not foo() */
700 if (!SvROK(svz) || items != 1)
701 croak_xs_usage(cv, "SCALAR");
706 if (SvTYPE(sv) == SVt_PVAV && AvFILLp(sv) != -1) {
707 /* for constant.pm; nobody else should be calling this
710 for (svp = AvARRAY(sv) + AvFILLp(sv)
713 if (*svp) SvPADTMP_on(*svp);
718 XS(XS_Internals_SvREFCNT); /* prototype to pass -Wmissing-prototypes */
719 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
722 SV * const svz = ST(0);
726 /* [perl #77776] - called as &foo() not foo() */
727 if ((items != 1 && items != 2) || !SvROK(svz))
728 croak_xs_usage(cv, "SCALAR[, REFCOUNT]");
732 /* I hope you really know what you are doing. */
733 /* idea is for SvREFCNT(sv) to be accessed only once */
734 refcnt = items == 2 ?
735 /* we free one ref on exit */
736 (SvREFCNT(sv) = SvUV(ST(1)) + 1)
738 XSRETURN_UV(refcnt - 1); /* Minus the ref created for us. */
742 XS(XS_Internals_hv_clear_placehold); /* prototype to pass -Wmissing-prototypes */
743 XS(XS_Internals_hv_clear_placehold)
747 if (items != 1 || !SvROK(ST(0)))
748 croak_xs_usage(cv, "hv");
750 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
751 hv_clear_placeholders(hv);
756 XS(XS_PerlIO_get_layers); /* prototype to pass -Wmissing-prototypes */
757 XS(XS_PerlIO_get_layers)
760 if (items < 1 || items % 2 == 0)
761 croak_xs_usage(cv, "filehandle[,args]");
762 #if defined(USE_PERLIO)
768 bool details = FALSE;
772 for (svp = MARK + 2; svp <= SP; svp += 2) {
773 SV * const * const varp = svp;
774 SV * const * const valp = svp + 1;
776 const char * const key = SvPV_const(*varp, klen);
780 if (memEQs(key, klen, "input")) {
781 input = SvTRUE(*valp);
786 if (memEQs(key, klen, "output")) {
787 input = !SvTRUE(*valp);
792 if (memEQs(key, klen, "details")) {
793 details = SvTRUE(*valp);
800 "get_layers: unknown argument '%s'",
810 /* MAYBE_DEREF_GV will call get magic */
811 if ((gv = MAYBE_DEREF_GV(sv)))
813 else if (SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVIO)
815 else if (!SvROK(sv) && (gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO)))
819 AV* const av = PerlIO_get_layers(aTHX_ input ?
820 IoIFP(io) : IoOFP(io));
822 const SSize_t last = av_top_index(av);
825 for (i = last; i >= 0; i -= 3) {
826 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
827 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
828 SV * const * const flgsvp = av_fetch(av, i, FALSE);
830 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
831 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
832 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
834 EXTEND(SP, 3); /* Three is the max in all branches: better check just once */
836 /* Indents of 5? Yuck. */
837 /* We know that PerlIO_get_layers creates a new SV for
838 the name and flags, so we can just take a reference
839 and "steal" it when we free the AV below. */
841 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
844 ? newSVpvn_flags(SvPVX_const(*argsvp),
846 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
850 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
856 PUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%" SVf "(%" SVf ")",
860 PUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
865 const IV flags = SvIVX(*flgsvp);
867 if (flags & PERLIO_F_UTF8) {
868 PUSHs(newSVpvs_flags("utf8", SVs_TEMP));
885 XS(XS_re_is_regexp); /* prototype to pass -Wmissing-prototypes */
891 croak_xs_usage(cv, "sv");
900 XS(XS_re_regnames_count); /* prototype to pass -Wmissing-prototypes */
901 XS(XS_re_regnames_count)
903 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
908 croak_xs_usage(cv, "");
913 ret = CALLREG_NAMED_BUFF_COUNT(rx);
916 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
920 XS(XS_re_regname); /* prototype to pass -Wmissing-prototypes */
928 if (items < 1 || items > 2)
929 croak_xs_usage(cv, "name[, all ]");
934 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
939 if (items == 2 && SvTRUE_NN(ST(1))) {
944 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
947 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
952 XS(XS_re_regnames); /* prototype to pass -Wmissing-prototypes */
965 croak_xs_usage(cv, "[all]");
967 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
972 if (items == 1 && SvTRUE_NN(ST(0))) {
981 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
988 av = MUTABLE_AV(SvRV(ret));
989 length = av_count(av);
991 EXTEND(SP, length); /* better extend stack just once */
992 for (i = 0; i < length; i++) {
993 entry = av_fetch(av, i, FALSE);
996 Perl_croak(aTHX_ "NULL array element in re::regnames()");
998 mPUSHs(SvREFCNT_inc_simple_NN(*entry));
1007 XS(XS_re_regexp_pattern); /* prototype to pass -Wmissing-prototypes */
1008 XS(XS_re_regexp_pattern)
1012 U8 const gimme = GIMME_V;
1017 croak_xs_usage(cv, "sv");
1020 Checks if a reference is a regex or not. If the parameter is
1021 not a ref, or is not the result of a qr// then returns false
1022 in scalar context and an empty list in list context.
1023 Otherwise in list context it returns the pattern and the
1024 modifiers, in scalar context it returns the pattern just as it
1025 would if the qr// was stringified normally, regardless as
1026 to the class of the variable and any stringification overloads
1030 if ((re = SvRX(ST(0)))) /* assign deliberate */
1032 /* Houston, we have a regex! */
1035 if ( gimme == G_LIST ) {
1037 char reflags[sizeof(INT_PAT_MODS) + MAX_CHARSET_NAME_LENGTH];
1043 we are in list context so stringify
1044 the modifiers that apply. We ignore "negative
1045 modifiers" in this scenario, and the default character set
1048 if (get_regex_charset(RX_EXTFLAGS(re)) != REGEX_DEPENDS_CHARSET) {
1050 const char* const name = get_regex_charset_name(RX_EXTFLAGS(re),
1052 Copy(name, reflags + left, len, char);
1055 fptr = INT_PAT_MODS;
1056 match_flags = (U16)((RX_EXTFLAGS(re) & RXf_PMf_COMPILETIME)
1057 >> RXf_PMf_STD_PMMOD_SHIFT);
1059 while((ch = *fptr++)) {
1060 if(match_flags & 1) {
1061 reflags[left++] = ch;
1066 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
1067 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
1069 /* return the pattern and the modifiers */
1071 PUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
1074 /* Scalar, so use the string that Perl would return */
1075 /* return the pattern in (?msixn:..) format */
1076 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
1081 /* It ain't a regexp folks */
1082 if ( gimme == G_LIST ) {
1083 /* return the empty list */
1086 /* Because of the (?:..) wrapping involved in a
1087 stringified pattern it is impossible to get a
1088 result for a real regexp that would evaluate to
1089 false. Therefore we can return PL_sv_no to signify
1090 that the object is not a regex, this means that one
1093 if (regex($might_be_a_regex) eq '(?:foo)') { }
1095 and not worry about undefined values.
1100 NOT_REACHED; /* NOTREACHED */
1103 #if defined(HAS_GETCWD) && defined(PERL_IS_MINIPERL)
1105 XS(XS_Internals_getcwd)
1108 SV *sv = sv_newmortal();
1111 croak_xs_usage(cv, "");
1113 (void)getcwd_sv(sv);
1122 XS(XS_NamedCapture_tie_it)
1127 croak_xs_usage(cv, "sv");
1130 GV * const gv = (GV *)sv;
1131 HV * const hv = GvHVn(gv);
1132 SV *rv = newSV_type(SVt_IV);
1133 const char *gv_name = GvNAME(gv);
1135 sv_setrv_noinc(rv, newSVuv(
1136 strEQ(gv_name, "-") || strEQ(gv_name, "\003APTURE_ALL")
1137 ? RXapif_ALL : RXapif_ONE));
1138 sv_bless(rv, GvSTASH(CvGV(cv)));
1140 sv_unmagic((SV *)hv, PERL_MAGIC_tied);
1141 sv_magic((SV *)hv, rv, PERL_MAGIC_tied, NULL, 0);
1142 SvREFCNT_dec(rv); /* As sv_magic increased it by one. */
1147 XS(XS_NamedCapture_TIEHASH)
1151 croak_xs_usage(cv, "package, ...");
1153 const char * package = (const char *)SvPV_nolen(ST(0));
1154 UV flag = RXapif_ONE;
1158 const char *p = SvPV_const(*mark, len);
1159 if(memEQs(p, len, "all"))
1160 flag = SvTRUE(mark[1]) ? RXapif_ALL : RXapif_ONE;
1163 ST(0) = newSV_type_mortal(SVt_IV);
1164 sv_setuv(newSVrv(ST(0), package), flag);
1169 /* These are tightly coupled to the RXapif_* flags defined in regexp.h */
1170 #define UNDEF_FATAL 0x80000
1171 #define DISCARD 0x40000
1172 #define EXPECT_SHIFT 24
1173 #define ACTION_MASK 0x000FF
1175 #define FETCH_ALIAS (RXapif_FETCH | (2 << EXPECT_SHIFT))
1176 #define STORE_ALIAS (RXapif_STORE | (3 << EXPECT_SHIFT) | UNDEF_FATAL | DISCARD)
1177 #define DELETE_ALIAS (RXapif_DELETE | (2 << EXPECT_SHIFT) | UNDEF_FATAL)
1178 #define CLEAR_ALIAS (RXapif_CLEAR | (1 << EXPECT_SHIFT) | UNDEF_FATAL | DISCARD)
1179 #define EXISTS_ALIAS (RXapif_EXISTS | (2 << EXPECT_SHIFT))
1180 #define SCALAR_ALIAS (RXapif_SCALAR | (1 << EXPECT_SHIFT))
1182 XS(XS_NamedCapture_FETCH)
1186 PERL_UNUSED_VAR(cv); /* -W */
1187 PERL_UNUSED_VAR(ax); /* -Wall */
1190 REGEXP *const rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1193 const U32 action = ix & ACTION_MASK;
1194 const int expect = ix >> EXPECT_SHIFT;
1195 if (items != expect)
1196 croak_xs_usage(cv, expect == 2 ? "$key"
1197 : (expect == 3 ? "$key, $value"
1200 if (!rx || !SvROK(ST(0))) {
1201 if (ix & UNDEF_FATAL)
1202 Perl_croak_no_modify();
1207 flags = (U32)SvUV(SvRV(MUTABLE_SV(ST(0))));
1210 ret = RX_ENGINE(rx)->named_buff(aTHX_ (rx), expect >= 2 ? ST(1) : NULL,
1211 expect >= 3 ? ST(2) : NULL, flags | action);
1215 /* Called with G_DISCARD, so our return stack state is thrown away.
1216 Hence if we were returned anything, free it immediately. */
1219 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
1227 XS(XS_NamedCapture_FIRSTKEY)
1231 PERL_UNUSED_VAR(cv); /* -W */
1232 PERL_UNUSED_VAR(ax); /* -Wall */
1235 REGEXP *const rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
1238 const int expect = ix ? 2 : 1;
1239 const U32 action = ix ? RXapif_NEXTKEY : RXapif_FIRSTKEY;
1240 if (items != expect)
1241 croak_xs_usage(cv, expect == 2 ? "$lastkey" : "");
1243 if (!rx || !SvROK(ST(0)))
1246 flags = (U32)SvUV(SvRV(MUTABLE_SV(ST(0))));
1249 ret = RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx),
1250 expect >= 2 ? ST(1) : NULL,
1254 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
1260 /* is this still needed? */
1261 XS(XS_NamedCapture_flags)
1264 PERL_UNUSED_VAR(cv); /* -W */
1265 PERL_UNUSED_VAR(ax); /* -Wall */
1279 struct xsub_details {
1286 static const struct xsub_details these_details[] = {
1287 {"UNIVERSAL::isa", XS_UNIVERSAL_isa, NULL, 0 },
1288 {"UNIVERSAL::can", XS_UNIVERSAL_can, NULL, 0 },
1289 {"UNIVERSAL::DOES", XS_UNIVERSAL_DOES, NULL, 0 },
1290 #define VXS_XSUB_DETAILS
1292 #undef VXS_XSUB_DETAILS
1293 {"utf8::is_utf8", XS_utf8_is_utf8, NULL, 0 },
1294 {"utf8::valid", XS_utf8_valid, NULL, 0 },
1295 {"utf8::encode", XS_utf8_encode, NULL, 0 },
1296 {"utf8::decode", XS_utf8_decode, NULL, 0 },
1297 {"utf8::upgrade", XS_utf8_upgrade, NULL, 0 },
1298 {"utf8::downgrade", XS_utf8_downgrade, NULL, 0 },
1299 {"utf8::native_to_unicode", XS_utf8_native_to_unicode, NULL, 0 },
1300 {"utf8::unicode_to_native", XS_utf8_unicode_to_native, NULL, 0 },
1301 {"Internals::SvREADONLY", XS_Internals_SvREADONLY, "\\[$%@];$", 0 },
1302 {"Internals::SvREFCNT", XS_Internals_SvREFCNT, "\\[$%@];$", 0 },
1303 {"Internals::hv_clear_placeholders", XS_Internals_hv_clear_placehold, "\\%", 0 },
1304 {"constant::_make_const", XS_constant__make_const, "\\[$@]", 0 },
1305 {"PerlIO::get_layers", XS_PerlIO_get_layers, "*;@", 0 },
1306 {"re::is_regexp", XS_re_is_regexp, "$", 0 },
1307 {"re::regname", XS_re_regname, ";$$", 0 },
1308 {"re::regnames", XS_re_regnames, ";$", 0 },
1309 {"re::regnames_count", XS_re_regnames_count, "", 0 },
1310 {"re::regexp_pattern", XS_re_regexp_pattern, "$", 0 },
1311 #if defined(HAS_GETCWD) && defined(PERL_IS_MINIPERL)
1312 {"Internals::getcwd", XS_Internals_getcwd, "", 0 },
1314 {"Tie::Hash::NamedCapture::_tie_it", XS_NamedCapture_tie_it, NULL, 0 },
1315 {"Tie::Hash::NamedCapture::TIEHASH", XS_NamedCapture_TIEHASH, NULL, 0 },
1316 {"Tie::Hash::NamedCapture::FETCH", XS_NamedCapture_FETCH, NULL, FETCH_ALIAS },
1317 {"Tie::Hash::NamedCapture::STORE", XS_NamedCapture_FETCH, NULL, STORE_ALIAS },
1318 {"Tie::Hash::NamedCapture::DELETE", XS_NamedCapture_FETCH, NULL, DELETE_ALIAS },
1319 {"Tie::Hash::NamedCapture::CLEAR", XS_NamedCapture_FETCH, NULL, CLEAR_ALIAS },
1320 {"Tie::Hash::NamedCapture::EXISTS", XS_NamedCapture_FETCH, NULL, EXISTS_ALIAS },
1321 {"Tie::Hash::NamedCapture::SCALAR", XS_NamedCapture_FETCH, NULL, SCALAR_ALIAS },
1322 {"Tie::Hash::NamedCapture::FIRSTKEY", XS_NamedCapture_FIRSTKEY, NULL, 0 },
1323 {"Tie::Hash::NamedCapture::NEXTKEY", XS_NamedCapture_FIRSTKEY, NULL, 1 },
1324 {"Tie::Hash::NamedCapture::flags", XS_NamedCapture_flags, NULL, 0 },
1328 optimize_out_native_convert_function(pTHX_ OP* entersubop,
1332 /* Optimizes out an identity function, i.e., one that just returns its
1333 * argument. The passed in function is assumed to be an identity function,
1334 * with no checking. This is designed to be called for utf8_to_native()
1335 * and native_to_utf8() on ASCII platforms, as they just return their
1336 * arguments, but it could work on any such function.
1338 * The code is mostly just cargo-culted from Memoize::Lift */
1342 SV* prototype = newSVpvs("$");
1344 PERL_UNUSED_ARG(protosv);
1346 assert(entersubop->op_type == OP_ENTERSUB);
1348 entersubop = ck_entersub_args_proto(entersubop, namegv, prototype);
1349 parent = entersubop;
1351 SvREFCNT_dec(prototype);
1353 pushop = cUNOPx(entersubop)->op_first;
1354 if (! OpHAS_SIBLING(pushop)) {
1356 pushop = cUNOPx(pushop)->op_first;
1358 argop = OpSIBLING(pushop);
1360 /* Carry on without doing the optimization if it is not something we're
1361 * expecting, so continues to work */
1363 || ! OpHAS_SIBLING(argop)
1364 || OpHAS_SIBLING(OpSIBLING(argop))
1369 /* cut argop from the subtree */
1370 (void)op_sibling_splice(parent, pushop, 1, NULL);
1372 op_free(entersubop);
1377 Perl_boot_core_UNIVERSAL(pTHX)
1379 static const char file[] = __FILE__;
1380 const struct xsub_details *xsub = these_details;
1381 const struct xsub_details *end = C_ARRAY_END(these_details);
1384 CV *cv = newXS_flags(xsub->name, xsub->xsub, file, xsub->proto, 0);
1385 XSANY.any_i32 = xsub->ix;
1386 } while (++xsub < end);
1389 { /* On ASCII platforms these functions just return their argument, so can
1390 be optimized away */
1392 CV* to_native_cv = get_cv("utf8::unicode_to_native", 0);
1393 CV* to_unicode_cv = get_cv("utf8::native_to_unicode", 0);
1395 cv_set_call_checker_flags(to_native_cv,
1396 optimize_out_native_convert_function,
1397 (SV*) to_native_cv, 0);
1398 cv_set_call_checker_flags(to_unicode_cv,
1399 optimize_out_native_convert_function,
1400 (SV*) to_unicode_cv, 0);
1404 /* Providing a Regexp::DESTROY fixes #21347. See test in t/op/ref.t */
1407 newCONSTSUB(get_hv("Regexp::", GV_ADD), "DESTROY", NULL);
1408 char ** cvfile = &CvFILE(cv);
1409 char * oldfile = *cvfile;
1411 *cvfile = (char *)file;
1417 * ex: set ts=8 sts=4 sw=4 et: