2a3e49b4f3b0e49b8273e20dbbd04a3059afbf29
[gitmo/Class-MOP.git] / MOP.xs
1 /* There's a lot of cases of doubled parens in here like this:
2
3   while ( (he = ...) ) {
4
5 This shuts up warnings from gcc -Wall
6 */
7
8 #include "EXTERN.h"
9 #include "perl.h"
10 #include "XSUB.h"
11
12 #define NEED_sv_2pv_flags
13 #define NEED_sv_2pv_nolen
14 #include "ppport.h"
15
16 SV *key_name;
17 U32 hash_name;
18
19 SV *key_package;
20 U32 hash_package;
21
22 SV *key_package_name;
23 U32 hash_package_name;
24
25 SV *key_body;
26 U32 hash_body;
27
28 SV *key_package_cache_flag;
29 U32 hash_package_cache_flag;
30
31 SV *key_methods;
32 U32 hash_methods;
33
34 SV *method_metaclass;
35 SV *associated_metaclass;
36 SV *wrap;
37
38
39 #define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
40 #ifdef HvMROMETA /* 5.10.0 */
41
42 #ifndef mro_meta_init
43 #define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */
44 #endif /* !mro_meta_init */
45
46 static UV
47 mop_check_package_cache_flag(pTHX_ HV* stash) {
48     assert(SvTYPE(stash) == SVt_PVHV);
49
50     return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */
51 }
52
53 #else /* pre 5.10.0 */
54
55 static UV
56 mop_check_package_cache_flag(pTHX_ HV *stash) {
57     PERL_UNUSED_ARG(stash);
58     assert(SvTYPE(stash) == SVt_PVHV);
59
60     return PL_sub_generation;
61 }
62 #endif
63
64 #define call0(s, m)  mop_call0(aTHX_ s, m)
65 static SV *
66 mop_call0(pTHX_ SV *const self, SV *const method) {
67     dSP;
68     SV *ret;
69
70     PUSHMARK(SP);
71     XPUSHs(self);
72     PUTBACK;
73
74     call_sv(method, G_SCALAR | G_METHOD);
75
76     SPAGAIN;
77     ret = POPs;
78     PUTBACK;
79
80     return ret;
81 }
82
83 static int
84 get_code_info (SV *coderef, char **pkg, char **name)
85 {
86     if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
87         return 0;
88     }
89
90     coderef = SvRV(coderef);
91     /* I think this only gets triggered with a mangled coderef, but if
92        we hit it without the guard, we segfault. The slightly odd return
93        value strikes me as an improvement (mst)
94     */
95 #ifdef isGV_with_GP
96     if ( isGV_with_GP(CvGV(coderef)) ) {
97 #endif
98         *pkg     = HvNAME( GvSTASH(CvGV(coderef)) );
99         *name    = GvNAME( CvGV(coderef) );
100 #ifdef isGV_with_GP
101     } else {
102         *pkg     = "__UNKNOWN__";
103         *name    = "__ANON__";
104     }
105 #endif
106
107     return 1;
108 }
109
110 typedef enum {
111     TYPE_FILTER_NONE,
112     TYPE_FILTER_CODE,
113     TYPE_FILTER_ARRAY,
114     TYPE_FILTER_IO,
115     TYPE_FILTER_HASH,
116     TYPE_FILTER_SCALAR,
117 } type_filter_t;
118
119 static HV *
120 get_all_package_symbols(HV *stash, type_filter_t filter)
121 {
122     HE *he;
123     HV *ret = newHV();
124
125     (void)hv_iterinit(stash);
126
127     if (filter == TYPE_FILTER_NONE) {
128         while ( (he = hv_iternext(stash)) ) {
129             STRLEN keylen;
130             char *key = HePV(he, keylen);
131             hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0);
132         }
133
134         return ret;
135     }
136
137     while ( (he = hv_iternext(stash)) ) {
138         SV *const gv = HeVAL(he);
139         SV *sv = NULL;
140         char *key;
141         STRLEN keylen;
142         char *package;
143         SV *fq;
144
145         switch( SvTYPE(gv) ) {
146 #ifndef SVt_RV
147             case SVt_RV:
148 #endif
149             case SVt_PV:
150             case SVt_IV:
151                 /* expand the gv into a real typeglob if it
152                  * contains stub functions and we were asked to
153                  * return CODE symbols */
154                 if (filter == TYPE_FILTER_CODE) {
155                     if (SvROK(gv)) {
156                         /* we don't really care about the length,
157                            but that's the API */
158                         key = HePV(he, keylen);
159                         package = HvNAME(stash);
160                         fq = newSVpvf("%s::%s", package, key);
161                         sv = (SV *)get_cv(SvPV_nolen(fq), 0);
162                         break;
163                     }
164
165                     key = HePV(he, keylen);
166                     gv_init((GV *)gv, stash, key, keylen, GV_ADDMULTI);
167                 }
168                 /* fall through */
169             case SVt_PVGV:
170                 switch (filter) {
171                     case TYPE_FILTER_CODE:   sv = (SV *)GvCVu(gv); break;
172                     case TYPE_FILTER_ARRAY:  sv = (SV *)GvAV(gv);  break;
173                     case TYPE_FILTER_IO:     sv = (SV *)GvIO(gv);  break;
174                     case TYPE_FILTER_HASH:   sv = (SV *)GvHV(gv);  break;
175                     case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv);  break;
176                     default:
177                         croak("Unknown type");
178                 }
179                 break;
180             default:
181                 continue;
182         }
183
184         if (sv) {
185             char *key = HePV(he, keylen);
186             hv_store(ret, key, keylen, newRV_inc(sv), 0);
187         }
188     }
189
190     return ret;
191 }
192
193
194 static void
195 mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
196     const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
197     SV   *method_metaclass_name;
198     char *method_name;
199     I32   method_name_len;
200     SV   *coderef;
201     HV   *symbols;
202     dSP;
203
204     symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
205
206     (void)hv_iterinit(symbols);
207     while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
208         CV *cv = (CV *)SvRV(coderef);
209         char *cvpkg_name;
210         char *cv_name;
211         SV *method_slot;
212         SV *method_object;
213
214         if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
215             continue;
216         }
217
218         /* this checks to see that the subroutine is actually from our package  */
219         if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
220             if ( strNE(cvpkg_name, class_name_pv) ) {
221                 continue;
222             }
223         }
224
225         method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
226         if ( SvOK(method_slot) ) {
227             SV* const body = call0(method_slot, key_body); /* $method_object->body() */
228             if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
229                 continue;
230             }
231         }
232
233         method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
234
235         /*
236             $method_object = $method_metaclass->wrap(
237                 $cv,
238                 associated_metaclass => $self,
239                 package_name         => $class_name,
240                 name                 => $method_name
241             );
242         */
243         ENTER;
244         SAVETMPS;
245
246         PUSHMARK(SP);
247         EXTEND(SP, 8);
248         PUSHs(method_metaclass_name); /* invocant */
249         mPUSHs(newRV_inc((SV *)cv));
250         PUSHs(associated_metaclass);
251         PUSHs(self);
252         PUSHs(key_package_name);
253         PUSHs(class_name);
254         PUSHs(key_name);
255         mPUSHs(newSVpv(method_name, method_name_len));
256         PUTBACK;
257
258         call_sv(wrap, G_SCALAR | G_METHOD);
259         SPAGAIN;
260         method_object = POPs;
261         PUTBACK;
262         /* $map->{$method_name} = $method_object */
263         sv_setsv(method_slot, method_object);
264
265         FREETMPS;
266         LEAVE;
267     }
268 }
269
270 /*
271 get_code_info:
272   Pass in a coderef, returns:
273   [ $pkg_name, $coderef_name ] ie:
274   [ 'Foo::Bar', 'new' ]
275 */
276
277 MODULE = Class::MOP   PACKAGE = Class::MOP
278
279 BOOT:
280     key_name = newSVpvs("name");
281     key_body = newSVpvs("body");
282     key_package = newSVpvs("package");
283     key_package_name = newSVpvs("package_name");
284     key_package_cache_flag = newSVpvs("_package_cache_flag");
285     key_methods = newSVpvs("methods");
286
287     PERL_HASH(hash_name, "name", 4);
288     PERL_HASH(hash_body, "body", 4);
289     PERL_HASH(hash_package, "package", 7);
290     PERL_HASH(hash_package_name, "package_name", 12);
291     PERL_HASH(hash_package_cache_flag, "_package_cache_flag", 19);
292     PERL_HASH(hash_methods, "methods", 7);
293
294     method_metaclass     = newSVpvs("method_metaclass");
295     wrap                 = newSVpvs("wrap");
296     associated_metaclass = newSVpvs("associated_metaclass");
297
298
299 PROTOTYPES: ENABLE
300
301
302 void
303 get_code_info(coderef)
304     SV *coderef
305     PREINIT:
306         char* pkg  = NULL;
307         char* name = NULL;
308     PPCODE:
309         if (get_code_info(coderef, &pkg, &name)) {
310             EXTEND(SP, 2);
311             PUSHs(newSVpv(pkg, 0));
312             PUSHs(newSVpv(name, 0));
313         }
314
315
316 MODULE = Class::MOP   PACKAGE = Class::MOP::Package
317
318 void
319 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
320     SV *self
321     type_filter_t filter
322     PROTOTYPE: $;$
323     PREINIT:
324         HV *stash = NULL;
325         HV *symbols = NULL;
326         register HE *he;
327     PPCODE:
328         if ( ! SvROK(self) ) {
329             die("Cannot call get_all_package_symbols as a class method");
330         }
331
332         if (GIMME_V == G_VOID) {
333             XSRETURN_EMPTY;
334         }
335
336
337         PUTBACK;
338
339         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) ) {
340             stash = gv_stashsv(HeVAL(he), 0);
341         }
342
343
344         if (!stash) {
345             switch (GIMME_V) {
346                 case G_SCALAR: XSRETURN_UNDEF; break;
347                 case G_ARRAY:  XSRETURN_EMPTY; break;
348             }
349         }
350
351         symbols = get_all_package_symbols(stash, filter);
352
353         switch (GIMME_V) {
354             case G_SCALAR:
355                 PUSHs(sv_2mortal(newRV_inc((SV *)symbols)));
356                 break;
357             case G_ARRAY:
358                 warn("Class::MOP::Package::get_all_package_symbols in list context is deprecated. use scalar context instead.");
359
360                 EXTEND(SP, HvKEYS(symbols) * 2);
361
362                 while ((he = hv_iternext(symbols))) {
363                     PUSHs(hv_iterkeysv(he));
364                     PUSHs(sv_2mortal(SvREFCNT_inc(HeVAL(he))));
365                 }
366
367                 break;
368             default:
369                 break;
370         }
371
372         SvREFCNT_dec((SV *)symbols);
373
374 void
375 name(self)
376     SV *self
377     PREINIT:
378         register HE *he;
379     PPCODE:
380         if ( ! SvROK(self) ) {
381             die("Cannot call name as a class method");
382         }
383
384         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)) )
385             XPUSHs(HeVAL(he));
386         else
387             ST(0) = &PL_sv_undef;
388
389 MODULE = Class::MOP   PACKAGE = Class::MOP::Attribute
390
391 void
392 name(self)
393     SV *self
394     PREINIT:
395         register HE *he;
396     PPCODE:
397         if ( ! SvROK(self) ) {
398             die("Cannot call name as a class method");
399         }
400
401         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
402             XPUSHs(HeVAL(he));
403         else
404             ST(0) = &PL_sv_undef;
405
406 MODULE = Class::MOP   PACKAGE = Class::MOP::Method
407
408 void
409 name(self)
410     SV *self
411     PREINIT:
412         register HE *he;
413     PPCODE:
414         if ( ! SvROK(self) ) {
415             die("Cannot call name as a class method");
416         }
417
418         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)) )
419             XPUSHs(HeVAL(he));
420         else
421             ST(0) = &PL_sv_undef;
422
423 void
424 package_name(self)
425     SV *self
426     PREINIT:
427         register HE *he;
428     PPCODE:
429         if ( ! SvROK(self) ) {
430             die("Cannot call package_name as a class method");
431         }
432
433         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)) )
434             XPUSHs(HeVAL(he));
435         else
436             ST(0) = &PL_sv_undef;
437
438 void
439 body(self)
440     SV *self
441     PREINIT:
442         register HE *he;
443     PPCODE:
444         if ( ! SvROK(self) ) {
445             die("Cannot call body as a class method");
446         }
447
448         if ( (he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)) )
449             XPUSHs(HeVAL(he));
450         else
451             ST(0) = &PL_sv_undef;
452
453
454 MODULE = Class::MOP    PACKAGE = Class::MOP::Class
455
456 void
457 get_method_map(self)
458     SV *self
459     PREINIT:
460         HV *const obj        = (HV *)SvRV(self);
461         SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
462         HV *const stash      = gv_stashsv(class_name, 0);
463         UV  const current    = check_package_cache_flag(stash);
464         SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
465         SV *const map_ref    = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
466     PPCODE:
467         /* in  $self->{methods} does not yet exist (or got deleted) */
468         if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
469             SV *new_map_ref = newRV_noinc((SV *)newHV());
470             sv_2mortal(new_map_ref);
471             sv_setsv(map_ref, new_map_ref);
472         }
473
474         if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
475             mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
476             sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */
477         }
478
479         XPUSHs(map_ref);