Skip to content
Newer
Older
100644 259 lines (217 sloc) 6.3 KB
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 10, 2009
1 #include "mop.h"
2
3 void
1be5617 @rafl Stop redefining the prototype for XSUBs.
rafl authored Mar 15, 2009
4 mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
5 {
e3dcef7 @rafl Remove tabs from c code.
rafl authored Mar 10, 2009
6 dSP;
7 PUSHMARK(mark);
8 (*subaddr)(aTHX_ cv);
9 PUTBACK;
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
10 }
11
12 #if PERL_VERSION >= 10
13 UV
14 mop_check_package_cache_flag (pTHX_ HV *stash)
15 {
16 assert(SvTYPE(stash) == SVt_PVHV);
17
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.
21 *
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.
28 *
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
32 * shared libraries.
33 *
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.
37 *
38 * :-(
39 *
40 */
41
42 return HvAUX(stash)->xhv_mro_meta
43 ? HvAUX(stash)->xhv_mro_meta->pkg_gen
44 : 0;
45 }
46
47 #else /* pre 5.10.0 */
48
49 UV
50 mop_check_package_cache_flag (pTHX_ HV *stash)
51 {
52 PERL_UNUSED_ARG(stash);
53 assert(SvTYPE(stash) == SVt_PVHV);
54
55 return PL_sub_generation;
56 }
57 #endif
58
59 SV *
60 mop_call0 (pTHX_ SV *const self, SV *const method)
61 {
62 dSP;
63 SV *ret;
64
65 PUSHMARK(SP);
66 XPUSHs(self);
67 PUTBACK;
68
69 call_sv(method, G_SCALAR | G_METHOD);
70
71 SPAGAIN;
72 ret = POPs;
73 PUTBACK;
74
75 return ret;
76 }
77
78 int
e1f52a8 @rafl Prefix all exported symbols with "mop_"
rafl authored Mar 10, 2009
79 mop_get_code_info (SV *coderef, char **pkg, char **name)
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
80 {
81 if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
82 return 0;
83 }
84
85 coderef = SvRV(coderef);
caa6b5c @rafl Merge branch 'master' into xs_reorg
rafl authored Mar 10, 2009
86
87 /* sub is still being compiled */
88 if (!CvGV(coderef)) {
89 return 0;
90 }
91
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
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)
95 */
9b52bbf @gfx Fix RT #48072 using isGV_with_GP directly, which is now ported by ppp…
gfx authored Jul 22, 2009
96
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
97 if ( isGV_with_GP(CvGV(coderef)) ) {
aa798f1 @autarch Revert "MS VC++ compilation fix"
autarch authored Sep 14, 2009
98 GV *gv = CvGV(coderef);
99 *pkg = HvNAME( GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef) );
100 *name = GvNAME( CvGV(coderef) );
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
101 } else {
aa798f1 @autarch Revert "MS VC++ compilation fix"
autarch authored Sep 15, 2009
102 *pkg = "__UNKNOWN__";
103 *name = "__ANON__";
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
104 }
105
106 return 1;
107 }
108
109 void
e1f52a8 @rafl Prefix all exported symbols with "mop_"
rafl authored Mar 10, 2009
110 mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
111 {
112 HE *he;
113
114 (void)hv_iterinit(stash);
115
116 if (filter == TYPE_FILTER_NONE) {
117 while ( (he = hv_iternext(stash)) ) {
118 STRLEN keylen;
119 const char *key = HePV(he, keylen);
120 if (!cb(key, keylen, HeVAL(he), ud)) {
121 return;
122 }
123 }
124 return;
125 }
126
127 while ( (he = hv_iternext(stash)) ) {
9135ad3 @gfx Refactor mop_get_package_symbols
gfx authored Aug 19, 2009
128 GV * const gv = (GV*)HeVAL(he);
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
129 STRLEN keylen;
9135ad3 @gfx Refactor mop_get_package_symbols
gfx authored Aug 19, 2009
130 const char * const key = HePV(he, keylen);
131 SV *sv = NULL;
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
132
9135ad3 @gfx Refactor mop_get_package_symbols
gfx authored Aug 19, 2009
133 if(isGV(gv)){
134 switch (filter) {
135 case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
136 case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
137 case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
138 case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
139 case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
140 default:
141 croak("Unknown type");
142 }
143 }
144 /* expand the gv into a real typeglob if it
145 * contains stub functions or constants and we
146 * were asked to return CODE references */
147 else if (filter == TYPE_FILTER_CODE) {
148 gv_init(gv, stash, key, keylen, GV_ADDMULTI);
149 sv = (SV *)GvCV(gv);
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
150 }
151
152 if (sv) {
153 if (!cb(key, keylen, sv, ud)) {
154 return;
155 }
156 }
157 }
158 }
159
160 static bool
161 collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
162 {
163 HV *hash = (HV *)ud;
164
165 if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
166 croak("failed to store symbol ref");
167 }
168
169 return TRUE;
170 }
171
172 HV *
e1f52a8 @rafl Prefix all exported symbols with "mop_"
rafl authored Mar 10, 2009
173 mop_get_all_package_symbols (HV *stash, type_filter_t filter)
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
174 {
175 HV *ret = newHV ();
e1f52a8 @rafl Prefix all exported symbols with "mop_"
rafl authored Mar 10, 2009
176 mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
d846ade @rafl Split MOP.xs into more managable parts under xs/
rafl authored Mar 9, 2009
177 return ret;
178 }
2293243 @rafl Refactor how prehashed hash keys work.
rafl authored Mar 11, 2009
179
95ac09c @rafl Don't make DECLARE_KEY* available for anything but mop.c.
rafl authored Mar 11, 2009
180 #define DECLARE_KEY(name) { #name, #name, NULL, 0 }
181 #define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
182
2293243 @rafl Refactor how prehashed hash keys work.
rafl authored Mar 11, 2009
183 /* the order of these has to match with those in mop.h */
184 static struct {
185 const char *name;
186 const char *value;
187 SV *key;
188 U32 hash;
189 } prehashed_keys[key_last] = {
190 DECLARE_KEY(name),
191 DECLARE_KEY(package),
192 DECLARE_KEY(package_name),
193 DECLARE_KEY(body),
194 DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
195 DECLARE_KEY(methods),
196 DECLARE_KEY(VERSION),
4154c4d @rafl Allow requiring a version with is_class_loaded, load_class and load_f…
rafl authored May 2, 2010
197 DECLARE_KEY(ISA),
198 DECLARE_KEY_WITH_VALUE(_version, "-version")
2293243 @rafl Refactor how prehashed hash keys work.
rafl authored Mar 11, 2009
199 };
200
20c230c @rafl Uninline mop_prehashed_{key,hash}_for.
rafl authored Mar 15, 2009
201 SV *
2293243 @rafl Refactor how prehashed hash keys work.
rafl authored Mar 11, 2009
202 mop_prehashed_key_for (mop_prehashed_key_t key)
203 {
204 return prehashed_keys[key].key;
205 }
206
20c230c @rafl Uninline mop_prehashed_{key,hash}_for.
rafl authored Mar 15, 2009
207 U32
2293243 @rafl Refactor how prehashed hash keys work.
rafl authored Mar 11, 2009
208 mop_prehashed_hash_for (mop_prehashed_key_t key)
209 {
210 return prehashed_keys[key].hash;
211 }
212
213 void
214 mop_prehash_keys ()
215 {
216 int i;
217 for (i = 0; i < key_last; i++) {
218 const char *value = prehashed_keys[i].value;
219 prehashed_keys[i].key = newSVpv(value, strlen(value));
220 PERL_HASH(prehashed_keys[i].hash, value, strlen(value));
221 }
222 }
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
223
384bb6c @rafl Inline mop_simple_reader.
rafl authored Mar 11, 2009
224 XS(mop_xs_simple_reader)
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
225 {
384bb6c @rafl Inline mop_simple_reader.
rafl authored Mar 11, 2009
226 #ifdef dVAR
227 dVAR; dXSARGS;
228 #else
229 dXSARGS;
230 #endif
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
231 register HE *he;
4f20150 @rafl Do a proper cast go get from I32 to mop_prehashed_key_t.
rafl authored Mar 15, 2009
232 mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
384bb6c @rafl Inline mop_simple_reader.
rafl authored Mar 11, 2009
233 SV *self;
234
235 if (items != 1) {
236 croak("expected exactly one argument");
237 }
238
239 self = ST(0);
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
240
241 if (!SvROK(self)) {
242 croak("can't call %s as a class method", prehashed_keys[key].name);
243 }
244
245 if (SvTYPE(SvRV(self)) != SVt_PVHV) {
246 croak("object is not a hashref");
247 }
248
38d91dd @rafl Make the logic easier to follow.
rafl authored Mar 11, 2009
249 if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
250 ST(0) = HeVAL(he);
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
251 }
384bb6c @rafl Inline mop_simple_reader.
rafl authored Mar 11, 2009
252 else {
38d91dd @rafl Make the logic easier to follow.
rafl authored Mar 11, 2009
253 ST(0) = &PL_sv_undef;
7ec7b95 @rafl Make it easier to simple xs hashref readers using prehashed keys.
rafl authored Mar 11, 2009
254 }
255
256 XSRETURN(1);
257 }
258
Something went wrong with that request. Please try again.