Include method name in immutable methods (fixes #49680)
[gitmo/Class-MOP.git] / mop.c
CommitLineData
d846ade3 1#include "mop.h"
2
3void
1be56175 4mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
d846ade3 5{
e3dcef7f 6 dSP;
7 PUSHMARK(mark);
8 (*subaddr)(aTHX_ cv);
9 PUTBACK;
d846ade3 10}
11
12#if PERL_VERSION >= 10
13UV
14mop_check_package_cache_flag (pTHX_ HV *stash)
15{
16 assert(SvTYPE(stash) == SVt_PVHV);
17
18 /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
19 * however the perl core doesn't make it easy for us. It doesn't provide an
20 * api that just does what we want.
21 *
22 * However, we know that the information we want is, inside the core,
23 * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
24 * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
25 * which is not public and only available inside the core, as the mro
26 * interface as well as the structure returned by mro_meta_init isn't
27 * considered to be stable yet.
28 *
29 * Perl_mro_meta_init isn't declared static, so we could just define it
30 * ourselfs if perls headers don't do that for us, except that won't work
31 * on platforms where symbols need to be explicitly exported when linking
32 * shared libraries.
33 *
34 * So our, hopefully temporary, solution is to be even more evil and
35 * basically reimplement HvMROMETA in a very fragile way that'll blow up
36 * when the relevant parts of the mro implementation in core change.
37 *
38 * :-(
39 *
40 */
41
42 return HvAUX(stash)->xhv_mro_meta
43 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
44 : 0;
45}
46
47#else /* pre 5.10.0 */
48
49UV
50mop_check_package_cache_flag (pTHX_ HV *stash)
51{
52 PERL_UNUSED_ARG(stash);
53 assert(SvTYPE(stash) == SVt_PVHV);
54
55 return PL_sub_generation;
56}
57#endif
58
59SV *
60mop_call0 (pTHX_ SV *const self, SV *const method)
61{
62 dSP;
63 SV *ret;
64
65 PUSHMARK(SP);
66 XPUSHs(self);
67 PUTBACK;
68
69 call_sv(method, G_SCALAR | G_METHOD);
70
71 SPAGAIN;
72 ret = POPs;
73 PUTBACK;
74
75 return ret;
76}
77
78int
e1f52a8a 79mop_get_code_info (SV *coderef, char **pkg, char **name)
d846ade3 80{
ca212610 81 GV *gv;
82
d846ade3 83 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
84 return 0;
85 }
86
87 coderef = SvRV(coderef);
caa6b5cd 88
89 /* sub is still being compiled */
90 if (!CvGV(coderef)) {
91 return 0;
92 }
93
d846ade3 94 /* I think this only gets triggered with a mangled coderef, but if
95 we hit it without the guard, we segfault. The slightly odd return
96 value strikes me as an improvement (mst)
97 */
9b52bbf1 98
d846ade3 99 if ( isGV_with_GP(CvGV(coderef)) ) {
ca212610 100 gv = CvGV(coderef);
101 *pkg = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
102 *name = GvNAME( CvGV(coderef) );
d846ade3 103 } else {
ca212610 104 *pkg = "__UNKNOWN__";
105 *name = "__ANON__";
d846ade3 106 }
d846ade3 107
108 return 1;
109}
110
111void
e1f52a8a 112mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
d846ade3 113{
114 HE *he;
115
116 (void)hv_iterinit(stash);
117
118 if (filter == TYPE_FILTER_NONE) {
119 while ( (he = hv_iternext(stash)) ) {
120 STRLEN keylen;
121 const char *key = HePV(he, keylen);
122 if (!cb(key, keylen, HeVAL(he), ud)) {
123 return;
124 }
125 }
126 return;
127 }
128
129 while ( (he = hv_iternext(stash)) ) {
9135ad30 130 GV * const gv = (GV*)HeVAL(he);
d846ade3 131 STRLEN keylen;
9135ad30 132 const char * const key = HePV(he, keylen);
133 SV *sv = NULL;
d846ade3 134
9135ad30 135 if(isGV(gv)){
136 switch (filter) {
137 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
138 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
139 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
140 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
141 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
142 default:
143 croak("Unknown type");
144 }
145 }
146 /* expand the gv into a real typeglob if it
147 * contains stub functions or constants and we
148 * were asked to return CODE references */
149 else if (filter == TYPE_FILTER_CODE) {
150 gv_init(gv, stash, key, keylen, GV_ADDMULTI);
151 sv = (SV *)GvCV(gv);
d846ade3 152 }
153
154 if (sv) {
d846ade3 155 if (!cb(key, keylen, sv, ud)) {
156 return;
157 }
158 }
159 }
160}
161
162static bool
163collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
164{
165 HV *hash = (HV *)ud;
166
167 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
168 croak("failed to store symbol ref");
169 }
170
171 return TRUE;
172}
173
174HV *
e1f52a8a 175mop_get_all_package_symbols (HV *stash, type_filter_t filter)
d846ade3 176{
177 HV *ret = newHV ();
e1f52a8a 178 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
d846ade3 179 return ret;
180}
22932438 181
95ac09c5 182#define DECLARE_KEY(name) { #name, #name, NULL, 0 }
183#define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
184
22932438 185/* the order of these has to match with those in mop.h */
186static struct {
187 const char *name;
188 const char *value;
189 SV *key;
190 U32 hash;
191} prehashed_keys[key_last] = {
192 DECLARE_KEY(name),
193 DECLARE_KEY(package),
194 DECLARE_KEY(package_name),
195 DECLARE_KEY(body),
196 DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
197 DECLARE_KEY(methods),
198 DECLARE_KEY(VERSION),
199 DECLARE_KEY(ISA)
200};
201
20c230c5 202SV *
22932438 203mop_prehashed_key_for (mop_prehashed_key_t key)
204{
205 return prehashed_keys[key].key;
206}
207
20c230c5 208U32
22932438 209mop_prehashed_hash_for (mop_prehashed_key_t key)
210{
211 return prehashed_keys[key].hash;
212}
213
214void
215mop_prehash_keys ()
216{
217 int i;
22932438 218 for (i = 0; i < key_last; i++) {
219 const char *value = prehashed_keys[i].value;
220 prehashed_keys[i].key = newSVpv(value, strlen(value));
221 PERL_HASH(prehashed_keys[i].hash, value, strlen(value));
222 }
223}
7ec7b950 224
384bb6c9 225XS(mop_xs_simple_reader)
7ec7b950 226{
384bb6c9 227#ifdef dVAR
228 dVAR; dXSARGS;
229#else
230 dXSARGS;
231#endif
7ec7b950 232 register HE *he;
4f201502 233 mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
384bb6c9 234 SV *self;
235
236 if (items != 1) {
237 croak("expected exactly one argument");
238 }
239
240 self = ST(0);
7ec7b950 241
242 if (!SvROK(self)) {
243 croak("can't call %s as a class method", prehashed_keys[key].name);
244 }
245
246 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
247 croak("object is not a hashref");
248 }
249
38d91ddc 250 if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
251 ST(0) = HeVAL(he);
7ec7b950 252 }
384bb6c9 253 else {
38d91ddc 254 ST(0) = &PL_sv_undef;
7ec7b950 255 }
256
7ec7b950 257 XSRETURN(1);
258}
259