get_linestr works, callback code works, set_linestr compiles but not tested
[p5sagit/Devel-Declare.git] / Declare.xs
index 28950eb..bcde6df 100644 (file)
 
 static int in_declare = 0;
 
+/* thing that decides whether we're dealing with a declarator */
+
+int dd_is_declarator(pTHX_ char* name) {
+  HV* is_declarator;
+  SV** is_declarator_pack_ref;
+  HV* is_declarator_pack_hash;
+  SV** is_declarator_flag_ref;
+  int dd_flags;
+
+  is_declarator = get_hv("Devel::Declare::declarators", FALSE);
+
+  if (!is_declarator)
+    return -1;
+
+  /* $declarators{$current_package_name} */
+
+  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
+                             strlen(HvNAME(PL_curstash)), FALSE);
+
+  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
+    return -1; /* not a hashref */
+
+  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
+
+  /* $declarators{$current_package_name}{$name} */
+
+  is_declarator_flag_ref = hv_fetch(
+    is_declarator_pack_hash, name,
+    strlen(name), FALSE
+  );
+
+  /* requires SvIOK as well as TRUE since flags not being an int is useless */
+
+  if (!is_declarator_flag_ref
+        || !SvIOK(*is_declarator_flag_ref) 
+        || !SvTRUE(*is_declarator_flag_ref))
+    return -1;
+
+  dd_flags = SvIVX(*is_declarator_flag_ref);
+
+  return dd_flags;
+}
+
+/* callback thingy */
+
+void dd_linestr_callback (pTHX_ char* type, char* name, char* s) {
+
+  char* linestr = SvPVX(PL_linestr);
+  int offset = s - linestr;
+
+  char* new_linestr;
+  int count;
+
+  dSP;
+
+  ENTER;
+  SAVETMPS;
+
+  PUSHMARK(SP);
+  XPUSHs(sv_2mortal(newSVpv(type, 0)));
+  XPUSHs(sv_2mortal(newSVpv(name, 0)));
+  XPUSHs(sv_2mortal(newSViv(offset)));
+  PUTBACK;
+
+  count = call_pv("Devel::Declare::linestr_callback", G_SCALAR);
+
+  SPAGAIN;
+
+  if (count != 1)
+    Perl_croak(aTHX_ "linestr_callback didn't return a value, bailing out");
+
+  printf("linestr_callback returned: %s\n", POPp);
+
+  PUTBACK;
+  FREETMPS;
+  LEAVE;
+}
+
+char* dd_get_linestr(pTHX) {
+  return SvPVX(PL_linestr);
+}
+
+void dd_set_linestr(pTHX_ char* new_value) {
+  int new_len = strlen(new_value);
+  char* old_linestr = SvPVX(PL_linestr);
+
+  SvGROW(PL_linestr, strlen(new_value));
+
+  if (SvPVX(PL_linestr) != old_linestr)
+    Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
+
+  memcpy(SvPVX(PL_linestr), new_value, new_len+1);
+
+  SvCUR_set(PL_linestr, new_len);
+
+  PL_bufend = SvPVX(PL_linestr) + new_len;
+}
+
 /* replacement PL_check rv2cv entry */
 
 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
@@ -49,11 +147,6 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   char found_name[sizeof PL_tokenbuf];
   char* found_proto = NULL, *found_traits = NULL;
   STRLEN len = 0;
-  HV *stash;
-  HV* is_declarator;
-  SV** is_declarator_pack_ref;
-  HV* is_declarator_pack_hash;
-  SV** is_declarator_flag_ref;
   int dd_flags;
   char* cb_args[6];
   dSP; /* define stack pointer for later call stuff */
@@ -91,37 +184,18 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
     return o; /* not lexing? */
 
-  stash = GvSTASH(kGVOP_gv);
+  /* I was doing this, but the CONST wrap can't so it didn't gain anything
+  stash = GvSTASH(kGVOP_gv); */
 
 #ifdef DD_DEBUG
