3 #define PERL_NO_GET_CONTEXT
8 #include "stolen_chunk_of_toke.c"
14 #define DD_HANDLE_NAME 1
15 #define DD_HANDLE_PROTO 2
18 #define DD_DEBUG_S printf("Buffer: %s\n", s);
24 #define LEX_INTERPNORMAL 9
26 /* placeholders for PL_check entries we wrap */
28 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
29 STATIC OP *(*dd_old_ck_nextstate)(pTHX_ OP *op);
31 /* flag to trigger removal of temporary declaree sub */
33 static int in_declare = 0;
35 /* replacement PL_check rv2cv entry */
37 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
41 char tmpbuf[sizeof PL_tokenbuf];
42 char found_name[sizeof PL_tokenbuf];
43 char* found_proto = NULL;
47 SV** is_declarator_pack_ref;
48 HV* is_declarator_pack_hash;
49 SV** is_declarator_flag_ref;
53 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
57 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
62 kid = cUNOPo->op_first;
64 if (kid->op_type != OP_GV) /* not a GV so ignore */
67 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
68 return o; /* not lexing? */
70 stash = GvSTASH(kGVOP_gv);
73 printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
76 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
81 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
82 strlen(HvNAME(stash)), FALSE);
84 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
85 return o; /* not a hashref */
87 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
89 is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
90 strlen(GvNAME(kGVOP_gv)), FALSE);
92 /* requires SvIOK as well as TRUE since flags not being an int is useless */
94 if (!is_declarator_flag_ref
95 || !SvIOK(*is_declarator_flag_ref)
96 || !SvTRUE(*is_declarator_flag_ref))
99 dd_flags = SvIVX(*is_declarator_flag_ref);
102 printf("dd_flags are: %i\n", dd_flags);
105 s = PL_bufptr; /* copy the current buffer pointer */
110 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
114 * buffer will be at the beginning of the declarator, -unless- the
115 * declarator is at EOL in which case it'll be the next useful line
116 * so we don't short-circuit out if we don't find the declarator
119 while (s < PL_bufend && isSPACE(*s)) s++;
120 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
121 s += strlen(PL_tokenbuf);
125 if (dd_flags & DD_HANDLE_NAME) {
133 /* 0 in arg 4 is allow_package - not trying that yet :) */
135 s = scan_word(s, tmpbuf, sizeof tmpbuf, 0, &len);
140 strcpy(found_name, tmpbuf);
142 printf("Found %s\n", found_name);
147 if (dd_flags & DD_HANDLE_PROTO) {
151 if (*s == '(') { /* found a prototype-ish thing */
153 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
154 if (SvPOK(PL_lex_stuff)) {
156 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
158 found_proto = SvPVX(PL_lex_stuff);
165 printf("Curbuf %s\n", PL_bufptr);
171 if (len || found_proto) {
175 printf("Calling init_declare");
177 cb_args[0] = HvNAME(stash);
178 cb_args[1] = GvNAME(kGVOP_gv);
179 cb_args[2] = found_name;
180 cb_args[3] = found_proto;
182 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
183 if (len && found_proto)
188 PL_lex_stuff = Nullsv;
194 static int initialized = 0;
196 MODULE = Devel::Declare PACKAGE = Devel::Declare
203 if (!initialized++) {
204 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
205 PL_check[OP_RV2CV] = dd_ck_rv2cv;
211 /* ensure we only uninit when number of teardown calls matches
212 number of setup calls */
213 if (initialized && !--initialized) {
214 PL_check[OP_RV2CV] = dd_old_ck_rv2cv;