2 #define PERL_NO_GET_CONTEXT
7 #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, *found_traits = 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 */
162 char *traitstart = s = skipspace(s);
164 while (*s && *s != '{') ++s;
166 int tlen = s - traitstart;
167 Newx(found_traits, tlen+1, char);
168 Copy(traitstart, found_traits, tlen, char);
169 found_traits[tlen] = 0;
171 printf("found traits..... (%s)\n", found_traits);
177 if (SvPOK(PL_lex_stuff)) {
179 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
181 found_proto = SvPVX(PL_lex_stuff);
182 if (len) /* foo name () => foo name X, only foo parsed so works */
184 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
191 printf("Curbuf %s\n", PL_bufptr);
201 printf("Calling init_declare\n");
203 cb_args[0] = HvNAME(stash);
204 cb_args[1] = GvNAME(kGVOP_gv);
205 cb_args[2] = HvNAME(PL_curstash);
206 cb_args[3] = found_name;
207 cb_args[4] = found_proto;
208 cb_args[5] = found_traits;
211 if (len && found_proto)
213 else if (len || found_proto)
216 PL_lex_stuff = Nullsv;
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));
225 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
229 if (retstr && strlen(retstr)) {
230 const int old_len = SvCUR(PL_linestr);
232 printf("Got string %s\n", retstr);
233 printf("retstr len: %d, old_len %d\n", strlen(retstr), old_len);
235 SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
236 memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
237 memmove(s, retstr, strlen(retstr));
238 SvCUR_set(PL_linestr, old_len + strlen(retstr));
239 PL_bufend += strlen(retstr);
241 printf("cur buf: %s\n", s);
242 printf("bufend at: %i\n", PL_bufend - s);
243 printf("linestr: %s\n", SvPVX(PL_linestr));
244 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
248 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
253 STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
256 int i, pad_inject_list_last;
258 o = dd_old_ck_lineseq(aTHX_ o);
260 pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
261 if (!pad_inject_list)
264 pad_inject_list_last = av_len(pad_inject_list);
266 if (pad_inject_list_last == -1)
269 for (i = 0; i <= pad_inject_list_last; i++) {
270 to_inject_ref = av_fetch(pad_inject_list, i, FALSE);
271 if (to_inject_ref && SvPOK(*to_inject_ref)) {
273 printf("Injecting %s into pad\n", SvPVX(*to_inject_ref));
275 allocmy(SvPVX(*to_inject_ref));
279 av_clear(pad_inject_list);
284 static int initialized = 0;
286 MODULE = Devel::Declare PACKAGE = Devel::Declare
293 if (!initialized++) {
294 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
295 PL_check[OP_RV2CV] = dd_ck_rv2cv;
296 dd_old_ck_lineseq = PL_check[OP_LINESEQ];
297 PL_check[OP_LINESEQ] = dd_ck_lineseq;
303 /* ensure we only uninit when number of teardown calls matches
304 number of setup calls */
305 if (initialized && !--initialized) {
306 PL_check[OP_RV2CV] = dd_old_ck_rv2cv;