]> git.vpit.fr Git - perl/modules/indirect.git/blobdiff - indirect.xs
Properly handle method_named ops
[perl/modules/indirect.git] / indirect.xs
index f0af0b68c699a2ca828e4ae2ae87de0cc45e8ab1..8a4a1920d9d89fa876ca4fcf01f24730d6d62814 100644 (file)
 
 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
 
-#undef ENTERn
-#if defined(ENTER_with_name) && !I_HAS_PERL(5, 11, 4)
-# define ENTERn(N) ENTER_with_name(N)
-#else
-# define ENTERn(N) ENTER
-#endif
-
-#undef LEAVEn
-#if defined(LEAVE_with_name) && !I_HAS_PERL(5, 11, 4)
-# define LEAVEn(N) LEAVE_with_name(N)
-#else
-# define LEAVEn(N) LEAVE
-#endif
-
 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
-# ifndef PL_lex_inwhat
-#  define PL_lex_inwhat PL_parser->lex_inwhat
-# endif
-# ifndef PL_linestr
-#  define PL_linestr PL_parser->linestr
-# endif
 # ifndef PL_bufptr
 #  define PL_bufptr PL_parser->bufptr
 # endif
 #  define PL_oldbufptr PL_parser->oldbufptr
 # endif
 #else
-# ifndef PL_lex_inwhat
-#  define PL_lex_inwhat PL_Ilex_inwhat
-# endif
-# ifndef PL_linestr
-#  define PL_linestr PL_Ilinestr
-# endif
 # ifndef PL_bufptr
 #  define PL_bufptr PL_Ibufptr
 # endif
@@ -226,11 +200,10 @@ typedef struct {
 
 typedef struct {
 #if I_THREADSAFE
- ptable     *tbl; /* It really is a ptable_hints */
- tTHX        owner;
+ ptable *tbl; /* It really is a ptable_hints */
+ tTHX    owner;
 #endif
- ptable     *map;
- const char *linestr;
+ ptable *map;
 } my_cxt_t;
 
 START_MY_CXT
@@ -288,22 +261,13 @@ STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
  ptable_hints_store(ud->tbl, ent->key, h2);
 }
 
-STATIC void indirect_thread_cleanup(pTHX_ void *);
+#include "reap.h"
 
 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
int *level = ud;
dMY_CXT;
 
- if (*level) {
-  *level = 0;
-  LEAVE;
-  SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
-  ENTER;
- } else {
-  dMY_CXT;
-  PerlMemShared_free(level);
-  ptable_free(MY_CXT.map);
-  ptable_hints_free(MY_CXT.tbl);
- }
+ ptable_free(MY_CXT.map);
+ ptable_hints_free(MY_CXT.tbl);
 }
 
 #endif /* I_THREADSAFE */
@@ -445,18 +409,6 @@ STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_
  STRLEN len;
  dMY_CXT;
 
- /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
-  * In this case the linestr has temporarly changed, but the old buffer should
-  * still be alive somewhere. */
-
- if (!PL_lex_inwhat) {
-  const char *pl_linestr = SvPVX_const(PL_linestr);
-  if (MY_CXT.linestr != pl_linestr) {
-   ptable_clear(MY_CXT.map);
-   MY_CXT.linestr = pl_linestr;
-  }
- }
-
  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
   Newx(oi, 1, indirect_op_info_t);
   ptable_store(MY_CXT.map, o, oi);
@@ -487,9 +439,6 @@ STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
  dMY_CXT;
 
- if (MY_CXT.linestr != SvPVX_const(PL_linestr))
-  return NULL;
-
  return ptable_fetch(MY_CXT.map, o);
 }
 
@@ -672,32 +621,74 @@ STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
  if (indirect_hint()) {
   OP *op = cUNOPo->op_first;
-  const indirect_op_info_t *oi = indirect_map_fetch(op);
-  const char *s = NULL;
+
+  /* Indirect method call is only possible when the method is a bareword, so
+   * don't trip up on $obj->$meth. */
+  if (op && op->op_type == OP_CONST) {
+   const indirect_op_info_t *oi = indirect_map_fetch(op);
+   const char *s = NULL;
+   line_t line;
+   SV *sv;
+
+   if (oi && (s = oi->pos)) {
+    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
+    /* Keep the old line so that we really point to the first line of the
+     * expression. */
+    line = oi->line;
+   } else {
+    sv = cSVOPx_sv(op);
+    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
+     goto done;
+    sv   = sv_mortalcopy(sv);
+    s    = indirect_find(sv, PL_oldbufptr);
+    line = CopLINE(&PL_compiling);
+   }
+
+   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
+   /* o may now be a method_named */
+
+   indirect_map_store(o, s, sv, line);
+   return o;
+  }
+ }
+
+done:
+ o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
+
+ indirect_map_delete(o);
+ return o;
+}
+
+/* ... ck_method_named ..................................................... */
+
+/* "use foo/no foo" compiles its call to import/unimport directly to a
+ * method_named op. */
+
+STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
+
+STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
+ if (indirect_hint()) {
+  const char *s;
   line_t line;
   SV *sv;
 
-  if (oi && (s = oi->pos)) {
-   sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
-   line = oi->line; /* Keep the old line so that we really point to the first */
-  } else {
-   sv = cSVOPx_sv(op);
-   if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
-    goto done;
-   sv   = sv_mortalcopy(sv);
-   s    = indirect_find(sv, PL_oldbufptr);
-   line = CopLINE(&PL_compiling);
-  }
+  sv   = cSVOPo_sv;
+  if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
+   goto done;
+  sv   = sv_mortalcopy(sv);
+  s    = indirect_find(sv, PL_oldbufptr);
+  if (!s)
+   goto done;
+  line = CopLINE(&PL_compiling);
 
-  o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
-  /* o may now be a method_named */
+  o = CALL_FPTR(indirect_old_ck_method_named)(aTHX_ o);
 
   indirect_map_store(o, s, sv, line);
   return o;
  }
 
 done:
