2 #define PERL_NO_GET_CONTEXT
7 #include "stolen_chunk_of_toke.c"
15 #define DD_HANDLE_NAME 1
16 #define DD_HANDLE_PROTO 2
17 #define DD_HANDLE_PACKAGE 8
20 #define DD_DEBUG_S printf("Buffer: %s\n", s);
26 #define LEX_INTERPNORMAL 9
28 /* placeholders for PL_check entries we wrap */
30 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
31 STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op);
33 /* flag to trigger removal of temporary declaree sub */
35 static int in_declare = 0;
37 /* replacement PL_check rv2cv entry */
39 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
43 char tmpbuf[sizeof PL_tokenbuf];
44 char found_name[sizeof PL_tokenbuf];
45 char* found_proto = NULL;
49 SV** is_declarator_pack_ref;
50 HV* is_declarator_pack_hash;
51 SV** is_declarator_flag_ref;
54 dSP; /* define stack pointer for later call stuff */
56 STRLEN n_a; /* for POPpx */
58 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
62 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
67 kid = cUNOPo->op_first;
69 if (kid->op_type != OP_GV) /* not a GV so ignore */
72 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
73 return o; /* not lexing? */
75 stash = GvSTASH(kGVOP_gv);
78 printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
81 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
86 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
87 strlen(HvNAME(stash)), FALSE);
89 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
90 return o; /* not a hashref */
92 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
94 is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
95 strlen(GvNAME(kGVOP_gv)), FALSE);
97 /* requires SvIOK as well as TRUE since flags not being an int is useless */
99 if (!is_declarator_flag_ref
100 || !SvIOK(*is_declarator_flag_ref)
101 || !SvTRUE(*is_declarator_flag_ref))
104 dd_flags = SvIVX(*is_declarator_flag_ref);
107 printf("dd_flags are: %i\n", dd_flags);
110 s = PL_bufptr; /* copy the current buffer pointer */
115 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
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
124 while (s < PL_bufend && isSPACE(*s)) s++;
125 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
126 s += strlen(PL_tokenbuf);
130 if (dd_flags & DD_HANDLE_NAME) {
138 /* arg 4 is allow_package */
140 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
145 strcpy(found_name, tmpbuf);
147 printf("Found %s\n", found_name);
152 if (dd_flags & DD_HANDLE_PROTO) {
156 if (*s == '(') { /* found a prototype-ish thing */
158 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
159 if (SvPOK(PL_lex_stuff)) {
161 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
163 found_proto = SvPVX(PL_lex_stuff);
164 if (len) /* foo name () => foo name X, only foo parsed so works */
166 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
173 printf("Curbuf %s\n", PL_bufptr);
183 printf("Calling init_declare\n");
185 cb_args[0] = HvNAME(stash);
186 cb_args[1] = GvNAME(kGVOP_gv);
187 cb_args[2] = HvNAME(PL_curstash);
188 cb_args[3] = found_name;
189 cb_args[4] = found_proto;
192 if (len && found_proto)
194 else if (len || found_proto)
197 PL_lex_stuff = Nullsv;
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));
206 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
210 if (retstr && strlen(retstr)) {
212 printf("Got string %s\n", retstr);
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);
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));
226 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
231 STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
234 int i, pad_inject_list_last;
236 o = dd_old_ck_lineseq(aTHX_ o);
238 pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
239 if (!pad_inject_list)
242 pad_inject_list_last = av_len(pad_inject_list);
244 if (pad_inject_list_last == -1)
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)) {
251 printf("Injecting %s into pad\n", SvPVX(*to_inject_ref));
253 allocmy(SvPVX(*to_inject_ref));
257 av_clear(pad_inject_list);
262 static int initialized = 0;
264 MODULE = Devel::Declare PACKAGE = Devel::Declare
271 if (!initialized++) {
272 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
273 PL_check[OP_RV2CV] = dd_ck_rv2cv;
274 dd_old_ck_lineseq = PL_check[OP_LINESEQ];
275 PL_check[OP_LINESEQ] = dd_ck_lineseq;
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;