4 mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
12 #if PERL_BCDVERSION >= 0x5010000
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__";
110 mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
114 (void)hv_iterinit(stash);
116 if (filter == TYPE_FILTER_NONE) {
117 while ( (he = hv_iternext(stash)) ) {
119 const char *key = HePV(he, keylen);
120 if (!cb(key, keylen, HeVAL(he), ud)) {
127 while ( (he = hv_iternext(stash)) ) {
128 SV *const gv = HeVAL(he);
134 switch( SvTYPE(gv) ) {
140 /* expand the gv into a real typeglob if it
141 * contains stub functions and we were asked to
142 * return CODE symbols */
143 if (filter == TYPE_FILTER_CODE) {
146 /* we don't really care about the length,
147 but that's the API */
148 key = HePV(he, keylen);
149 package = HvNAME(stash);
150 fq = sv_2mortal(newSVpvf("%s::%s", package, key));
151 sv = (SV *)get_cv(SvPV_nolen(fq), 0);
155 key = HePV(he, keylen);
156 gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
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;
167 croak("Unknown type");
175 const char *key = HePV(he, keylen);
176 if (!cb(key, keylen, sv, ud)) {
184 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
188 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
189 croak("failed to store symbol ref");
196 mop_get_all_package_symbols (HV *stash, type_filter_t filter)
199 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
203 static MGVTBL mop_accessor_vtbl; /* the MAGIC identity */
206 mop_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSPROTO(accessor_impl)){
207 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
208 SV* const keysv = newSVpvn_share(key, keylen, 0U);
210 sv_magicext((SV*)xsub, keysv, PERL_MAGIC_ext, &mop_accessor_vtbl, NULL, 0);
211 SvREFCNT_dec(keysv); /* sv_magicext() increases refcnt in mg_obj */
216 mop_mg_find_by_vtbl(pTHX_ SV* const sv, const MGVTBL* const vtbl){
220 for(mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic){
221 if(mg->mg_virtual == vtbl){
229 mop_fetch_attr(pTHX_ SV* const self, SV* const key, I32 const create, CV* const cv){
232 croak("can't call %s as a class method", GvNAME(CvGV(cv)));
234 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
235 croak("object is not a hashref");
237 if((he = hv_fetch_ent((HV*)SvRV(self), key, create, 0U))){
243 mop_delete_attr(pTHX_ SV* const self, SV* const key, CV* const cv){
246 croak("can't call %s as a class method", GvNAME(CvGV(cv)));
248 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
249 croak("object is not a hashref");
251 if((sv = hv_delete_ent((HV*)SvRV(self), key, 0, 0U))){
257 XS(mop_xs_simple_accessor)
260 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
261 SV* const key = mg->mg_obj;
263 if(items == 1){ /* reader */
264 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
266 else if (items == 2){ /* writer */
267 attr = mop_fetch_attr(aTHX_ ST(0), key, TRUE, cv);
268 sv_setsv(attr, ST(1));
271 croak("expected exactly one or two argument");
273 ST(0) = attr ? attr : &PL_sv_undef;
278 XS(mop_xs_simple_reader)
281 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
282 SV* const key = mg->mg_obj;
286 croak("expected exactly one argument");
289 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
290 ST(0) = attr ? attr : &PL_sv_undef;
294 XS(mop_xs_simple_writer)
297 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
298 SV* const key = mg->mg_obj;
302 croak("expected exactly two argument");
305 attr = mop_fetch_attr(aTHX_ ST(0), key, TRUE, cv);
306 sv_setsv(attr, ST(1));
311 XS(mop_xs_simple_clearer)
314 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
315 SV* const key = mg->mg_obj;
319 croak("expected exactly one argument");
322 attr = mop_delete_attr(aTHX_ ST(0), key, cv);
323 ST(0) = attr ? attr : &PL_sv_undef;
328 XS(mop_xs_simple_predicate)
331 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
332 SV* const key = mg->mg_obj;
336 croak("expected exactly one argument");
339 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
340 ST(0) = boolSV(attr); /* exists */
345 XS(mop_xs_simple_predicate_for_metaclass)
348 MAGIC* const mg = mop_mg_find_by_vtbl(aTHX_ (SV*)cv, &mop_accessor_vtbl);
349 SV* const key = mg->mg_obj;
353 croak("expected exactly one argument");
356 attr = mop_fetch_attr(aTHX_ ST(0), key, FALSE, cv);
357 ST(0) = boolSV(attr && SvOK(attr)); /* defined */