Ignore all .git.*
[gitmo/Class-MOP.git] / xs / MOP.xs
1 #include "mop.h"
2
3 SV *mop_method_metaclass;
4 SV *mop_associated_metaclass;
5 SV *mop_wrap;
6
7 static bool
8 find_method (const char *key, STRLEN keylen, SV *val, void *ud)
9 {
10     bool *found_method = (bool *)ud;
11     *found_method = TRUE;
12     return FALSE;
13 }
14
15 EXTERN_C XS(boot_Class__MOP__Package);
16 EXTERN_C XS(boot_Class__MOP__Class);
17 EXTERN_C XS(boot_Class__MOP__Attribute);
18 EXTERN_C XS(boot_Class__MOP__Method);
19
20 MODULE = Class::MOP   PACKAGE = Class::MOP
21
22 PROTOTYPES: DISABLE
23
24 BOOT:
25     mop_prehash_keys();
26
27     mop_method_metaclass     = newSVpvs("method_metaclass");
28     mop_wrap                 = newSVpvs("wrap");
29     mop_associated_metaclass = newSVpvs("associated_metaclass");
30
31     MOP_CALL_BOOT (boot_Class__MOP__Package);
32     MOP_CALL_BOOT (boot_Class__MOP__Class);
33     MOP_CALL_BOOT (boot_Class__MOP__Attribute);
34     MOP_CALL_BOOT (boot_Class__MOP__Method);
35
36 # use prototype here to be compatible with get_code_info from Sub::Identify
37 void
38 get_code_info(coderef)
39     SV *coderef
40     PROTOTYPE: $
41     PREINIT:
42         char *pkg  = NULL;
43         char *name = NULL;
44     PPCODE:
45         if (mop_get_code_info(coderef, &pkg, &name)) {
46             EXTEND(SP, 2);
47             PUSHs(newSVpv(pkg, 0));
48             PUSHs(newSVpv(name, 0));
49         }
50
51 # This is some pretty grotty logic. It _should_ be parallel to the
52 # pure Perl version in lib/Class/MOP.pm, so if you want to understand
53 # it we suggest you start there.
54 void
55 is_class_loaded(klass=&PL_sv_undef)
56     SV *klass
57     PREINIT:
58         HV *stash;
59         bool found_method = FALSE;
60     PPCODE:
61         if (!SvPOK(klass) || !SvCUR(klass)) {
62             XSRETURN_NO;
63         }
64
65         stash = gv_stashsv(klass, 0);
66         if (!stash) {
67             XSRETURN_NO;
68         }
69
70         if (hv_exists_ent (stash, KEY_FOR(VERSION), HASH_FOR(VERSION))) {
71             HE *version = hv_fetch_ent(stash, KEY_FOR(VERSION), 0, HASH_FOR(VERSION));
72             SV *version_sv;
73             if (version && HeVAL(version) && (version_sv = GvSV(HeVAL(version)))) {
74                 if (SvROK(version_sv)) {
75                     SV *version_sv_ref = SvRV(version_sv);
76
77                     if (SvOK(version_sv_ref)) {
78                         XSRETURN_YES;
79                     }
80                 }
81                 else if (SvOK(version_sv)) {
82                     XSRETURN_YES;
83                 }
84             }
85         }
86
87         if (hv_exists_ent (stash, KEY_FOR(ISA), HASH_FOR(ISA))) {
88             HE *isa = hv_fetch_ent(stash, KEY_FOR(ISA), 0, HASH_FOR(ISA));
89             if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
90                 XSRETURN_YES;
91             }
92         }
93
94         mop_get_package_symbols(stash, TYPE_FILTER_CODE, find_method, &found_method);
95         if (found_method) {
96             XSRETURN_YES;
97         }
98
99         XSRETURN_NO;