]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Stop leaking the SVs stored in the hints structure.
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #ifndef NOOP
15 # define NOOP
16 #endif
17
18 #ifndef dNOOP
19 # define dNOOP
20 #endif
21
22 #ifndef Newx
23 # define Newx(v, n, c) New(0, v, n, c)
24 #endif
25
26 #ifndef SvPV_const
27 # define SvPV_const SvPV
28 #endif
29
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
32 #endif
33
34 #ifndef SvPVX_const
35 # define SvPVX_const SvPVX
36 #endif
37
38 #ifndef SvREFCNT_inc_simple_NN
39 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
40 #endif
41
42 #ifndef sv_catpvn_nomg
43 # define sv_catpvn_nomg sv_catpvn
44 #endif
45
46 #ifndef mPUSHp
47 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
48 #endif
49
50 #ifndef mPUSHu
51 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
52 #endif
53
54 #ifndef HvNAME_get
55 # define HvNAME_get(H) HvNAME(H)
56 #endif
57
58 #ifndef HvNAMELEN_get
59 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
60 #endif
61
62 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
63
64 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
65 # ifndef PL_linestr
66 #  define PL_linestr PL_parser->linestr
67 # endif
68 # ifndef PL_bufptr
69 #  define PL_bufptr PL_parser->bufptr
70 # endif
71 # ifndef PL_oldbufptr
72 #  define PL_oldbufptr PL_parser->oldbufptr
73 # endif
74 #else
75 # ifndef PL_linestr
76 #  define PL_linestr PL_Ilinestr
77 # endif
78 # ifndef PL_bufptr
79 #  define PL_bufptr PL_Ibufptr
80 # endif
81 # ifndef PL_oldbufptr
82 #  define PL_oldbufptr PL_Ioldbufptr
83 # endif
84 #endif
85
86 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
87 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
88 #endif
89
90 /* ... Thread safety and multiplicity ...................................... */
91
92 /* Safe unless stated otherwise in Makefile.PL */
93 #ifndef I_FORKSAFE
94 # define I_FORKSAFE 1
95 #endif
96
97 #ifndef I_MULTIPLICITY
98 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
99 #  define I_MULTIPLICITY 1
100 # else
101 #  define I_MULTIPLICITY 0
102 # endif
103 #endif
104 #if I_MULTIPLICITY && !defined(tTHX)
105 # define tTHX PerlInterpreter*
106 #endif
107
108 #if I_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
109 # define I_THREADSAFE 1
110 # ifndef MY_CXT_CLONE
111 #  define MY_CXT_CLONE \
112     dMY_CXT_SV;                                                      \
113     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
114     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
115     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
116 # endif
117 #else
118 # define I_THREADSAFE 0
119 # undef  dMY_CXT
120 # define dMY_CXT      dNOOP
121 # undef  MY_CXT
122 # define MY_CXT       indirect_globaldata
123 # undef  START_MY_CXT
124 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
125 # undef  MY_CXT_INIT
126 # define MY_CXT_INIT  NOOP
127 # undef  MY_CXT_CLONE
128 # define MY_CXT_CLONE NOOP
129 #endif
130
131 /* --- Helpers ------------------------------------------------------------- */
132
133 /* ... Thread-safe hints ................................................... */
134
135 #if I_WORKAROUND_REQUIRE_PROPAGATION
136
137 typedef struct {
138  SV *code;
139  IV  require_tag;
140 } indirect_hint_t;
141
142 #define I_HINT_STRUCT 1
143
144 #define I_HINT_CODE(H) ((H)->code)
145
146 #define I_HINT_FREE(H) {   \
147  indirect_hint_t *h = (H); \
148  SvREFCNT_dec(h->code);    \
149  PerlMemShared_free(h);    \
150 }
151
152 #else  /*  I_WORKAROUND_REQUIRE_PROPAGATION */
153
154 typedef SV indirect_hint_t;
155
156 #define I_HINT_STRUCT 0
157
158 #define I_HINT_CODE(H) (H)
159
160 #define I_HINT_FREE(H) SvREFCNT_dec(H);
161
162 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
163
164 #if I_THREADSAFE
165
166 #define PTABLE_NAME        ptable_hints
167 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
168
169 #define pPTBL  pTHX
170 #define pPTBL_ pTHX_
171 #define aPTBL  aTHX
172 #define aPTBL_ aTHX_
173
174 #include "ptable.h"
175
176 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
177 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
178
179 #endif /* I_THREADSAFE */
180
181 /* Define the op->str ptable here because we need to be able to clean it during
182  * thread cleanup. */
183
184 typedef struct {
185  STRLEN  pos;
186  STRLEN  size;
187  STRLEN  len;
188  char   *buf;
189  line_t  line;
190 } indirect_op_info_t;
191
192 #define PTABLE_NAME        ptable
193 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
194
195 #define pPTBL  pTHX
196 #define pPTBL_ pTHX_
197 #define aPTBL  aTHX
198 #define aPTBL_ aTHX_
199
200 #include "ptable.h"
201
202 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
203 #define ptable_delete(T, K)   ptable_delete(aTHX_ (T), (K))
204 #define ptable_clear(T)       ptable_clear(aTHX_ (T))
205 #define ptable_free(T)        ptable_free(aTHX_ (T))
206
207 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
208
209 typedef struct {
210 #if I_THREADSAFE
211  ptable *tbl; /* It really is a ptable_hints */
212  tTHX    owner;
213 #endif
214  ptable *map;
215 } my_cxt_t;
216
217 START_MY_CXT
218
219 #if I_THREADSAFE
220
221 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
222 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
223  CLONE_PARAMS  param;
224  AV           *stashes = NULL;
225  SV           *dupsv;
226
227  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
228   stashes = newAV();
229
230  param.stashes    = stashes;
231  param.flags      = 0;
232  param.proto_perl = owner;
233
234  dupsv = sv_dup(sv, &param);
235
236  if (stashes) {
237   av_undef(stashes);
238   SvREFCNT_dec(stashes);
239  }
240
241  return SvREFCNT_inc(dupsv);
242 }
243
244 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
245  my_cxt_t        *ud = ud_;
246  indirect_hint_t *h1 = ent->val;
247  indirect_hint_t *h2;
248
249  if (ud->owner == aTHX)
250   return;
251
252 #if I_HINT_STRUCT
253
254  h2       = PerlMemShared_malloc(sizeof *h2);
255  h2->code = indirect_clone(h1->code, ud->owner);
256 #if I_WORKAROUND_REQUIRE_PROPAGATION
257  h2->require_tag = PTR2IV(indirect_clone(INT2PTR(SV *, h1->require_tag),
258                                          ud->owner));
259 #endif
260
261 #else  /*  I_HINT_STRUCT */
262
263  h2 = indirect_clone(h1, ud->owner);
264
265 #endif /* !I_HINT_STRUCT */
266
267  ptable_hints_store(ud->tbl, ent->key, h2);
268 }
269
270 #include "reap.h"
271
272 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
273  dMY_CXT;
274
275  ptable_free(MY_CXT.map);
276  ptable_hints_free(MY_CXT.tbl);
277 }
278
279 #endif /* I_THREADSAFE */
280
281 #if I_WORKAROUND_REQUIRE_PROPAGATION
282 STATIC IV indirect_require_tag(pTHX) {
283 #define indirect_require_tag() indirect_require_tag(aTHX)
284  const CV *cv, *outside;
285
286  cv = PL_compcv;
287
288  if (!cv) {
289   /* If for some reason the pragma is operational at run-time, try to discover
290    * the current cv in use. */
291   const PERL_SI *si;
292
293   for (si = PL_curstackinfo; si; si = si->si_prev) {
294    I32 cxix;
295
296    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
297     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
298
299     switch (CxTYPE(cx)) {
300      case CXt_SUB:
301      case CXt_FORMAT:
302       /* The propagation workaround is only needed up to 5.10.0 and at that
303        * time format and sub contexts were still identical. And even later the
304        * cv members offsets should have been kept the same. */
305       cv = cx->blk_sub.cv;
306       goto get_enclosing_cv;
307      case CXt_EVAL:
308       cv = cx->blk_eval.cv;
309       goto get_enclosing_cv;
310      default:
311       break;
312     }
313    }
314   }
315
316   cv = PL_main_cv;
317  }
318
319 get_enclosing_cv:
320  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
321   cv = outside;
322
323  return PTR2IV(cv);
324 }
325 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
326
327 STATIC SV *indirect_tag(pTHX_ SV *value) {
328 #define indirect_tag(V) indirect_tag(aTHX_ (V))
329  indirect_hint_t *h;
330  SV *code = NULL;
331
332  if (SvROK(value)) {
333   value = SvRV(value);
334   if (SvTYPE(value) >= SVt_PVCV) {
335    code = value;
336    SvREFCNT_inc_simple_NN(code);
337   }
338  }
339
340 #if I_HINT_STRUCT
341  h = PerlMemShared_malloc(sizeof *h);
342  h->code        = code;
343 # if I_WORKAROUND_REQUIRE_PROPAGATION
344  h->require_tag = indirect_require_tag();
345 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
346 #else  /*  I_HINT_STRUCT */
347  h = code;
348 #endif /* !I_HINT_STRUCT */
349
350 #if I_THREADSAFE
351  {
352   dMY_CXT;
353   /* We only need for the key to be an unique tag for looking up the value later
354    * Allocated memory provides convenient unique identifiers, so that's why we
355    * use the hint as the key itself. */
356   ptable_hints_store(MY_CXT.tbl, h, h);
357  }
358 #endif /* I_THREADSAFE */
359
360  return newSViv(PTR2IV(h));
361 }
362
363 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
364 #define indirect_detag(H) indirect_detag(aTHX_ (H))
365  indirect_hint_t *h;
366
367  if (!(hint && SvIOK(hint)))
368   return NULL;
369
370  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
371 #if I_THREADSAFE
372  {
373   dMY_CXT;
374   h = ptable_fetch(MY_CXT.tbl, h);
375  }
376 #endif /* I_THREADSAFE */
377
378 #if I_WORKAROUND_REQUIRE_PROPAGATION
379  if (indirect_require_tag() != h->require_tag)
380   return NULL;
381 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
382
383  return I_HINT_CODE(h);
384 }
385
386 STATIC U32 indirect_hash = 0;
387
388 STATIC SV *indirect_hint(pTHX) {
389 #define indirect_hint() indirect_hint(aTHX)
390  SV *hint;
391
392  if (IN_PERL_RUNTIME)
393   return NULL;
394
395 #ifdef cop_hints_fetch_pvn
396  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
397                                                               indirect_hash, 0);
398 #elif I_HAS_PERL(5, 9, 5)
399  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
400                                        NULL,
401                                        __PACKAGE__, __PACKAGE_LEN__,
402                                        0,
403                                        indirect_hash);
404 #else
405  {
406   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
407   if (!val)
408    return 0;
409   hint = *val;
410  }
411 #endif
412
413  return indirect_detag(hint);
414 }
415
416 /* ... op -> source position ............................................... */
417
418 STATIC void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
419 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
420  indirect_op_info_t *oi;
421  const char *s;
422  STRLEN len;
423  dMY_CXT;
424
425  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
426   Newx(oi, 1, indirect_op_info_t);
427   ptable_store(MY_CXT.map, o, oi);
428   oi->buf  = NULL;
429   oi->size = 0;
430  }
431
432  if (sv) {
433   s = SvPV_const(sv, len);
434  } else {
435   s   = "{";
436   len = 1;
437  }
438
439  if (len > oi->size) {
440   Safefree(oi->buf);
441   Newx(oi->buf, len, char);
442   oi->size = len;
443  }
444  Copy(s, oi->buf, len, char);
445
446  oi->len  = len;
447  oi->pos  = pos;
448  oi->line = line;
449 }
450
451 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
452 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
453  dMY_CXT;
454
455  return ptable_fetch(MY_CXT.map, o);
456 }
457
458 STATIC void indirect_map_delete(pTHX_ const OP *o) {
459 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
460  dMY_CXT;
461
462  ptable_delete(MY_CXT.map, o);
463 }
464
465 /* --- Check functions ----------------------------------------------------- */
466
467 STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
468 #define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
469  STRLEN len;
470  const char *p, *r = SvPV_const(sv, len);
471
472  if (len >= 1 && *r == '$') {
473   ++r;
474   --len;
475   s = strchr(s, '$');
476   if (!s)
477    return 0;
478  }
479
480  p = s;
481  while (1) {
482   p = strstr(p, r);
483   if (!p)
484    return 0;
485   if (!isALNUM(p[len]))
486    break;
487   /* p points to a word that has r as prefix, skip the rest of the word */
488   p += len + 1;
489   while (isALNUM(*p))
490    ++p;
491  }
492
493  *pos = p - SvPVX_const(PL_linestr);
494
495  return 1;
496 }
497
498 /* ... ck_const ............................................................ */
499
500 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
501
502 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
503  o = indirect_old_ck_const(aTHX_ o);
504
505  if (indirect_hint()) {
506   SV *sv = cSVOPo_sv;
507
508   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
509    STRLEN pos;
510
511    if (indirect_find(sv, PL_oldbufptr, &pos)) {
512     indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
513     return o;
514    }
515   }
516  }
517
518  indirect_map_delete(o);
519  return o;
520 }
521
522 /* ... ck_rv2sv ............................................................ */
523
524 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
525
526 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
527  if (indirect_hint()) {
528   OP *op = cUNOPo->op_first;
529   SV *sv;
530   const char *name = NULL;
531   STRLEN pos, len;
532   OPCODE type = (OPCODE) op->op_type;
533
534   switch (type) {
535    case OP_GV:
536    case OP_GVSV: {
537     GV *gv = cGVOPx_gv(op);
538     name = GvNAME(gv);
539     len  = GvNAMELEN(gv);
540     break;
541    }
542    default:
543     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
544      SV *nsv = cSVOPx_sv(op);
545      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
546       name = SvPV_const(nsv, len);
547     }
548   }
549   if (!name)
550    goto done;
551
552   sv = sv_2mortal(newSVpvn("$", 1));
553   sv_catpvn_nomg(sv, name, len);
554   if (!indirect_find(sv, PL_oldbufptr, &pos)) {
555    /* If it failed, retry without the current stash */
556    const char *stash = HvNAME_get(PL_curstash);
557    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
558
559    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
560        || name[stashlen] != ':' || name[stashlen+1] != ':') {
561     /* Failed again ? Try to remove main */
562     stash = "main";
563     stashlen = 4;
564     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
565         || name[stashlen] != ':' || name[stashlen+1] != ':')
566      goto done;
567    }
568
569    sv_setpvn(sv, "$", 1);
570    stashlen += 2;
571    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
572    if (!indirect_find(sv, PL_oldbufptr, &pos))
573     goto done;
574   }
575
576   o = indirect_old_ck_rv2sv(aTHX_ o);
577
578   indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
579   return o;
580  }
581
582 done:
583  o = indirect_old_ck_rv2sv(aTHX_ o);
584
585  indirect_map_delete(o);
586  return o;
587 }
588
589 /* ... ck_padany ........................................................... */
590
591 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
592
593 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
594  o = indirect_old_ck_padany(aTHX_ o);
595
596  if (indirect_hint()) {
597   SV *sv;
598   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
599
600   while (s < t && isSPACE(*s)) ++s;
601   if (*s == '$' && ++s <= t) {
602    while (s < t && isSPACE(*s)) ++s;
603    while (s < t && isSPACE(*t)) --t;
604    sv = sv_2mortal(newSVpvn("$", 1));
605    sv_catpvn_nomg(sv, s, t - s + 1);
606    indirect_map_store(o, s - SvPVX_const(PL_linestr),
607                          sv, CopLINE(&PL_compiling));
608    return o;
609   }
610  }
611
612  indirect_map_delete(o);
613  return o;
614 }
615
616 /* ... ck_scope ............................................................ */
617
618 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
619 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
620
621 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
622  OP *(*old_ck)(pTHX_ OP *) = 0;
623
624  switch (o->op_type) {
625   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
626   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
627  }
628  o = old_ck(aTHX_ o);
629
630  if (indirect_hint()) {
631   indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
632                         NULL, CopLINE(&PL_compiling));
633   return o;
634  }
635
636  indirect_map_delete(o);
637  return o;
638 }
639
640 /* We don't need to clean the map entries for leave ops because they can only
641  * be created by mutating from a lineseq. */
642
643 /* ... ck_method ........................................................... */
644
645 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
646
647 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
648  if (indirect_hint()) {
649   OP *op = cUNOPo->op_first;
650
651   /* Indirect method call is only possible when the method is a bareword, so
652    * don't trip up on $obj->$meth. */
653   if (op && op->op_type == OP_CONST) {
654    const indirect_op_info_t *oi = indirect_map_fetch(op);
655    STRLEN pos;
656    line_t line;
657    SV *sv;
658
659    if (!oi)
660     goto done;
661
662    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
663    pos  = oi->pos;
664    /* Keep the old line so that we really point to the first line of the
665     * expression. */
666    line = oi->line;
667
668    o = indirect_old_ck_method(aTHX_ o);
669    /* o may now be a method_named */
670
671    indirect_map_store(o, pos, sv, line);
672    return o;
673   }
674  }
675
676 done:
677  o = indirect_old_ck_method(aTHX_ o);
678
679  indirect_map_delete(o);
680  return o;
681 }
682
683 /* ... ck_method_named ..................................................... */
684
685 /* "use foo/no foo" compiles its call to import/unimport directly to a
686  * method_named op. */
687
688 STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
689
690 STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
691  if (indirect_hint()) {
692   STRLEN pos;
693   line_t line;
694   SV *sv;
695
696   sv = cSVOPo_sv;
697   if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
698    goto done;
699   sv = sv_mortalcopy(sv);
700
701   if (!indirect_find(sv, PL_oldbufptr, &pos))
702    goto done;
703   line = CopLINE(&PL_compiling);
704
705   o = indirect_old_ck_method_named(aTHX_ o);
706
707   indirect_map_store(o, pos, sv, line);
708   return o;
709  }
710
711 done:
712  o = indirect_old_ck_method_named(aTHX_ o);
713
714  indirect_map_delete(o);
715  return o;
716 }
717
718 /* ... ck_entersub ......................................................... */
719
720 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
721
722 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
723  SV *code = indirect_hint();
724
725  o = indirect_old_ck_entersub(aTHX_ o);
726
727  if (code) {
728   const indirect_op_info_t *moi, *ooi;
729   OP     *mop, *oop;
730   LISTOP *lop;
731
732   oop = o;
733   do {
734    lop = (LISTOP *) oop;
735    if (!(lop->op_flags & OPf_KIDS))
736     goto done;
737    oop = lop->op_first;
738   } while (oop->op_type != OP_PUSHMARK);
739   oop = oop->op_sibling;
740   mop = lop->op_last;
741
742   if (!oop)
743    goto done;
744
745   switch (oop->op_type) {
746    case OP_CONST:
747    case OP_RV2SV:
748    case OP_PADSV:
749    case OP_SCOPE:
750    case OP_LEAVE:
751     break;
752    default:
753     goto done;
754   }
755
756   if (mop->op_type == OP_METHOD)
757    mop = cUNOPx(mop)->op_first;
758   else if (mop->op_type != OP_METHOD_NAMED)
759    goto done;
760
761   moi = indirect_map_fetch(mop);
762   if (!moi)
763    goto done;
764
765   ooi = indirect_map_fetch(oop);
766   if (!ooi)
767    goto done;
768
769   /* When positions are identical, the method and the object must have the
770    * same name. But it also means that it is an indirect call, as "foo->foo"
771    * results in different positions. */
772   if (moi->pos <= ooi->pos) {
773    SV *file;
774    dSP;
775
776    ENTER;
777    SAVETMPS;
778
779 #ifdef USE_ITHREADS
780    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
781 #else
782    file = sv_mortalcopy(CopFILESV(&PL_compiling));
783 #endif
784
785    PUSHMARK(SP);
786    EXTEND(SP, 4);
787    mPUSHp(ooi->buf, ooi->len);
788    mPUSHp(moi->buf, moi->len);
789    PUSHs(file);
790    mPUSHu(moi->line);
791    PUTBACK;
792
793    call_sv(code, G_VOID);
794
795    PUTBACK;
796
797    FREETMPS;
798    LEAVE;
799   }
800  }
801
802 done:
803  return o;
804 }
805
806 STATIC U32 indirect_initialized = 0;
807
808 STATIC void indirect_teardown(pTHX_ void *root) {
809  if (!indirect_initialized)
810   return;
811
812 #if I_MULTIPLICITY
813  if (aTHX != root)
814   return;
815 #endif
816
817  {
818   dMY_CXT;
819   ptable_free(MY_CXT.map);
820 #if I_THREADSAFE
821   ptable_hints_free(MY_CXT.tbl);
822 #endif
823  }
824
825  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_old_ck_const);
826  indirect_old_ck_const        = 0;
827  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
828  indirect_old_ck_rv2sv        = 0;
829  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_old_ck_padany);
830  indirect_old_ck_padany       = 0;
831  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_old_ck_scope);
832  indirect_old_ck_scope        = 0;
833  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
834  indirect_old_ck_lineseq      = 0;
835
836  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_old_ck_method);
837  indirect_old_ck_method       = 0;
838  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_old_ck_method_named);
839  indirect_old_ck_method_named = 0;
840  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_old_ck_entersub);
841  indirect_old_ck_entersub     = 0;
842
843  indirect_initialized = 0;
844 }
845
846 STATIC void indirect_setup(pTHX) {
847 #define indirect_setup() indirect_setup(aTHX)
848  if (indirect_initialized)
849   return;
850
851  {
852   MY_CXT_INIT;
853 #if I_THREADSAFE
854   MY_CXT.tbl   = ptable_new();
855   MY_CXT.owner = aTHX;
856 #endif
857   MY_CXT.map   = ptable_new();
858  }
859
860  indirect_old_ck_const        = PL_check[OP_CONST];
861  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_ck_const);
862  indirect_old_ck_rv2sv        = PL_check[OP_RV2SV];
863  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_ck_rv2sv);
864  indirect_old_ck_padany       = PL_check[OP_PADANY];
865  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_ck_padany);
866  indirect_old_ck_scope        = PL_check[OP_SCOPE];
867  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_ck_scope);
868  indirect_old_ck_lineseq      = PL_check[OP_LINESEQ];
869  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_ck_scope);
870
871  indirect_old_ck_method       = PL_check[OP_METHOD];
872  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_ck_method);
873  indirect_old_ck_method_named = PL_check[OP_METHOD_NAMED];
874  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_ck_method_named);
875  indirect_old_ck_entersub     = PL_check[OP_ENTERSUB];
876  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_ck_entersub);
877
878 #if I_MULTIPLICITY
879  call_atexit(indirect_teardown, aTHX);
880 #else
881  call_atexit(indirect_teardown, NULL);
882 #endif
883
884  indirect_initialized = 1;
885 }
886
887 STATIC U32 indirect_booted = 0;
888
889 /* --- XS ------------------------------------------------------------------ */
890
891 MODULE = indirect      PACKAGE = indirect
892
893 PROTOTYPES: ENABLE
894
895 BOOT:
896 {
897  if (!indirect_booted++) {
898   HV *stash;
899
900   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
901
902   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
903   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
904   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
905  }
906
907  indirect_setup();
908 }
909
910 #if I_THREADSAFE
911
912 void
913 CLONE(...)
914 PROTOTYPE: DISABLE
915 PREINIT:
916  ptable *t;
917 PPCODE:
918  {
919   my_cxt_t ud;
920   dMY_CXT;
921   ud.tbl   = t = ptable_new();
922   ud.owner = MY_CXT.owner;
923   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
924  }
925  {
926   MY_CXT_CLONE;
927   MY_CXT.map   = ptable_new();
928   MY_CXT.tbl   = t;
929   MY_CXT.owner = aTHX;
930  }
931  reap(3, indirect_thread_cleanup, NULL);
932  XSRETURN(0);
933
934 #endif
935
936 SV *
937 _tag(SV *value)
938 PROTOTYPE: $
939 CODE:
940  RETVAL = indirect_tag(value);
941 OUTPUT:
942  RETVAL