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; |
9026391e |
54 | char* cb_args[6]; |
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); |
9026391e |
188 | cb_args[2] = HvNAME(PL_curstash); |
189 | cb_args[3] = found_name; |
190 | cb_args[4] = found_proto; |
191 | cb_args[5] = NULL; |
53e3ab32 |
192 | |
193 | if (len && found_proto) |
194 | in_declare = 2; |
195 | else if (len || found_proto) |
196 | in_declare = 1; |
197 | if (found_proto) |
198 | PL_lex_stuff = Nullsv; |
199 | s = skipspace(s); |
200 | #ifdef DD_DEBUG |
201 | printf("cur buf: %s\n", s); |
202 | printf("bufend at: %i\n", PL_bufend - s); |
203 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
204 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
205 | #endif |
206 | if (*s++ == '{') { |
207 | call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args); |
208 | SPAGAIN; |
209 | retstr = POPpx; |
210 | PUTBACK; |
211 | if (retstr && strlen(retstr)) { |
0ba8c7aa |
212 | #ifdef DD_DEBUG |
53e3ab32 |
213 | printf("Got string %s\n", retstr); |
0ba8c7aa |
214 | #endif |
53e3ab32 |
215 | SvGROW(PL_linestr, strlen(retstr)); |
216 | memmove(s+strlen(retstr), s, (PL_bufend - s)+1); |
217 | memmove(s, retstr, strlen(retstr)); |
218 | PL_bufend += strlen(retstr); |
219 | #ifdef DD_DEBUG |
220 | printf("cur buf: %s\n", s); |
221 | printf("bufend at: %i\n", PL_bufend - s); |
222 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
223 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
224 | #endif |
225 | } |
226 | } else { |
94caac6e |
227 | call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args); |
94caac6e |
228 | } |
53e3ab32 |
229 | return o; |
230 | } |
231 | |
232 | STATIC OP *dd_ck_lineseq(pTHX_ OP *o) { |
233 | AV* pad_inject_list; |
234 | SV** to_inject_ref; |
235 | int i, pad_inject_list_last; |
236 | |
d62484bb |
237 | o = dd_old_ck_lineseq(aTHX_ o); |
53e3ab32 |
238 | |
239 | pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE); |
240 | if (!pad_inject_list) |
241 | return o; |
242 | |
243 | pad_inject_list_last = av_len(pad_inject_list); |
244 | |
245 | if (pad_inject_list_last == -1) |
246 | return o; |
247 | |
248 | for (i = 0; i <= pad_inject_list_last; i++) { |
249 | to_inject_ref = av_fetch(pad_inject_list, i, FALSE); |
250 | if (to_inject_ref && SvPOK(*to_inject_ref)) { |
251 | #ifdef DD_DEBUG |
252 | printf("Injecting %s into pad\n", SvPVX(*to_inject_ref)); |
253 | #endif |
254 | allocmy(SvPVX(*to_inject_ref)); |
255 | } |
256 | } |
257 | |
258 | av_clear(pad_inject_list); |
94caac6e |
259 | |
260 | return o; |
261 | } |
262 | |
263 | static int initialized = 0; |
264 | |
265 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
266 | |
267 | PROTOTYPES: DISABLE |
268 | |
269 | void |
270 | setup() |
271 | CODE: |
272 | if (!initialized++) { |
273 | dd_old_ck_rv2cv = PL_check[OP_RV2CV]; |
274 | PL_check[OP_RV2CV] = dd_ck_rv2cv; |
53e3ab32 |
275 | dd_old_ck_lineseq = PL_check[OP_LINESEQ]; |
276 | PL_check[OP_LINESEQ] = dd_ck_lineseq; |
94caac6e |
277 | } |
278 | |
279 | void |
280 | teardown() |
281 | CODE: |
282 | /* ensure we only uninit when number of teardown calls matches |
283 | number of setup calls */ |
284 | if (initialized && !--initialized) { |
285 | PL_check[OP_RV2CV] = dd_old_ck_rv2cv; |
286 | } |