Commit | Line | Data |
d846ade3 |
1 | #include "mop.h" |
2 | |
e2e116c2 |
3 | static void |
b1ff395f |
4 | mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) |
5 | { |
6 | const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */ |
7 | SV *method_metaclass_name; |
8 | char *method_name; |
9 | I32 method_name_len; |
10 | SV *coderef; |
11 | HV *symbols; |
12 | dSP; |
13 | |
14 | symbols = mop_get_all_package_symbols(stash, TYPE_FILTER_CODE); |
15 | sv_2mortal((SV*)symbols); |
16 | (void)hv_iterinit(symbols); |
17 | while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) { |
18 | CV *cv = (CV *)SvRV(coderef); |
19 | char *cvpkg_name; |
20 | char *cv_name; |
21 | SV *method_slot; |
22 | SV *method_object; |
23 | |
24 | if (!mop_get_code_info(coderef, &cvpkg_name, &cv_name)) { |
25 | continue; |
26 | } |
27 | |
28 | /* this checks to see that the subroutine is actually from our package */ |
29 | if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) { |
30 | if ( strNE(cvpkg_name, class_name_pv) ) { |
31 | continue; |
32 | } |
33 | } |
34 | |
35 | method_slot = *hv_fetch(map, method_name, method_name_len, TRUE); |
36 | if ( SvOK(method_slot) ) { |
37 | SV *const body = mop_call0(aTHX_ method_slot, KEY_FOR(body)); /* $method_object->body() */ |
38 | if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) { |
39 | continue; |
40 | } |
41 | } |
42 | |
43 | method_metaclass_name = mop_call0(aTHX_ self, mop_method_metaclass); /* $self->method_metaclass() */ |
44 | |
45 | /* |
46 | $method_object = $method_metaclass->wrap( |
47 | $cv, |
48 | associated_metaclass => $self, |
49 | package_name => $class_name, |
50 | name => $method_name |
51 | ); |
52 | */ |
53 | ENTER; |
54 | SAVETMPS; |
55 | |
56 | PUSHMARK(SP); |
57 | EXTEND(SP, 8); |
58 | PUSHs(method_metaclass_name); /* invocant */ |
59 | mPUSHs(newRV_inc((SV *)cv)); |
60 | PUSHs(mop_associated_metaclass); |
61 | PUSHs(self); |
62 | PUSHs(KEY_FOR(package_name)); |
63 | PUSHs(class_name); |
64 | PUSHs(KEY_FOR(name)); |
65 | mPUSHs(newSVpv(method_name, method_name_len)); |
66 | PUTBACK; |
67 | |
68 | call_sv(mop_wrap, G_SCALAR | G_METHOD); |
69 | SPAGAIN; |
70 | method_object = POPs; |
71 | PUTBACK; |
72 | /* $map->{$method_name} = $method_object */ |
73 | sv_setsv(method_slot, method_object); |
74 | |
75 | FREETMPS; |
76 | LEAVE; |
77 | } |
78 | } |
79 | |
d846ade3 |
80 | MODULE = Class::MOP::Package PACKAGE = Class::MOP::Package |
81 | |
82 | PROTOTYPES: DISABLE |
83 | |
84 | void |
85 | get_all_package_symbols(self, filter=TYPE_FILTER_NONE) |
86 | SV *self |
87 | type_filter_t filter |
88 | PREINIT: |
89 | HV *stash = NULL; |
90 | HV *symbols = NULL; |
91 | register HE *he; |
92 | PPCODE: |
93 | if ( ! SvROK(self) ) { |
94 | die("Cannot call get_all_package_symbols as a class method"); |
95 | } |
96 | |
97 | if (GIMME_V == G_VOID) { |
98 | XSRETURN_EMPTY; |
99 | } |
100 | |
101 | PUTBACK; |
102 | |
22932438 |
103 | if ( (he = hv_fetch_ent((HV *)SvRV(self), KEY_FOR(package), 0, HASH_FOR(package))) ) { |
d846ade3 |
104 | stash = gv_stashsv(HeVAL(he), 0); |
105 | } |
106 | |
107 | |
108 | if (!stash) { |
109 | XSRETURN_UNDEF; |
110 | } |
111 | |
e1f52a8a |
112 | symbols = mop_get_all_package_symbols(stash, filter); |
d846ade3 |
113 | PUSHs(sv_2mortal(newRV_noinc((SV *)symbols))); |
114 | |
b1ff395f |
115 | void |
116 | get_method_map(self) |
117 | SV *self |
118 | PREINIT: |
119 | HV *const obj = (HV *)SvRV(self); |
120 | SV *const class_name = HeVAL( hv_fetch_ent(obj, KEY_FOR(package), 0, HASH_FOR(package)) ); |
121 | HV *const stash = gv_stashsv(class_name, 0); |
122 | UV current; |
123 | SV *cache_flag; |
124 | SV *map_ref; |
125 | PPCODE: |
126 | if (!stash) { |
127 | mXPUSHs(newRV_noinc((SV *)newHV())); |
128 | return; |
129 | } |
130 | |
131 | current = mop_check_package_cache_flag(aTHX_ stash); |
132 | cache_flag = HeVAL( hv_fetch_ent(obj, KEY_FOR(package_cache_flag), TRUE, HASH_FOR(package_cache_flag))); |
133 | map_ref = HeVAL( hv_fetch_ent(obj, KEY_FOR(methods), TRUE, HASH_FOR(methods))); |
134 | |
135 | /* $self->{methods} does not yet exist (or got deleted) */ |
136 | if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) { |
137 | SV *new_map_ref = newRV_noinc((SV *)newHV()); |
138 | sv_2mortal(new_map_ref); |
139 | sv_setsv(map_ref, new_map_ref); |
140 | } |
141 | |
142 | if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) { |
143 | mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref)); |
144 | sv_setuv(cache_flag, mop_check_package_cache_flag(aTHX_ stash)); /* update_cache_flag() */ |
145 | } |
146 | |
147 | XPUSHs(map_ref); |
148 | |
7ec7b950 |
149 | BOOT: |
150 | INSTALL_SIMPLE_READER_WITH_KEY(Package, name, package); |