Revert "Remove subname() in add_method(). Now add_package_symbol() does it in a smart...
[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{
81 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
82 return 0;
83 }
84
85 coderef = SvRV(coderef);
caa6b5cd 86
87 /* sub is still being compiled */
88 if (!CvGV(coderef)) {
89 return 0;
90 }
91
d846ade3 92 /* I think this only gets triggered with a mangled coderef, but if
93 we hit it without the guard, we segfault. The slightly odd return
94 value strikes me as an improvement (mst)
95 */
9b52bbf1 96
d846ade3 97 if ( isGV_with_GP(CvGV(coderef)) ) {
2087a201 98 GV *gv = CvGV(coderef);
99 *pkg = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
d846ade3 100 *name = GvNAME( CvGV(coderef) );
d846ade3 101 } else {
102 *pkg = "__UNKNOWN__";
103 *name = "__ANON__";
104 }
d846ade3 105
106 return 1;
107}
108
109void
e1f52a8a 110mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
d846ade3 111{
112 HE *he;
113
114 (void)hv_iterinit(stash);
115
116 if (filter == TYPE_FILTER_NONE) {
117 while ( (he = hv_iternext(stash)) ) {
118 STRLEN keylen;
119 const char *key = HePV(he, keylen);
120 if (!cb(key, keylen, HeVAL(he), ud)) {
121 return;
122 }
123 }
124 return;
125 }
126
127 while ( (he = hv_iternext(stash)) ) {
128 SV *const gv = HeVAL(he);
129 SV *sv = NULL;
130 char *key;
131 STRLEN keylen;
132 char *package;
133 SV *fq;
134
135 switch( SvTYPE(gv) ) {
136#ifndef SVt_RV
137 case SVt_RV:
138#endif
139 case SVt_PV:
140 case SVt_IV:
141 /* expand the gv into a real typeglob if it
142 * contains stub functions and we were asked to
143 * return CODE symbols */
144 if (filter == TYPE_FILTER_CODE) {
145 if (SvROK(gv)) {
146 /* we don't really care about the length,
147 but that's the API */
148 key = HePV(he, keylen);
149 package = HvNAME(stash);
efc98200 150 fq = sv_2mortal(newSVpvf("%s::%s", package, key));
d846ade3 151 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
152 break;
153 }
154
155 key = HePV(he, keylen);
156 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
157 }
158 /* fall through */
159 case SVt_PVGV:
160 switch (filter) {
161 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
162 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
163 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
164 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
165 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
166 default:
167 croak("Unknown type");
168 }
169 break;
170 default:
171 continue;
172 }
173
174 if (sv) {
175 const char *key = HePV(he, keylen);
176 if (!cb(key, keylen, sv, ud)) {
177 return;
178 }
179 }
180 }
181}
182
183static bool
184collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
185{
186 HV *hash = (HV *)ud;
187
188 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
189 croak("failed to store symbol ref");
190 }
191
192 return TRUE;
193}
194
195HV *
e1f52a8a 196mop_get_all_package_symbols (HV *stash, type_filter_t filter)
d846ade3 197{
198 HV *ret = newHV ();
e1f52a8a 199 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
d846ade3 200 return ret;
201}
22932438 202
95ac09c5 203#define DECLARE_KEY(name) { #name, #name, NULL, 0 }
204#define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
205
22932438 206/* the order of these has to match with those in mop.h */
207static struct {
208 const char *name;
209 const char *value;
210 SV *key;
211 U32 hash;
212} prehashed_keys[key_last] = {
213 DECLARE_KEY(name),
214 DECLARE_KEY(package),
215 DECLARE_KEY(package_name),
216 DECLARE_KEY(body),
217 DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
218 DECLARE_KEY(methods),
219 DECLARE_KEY(VERSION),
220 DECLARE_KEY(ISA)
221};
222
20c230c5 223SV *
22932438 224mop_prehashed_key_for (mop_prehashed_key_t key)
225{
226 return prehashed_keys[key].key;
227}
228
20c230c5 229U32
22932438 230mop_prehashed_hash_for (mop_prehashed_key_t key)
231{
232 return prehashed_keys[key].hash;
233}
234
235void
236mop_prehash_keys ()
237{
238 int i;
22932438 239 for (i = 0; i < key_last; i++) {
240 const char *value = prehashed_keys[i].value;
241 prehashed_keys[i].key = newSVpv(value, strlen(value));
242 PERL_HASH(prehashed_keys[i].hash, value, strlen(value));
243 }
244}
7ec7b950 245
384bb6c9 246XS(mop_xs_simple_reader)
7ec7b950 247{
384bb6c9 248#ifdef dVAR
249 dVAR; dXSARGS;
250#else
251 dXSARGS;
252#endif
7ec7b950 253 register HE *he;
4f201502 254 mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
384bb6c9 255 SV *self;
256
257 if (items != 1) {
258 croak("expected exactly one argument");
259 }
260
261 self = ST(0);
7ec7b950 262
263 if (!SvROK(self)) {
264 croak("can't call %s as a class method", prehashed_keys[key].name);
265 }
266
267 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
268 croak("object is not a hashref");
269 }
270
38d91ddc 271 if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
272 ST(0) = HeVAL(he);
7ec7b950 273 }
384bb6c9 274 else {
38d91ddc 275 ST(0) = &PL_sv_undef;
7ec7b950 276 }
277
7ec7b950 278 XSRETURN(1);
279}
280