3 #define PERL_NO_GET_CONTEXT
8 #include "stolen_chunk_of_toke.c"
16 #define DD_HANDLE_NAME 1
17 #define DD_HANDLE_PROTO 2
18 #define DD_HANDLE_PACKAGE 8
21 #define DD_DEBUG_S printf("Buffer: %s\n", s);
27 #define LEX_INTERPNORMAL 9
29 /* placeholders for PL_check entries we wrap */
31 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
32 STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op);
34 /* flag to trigger removal of temporary declaree sub */
36 static int in_declare = 0;
38 /* replacement PL_check rv2cv entry */
40 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
44 char tmpbuf[sizeof PL_tokenbuf];
45 char found_name[sizeof PL_tokenbuf];
46 char* found_proto = NULL;
50 SV** is_declarator_pack_ref;
51 HV* is_declarator_pack_hash;
52 SV** is_declarator_flag_ref;
55 dSP; /* define stack pointer for later call stuff */
57 STRLEN n_a; /* for POPpx */
59 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
63 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
68 kid = cUNOPo->op_first;
70 if (kid->op_type != OP_GV) /* not a GV so ignore */
73 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
74 return o; /* not lexing? */
76 stash = GvSTASH(kGVOP_gv);
79 printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
82 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
87 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
88 strlen(HvNAME(stash)), FALSE);
90 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
91 return o; /* not a hashref */
93 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
95 is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
96 strlen(GvNAME(kGVOP_gv)), FALSE);
98 /* requires SvIOK as well as TRUE since flags not being an int is useless */
100 if (!is_declarator_flag_ref
101 || !SvIOK(*is_declarator_flag_ref)
102 || !SvTRUE(*is_declarator_flag_ref))
105 dd_flags = SvIVX(*is_declarator_flag_ref);
108 printf("dd_flags are: %i\n", dd_flags);
111 s = PL_bufptr; /* copy the current buffer pointer */
116 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
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
125 while (s < PL_bufend && isSPACE(*s)) s++;
126 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
127 s += strlen(PL_tokenbuf);
131 if (dd_flags & DD_HANDLE_NAME) {
139 /* arg 4 is allow_package */
141 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
146 strcpy(found_name, tmpbuf);
148 printf("Found %s\n", found_name);
153 if (dd_flags & DD_HANDLE_PROTO) {
157 if (*s == '(') { /* found a prototype-ish thing */
159 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
160 if (SvPOK(PL_lex_stuff)) {
162 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
164 found_proto = SvPVX(PL_lex_stuff);
165 if (len) /* foo name () => foo name X, only foo parsed so works */
167 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
174 printf("Curbuf %s\n", PL_bufptr);
184 printf("Calling init_declare\n");
186 cb_args[0] = HvNAME(stash);
187 cb_args[1] = GvNAME(kGVOP_gv);
188 cb_args[2] = HvNAME(PL_curstash);
189 cb_args[3] = found_name;
190 cb_args[4] = found_proto;
193 if (len && found_proto)
195 else if (len || found_proto)
198 PL_lex_stuff = Nullsv;
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));
207 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
211 if (retstr && strlen(retstr)) {
213 printf("Got string %s\n", retstr);
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);
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));
227 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
232 STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
235 int i, pad_inject_list_last;
237 o = dd_old_ck_lineseq(o);
239 pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
240 if (!pad_inject_list)
243 pad_inject_list_last = av_len(pad_inject_list);
245 if (pad_inject_list_last == -1)
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)) {
252 printf("Injecting %s into pad\n", SvPVX(*to_inject_ref));
254 allocmy(SvPVX(*to_inject_ref));
258 av_clear(pad_inject_list);
263 static int initialized = 0;
265 MODULE = Devel::Declare PACKAGE = Devel::Declare
272 if (!initialized++) {
273 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
274 PL_check[OP_RV2CV] = dd_ck_rv2cv;
275 dd_old_ck_lineseq = PL_check[OP_LINESEQ];
276 PL_check[OP_LINESEQ] = dd_ck_lineseq;
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;