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_entereval)(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 */
64 printf("Deconstructing declare\n");
65 printf("PL_bufptr: %s\n", PL_bufptr);
66 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
67 printf("linestr: %s\n", SvPVX(PL_linestr));
68 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
70 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
73 printf("PL_bufptr: %s\n", PL_bufptr);
74 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
75 printf("linestr: %s\n", SvPVX(PL_linestr));
76 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
77 printf("actual len: %i\n", strlen(PL_bufptr));
82 kid = cUNOPo->op_first;
84 if (kid->op_type != OP_GV) /* not a GV so ignore */
87 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
88 return o; /* not lexing? */
90 stash = GvSTASH(kGVOP_gv);
93 printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
96 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
101 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
102 strlen(HvNAME(stash)), FALSE);
104 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
105 return o; /* not a hashref */
107 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
109 is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
110 strlen(GvNAME(kGVOP_gv)), FALSE);
112 /* requires SvIOK as well as TRUE since flags not being an int is useless */
114 if (!is_declarator_flag_ref
115 || !SvIOK(*is_declarator_flag_ref)
116 || !SvTRUE(*is_declarator_flag_ref))
119 dd_flags = SvIVX(*is_declarator_flag_ref);
122 printf("dd_flags are: %i\n", dd_flags);
125 s = PL_bufptr; /* copy the current buffer pointer */
130 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
134 * buffer will be at the beginning of the declarator, -unless- the
135 * declarator is at EOL in which case it'll be the next useful line
136 * so we don't short-circuit out if we don't find the declarator
139 while (s < PL_bufend && isSPACE(*s)) s++;
140 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
141 s += strlen(PL_tokenbuf);
145 if (dd_flags & DD_HANDLE_NAME) {
153 /* arg 4 is allow_package */
155 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
160 strcpy(found_name, tmpbuf);
162 printf("Found %s\n", found_name);
167 if (dd_flags & DD_HANDLE_PROTO) {
171 if (*s == '(') { /* found a prototype-ish thing */
173 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
176 char *traitstart = s = skipspace(s);
178 while (*s && *s != '{') ++s;
180 int tlen = s - traitstart;
181 Newx(found_traits, tlen+1, char);
182 Copy(traitstart, found_traits, tlen, char);
183 found_traits[tlen] = 0;
185 printf("found traits..... (%s)\n", found_traits);
191 if (SvPOK(PL_lex_stuff)) {
193 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
195 found_proto = SvPVX(PL_lex_stuff);
196 if (len) /* foo name () => foo name X, only foo parsed so works */
198 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
205 printf("Curbuf %s\n", PL_bufptr);
215 printf("Calling init_declare\n");
217 cb_args[0] = HvNAME(stash);
218 cb_args[1] = GvNAME(kGVOP_gv);
219 cb_args[2] = HvNAME(PL_curstash);
220 cb_args[3] = found_name;
221 cb_args[4] = found_proto;
222 cb_args[5] = found_traits;
225 if (len && found_proto)
227 else if (len || found_proto)
230 PL_lex_stuff = Nullsv;
233 printf("cur buf: %s\n", s);
234 printf("bufend at: %i\n", PL_bufend - s);
235 printf("linestr: %s\n", SvPVX(PL_linestr));
236 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
239 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
243 if (retstr && strlen(retstr)) {
244 const char* old_start = SvPVX(PL_linestr);
246 const int old_len = SvCUR(PL_linestr);
248 printf("Got string %s\n", retstr);
250 SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
251 if (start_diff = SvPVX(PL_linestr) - old_start) {
252 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
254 memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
255 memmove(s, retstr, strlen(retstr));
256 SvCUR_set(PL_linestr, old_len + strlen(retstr));
257 PL_bufend += strlen(retstr);
259 printf("cur buf: %s\n", s);
260 printf("PL_bufptr: %s\n", PL_bufptr);
261 printf("bufend at: %i\n", PL_bufend - s);
262 printf("linestr: %s\n", SvPVX(PL_linestr));
263 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
264 printf("tokenbuf now: %s\n", PL_tokenbuf);
268 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
273 OP* dd_pp_entereval(pTHX) {
280 printf("mangling eval sv\n");
283 sv = sv_2mortal(newSVsv(sv));
286 if (!len || s[len-1] != ';') {
287 if (!(SvFLAGS(sv) & SVs_TEMP))
288 sv = sv_2mortal(newSVsv(sv));
289 sv_catpvn(sv, "\n;", 2);
294 return PL_ppaddr[OP_ENTEREVAL](aTHX);
297 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
298 o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
299 o->op_flags |= OPf_SPECIAL;
300 o->op_ppaddr = dd_pp_entereval;
304 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
306 const I32 count = FILTER_READ(idx+1, sv, maxlen);
307 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
308 /* filter_del(dd_filter_realloc); */
312 static int initialized = 0;
314 MODULE = Devel::Declare PACKAGE = Devel::Declare
321 if (!initialized++) {
322 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
323 PL_check[OP_RV2CV] = dd_ck_rv2cv;
324 dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
325 PL_check[OP_ENTEREVAL] = dd_ck_entereval;
327 filter_add(dd_filter_realloc, NULL);