3 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 * 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * '"The roots of those mountains must be roots indeed; there must be
13 * great secrets buried there which have not been discovered since the
14 * beginning."' --Gandalf, relating Gollum's history
16 * [p.54 of _The Lord of the Rings_, I/ii: "The Shadow of the Past"]
19 /* This file contains the code that implements the functions in Perl's
20 * UNIVERSAL package, such as UNIVERSAL->can().
22 * It is also used to store XS functions that need to be present in
23 * miniperl for a lack of a better place to put them. It might be
24 * clever to move them to separate XS files which would then be pulled
25 * in by some to-be-written build process.
29 #define PERL_IN_UNIVERSAL_C
32 #if defined(USE_PERLIO)
33 #include "perliol.h" /* For the PERLIO_F_XXX */
37 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
38 * The main guts of traverse_isa was actually copied from gv_fetchmeth
42 S_isa_lookup(pTHX_ HV *stash, const char * const name, STRLEN len, U32 flags)
44 const struct mro_meta *const meta = HvMROMETA(stash);
48 PERL_ARGS_ASSERT_ISA_LOOKUP;
51 (void)mro_get_linear_isa(stash);
55 if (hv_common(isa, NULL, name, len, ( flags & SVf_UTF8 ? HVhek_UTF8 : 0),
56 HV_FETCH_ISEXISTS, NULL, 0)) {
57 /* Direct name lookup worked. */
61 /* A stash/class can go by many names (ie. User == main::User), so
62 we use the HvENAME in the stash itself, which is canonical, falling
63 back to HvNAME if necessary. */
64 our_stash = gv_stashpvn(name, len, flags);
67 HEK *canon_name = HvENAME_HEK(our_stash);
68 if (!canon_name) canon_name = HvNAME_HEK(our_stash);
70 if (hv_common(isa, NULL, HEK_KEY(canon_name), HEK_LEN(canon_name),
71 HEK_FLAGS(canon_name),
72 HV_FETCH_ISEXISTS, NULL, HEK_HASH(canon_name))) {
81 =head1 SV Manipulation Functions
83 =for apidoc sv_derived_from_pvn
85 Returns a boolean indicating whether the SV is derived from the specified class
86 I<at the C level>. To check derivation at the Perl level, call C<isa()> as a
89 Currently, the only significant value for C<flags> is SVf_UTF8.
93 =for apidoc sv_derived_from_sv
95 Exactly like L</sv_derived_from_pvn>, but takes the name string in the form
96 of an SV instead of a string/length pair.
103 Perl_sv_derived_from_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
107 PERL_ARGS_ASSERT_SV_DERIVED_FROM_SV;
108 namepv = SvPV(namesv, namelen);
111 return sv_derived_from_pvn(sv, namepv, namelen, flags);
115 =for apidoc sv_derived_from
117 Exactly like L</sv_derived_from_pv>, but doesn't take a C<flags> parameter.
123 Perl_sv_derived_from(pTHX_ SV *sv, const char *const name)
125 PERL_ARGS_ASSERT_SV_DERIVED_FROM;
126 return sv_derived_from_pvn(sv, name, strlen(name), 0);
130 =for apidoc sv_derived_from_pv
132 Exactly like L</sv_derived_from_pvn>, but takes a nul-terminated string
133 instead of a string/length pair.
140 Perl_sv_derived_from_pv(pTHX_ SV *sv, const char *const name, U32 flags)
142 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PV;
143 return sv_derived_from_pvn(sv, name, strlen(name), flags);
147 Perl_sv_derived_from_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
151 PERL_ARGS_ASSERT_SV_DERIVED_FROM_PVN;
158 type = sv_reftype(sv,0);
159 if (type && strEQ(type,name))
166 stash = gv_stashsv(sv, 0);
169 if (stash && isa_lookup(stash, name, len, flags))
172 stash = gv_stashpvs("UNIVERSAL", 0);
173 return stash && isa_lookup(stash, name, len, flags);
177 =for apidoc sv_does_sv
179 Returns a boolean indicating whether the SV performs a specific, named role.
180 The SV can be a Perl object or the name of a Perl class.
188 Perl_sv_does_sv(pTHX_ SV *sv, SV *namesv, U32 flags)
195 PERL_ARGS_ASSERT_SV_DOES_SV;
196 PERL_UNUSED_ARG(flags);
203 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv)))) {
208 if (sv_isobject(sv)) {
209 classname = sv_ref(NULL,SvRV(sv),TRUE);
214 if (sv_eq(classname, namesv)) {
225 methodname = newSVpvs_flags("isa", SVs_TEMP);
226 /* ugly hack: use the SvSCREAM flag so S_method_common
227 * can figure out we're calling DOES() and not isa(),
228 * and report eventual errors correctly. --rgs */
229 SvSCREAM_on(methodname);
230 call_sv(methodname, G_SCALAR | G_METHOD);
233 does_it = SvTRUE( TOPs );
243 Like L</sv_does_pv>, but doesn't take a C<flags> parameter.
249 Perl_sv_does(pTHX_ SV *sv, const char *const name)
251 PERL_ARGS_ASSERT_SV_DOES;
252 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP), 0);
256 =for apidoc sv_does_pv
258 Like L</sv_does_sv>, but takes a nul-terminated string instead of an SV.
265 Perl_sv_does_pv(pTHX_ SV *sv, const char *const name, U32 flags)
267 PERL_ARGS_ASSERT_SV_DOES_PV;
268 return sv_does_sv(sv, newSVpvn_flags(name, strlen(name), SVs_TEMP | flags), flags);
272 =for apidoc sv_does_pvn
274 Like L</sv_does_sv>, but takes a string/length pair instead of an SV.
280 Perl_sv_does_pvn(pTHX_ SV *sv, const char *const name, const STRLEN len, U32 flags)
282 PERL_ARGS_ASSERT_SV_DOES_PVN;
284 return sv_does_sv(sv, newSVpvn_flags(name, len, flags | SVs_TEMP), flags);
288 =for apidoc croak_xs_usage
290 A specialised variant of C<croak()> for emitting the usage message for xsubs
292 croak_xs_usage(cv, "eee_yow");
294 works out the package name and subroutine name from C<cv>, and then calls
295 C<croak()>. Hence if C<cv> is C<&ouch::awk>, it would call C<croak> as:
297 Perl_croak(aTHX_ "Usage: %"SVf"::%"SVf"(%s)", "ouch" "awk", "eee_yow");
303 Perl_croak_xs_usage(const CV *const cv, const char *const params)
305 const GV *const gv = CvGV(cv);
307 PERL_ARGS_ASSERT_CROAK_XS_USAGE;
310 const HV *const stash = GvSTASH(gv);
312 if (HvNAME_get(stash))
313 /* diag_listed_as: SKIPME */
314 Perl_croak_nocontext("Usage: %"HEKf"::%"HEKf"(%s)",
315 HEKfARG(HvNAME_HEK(stash)),
316 HEKfARG(GvNAME_HEK(gv)),
319 /* diag_listed_as: SKIPME */
320 Perl_croak_nocontext("Usage: %"HEKf"(%s)",
321 HEKfARG(GvNAME_HEK(gv)), params);
323 /* Pants. I don't think that it should be possible to get here. */
324 /* diag_listed_as: SKIPME */
325 Perl_croak_nocontext("Usage: CODE(0x%"UVxf")(%s)", PTR2UV(cv), params);
329 XS(XS_UNIVERSAL_isa); /* prototype to pass -Wmissing-prototypes */
335 croak_xs_usage(cv, "reference, kind");
337 SV * const sv = ST(0);
341 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
344 ST(0) = boolSV(sv_derived_from_sv(sv, ST(1), 0));
349 XS(XS_UNIVERSAL_can); /* prototype to pass -Wmissing-prototypes */
359 croak_xs_usage(cv, "object-ref, method");
365 /* Reject undef and empty string. Note that the string form takes
366 precedence here over the numeric form, as (!1)->foo treats the
367 invocant as the empty string, though it is a dualvar. */
368 if (!SvOK(sv) || (SvPOK(sv) && !SvCUR(sv)))
374 sv = MUTABLE_SV(SvRV(sv));
377 else if (isGV_with_GP(sv) && GvIO(sv))
378 pkg = SvSTASH(GvIO(sv));
380 else if (isGV_with_GP(sv) && GvIO(sv))
381 pkg = SvSTASH(GvIO(sv));
382 else if ((iogv = gv_fetchsv_nomg(sv, 0, SVt_PVIO)) && GvIO(iogv))
383 pkg = SvSTASH(GvIO(iogv));
385 pkg = gv_stashsv(sv, 0);
387 pkg = gv_stashpvs("UNIVERSAL", 0);
391 GV * const gv = gv_fetchmethod_sv_flags(pkg, ST(1), 0);
393 rv = sv_2mortal(newRV(MUTABLE_SV(GvCV(gv))));
400 XS(XS_UNIVERSAL_DOES); /* prototype to pass -Wmissing-prototypes */
401 XS(XS_UNIVERSAL_DOES)
407 Perl_croak(aTHX_ "Usage: invocant->DOES(kind)");
409 SV * const sv = ST(0);
410 if (sv_does_sv( sv, ST(1), 0 ))
417 XS(XS_utf8_is_utf8); /* prototype to pass -Wmissing-prototypes */
422 croak_xs_usage(cv, "sv");
424 SV * const sv = ST(0);
434 XS(XS_utf8_valid); /* prototype to pass -Wmissing-prototypes */
439 croak_xs_usage(cv, "sv");
441 SV * const sv = ST(0);
443 const char * const s = SvPV_const(sv,len);
444 if (!SvUTF8(sv) || is_utf8_string((const U8*)s,len))
452 XS(XS_utf8_encode); /* prototype to pass -Wmissing-prototypes */
457 croak_xs_usage(cv, "sv");
458 sv_utf8_encode(ST(0));
463 XS(XS_utf8_decode); /* prototype to pass -Wmissing-prototypes */
468 croak_xs_usage(cv, "sv");
470 SV * const sv = ST(0);
472 SvPV_force_nolen(sv);
473 RETVAL = sv_utf8_decode(sv);
475 ST(0) = boolSV(RETVAL);
480 XS(XS_utf8_upgrade); /* prototype to pass -Wmissing-prototypes */
485 croak_xs_usage(cv, "sv");
487 SV * const sv = ST(0);
491 RETVAL = sv_utf8_upgrade(sv);
492 XSprePUSH; PUSHi((IV)RETVAL);
497 XS(XS_utf8_downgrade); /* prototype to pass -Wmissing-prototypes */
498 XS(XS_utf8_downgrade)
501 if (items < 1 || items > 2)
502 croak_xs_usage(cv, "sv, failok=0");
504 SV * const sv = ST(0);
505 const bool failok = (items < 2) ? 0 : SvTRUE(ST(1)) ? 1 : 0;
506 const bool RETVAL = sv_utf8_downgrade(sv, failok);
508 ST(0) = boolSV(RETVAL);
513 XS(XS_utf8_native_to_unicode); /* prototype to pass -Wmissing-prototypes */
514 XS(XS_utf8_native_to_unicode)
517 const UV uv = SvUV(ST(0));
520 croak_xs_usage(cv, "sv");
522 ST(0) = sv_2mortal(newSViv(NATIVE_TO_UNI(uv)));
526 XS(XS_utf8_unicode_to_native); /* prototype to pass -Wmissing-prototypes */
527 XS(XS_utf8_unicode_to_native)
530 const UV uv = SvUV(ST(0));
533 croak_xs_usage(cv, "sv");
535 ST(0) = sv_2mortal(newSViv(UNI_TO_NATIVE(uv)));
539 XS(XS_Internals_SvREADONLY); /* prototype to pass -Wmissing-prototypes */
540 XS(XS_Internals_SvREADONLY) /* This is dangerous stuff. */
543 SV * const svz = ST(0);
547 /* [perl #77776] - called as &foo() not foo() */
549 croak_xs_usage(cv, "SCALAR[, ON]");
559 else if (items == 2) {
561 #ifdef PERL_OLD_COPY_ON_WRITE
562 if (SvIsCOW(sv)) sv_force_normal(sv);
568 /* I hope you really know what you are doing. */
573 XSRETURN_UNDEF; /* Can't happen. */
576 XS(XS_constant__make_const); /* prototype to pass -Wmissing-prototypes */
577 XS(XS_constant__make_const) /* This is dangerous stuff. */
580 SV * const svz = ST(0);
584 /* [perl #77776] - called as &foo() not foo() */
585 if (!SvROK(svz) || items != 1)
586 croak_xs_usage(cv, "SCALAR");
590 #ifdef PERL_OLD_COPY_ON_WRITE
591 if (SvIsCOW(sv)) sv_force_normal(sv);
594 if (SvTYPE(sv) == SVt_PVAV && AvFILLp(sv) != -1) {
595 /* for constant.pm; nobody else should be calling this
598 for (svp = AvARRAY(sv) + AvFILLp(sv)
601 if (*svp) SvPADTMP_on(*svp);
606 XS(XS_Internals_SvREFCNT); /* prototype to pass -Wmissing-prototypes */
607 XS(XS_Internals_SvREFCNT) /* This is dangerous stuff. */
610 SV * const svz = ST(0);
615 /* [perl #77776] - called as &foo() not foo() */
616 if ((items != 1 && items != 2) || !SvROK(svz))
617 croak_xs_usage(cv, "SCALAR[, REFCOUNT]");
621 /* I hope you really know what you are doing. */
622 /* idea is for SvREFCNT(sv) to be accessed only once */
623 refcnt = items == 2 ?
624 /* we free one ref on exit */
625 (SvREFCNT(sv) = SvUV(ST(1)) + 1)
627 XSRETURN_UV(refcnt - 1); /* Minus the ref created for us. */
631 XS(XS_Internals_hv_clear_placehold); /* prototype to pass -Wmissing-prototypes */
632 XS(XS_Internals_hv_clear_placehold)
636 if (items != 1 || !SvROK(ST(0)))
637 croak_xs_usage(cv, "hv");
639 HV * const hv = MUTABLE_HV(SvRV(ST(0)));
640 hv_clear_placeholders(hv);
645 XS(XS_PerlIO_get_layers); /* prototype to pass -Wmissing-prototypes */
646 XS(XS_PerlIO_get_layers)
649 if (items < 1 || items % 2 == 0)
650 croak_xs_usage(cv, "filehandle[,args]");
651 #if defined(USE_PERLIO)
657 bool details = FALSE;
661 for (svp = MARK + 2; svp <= SP; svp += 2) {
662 SV * const * const varp = svp;
663 SV * const * const valp = svp + 1;
665 const char * const key = SvPV_const(*varp, klen);
669 if (klen == 5 && memEQ(key, "input", 5)) {
670 input = SvTRUE(*valp);
675 if (klen == 6 && memEQ(key, "output", 6)) {
676 input = !SvTRUE(*valp);
681 if (klen == 7 && memEQ(key, "details", 7)) {
682 details = SvTRUE(*valp);
689 "get_layers: unknown argument '%s'",
698 gv = MAYBE_DEREF_GV(sv);
700 if (!gv && !SvROK(sv))
701 gv = gv_fetchsv_nomg(sv, 0, SVt_PVIO);
703 if (gv && (io = GvIO(gv))) {
704 AV* const av = PerlIO_get_layers(aTHX_ input ?
705 IoIFP(io) : IoOFP(io));
707 const SSize_t last = av_tindex(av);
710 for (i = last; i >= 0; i -= 3) {
711 SV * const * const namsvp = av_fetch(av, i - 2, FALSE);
712 SV * const * const argsvp = av_fetch(av, i - 1, FALSE);
713 SV * const * const flgsvp = av_fetch(av, i, FALSE);
715 const bool namok = namsvp && *namsvp && SvPOK(*namsvp);
716 const bool argok = argsvp && *argsvp && SvPOK(*argsvp);
717 const bool flgok = flgsvp && *flgsvp && SvIOK(*flgsvp);
719 EXTEND(SP, 3); /* Three is the max in all branches: better check just once */
721 /* Indents of 5? Yuck. */
722 /* We know that PerlIO_get_layers creates a new SV for
723 the name and flags, so we can just take a reference
724 and "steal" it when we free the AV below. */
726 ? sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp))
729 ? newSVpvn_flags(SvPVX_const(*argsvp),
731 (SvUTF8(*argsvp) ? SVf_UTF8 : 0)
735 ? sv_2mortal(SvREFCNT_inc_simple_NN(*flgsvp))
741 PUSHs(sv_2mortal(Perl_newSVpvf(aTHX_ "%"SVf"(%"SVf")",
745 PUSHs(sv_2mortal(SvREFCNT_inc_simple_NN(*namsvp)));
750 const IV flags = SvIVX(*flgsvp);
752 if (flags & PERLIO_F_UTF8) {
753 PUSHs(newSVpvs_flags("utf8", SVs_TEMP));
771 XS(XS_re_is_regexp); /* prototype to pass -Wmissing-prototypes */
778 croak_xs_usage(cv, "sv");
787 XS(XS_re_regnames_count); /* prototype to pass -Wmissing-prototypes */
788 XS(XS_re_regnames_count)
790 REGEXP *rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
795 croak_xs_usage(cv, "");
803 ret = CALLREG_NAMED_BUFF_COUNT(rx);
806 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
810 XS(XS_re_regname); /* prototype to pass -Wmissing-prototypes */
818 if (items < 1 || items > 2)
819 croak_xs_usage(cv, "name[, all ]");
824 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
829 if (items == 2 && SvTRUE(ST(1))) {
834 ret = CALLREG_NAMED_BUFF_FETCH(rx, ST(0), (flags | RXapif_REGNAME));
837 PUSHs(ret ? sv_2mortal(ret) : &PL_sv_undef);
842 XS(XS_re_regnames); /* prototype to pass -Wmissing-prototypes */
855 croak_xs_usage(cv, "[all]");
857 rx = PL_curpm ? PM_GETRE(PL_curpm) : NULL;
862 if (items == 1 && SvTRUE(ST(0))) {
871 ret = CALLREG_NAMED_BUFF_ALL(rx, (flags | RXapif_REGNAMES));
878 av = MUTABLE_AV(SvRV(ret));
879 length = av_tindex(av);
881 EXTEND(SP, length+1); /* better extend stack just once */
882 for (i = 0; i <= length; i++) {
883 entry = av_fetch(av, i, FALSE);
886 Perl_croak(aTHX_ "NULL array element in re::regnames()");
888 mPUSHs(SvREFCNT_inc_simple_NN(*entry));
897 XS(XS_re_regexp_pattern); /* prototype to pass -Wmissing-prototypes */
898 XS(XS_re_regexp_pattern)
906 croak_xs_usage(cv, "sv");
909 Checks if a reference is a regex or not. If the parameter is
910 not a ref, or is not the result of a qr// then returns false
911 in scalar context and an empty list in list context.
912 Otherwise in list context it returns the pattern and the
913 modifiers, in scalar context it returns the pattern just as it
914 would if the qr// was stringified normally, regardless as
915 to the class of the variable and any stringification overloads
919 if ((re = SvRX(ST(0)))) /* assign deliberate */
921 /* Houston, we have a regex! */
924 if ( GIMME_V == G_ARRAY ) {
926 char reflags[sizeof(INT_PAT_MODS) + MAX_CHARSET_NAME_LENGTH];
932 we are in list context so stringify
933 the modifiers that apply. We ignore "negative
934 modifiers" in this scenario, and the default character set
937 if (get_regex_charset(RX_EXTFLAGS(re)) != REGEX_DEPENDS_CHARSET) {
939 const char* const name = get_regex_charset_name(RX_EXTFLAGS(re),
941 Copy(name, reflags + left, len, char);
945 match_flags = (U16)((RX_EXTFLAGS(re) & RXf_PMf_COMPILETIME)
946 >> RXf_PMf_STD_PMMOD_SHIFT);
948 while((ch = *fptr++)) {
949 if(match_flags & 1) {
950 reflags[left++] = ch;
955 pattern = newSVpvn_flags(RX_PRECOMP(re),RX_PRELEN(re),
956 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
958 /* return the pattern and the modifiers */
960 PUSHs(newSVpvn_flags(reflags, left, SVs_TEMP));
963 /* Scalar, so use the string that Perl would return */
964 /* return the pattern in (?msix:..) format */
965 #if PERL_VERSION >= 11
966 pattern = sv_2mortal(newSVsv(MUTABLE_SV(re)));
968 pattern = newSVpvn_flags(RX_WRAPPED(re), RX_WRAPLEN(re),
969 (RX_UTF8(re) ? SVf_UTF8 : 0) | SVs_TEMP);
975 /* It ain't a regexp folks */
976 if ( GIMME_V == G_ARRAY ) {
977 /* return the empty list */
980 /* Because of the (?:..) wrapping involved in a
981 stringified pattern it is impossible to get a
982 result for a real regexp that would evaluate to
983 false. Therefore we can return PL_sv_no to signify
984 that the object is not a regex, this means that one
987 if (regex($might_be_a_regex) eq '(?:foo)') { }
989 and not worry about undefined values.
1000 struct xsub_details {
1006 static const struct xsub_details details[] = {
1007 {"UNIVERSAL::isa", XS_UNIVERSAL_isa, NULL},
1008 {"UNIVERSAL::can", XS_UNIVERSAL_can, NULL},
1009 {"UNIVERSAL::DOES", XS_UNIVERSAL_DOES, NULL},
1010 #define VXS_XSUB_DETAILS
1012 #undef VXS_XSUB_DETAILS
1013 {"utf8::is_utf8", XS_utf8_is_utf8, NULL},
1014 {"utf8::valid", XS_utf8_valid, NULL},
1015 {"utf8::encode", XS_utf8_encode, NULL},
1016 {"utf8::decode", XS_utf8_decode, NULL},
1017 {"utf8::upgrade", XS_utf8_upgrade, NULL},
1018 {"utf8::downgrade", XS_utf8_downgrade, NULL},
1019 {"utf8::native_to_unicode", XS_utf8_native_to_unicode, NULL},
1020 {"utf8::unicode_to_native", XS_utf8_unicode_to_native, NULL},
1021 {"Internals::SvREADONLY", XS_Internals_SvREADONLY, "\\[$%@];$"},
1022 {"constant::_make_const", XS_constant__make_const, "\\[$@]"},
1023 {"Internals::SvREFCNT", XS_Internals_SvREFCNT, "\\[$%@];$"},
1024 {"Internals::hv_clear_placeholders", XS_Internals_hv_clear_placehold, "\\%"},
1025 {"PerlIO::get_layers", XS_PerlIO_get_layers, "*;@"},
1026 {"re::is_regexp", XS_re_is_regexp, "$"},
1027 {"re::regname", XS_re_regname, ";$$"},
1028 {"re::regnames", XS_re_regnames, ";$"},
1029 {"re::regnames_count", XS_re_regnames_count, ""},
1030 {"re::regexp_pattern", XS_re_regexp_pattern, "$"},
1034 Perl_boot_core_UNIVERSAL(pTHX)
1036 static const char file[] = __FILE__;
1037 const struct xsub_details *xsub = details;
1038 const struct xsub_details *end = C_ARRAY_END(details);
1041 newXS_flags(xsub->name, xsub->xsub, file, xsub->proto, 0);
1042 } while (++xsub < end);
1044 /* Providing a Regexp::DESTROY fixes #21347. See test in t/op/ref.t */
1047 newCONSTSUB(get_hv("Regexp::", GV_ADD), "DESTROY", NULL);
1048 Safefree(CvFILE(cv));
1049 CvFILE(cv) = (char *)file;
1056 * c-indentation-style: bsd
1058 * indent-tabs-mode: nil
1061 * ex: set ts=8 sts=4 sw=4 et: