]> git.vpit.fr Git - perl/modules/indirect.git/blobdiff - indirect.xs
Properly set and check the line number of method and object tokens
[perl/modules/indirect.git] / indirect.xs
index 1bd3ffa662c923708e18167f4f2de0d1069a8257..8ff064bc0796a52d89683502c6467986542bedb1 100644 (file)
 # define SvPVX_const SvPVX
 #endif
 
-#ifndef SvREFCNT_inc_simple_NN
-# define SvREFCNT_inc_simple_NN SvREFCNT_inc
+#ifndef SvREFCNT_inc_simple_void_NN
+# ifdef SvREFCNT_inc_simple_NN
+#  define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
+# else
+#  define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
+# endif
 #endif
 
 #ifndef sv_catpvn_nomg
@@ -182,10 +186,10 @@ typedef SV indirect_hint_t;
  * thread cleanup. */
 
 typedef struct {
+ char   *buf;
  STRLEN  pos;
  STRLEN  size;
  STRLEN  len;
- char   *buf;
  line_t  line;
 } indirect_op_info_t;
 
@@ -338,7 +342,7 @@ STATIC SV *indirect_tag(pTHX_ SV *value) {
   value = SvRV(value);
   if (SvTYPE(value) >= SVt_PVCV) {
    code = value;
-   SvREFCNT_inc_simple_NN(code);
+   SvREFCNT_inc_simple_void_NN(code);
   }
  }
 
@@ -394,6 +398,11 @@ STATIC SV *indirect_hint(pTHX) {
  if (IN_PERL_RUNTIME)
   return NULL;
 
+#if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
+ if (!PL_parser)
+  return NULL;
+#endif
+
 #ifdef cop_hints_fetch_pvn
  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
                                                               indirect_hash, 0);
@@ -470,11 +479,27 @@ STATIC void indirect_map_delete(pTHX_ const OP *o) {
 
 /* --- Check functions ----------------------------------------------------- */
 
+STATIC STRLEN indirect_nextline(const char *s, STRLEN len) {
+ STRLEN i;
+
+ for (i = 0; i < len; ++i) {
+  if (s[i] == '\n') {
+   ++i;
+   while (i < len && s[i] == '\r')
+    ++i;
+   break;
+  }
+ }
+
+ return i;
+}
+
 STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
 #define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
  STRLEN len;
- const char *p, *r = SvPV_const(sv, len);
+ const char *p, *r, *t, *u;
 
+ r = SvPV_const(sv, len);
  if (len >= 1 && *r == '$') {
   ++r;
   --len;
@@ -496,7 +521,17 @@ STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
    ++p;
  }
 
- *pos = p - SvPVX_const(PL_linestr);
+ t = SvPV_const(PL_linestr, len);
+ u = t;
+ while (t <= p) {
+  STRLEN i = indirect_nextline(t, len);
+  if (i >= len)
+   break;
+  u    = t;
+  t   += i;
+  len -= i;
+ }
+ *pos = p - u;
 
  return 1;
 }
@@ -775,7 +810,8 @@ STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
   /* When positions are identical, the method and the object must have the
    * same name. But it also means that it is an indirect call, as "foo->foo"
    * results in different positions. */
-  if (moi->pos <= ooi->pos) {
+  if (   moi->line < ooi->line
+      || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
    SV *file;
    dSP;