]> git.vpit.fr Git - perl/modules/indirect.git/blobdiff - indirect.xs
Factor the cloning logic in a separate function
[perl/modules/indirect.git] / indirect.xs
index 4e538b3d63ca7803d4ba7222d9eed839416fba7a..02a13f8059dcab4bad9e56dbc3ef7c8b8783780a 100644 (file)
@@ -134,12 +134,8 @@ typedef struct {
 
 #define PTABLE_NAME ptable_hints
 
-#if I_WORKAROUND_REQUIRE_PROPAGATION
-# define PTABLE_VAL_FREE(V) \
+#define PTABLE_VAL_FREE(V) \
    { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
-#else
-# define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
-#endif
 
 #define pPTBL  pTHX
 #define pPTBL_ pTHX_
@@ -187,6 +183,29 @@ START_MY_CXT
 
 #if I_THREADSAFE
 
+STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
+#define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
+ CLONE_PARAMS  param;
+ AV           *stashes = NULL;
+ SV           *dupsv;
+
+ if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
+  stashes = newAV();
+
+ param.stashes    = stashes;
+ param.flags      = 0;
+ param.proto_perl = owner;
+
+ dupsv = sv_dup(sv, &param);
+
+ if (stashes) {
+  av_undef(stashes);
+  SvREFCNT_dec(stashes);
+ }
+
+ return SvREFCNT_inc(dupsv);
+}
+
 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
  my_cxt_t        *ud = ud_;
  indirect_hint_t *h1 = ent->val;
@@ -194,19 +213,8 @@ STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
 
  *h2 = *h1;
 
- if (ud->owner != aTHX) {
-  SV *val = h1->code;
-  CLONE_PARAMS param;
-  AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
-  param.stashes    = stashes;
-  param.flags      = 0;
-  param.proto_perl = ud->owner;
-  h2->code = sv_dup(val, &param);
-  if (stashes) {
-   av_undef(stashes);
-   SvREFCNT_dec(stashes);
-  }
- }
+ if (ud->owner != aTHX)
+  h2->code = indirect_clone(h1->code, ud->owner);
 
  ptable_hints_store(ud->tbl, ent->key, h2);
  SvREFCNT_inc(h2->code);
@@ -327,7 +335,7 @@ STATIC U32 indirect_hash = 0;
 
 STATIC SV *indirect_hint(pTHX) {
 #define indirect_hint() indirect_hint(aTHX)
- SV *hint, *code;
+ SV *hint;
 #if I_HAS_PERL(5, 9, 5)
  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
                                        NULL,
@@ -391,6 +399,13 @@ STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
  return INT2PTR(const char *, SvUVX(val));
 }
 
+STATIC void indirect_map_delete(pTHX_ const OP *o) {
+#define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
+ dMY_CXT;
+
+ ptable_store(MY_CXT.map, o, NULL);
+}
+
 /* --- Check functions ----------------------------------------------------- */
 
 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
@@ -426,10 +441,13 @@ STATIC OP *indirect_ck_const(pTHX_ OP *o) {
 
  if (indirect_hint()) {
   SV *sv = cSVOPo_sv;
-  if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
+  if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
+   return o;
+  }
  }
 
+ indirect_map_delete(o);
  return o;
 }
 
@@ -494,7 +512,10 @@ STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
  }
 
 done:
- return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
+ o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
+
+ indirect_map_delete(o);
+ return o;
 }
 
 /* ... ck_padany ........................................................... */
@@ -515,9 +536,11 @@ STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
    sv = sv_2mortal(newSVpvn("$", 1));
    sv_catpvn_nomg(sv, s, t - s + 1);
    indirect_map_store(o, s, sv);
+   return o;
   }
  }
 
+ indirect_map_delete(o);
  return o;
 }
 
@@ -544,7 +567,10 @@ STATIC OP *indirect_ck_method(pTHX_ OP *o) {
  }
 
 done:
- return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
+ o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
+
+ indirect_map_delete(o);
+ return o;
 }
 
 /* ... ck_entersub ......................................................... */
@@ -572,6 +598,18 @@ STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
   oop = oop->op_sibling;
   mop = lop->op_last;
 
+  if (!oop)
+   goto done;
+
+  switch (oop->op_type) {
+   case OP_CONST:
+   case OP_RV2SV:
+   case OP_PADSV:
+    break;
+   default:
+    goto done;
+  }
+
   if (mop->op_type == OP_METHOD)
    mop = cUNOPx(mop)->op_first;
   else if (mop->op_type != OP_METHOD_NAMED)