Doh, I'm retarded.
[gitmo/Class-MOP.git] / MOP.xs
1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5
6 #define NEED_sv_2pv_flags
7 #define NEED_sv_2pv_nolen
8 #include "ppport.h"
9
10 SV *key_name;
11 U32 hash_name;
12
13 SV *key_package;
14 U32 hash_package;
15
16 SV *key_package_name;
17 U32 hash_package_name;
18
19 SV *key_body;
20 U32 hash_body;
21
22 /*
23 get_code_info:
24   Pass in a coderef, returns:
25   [ $pkg_name, $coderef_name ] ie:
26   [ 'Foo::Bar', 'new' ]
27 */
28
29 MODULE = Class::MOP   PACKAGE = Class::MOP
30
31 BOOT:
32     key_name = newSVpvs("name");
33     key_body = newSVpvs("body");
34     key_package = newSVpvs("package");
35     key_package_name = newSVpvs("package_name");
36
37     PERL_HASH(hash_name, "name", 4);
38     PERL_HASH(hash_body, "body", 4);
39     PERL_HASH(hash_package, "package", 7);
40     PERL_HASH(hash_package_name, "package_name", 12);
41
42
43 PROTOTYPES: ENABLE
44
45
46 void
47 get_code_info(coderef)
48   SV* coderef
49   PREINIT:
50     char* name;
51     char* pkg;
52   PPCODE:
53     if( SvOK(coderef) && SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV){
54       coderef = SvRV(coderef);
55       /* I think this only gets triggered with a mangled coderef, but if
56          we hit it without the guard, we segfault. The slightly odd return
57          value strikes me as an improvement (mst)
58       */
59 #ifdef isGV_with_GP
60       if ( isGV_with_GP(CvGV(coderef))) {
61 #endif
62         pkg     = HvNAME( GvSTASH(CvGV(coderef)) );
63         name    = GvNAME( CvGV(coderef) );
64 #ifdef isGV_with_GP
65       } else {
66         pkg     = "__UNKNOWN__";
67         name    = "__ANON__";
68       }
69 #endif
70
71       EXTEND(SP, 2);
72       PUSHs(newSVpvn(pkg, strlen(pkg)));
73       PUSHs(newSVpvn(name, strlen(name)));
74     }
75
76
77 MODULE = Class::MOP   PACKAGE = Class::MOP::Package
78
79 void
80 get_all_package_symbols(self, ...)
81     SV *self
82     PROTOTYPE: $;$
83     PREINIT:
84         HV *stash = NULL;
85         SV *type_filter = NULL;
86         register HE *he;
87     PPCODE:
88         if (! SvROK(self)) {
89             die("Cannot call get_all_package_symbols as a class method");
90         }
91
92         switch ( GIMME_V ) {
93             case G_VOID: return; break;
94             case G_SCALAR: ST(0) = &PL_sv_undef; return; break;
95         }
96
97         if ( items > 1 ) type_filter = ST(1);
98
99         PUTBACK;
100
101         if ((he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)))
102             stash = gv_stashsv(HeVAL(he),0);
103
104         if ( stash ) {
105
106             (void)hv_iterinit(stash);
107
108             if ( type_filter && SvPOK(type_filter) ) {
109                 const char *const type = SvPV_nolen(type_filter);
110
111                 while ((he = hv_iternext(stash))) {
112                     SV *const gv = HeVAL(he);
113                     SV *sv;
114                     char *key;
115                     STRLEN keylen;
116                     char *package;
117                     SV *fq;
118
119                     switch( SvTYPE(gv) ) {
120                         case SVt_PVGV:
121                             switch (*type) {
122                                 case 'C': sv = (SV *)GvCVu(gv); break; /* CODE */
123                                 case 'A': sv = (SV *)GvAV(gv); break; /* ARRAY */
124                                 case 'I': sv = (SV *)GvIO(gv); break; /* IO */
125                                 case 'H': sv = (SV *)GvHV(gv); break; /* HASH */
126                                 case 'S': sv = (SV *)GvSV(gv); break; /* SCALAR */
127                                 default:
128                                           croak("Unknown type %s\n", type);
129                             }
130                             break;
131                         case SVt_RV:
132                             /* BAH! constants are horrible */
133
134                             if (!SvROK (gv)) {
135                                 continue;
136                             }
137
138                             /* we don't really care about the length,
139                                but that's the API */
140                             key = HePV(he, keylen);
141                             package = HvNAME(stash);
142                             fq = newSVpvf("%s::%s", package, key);
143                             sv = (SV*)get_cv(SvPV_nolen(fq), 0);
144                             break;
145                         default:
146                             continue;
147                     }
148
149                     if ( sv ) {
150                         SV *key = hv_iterkeysv(he);
151                         SPAGAIN;
152                         EXTEND(SP, 2);
153                         PUSHs(key);
154                         PUSHs(sv_2mortal(newRV_inc(sv)));
155                         PUTBACK;
156                     }
157                 }
158             } else {
159                 EXTEND(SP, HvKEYS(stash) * 2);
160
161                 while ((he = hv_iternext(stash))) {
162                     SV *key = hv_iterkeysv(he);
163                     SV *sv = HeVAL(he);
164                     SPAGAIN;
165                     PUSHs(key);
166                     PUSHs(sv);
167                     PUTBACK;
168                 }
169             }
170
171         }
172
173 void
174 name(self)
175     SV *self
176     PREINIT:
177         register HE *he;
178     PPCODE:
179         if (! SvROK(self)) {
180             die("Cannot call name as a class method");
181         }
182
183         if ((he = hv_fetch_ent((HV *)SvRV(self), key_package, 0, hash_package)))
184             XPUSHs(HeVAL(he));
185         else
186             ST(0) = &PL_sv_undef;
187
188 MODULE = Class::MOP   PACKAGE = Class::MOP::Attribute
189
190 void
191 name(self)
192     SV *self
193     PREINIT:
194         register HE *he;
195     PPCODE:
196         if (! SvROK(self)) {
197             die("Cannot call name as a class method");
198         }
199
200         if ((he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)))
201             XPUSHs(HeVAL(he));
202         else
203             ST(0) = &PL_sv_undef;
204
205 MODULE = Class::MOP   PACKAGE = Class::MOP::Method
206
207 void
208 name(self)
209     SV *self
210     PREINIT:
211         register HE *he;
212     PPCODE:
213         if (! SvROK(self)) {
214             die("Cannot call name as a class method");
215         }
216
217         if ((he = hv_fetch_ent((HV *)SvRV(self), key_name, 0, hash_name)))
218             XPUSHs(HeVAL(he));
219         else
220             ST(0) = &PL_sv_undef;
221
222 void
223 package_name(self)
224     SV *self
225     PREINIT:
226         register HE *he;
227     PPCODE:
228         if (! SvROK(self)) {
229             die("Cannot call package_name as a class method");
230         }
231
232         if ((he = hv_fetch_ent((HV *)SvRV(self), key_package_name, 0, hash_package_name)))
233             XPUSHs(HeVAL(he));
234         else
235             ST(0) = &PL_sv_undef;
236
237 void
238 body(self)
239     SV *self
240     PREINIT:
241         register HE *he;
242     PPCODE:
243         if (! SvROK(self)) {
244             die("Cannot call body as a class method");
245         }
246
247         if ((he = hv_fetch_ent((HV *)SvRV(self), key_body, 0, hash_body)))
248             XPUSHs(HeVAL(he));
249         else
250             ST(0) = &PL_sv_undef;