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