Commit | Line | Data |
94caac6e |
1 | #define PERL_CORE |
2 | #define PERL_NO_GET_CONTEXT |
94caac6e |
3 | #include "EXTERN.h" |
4 | #include "perl.h" |
5 | #include "XSUB.h" |
6 | #undef printf |
e807ee50 |
7 | #include "stolen_chunk_of_toke.c" |
94caac6e |
8 | #include <stdio.h> |
9 | #include <string.h> |
10 | |
53e3ab32 |
11 | #if 0 |
12 | #define DD_DEBUG |
13 | #endif |
c630715a |
14 | |
0ba8c7aa |
15 | #define DD_HANDLE_NAME 1 |
16 | #define DD_HANDLE_PROTO 2 |
15d0d014 |
17 | #define DD_HANDLE_PACKAGE 8 |
0ba8c7aa |
18 | |
c630715a |
19 | #ifdef DD_DEBUG |
20 | #define DD_DEBUG_S printf("Buffer: %s\n", s); |
21 | #else |
22 | #define DD_DEBUG_S |
23 | #endif |
24 | |
94caac6e |
25 | #define LEX_NORMAL 10 |
26 | #define LEX_INTERPNORMAL 9 |
27 | |
28 | /* placeholders for PL_check entries we wrap */ |
29 | |
30 | STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op); |
53e3ab32 |
31 | STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op); |
94caac6e |
32 | |
33 | /* flag to trigger removal of temporary declaree sub */ |
34 | |
35 | static int in_declare = 0; |
36 | |
37 | /* replacement PL_check rv2cv entry */ |
38 | |
39 | STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { |
40 | OP* kid; |
41 | char* s; |
0ba8c7aa |
42 | char* save_s; |
94caac6e |
43 | char tmpbuf[sizeof PL_tokenbuf]; |
0ba8c7aa |
44 | char found_name[sizeof PL_tokenbuf]; |
45 | char* found_proto = NULL; |
46 | STRLEN len = 0; |
94caac6e |
47 | HV *stash; |
48 | HV* is_declarator; |
49 | SV** is_declarator_pack_ref; |
50 | HV* is_declarator_pack_hash; |
51 | SV** is_declarator_flag_ref; |
0ba8c7aa |
52 | int dd_flags; |
9026391e |
53 | char* cb_args[6]; |
53e3ab32 |
54 | dSP; /* define stack pointer for later call stuff */ |
55 | char* retstr; |
56 | STRLEN n_a; /* for POPpx */ |
94caac6e |
57 | |
58 | o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */ |
59 | |
60 | if (in_declare) { |
61 | cb_args[0] = NULL; |
62 | call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args); |
0ba8c7aa |
63 | in_declare--; |
94caac6e |
64 | return o; |
65 | } |
66 | |
67 | kid = cUNOPo->op_first; |
68 | |
69 | if (kid->op_type != OP_GV) /* not a GV so ignore */ |
70 | return o; |
71 | |
72 | if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL) |
73 | return o; /* not lexing? */ |
74 | |
75 | stash = GvSTASH(kGVOP_gv); |
76 | |
c630715a |
77 | #ifdef DD_DEBUG |
78 | printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv)); |
79 | #endif |
94caac6e |
80 | |
81 | is_declarator = get_hv("Devel::Declare::declarators", FALSE); |
82 | |
83 | if (!is_declarator) |
84 | return o; |
85 | |
86 | is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash), |
87 | strlen(HvNAME(stash)), FALSE); |
88 | |
89 | if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref)) |
90 | return o; /* not a hashref */ |
91 | |
92 | is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref); |
93 | |
94 | is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv), |
95 | strlen(GvNAME(kGVOP_gv)), FALSE); |
96 | |
0ba8c7aa |
97 | /* requires SvIOK as well as TRUE since flags not being an int is useless */ |
98 | |
99 | if (!is_declarator_flag_ref |
100 | || !SvIOK(*is_declarator_flag_ref) |
101 | || !SvTRUE(*is_declarator_flag_ref)) |
94caac6e |
102 | return o; |
103 | |
0ba8c7aa |
104 | dd_flags = SvIVX(*is_declarator_flag_ref); |
105 | |
106 | #ifdef DD_DEBUG |
107 | printf("dd_flags are: %i\n", dd_flags); |
108 | #endif |
109 | |
94caac6e |
110 | s = PL_bufptr; /* copy the current buffer pointer */ |
111 | |
c630715a |
112 | DD_DEBUG_S |
113 | |
114 | #ifdef DD_DEBUG |
0ba8c7aa |
115 | printf("PL_tokenbuf: %s\n", PL_tokenbuf); |
c630715a |
116 | #endif |
117 | |
118 | /* |
119 | * buffer will be at the beginning of the declarator, -unless- the |
120 | * declarator is at EOL in which case it'll be the next useful line |
121 | * so we don't short-circuit out if we don't find the declarator |
122 | */ |
123 | |
94caac6e |
124 | while (s < PL_bufend && isSPACE(*s)) s++; |
125 | if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf))) |
126 | s += strlen(PL_tokenbuf); |
c630715a |
127 | |
128 | DD_DEBUG_S |
94caac6e |
129 | |
0ba8c7aa |
130 | if (dd_flags & DD_HANDLE_NAME) { |
94caac6e |
131 | |
0ba8c7aa |
132 | /* find next word */ |
94caac6e |
133 | |
0ba8c7aa |
134 | s = skipspace(s); |
c630715a |
135 | |
0ba8c7aa |
136 | DD_DEBUG_S |
94caac6e |
137 | |
15d0d014 |
138 | /* arg 4 is allow_package */ |
94caac6e |
139 | |
15d0d014 |
140 | s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len); |
0ba8c7aa |
141 | |
142 | DD_DEBUG_S |
c630715a |
143 | |
0ba8c7aa |
144 | if (len) { |
145 | strcpy(found_name, tmpbuf); |
146 | #ifdef DD_DEBUG |
147 | printf("Found %s\n", found_name); |
148 | #endif |
149 | } |
150 | } |
151 | |
152 | if (dd_flags & DD_HANDLE_PROTO) { |
153 | |
154 | s = skipspace(s); |
155 | |
156 | if (*s == '(') { /* found a prototype-ish thing */ |
157 | save_s = s; |
158 | s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */ |
159 | if (SvPOK(PL_lex_stuff)) { |
160 | #ifdef DD_DEBUG |
161 | printf("Found proto %s\n", SvPVX(PL_lex_stuff)); |
162 | #endif |
163 | found_proto = SvPVX(PL_lex_stuff); |
53e3ab32 |
164 | if (len) /* foo name () => foo name X, only foo parsed so works */ |
165 | *save_s++ = ' '; |
166 | else /* foo () => foo =X, TOKEN('&') won't handle foo X */ |
167 | *save_s++ = '='; |
0ba8c7aa |
168 | *save_s++ = 'X'; |
169 | while (save_s < s) { |
170 | *save_s++ = ' '; |
171 | } |
172 | #ifdef DD_DEBUG |
173 | printf("Curbuf %s\n", PL_bufptr); |
174 | #endif |
175 | } |
176 | } |
177 | } |
178 | |
53e3ab32 |
179 | if (!len) |
180 | found_name[0] = 0; |
181 | |
182 | #ifdef DD_DEBUG |
183 | printf("Calling init_declare\n"); |
184 | #endif |
185 | cb_args[0] = HvNAME(stash); |
186 | cb_args[1] = GvNAME(kGVOP_gv); |
9026391e |
187 | cb_args[2] = HvNAME(PL_curstash); |
188 | cb_args[3] = found_name; |
189 | cb_args[4] = found_proto; |
190 | cb_args[5] = NULL; |
53e3ab32 |
191 | |
192 | if (len && found_proto) |
193 | in_declare = 2; |
194 | else if (len || found_proto) |
195 | in_declare = 1; |
196 | if (found_proto) |
197 | PL_lex_stuff = Nullsv; |
198 | s = skipspace(s); |
199 | #ifdef DD_DEBUG |
200 | printf("cur buf: %s\n", s); |
201 | printf("bufend at: %i\n", PL_bufend - s); |
202 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
203 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
204 | #endif |
205 | if (*s++ == '{') { |
206 | call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args); |
207 | SPAGAIN; |
208 | retstr = POPpx; |
209 | PUTBACK; |
210 | if (retstr && strlen(retstr)) { |
0ba8c7aa |
211 | #ifdef DD_DEBUG |
53e3ab32 |
212 | printf("Got string %s\n", retstr); |
0ba8c7aa |
213 | #endif |
53e3ab32 |
214 | SvGROW(PL_linestr, strlen(retstr)); |
215 | memmove(s+strlen(retstr), s, (PL_bufend - s)+1); |
216 | memmove(s, retstr, strlen(retstr)); |
217 | PL_bufend += strlen(retstr); |
218 | #ifdef DD_DEBUG |
219 | printf("cur buf: %s\n", s); |
220 | printf("bufend at: %i\n", PL_bufend - s); |
221 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
222 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
223 | #endif |
224 | } |
225 | } else { |
94caac6e |
226 | call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args); |
94caac6e |
227 | } |
53e3ab32 |
228 | return o; |
229 | } |
230 | |
231 | STATIC OP *dd_ck_lineseq(pTHX_ OP *o) { |
232 | AV* pad_inject_list; |
233 | SV** to_inject_ref; |
234 | int i, pad_inject_list_last; |
235 | |
d62484bb |
236 | o = dd_old_ck_lineseq(aTHX_ o); |
53e3ab32 |
237 | |
238 | pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE); |
239 | if (!pad_inject_list) |
240 | return o; |
241 | |
242 | pad_inject_list_last = av_len(pad_inject_list); |
243 | |
244 | if (pad_inject_list_last == -1) |
245 | return o; |
246 | |
247 | for (i = 0; i <= pad_inject_list_last; i++) { |
248 | to_inject_ref = av_fetch(pad_inject_list, i, FALSE); |
249 | if (to_inject_ref && SvPOK(*to_inject_ref)) { |
250 | #ifdef DD_DEBUG |
251 | printf("Injecting %s into pad\n", SvPVX(*to_inject_ref)); |
252 | #endif |
253 | allocmy(SvPVX(*to_inject_ref)); |
254 | } |
255 | } |
256 | |
257 | av_clear(pad_inject_list); |
94caac6e |
258 | |
259 | return o; |
260 | } |
261 | |
262 | static int initialized = 0; |
263 | |
264 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
265 | |
266 | PROTOTYPES: DISABLE |
267 | |
268 | void |
269 | setup() |
270 | CODE: |
271 | if (!initialized++) { |
272 | dd_old_ck_rv2cv = PL_check[OP_RV2CV]; |
273 | PL_check[OP_RV2CV] = dd_ck_rv2cv; |
53e3ab32 |
274 | dd_old_ck_lineseq = PL_check[OP_LINESEQ]; |
275 | PL_check[OP_LINESEQ] = dd_ck_lineseq; |
94caac6e |
276 | } |
277 | |
278 | void |
279 | teardown() |
280 | CODE: |
281 | /* ensure we only uninit when number of teardown calls matches |
282 | number of setup calls */ |
283 | if (initialized && !--initialized) { |
284 | PL_check[OP_RV2CV] = dd_old_ck_rv2cv; |
285 | } |