get_linestr works, callback code works, set_linestr compiles but not tested
matthewt [Wed, 17 Sep 2008 05:16:04 +0000 (05:16 +0000)]
git-svn-id: http://dev.catalyst.perl.org/repos/bast/trunk/Devel-Declare@4815 bd8105ee-0ff8-0310-8827-fb3f25b6796d

Declare.xs
lib/Devel/Declare.pm

index 4eda199..bcde6df 100644 (file)
@@ -51,6 +51,8 @@ int dd_is_declarator(pTHX_ char* name) {
   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);
 
@@ -59,6 +61,8 @@ int dd_is_declarator(pTHX_ char* name) {
 
   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
@@ -76,6 +80,61 @@ int dd_is_declarator(pTHX_ char* name) {
   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);
@@ -371,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 */
@@ -428,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
index 1840ec0..de0ae81 100644 (file)
@@ -196,6 +196,20 @@ sub install_declarator {
   });
 }
 
+sub linestr_callback_const {
+  warn "Linestr_callback_const: @_\n";
+  my $l = get_linestr();
+  warn "linestr: ${l}\n";
+  warn "w/offset: ".substr($l, $_[1])."\n";
+}
+
+sub linestr_callback {
+  my $type = shift;
+  my $meth = "linestr_callback_${type}";
+  __PACKAGE__->can($meth)->(@_);
+  return 'foo';
+}
+
 =head1 NAME
 
 Devel::Declare -