forked from moose/Moose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mop.c
286 lines (244 loc) · 7.22 KB
/
mop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "mop.h"
void
mop_call_xs (pTHX_ XSPROTO(subaddr), CV *cv, SV **mark)
{
dSP;
PUSHMARK(mark);
(*subaddr)(aTHX_ cv);
PUTBACK;
}
#if PERL_VERSION >= 10
UV
mop_check_package_cache_flag (pTHX_ HV *stash)
{
assert(SvTYPE(stash) == SVt_PVHV);
/* here we're trying to implement a c version of mro::get_pkg_gen($stash),
* however the perl core doesn't make it easy for us. It doesn't provide an
* api that just does what we want.
*
* However, we know that the information we want is, inside the core,
* available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
* HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
* which is not public and only available inside the core, as the mro
* interface as well as the structure returned by mro_meta_init isn't
* considered to be stable yet.
*
* Perl_mro_meta_init isn't declared static, so we could just define it
* ourselfs if perls headers don't do that for us, except that won't work
* on platforms where symbols need to be explicitly exported when linking
* shared libraries.
*
* So our, hopefully temporary, solution is to be even more evil and
* basically reimplement HvMROMETA in a very fragile way that'll blow up
* when the relevant parts of the mro implementation in core change.
*
* :-(
*
*/
return HvAUX(stash)->xhv_mro_meta
? HvAUX(stash)->xhv_mro_meta->pkg_gen
: 0;
}
#else /* pre 5.10.0 */
UV
mop_check_package_cache_flag (pTHX_ HV *stash)
{
PERL_UNUSED_ARG(stash);
assert(SvTYPE(stash) == SVt_PVHV);
return PL_sub_generation;
}
#endif
SV *
mop_call0 (pTHX_ SV *const self, SV *const method)
{
dSP;
SV *ret;
PUSHMARK(SP);
XPUSHs(self);
PUTBACK;
call_sv(method, G_SCALAR | G_METHOD);
SPAGAIN;
ret = POPs;
PUTBACK;
return ret;
}
int
mop_get_code_info (SV *coderef, char **pkg, char **name)
{
if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
return 0;
}
coderef = SvRV(coderef);
/* sub is still being compiled */
if (!CvGV(coderef)) {
return 0;
}
/* I think this only gets triggered with a mangled coderef, but if
we hit it without the guard, we segfault. The slightly odd return
value strikes me as an improvement (mst)
*/
if ( isGV_with_GP(CvGV(coderef)) ) {
GV *gv = CvGV(coderef);
HV *stash = GvSTASH(gv) ? GvSTASH(gv) : CvSTASH(coderef);
*pkg = stash ? HvNAME(stash) : "__UNKNOWN__";
*name = GvNAME( CvGV(coderef) );
} else {
*pkg = "__UNKNOWN__";
*name = "__ANON__";
}
return 1;
}
/* XXX: eventually this should just use the implementation in Package::Stash */
void
mop_get_package_symbols (HV *stash, type_filter_t filter, get_package_symbols_cb_t cb, void *ud)
{
HE *he;
(void)hv_iterinit(stash);
if (filter == TYPE_FILTER_NONE) {
while ( (he = hv_iternext(stash)) ) {
STRLEN keylen;
const char *key = HePV(he, keylen);
if (!cb(key, keylen, HeVAL(he), ud)) {
return;
}
}
return;
}
while ( (he = hv_iternext(stash)) ) {
GV * const gv = (GV*)HeVAL(he);
STRLEN keylen;
const char * const key = HePV(he, keylen);
SV *sv = NULL;
if(isGV(gv)){
switch (filter) {
case TYPE_FILTER_CODE: sv = (SV *)GvCVu(gv); break;
case TYPE_FILTER_ARRAY: sv = (SV *)GvAV(gv); break;
case TYPE_FILTER_IO: sv = (SV *)GvIO(gv); break;
case TYPE_FILTER_HASH: sv = (SV *)GvHV(gv); break;
case TYPE_FILTER_SCALAR: sv = (SV *)GvSV(gv); break;
default:
croak("Unknown type");
}
}
/* expand the gv into a real typeglob if it
* contains stub functions or constants and we
* were asked to return CODE references */
else if (filter == TYPE_FILTER_CODE) {
gv_init(gv, stash, key, keylen, GV_ADDMULTI);
sv = (SV *)GvCV(gv);
}
if (sv) {
if (!cb(key, keylen, sv, ud)) {
return;
}
}
}
}
static bool
collect_all_symbols (const char *key, STRLEN keylen, SV *val, void *ud)
{
HV *hash = (HV *)ud;
if (!hv_store (hash, key, keylen, newRV_inc(val), 0)) {
croak("failed to store symbol ref");
}
return TRUE;
}
HV *
mop_get_all_package_symbols (HV *stash, type_filter_t filter)
{
HV *ret = newHV ();
mop_get_package_symbols (stash, filter, collect_all_symbols, ret);
return ret;
}
#define DECLARE_KEY(name) { #name, #name, NULL, 0 }
#define DECLARE_KEY_WITH_VALUE(name, value) { #name, value, NULL, 0 }
/* the order of these has to match with those in mop.h */
static struct {
const char *name;
const char *value;
SV *key;
U32 hash;
} prehashed_keys[key_last] = {
DECLARE_KEY(_expected_method_class),
DECLARE_KEY(ISA),
DECLARE_KEY(VERSION),
DECLARE_KEY(accessor),
DECLARE_KEY(associated_class),
DECLARE_KEY(associated_metaclass),
DECLARE_KEY(associated_methods),
DECLARE_KEY(attribute_metaclass),
DECLARE_KEY(attributes),
DECLARE_KEY(body),
DECLARE_KEY(builder),
DECLARE_KEY(clearer),
DECLARE_KEY(constructor_class),
DECLARE_KEY(constructor_name),
DECLARE_KEY(definition_context),
DECLARE_KEY(destructor_class),
DECLARE_KEY(immutable_trait),
DECLARE_KEY(init_arg),
DECLARE_KEY(initializer),
DECLARE_KEY(insertion_order),
DECLARE_KEY(instance_metaclass),
DECLARE_KEY(is_inline),
DECLARE_KEY(method_metaclass),
DECLARE_KEY(methods),
DECLARE_KEY(name),
DECLARE_KEY(package),
DECLARE_KEY(package_name),
DECLARE_KEY(predicate),
DECLARE_KEY(reader),
DECLARE_KEY(wrapped_method_metaclass),
DECLARE_KEY(writer),
DECLARE_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag"),
DECLARE_KEY_WITH_VALUE(_version, "-version"),
DECLARE_KEY(operator)
};
SV *
mop_prehashed_key_for (mop_prehashed_key_t key)
{
return prehashed_keys[key].key;
}
U32
mop_prehashed_hash_for (mop_prehashed_key_t key)
{
return prehashed_keys[key].hash;
}
void
mop_prehash_keys ()
{
int i;
for (i = 0; i < key_last; i++) {
const char *value = prehashed_keys[i].value;
prehashed_keys[i].key = newSVpv(value, strlen(value));
PERL_HASH(prehashed_keys[i].hash, value, strlen(value));
}
}
XS_EXTERNAL(mop_xs_simple_reader)
{
#ifdef dVAR
dVAR; dXSARGS;
#else
dXSARGS;
#endif
register HE *he;
mop_prehashed_key_t key = (mop_prehashed_key_t)CvXSUBANY(cv).any_i32;
SV *self;
if (items != 1) {
croak("expected exactly one argument");
}
self = ST(0);
if (!SvROK(self)) {
croak("can't call %s as a class method", prehashed_keys[key].name);
}
if (SvTYPE(SvRV(self)) != SVt_PVHV) {
croak("object is not a hashref");
}
if ((he = hv_fetch_ent((HV *)SvRV(self), prehashed_keys[key].key, 0, prehashed_keys[key].hash))) {
ST(0) = HeVAL(he);
}
else {
ST(0) = &PL_sv_undef;
}
XSRETURN(1);
}