Commit | Line | Data |
e807ee50 |
1 | #define PERL_IN_TOKE_C |
94caac6e |
2 | #define PERL_CORE |
3 | #define PERL_NO_GET_CONTEXT |
94caac6e |
4 | #include "EXTERN.h" |
5 | #include "perl.h" |
6 | #include "XSUB.h" |
7 | #undef printf |
e807ee50 |
8 | #include "stolen_chunk_of_toke.c" |
94caac6e |
9 | #include <stdio.h> |
10 | #include <string.h> |
11 | |
53e3ab32 |
12 | #if 0 |
13 | #define DD_DEBUG |
14 | #endif |
c630715a |
15 | |
0ba8c7aa |
16 | #define DD_HANDLE_NAME 1 |
17 | #define DD_HANDLE_PROTO 2 |
15d0d014 |
18 | #define DD_HANDLE_PACKAGE 8 |
0ba8c7aa |
19 | |
c630715a |
20 | #ifdef DD_DEBUG |
21 | #define DD_DEBUG_S printf("Buffer: %s\n", s); |
22 | #else |
23 | #define DD_DEBUG_S |
24 | #endif |
25 | |
94caac6e |
26 | #define LEX_NORMAL 10 |
27 | #define LEX_INTERPNORMAL 9 |
28 | |
29 | /* placeholders for PL_check entries we wrap */ |
30 | |
31 | STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op); |
53e3ab32 |
32 | STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op); |
94caac6e |
33 | |
34 | /* flag to trigger removal of temporary declaree sub */ |
35 | |
36 | static int in_declare = 0; |
37 | |
38 | /* replacement PL_check rv2cv entry */ |
39 | |
40 | STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { |
41 | OP* kid; |
42 | char* s; |
0ba8c7aa |
43 | char* save_s; |
94caac6e |
44 | char tmpbuf[sizeof PL_tokenbuf]; |
0ba8c7aa |
45 | char found_name[sizeof PL_tokenbuf]; |
46 | char* found_proto = NULL; |
47 | STRLEN len = 0; |
94caac6e |
48 | HV *stash; |
49 | HV* is_declarator; |
50 | SV** is_declarator_pack_ref; |
51 | HV* is_declarator_pack_hash; |
52 | SV** is_declarator_flag_ref; |
0ba8c7aa |
53 | int dd_flags; |
54 | char* cb_args[5]; |
53e3ab32 |
55 | dSP; /* define stack pointer for later call stuff */ |
56 | char* retstr; |
57 | STRLEN n_a; /* for POPpx */ |
94caac6e |
58 | |
59 | o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */ |
60 | |
61 | if (in_declare) { |
62 | cb_args[0] = NULL; |
63 | call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args); |
0ba8c7aa |
64 | in_declare--; |
94caac6e |
65 | return o; |
66 | } |
67 | |
68 | kid = cUNOPo->op_first; |
69 | |
70 | if (kid->op_type != OP_GV) /* not a GV so ignore */ |
71 | return o; |
72 | |
73 | if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL) |
74 | return o; /* not lexing? */ |
75 | |
76 | stash = GvSTASH(kGVOP_gv); |
77 | |
c630715a |
78 | #ifdef DD_DEBUG |
79 | printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv)); |
80 | #endif |
94caac6e |
81 | |
82 | is_declarator = get_hv("Devel::Declare::declarators", FALSE); |
83 | |
84 | if (!is_declarator) |
85 | return o; |
86 | |
87 | is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash), |
88 | strlen(HvNAME(stash)), FALSE); |
89 | |
90 | if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref)) |
91 | return o; /* not a hashref */ |
92 | |
93 | is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref); |
94 | |
95 | is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv), |
96 | strlen(GvNAME(kGVOP_gv)), FALSE); |
97 | |
0ba8c7aa |
98 | /* requires SvIOK as well as TRUE since flags not being an int is useless */ |
99 | |
100 | if (!is_declarator_flag_ref |
101 | || !SvIOK(*is_declarator_flag_ref) |
102 | || !SvTRUE(*is_declarator_flag_ref)) |
94caac6e |
103 | return o; |
104 | |
0ba8c7aa |
105 | dd_flags = SvIVX(*is_declarator_flag_ref); |
106 | |
107 | #ifdef DD_DEBUG |
108 | printf("dd_flags are: %i\n", dd_flags); |
109 | #endif |
110 | |
94caac6e |
111 | s = PL_bufptr; /* copy the current buffer pointer */ |
112 | |
c630715a |
113 | DD_DEBUG_S |
114 | |
115 | #ifdef DD_DEBUG |
0ba8c7aa |
116 | printf("PL_tokenbuf: %s\n", PL_tokenbuf); |
c630715a |
117 | #endif |
118 | |
119 | /* |
120 | * buffer will be at the beginning of the declarator, -unless- the |
121 | * declarator is at EOL in which case it'll be the next useful line |
122 | * so we don't short-circuit out if we don't find the declarator |
123 | */ |
124 | |
94caac6e |
125 | while (s < PL_bufend && isSPACE(*s)) s++; |
126 | if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf))) |
127 | s += strlen(PL_tokenbuf); |
c630715a |
128 | |
129 | DD_DEBUG_S |
94caac6e |
130 | |
0ba8c7aa |
131 | if (dd_flags & DD_HANDLE_NAME) { |
94caac6e |
132 | |
0ba8c7aa |
133 | /* find next word */ |
94caac6e |
134 | |
0ba8c7aa |
135 | s = skipspace(s); |
c630715a |
136 | |
0ba8c7aa |
137 | DD_DEBUG_S |
94caac6e |
138 | |
15d0d014 |
139 | /* arg 4 is allow_package */ |
94caac6e |
140 | |
15d0d014 |
141 | s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len); |
0ba8c7aa |
142 | |
143 | DD_DEBUG_S |
c630715a |
144 | |
0ba8c7aa |
145 | if (len) { |
146 | strcpy(found_name, tmpbuf); |
147 | #ifdef DD_DEBUG |
148 | printf("Found %s\n", found_name); |
149 | #endif |
150 | } |
151 | } |
152 | |
153 | if (dd_flags & DD_HANDLE_PROTO) { |
154 | |
155 | s = skipspace(s); |
156 | |
157 | if (*s == '(') { /* found a prototype-ish thing */ |
158 | save_s = s; |
159 | s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */ |
160 | if (SvPOK(PL_lex_stuff)) { |
161 | #ifdef DD_DEBUG |
162 | printf("Found proto %s\n", SvPVX(PL_lex_stuff)); |
163 | #endif |
164 | found_proto = SvPVX(PL_lex_stuff); |
53e3ab32 |
165 | if (len) /* foo name () => foo name X, only foo parsed so works */ |
166 | *save_s++ = ' '; |
167 | else /* foo () => foo =X, TOKEN('&') won't handle foo X */ |
168 | *save_s++ = '='; |
0ba8c7aa |
169 | *save_s++ = 'X'; |
170 | while (save_s < s) { |
171 | *save_s++ = ' '; |
172 | } |
173 | #ifdef DD_DEBUG |
174 | printf("Curbuf %s\n", PL_bufptr); |
175 | #endif |
176 | } |
177 | } |
178 | } |
179 | |
53e3ab32 |
180 | if (!len) |
181 | found_name[0] = 0; |
182 | |
183 | #ifdef DD_DEBUG |
184 | printf("Calling init_declare\n"); |
185 | #endif |
186 | cb_args[0] = HvNAME(stash); |
187 | cb_args[1] = GvNAME(kGVOP_gv); |
188 | cb_args[2] = found_name; |
189 | cb_args[3] = found_proto; |
190 | cb_args[4] = NULL; |
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 | |
236 | o = dd_old_ck_lineseq(o); |
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 | } |