-  printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
+  printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
 #endif
 
-  is_declarator = get_hv("Devel::Declare::declarators", FALSE);
-
-  if (!is_declarator)
-    return o;
-
-  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
-                             strlen(HvNAME(stash)), FALSE);
-
-  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
-    return o; /* not a hashref */
+  dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
 
-  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
-
-  is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
-                                strlen(GvNAME(kGVOP_gv)), FALSE);
-
-  /* requires SvIOK as well as TRUE since flags not being an int is useless */
-
-  if (!is_declarator_flag_ref
-        || !SvIOK(*is_declarator_flag_ref) 
-        || !SvTRUE(*is_declarator_flag_ref))
+  if (dd_flags == -1)
     return o;
 
-  dd_flags = SvIVX(*is_declarator_flag_ref);
-
 #ifdef DD_DEBUG
   printf("dd_flags are: %i\n", dd_flags);
 #endif
@@ -224,7 +298,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
 #ifdef DD_DEBUG
   printf("Calling init_declare\n");
 #endif
-  cb_args[0] = HvNAME(stash);
+  cb_args[0] = HvNAME(PL_curstash);
   cb_args[1] = GvNAME(kGVOP_gv);
   cb_args[2] = HvNAME(PL_curstash);
   cb_args[3] = found_name;
@@ -245,6 +319,7 @@ STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
   printf("linestr: %s\n", SvPVX(PL_linestr));
   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
 #endif
+  
   if (*s++ == '{') {
     call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
     SPAGAIN;
@@ -324,10 +399,6 @@ static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
 
 STATIC OP *dd_ck_const(pTHX_ OP *o) {
-  HV* is_declarator;
-  SV** is_declarator_pack_ref;
-  HV* is_declarator_pack_hash;
-  SV** is_declarator_flag_ref;
   int dd_flags;
   char* s;
   char tmpbuf[sizeof PL_tokenbuf];
@@ -336,34 +407,15 @@ STATIC OP *dd_ck_const(pTHX_ OP *o) {
 
   o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
 
-  is_declarator = get_hv("Devel::Declare::declarators", FALSE);
-
-  is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
-                             strlen(HvNAME(PL_curstash)), FALSE);
-
-  if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
-    return o; /* not a hashref */
-
-  is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
-
   /* don't try and look this up if it's not a string const */
   if (!SvPOK(cSVOPo->op_sv))
     return o;
 
-  is_declarator_flag_ref = hv_fetch(
-    is_declarator_pack_hash, SvPVX(cSVOPo->op_sv),
-    strlen(SvPVX(cSVOPo->op_sv)), FALSE
-  );
-
-  /* requires SvIOK as well as TRUE since flags not being an int is useless */
+  dd_flags = dd_is_declarator(aTHX_ SvPVX(cSVOPo->op_sv));
 
-  if (!is_declarator_flag_ref
-        || !SvIOK(*is_declarator_flag_ref) 
-        || !SvTRUE(*is_declarator_flag_ref))
+  if (dd_flags == -1)
     return o;
 
-  dd_flags = SvIVX(*is_declarator_flag_ref);
-
   if (!(dd_flags & DD_HANDLE_NAME))
     return o; /* if we're not handling name, method intuiting not an issue */
 
@@ -378,6 +430,8 @@ STATIC OP *dd_ck_const(pTHX_ OP *o) {
   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
     s += strlen(PL_tokenbuf);
 
+  /* dd_linestr_callback(aTHX_ "const", SvPVX(cSVOPo->op_sv), s); */
+
   DD_DEBUG_S
 
   /* find next word */
@@ -435,3 +489,10 @@ setup()
     PL_check[OP_CONST] = dd_ck_const;
   }
   filter_add(dd_filter_realloc, NULL);
+
+char*
+get_linestr()
+  CODE:
+    RETVAL = dd_get_linestr(aTHX);
+  OUTPUT:
+    RETVAL