- o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
+ o = CALL_FPTR(indirect_old_ck_method_named)(aTHX_ o);
 
  indirect_map_delete(o);
  return o;
@@ -816,21 +807,23 @@ STATIC void indirect_teardown(pTHX_ void *root) {
  ptable_hints_free(MY_CXT.tbl);
 #endif
 
- PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_old_ck_const);
- indirect_old_ck_const    = 0;
- PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
- indirect_old_ck_rv2sv    = 0;
- PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_old_ck_padany);
- indirect_old_ck_padany   = 0;
- PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_old_ck_scope);
- indirect_old_ck_scope    = 0;
- PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
- indirect_old_ck_lineseq  = 0;
-
- PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_old_ck_method);
- indirect_old_ck_method   = 0;
- PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_old_ck_entersub);
- indirect_old_ck_entersub = 0;
+ PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_old_ck_const);
+ indirect_old_ck_const        = 0;
+ PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
+ indirect_old_ck_rv2sv        = 0;
+ PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_old_ck_padany);
+ indirect_old_ck_padany       = 0;
+ PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_old_ck_scope);
+ indirect_old_ck_scope        = 0;
+ PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
+ indirect_old_ck_lineseq      = 0;
+
+ PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_old_ck_method);
+ indirect_old_ck_method       = 0;
+ PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_old_ck_method_named);
+ indirect_old_ck_method_named = 0;
+ PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_old_ck_entersub);
+ indirect_old_ck_entersub     = 0;
 
  indirect_initialized = 0;
 }
@@ -843,28 +836,29 @@ STATIC void indirect_setup(pTHX) {
  {
   MY_CXT_INIT;
 #if I_THREADSAFE
-  MY_CXT.tbl     = ptable_new();
-  MY_CXT.owner   = aTHX;
+  MY_CXT.tbl   = ptable_new();
+  MY_CXT.owner = aTHX;
 #endif
-  MY_CXT.map     = ptable_new();
-  MY_CXT.linestr = NULL;
+  MY_CXT.map   = ptable_new();
  }
 
- indirect_old_ck_const    = PL_check[OP_CONST];
- PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
- indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
- PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
- indirect_old_ck_padany   = PL_check[OP_PADANY];
- PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
- indirect_old_ck_scope    = PL_check[OP_SCOPE];
- PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_ck_scope);
- indirect_old_ck_lineseq  = PL_check[OP_LINESEQ];
- PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_ck_scope);
-
- indirect_old_ck_method   = PL_check[OP_METHOD];
- PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
- indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
- PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
+ indirect_old_ck_const        = PL_check[OP_CONST];
+ PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_ck_const);
+ indirect_old_ck_rv2sv        = PL_check[OP_RV2SV];
+ PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_ck_rv2sv);
+ indirect_old_ck_padany       = PL_check[OP_PADANY];
+ PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_ck_padany);
+ indirect_old_ck_scope        = PL_check[OP_SCOPE];
+ PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_ck_scope);
+ indirect_old_ck_lineseq      = PL_check[OP_LINESEQ];
+ PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_ck_scope);
+
+ indirect_old_ck_method       = PL_check[OP_METHOD];
+ PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_ck_method);
+ indirect_old_ck_method_named = PL_check[OP_METHOD_NAMED];
+ PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_ck_method_named);
+ indirect_old_ck_entersub     = PL_check[OP_ENTERSUB];
+ PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_ck_entersub);
 
 #if I_MULTIPLICITY
  call_atexit(indirect_teardown, aTHX);
@@ -905,8 +899,7 @@ CLONE(...)
 PROTOTYPE: DISABLE
 PREINIT:
  ptable *t;
- int    *level;
-CODE:
+PPCODE:
  {
   my_cxt_t ud;
   dMY_CXT;
@@ -916,18 +909,12 @@ CODE:
  }
  {
   MY_CXT_CLONE;
-  MY_CXT.map     = ptable_new();
-  MY_CXT.linestr = NULL;
-  MY_CXT.tbl     = t;
-  MY_CXT.owner   = aTHX;
- }
- {
-  level = PerlMemShared_malloc(sizeof *level);
-  *level = 1;
-  LEAVEn("sub");
-  SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
-  ENTERn("sub");
+  MY_CXT.map   = ptable_new();
+  MY_CXT.tbl   = t;
+  MY_CXT.owner = aTHX;
  }
+ reap(3, indirect_thread_cleanup, NULL);
+ XSRETURN(0);
 
 #endif