4 mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
12 #if PERL_VERSION >= 10
14 mop_check_package_cache_flag (pTHX_ HV *stash)
16 assert(SvTYPE(stash) == SVt_PVHV);
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.
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.
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
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.
42 return HvAUX(stash)->xhv_mro_meta
43 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
47 #else /* pre 5.10.0 */
50 mop_check_package_cache_flag (pTHX_ HV *stash)
52 PERL_UNUSED_ARG(stash);
53 assert(SvTYPE(stash) == SVt_PVHV);
55 return PL_sub_generation;
60 mop_call0 (pTHX_ SV *const self, SV *const method)
69 call_sv(method, G_SCALAR | G_METHOD);
79 mop_get_code_info (SV *coderef, char **pkg, char **name)
81 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
85 coderef = SvRV(coderef);
87 /* sub is still being compiled */
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)
97 if ( isGV_with_GP(CvGV(coderef)) ) {
98 GV *gv = CvGV(coderef);
99 *pkg = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
100 *name = GvNAME( CvGV(coderef) );
102 *pkg = "__UNKNOWN__";
109 /* XXX: eventually this should just use the implementation in Package::Stash */
111 mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
115 (void)hv_iterinit(stash);
117 if (filter == TYPE_FILTER_NONE) {
118 while ( (he = hv_iternext(stash)) ) {
120 const char *key = HePV(he, keylen);
121 if (!cb(key, keylen, HeVAL(he), ud)) {
128 while ( (he = hv_iternext(stash)) ) {
129 GV * const gv = (GV*)HeVAL(he);
131 const char * const key = HePV(he, keylen);
136 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
137 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
138 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
139 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
140 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
142 croak("Unknown type");
145 /* expand the gv into a real typeglob if it
146 * contains stub functions or constants and we
147 * were asked to return CODE references */
148 else if (filter == TYPE_FILTER_CODE) {
149 gv_init(gv, stash, key, keylen, GV_ADDMULTI);
154 if (!cb(key, keylen, sv, ud)) {
162 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
166 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
167 croak("failed to store symbol ref");
174 mop_get_all_package_symbols (HV *stash, type_filter_t filter)
177 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
181 #define DECLARE_KEY(name) { #name, #name, NULL, 0 }
182 #define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
184 /* the order of these has to match with those in mop.h */
190 } prehashed_keys[key_last] = {
191 DECLARE_KEY(_expected_method_class),
193 DECLARE_KEY(VERSION),
194 DECLARE_KEY(accessor),
195 DECLARE_KEY(associated_class),
196 DECLARE_KEY(associated_metaclass),
197 DECLARE_KEY(associated_methods),
198 DECLARE_KEY(attribute_metaclass),
199 DECLARE_KEY(attributes),
201 DECLARE_KEY(builder),
202 DECLARE_KEY(clearer),
203 DECLARE_KEY(constructor_class),
204 DECLARE_KEY(constructor_name),
205 DECLARE_KEY(definition_context),
206 DECLARE_KEY(destructor_class),
207 DECLARE_KEY(immutable_trait),
208 DECLARE_KEY(init_arg),
209 DECLARE_KEY(initializer),
210 DECLARE_KEY(insertion_order),
211 DECLARE_KEY(instance_metaclass),
212 DECLARE_KEY(is_inline),
213 DECLARE_KEY(method_metaclass),
214 DECLARE_KEY(methods),
216 DECLARE_KEY(package),
217 DECLARE_KEY(package_name),
218 DECLARE_KEY(predicate),
220 DECLARE_KEY(wrapped_method_metaclass),
222 DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
223 DECLARE_KEY_WITH_VALUE(_version, "-version")
227 mop_prehashed_key_for (mop_prehashed_key_t key)
229 return prehashed_keys[key].key;
233 mop_prehashed_hash_for (mop_prehashed_key_t key)
235 return prehashed_keys[key].hash;
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));
249 XS_EXTERNAL(mop_xs_simple_reader)
257 mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
261 croak("expected exactly one argument");
267 croak("can't call %s as a class method", prehashed_keys[key].name);
270 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
271 croak("object is not a hashref");
274 if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
278 ST(0) = &PL_sv_undef;