Make indirect play nice with Devel::CallParser
[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  char   *buf;
186  STRLEN  pos;
187  STRLEN  size;
188  STRLEN  len;
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  SV     *global_code;
216 } my_cxt_t;
217
218 START_MY_CXT
219
220 #if I_THREADSAFE
221
222 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
223 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
224  CLONE_PARAMS  param;
225  AV           *stashes = NULL;
226  SV           *dupsv;
227
228  if (!sv)
229   return NULL;
230
231  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
232   stashes = newAV();
233
234  param.stashes    = stashes;
235  param.flags      = 0;
236  param.proto_perl = owner;
237
238  dupsv = sv_dup(sv, &param);
239
240  if (stashes) {
241   av_undef(stashes);
242   SvREFCNT_dec(stashes);
243  }
244
245  return SvREFCNT_inc(dupsv);
246 }
247
248 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
249  my_cxt_t        *ud = ud_;
250  indirect_hint_t *h1 = ent->val;
251  indirect_hint_t *h2;
252
253  if (ud->owner == aTHX)
254   return;
255
256 #if I_HINT_STRUCT
257
258  h2       = PerlMemShared_malloc(sizeof *h2);
259  h2->code = indirect_clone(h1->code, ud->owner);
260 #if I_WORKAROUND_REQUIRE_PROPAGATION
261  h2->require_tag = PTR2IV(indirect_clone(INT2PTR(SV *, h1->require_tag),
262                                          ud->owner));
263 #endif
264
265 #else  /*  I_HINT_STRUCT */
266
267  h2 = indirect_clone(h1, ud->owner);
268
269 #endif /* !I_HINT_STRUCT */
270
271  ptable_hints_store(ud->tbl, ent->key, h2);
272 }
273
274 #include "reap.h"
275
276 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
277  dMY_CXT;
278
279  SvREFCNT_dec(MY_CXT.global_code);
280  ptable_free(MY_CXT.map);
281  ptable_hints_free(MY_CXT.tbl);
282 }
283
284 #endif /* I_THREADSAFE */
285
286 #if I_WORKAROUND_REQUIRE_PROPAGATION
287 STATIC IV indirect_require_tag(pTHX) {
288 #define indirect_require_tag() indirect_require_tag(aTHX)
289  const CV *cv, *outside;
290
291  cv = PL_compcv;
292
293  if (!cv) {
294   /* If for some reason the pragma is operational at run-time, try to discover
295    * the current cv in use. */
296   const PERL_SI *si;
297
298   for (si = PL_curstackinfo; si; si = si->si_prev) {
299    I32 cxix;
300
301    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
302     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
303
304     switch (CxTYPE(cx)) {
305      case CXt_SUB:
306      case CXt_FORMAT:
307       /* The propagation workaround is only needed up to 5.10.0 and at that
308        * time format and sub contexts were still identical. And even later the
309        * cv members offsets should have been kept the same. */
310       cv = cx->blk_sub.cv;
311       goto get_enclosing_cv;
312      case CXt_EVAL:
313       cv = cx->blk_eval.cv;
314       goto get_enclosing_cv;
315      default:
316       break;
317     }
318    }
319   }
320
321   cv = PL_main_cv;
322  }
323
324 get_enclosing_cv:
325  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
326   cv = outside;
327
328  return PTR2IV(cv);
329 }
330 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
331
332 STATIC SV *indirect_tag(pTHX_ SV *value) {
333 #define indirect_tag(V) indirect_tag(aTHX_ (V))
334  indirect_hint_t *h;
335  SV *code = NULL;
336
337  if (SvROK(value)) {
338   value = SvRV(value);
339   if (SvTYPE(value) >= SVt_PVCV) {
340    code = value;
341    SvREFCNT_inc_simple_NN(code);
342   }
343  }
344
345 #if I_HINT_STRUCT
346  h = PerlMemShared_malloc(sizeof *h);
347  h->code        = code;
348 # if I_WORKAROUND_REQUIRE_PROPAGATION
349  h->require_tag = indirect_require_tag();
350 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
351 #else  /*  I_HINT_STRUCT */
352  h = code;
353 #endif /* !I_HINT_STRUCT */
354
355 #if I_THREADSAFE
356  {
357   dMY_CXT;
358   /* We only need for the key to be an unique tag for looking up the value later
359    * Allocated memory provides convenient unique identifiers, so that's why we
360    * use the hint as the key itself. */
361   ptable_hints_store(MY_CXT.tbl, h, h);
362  }
363 #endif /* I_THREADSAFE */
364
365  return newSViv(PTR2IV(h));
366 }
367
368 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
369 #define indirect_detag(H) indirect_detag(aTHX_ (H))
370  indirect_hint_t *h;
371 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
372  dMY_CXT;
373 #endif
374
375  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
376 #if I_THREADSAFE
377  h = ptable_fetch(MY_CXT.tbl, h);
378 #endif /* I_THREADSAFE */
379
380 #if I_WORKAROUND_REQUIRE_PROPAGATION
381  if (indirect_require_tag() != h->require_tag)
382   return MY_CXT.global_code;
383 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
384
385  return I_HINT_CODE(h);
386 }
387
388 STATIC U32 indirect_hash = 0;
389
390 STATIC SV *indirect_hint(pTHX) {
391 #define indirect_hint() indirect_hint(aTHX)
392  SV *hint = NULL;
393
394  if (IN_PERL_RUNTIME)
395   return NULL;
396
397 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
398  if (!PL_parser)
399   return NULL;
400 #endif
401
402 #ifdef cop_hints_fetch_pvn
403  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
404                                                               indirect_hash, 0);
405 #elif I_HAS_PERL(5, 9, 5)
406  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
407                                        NULL,
408                                        __PACKAGE__, __PACKAGE_LEN__,
409                                        0,
410                                        indirect_hash);
411 #else
412  {
413   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
414   if (val)
415    hint = *val;
416  }
417 #endif
418
419  if (hint && SvIOK(hint))
420   return indirect_detag(hint);
421  else {
422   dMY_CXT;
423   return MY_CXT.global_code;
424  }
425 }
426
427 /* ... op -> source position ............................................... */
428
429 STATIC void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
430 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
431  indirect_op_info_t *oi;
432  const char *s;
433  STRLEN len;
434  dMY_CXT;
435
436  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
437   Newx(oi, 1, indirect_op_info_t);
438   ptable_store(MY_CXT.map, o, oi);
439   oi->buf  = NULL;
440   oi->size = 0;
441  }
442
443  if (sv) {
444   s = SvPV_const(sv, len);
445  } else {
446   s   = "{";
447   len = 1;
448  }
449
450  if (len > oi->size) {
451   Safefree(oi->buf);
452   Newx(oi->buf, len, char);
453   oi->size = len;
454  }
455  Copy(s, oi->buf, len, char);
456
457  oi->len  = len;
458  oi->pos  = pos;
459  oi->line = line;
460 }
461
462 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
463 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
464  dMY_CXT;
465
466  return ptable_fetch(MY_CXT.map, o);
467 }
468
469 STATIC void indirect_map_delete(pTHX_ const OP *o) {
470 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
471  dMY_CXT;
472
473  ptable_delete(MY_CXT.map, o);
474 }
475
476 /* --- Check functions ----------------------------------------------------- */
477
478 STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
479 #define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
480  STRLEN len;
481  const char *p, *r = SvPV_const(sv, len);
482
483  if (len >= 1 && *r == '$') {
484   ++r;
485   --len;
486   s = strchr(s, '$');
487   if (!s)
488    return 0;
489  }
490
491  p = s;
492  while (1) {
493   p = strstr(p, r);
494   if (!p)
495    return 0;
496   if (!isALNUM(p[len]))
497    break;
498   /* p points to a word that has r as prefix, skip the rest of the word */
499   p += len + 1;
500   while (isALNUM(*p))
501    ++p;
502  }
503
504  *pos = p - SvPVX_const(PL_linestr);
505
506  return 1;
507 }
508
509 /* ... ck_const ............................................................ */
510
511 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
512
513 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
514  o = indirect_old_ck_const(aTHX_ o);
515
516  if (indirect_hint()) {
517   SV *sv = cSVOPo_sv;
518
519   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
520    STRLEN pos;
521
522    if (indirect_find(sv, PL_oldbufptr, &pos)) {
523     indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
524     return o;
525    }
526   }
527  }
528
529  indirect_map_delete(o);
530  return o;
531 }
532
533 /* ... ck_rv2sv ............................................................ */
534
535 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
536
537 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
538  if (indirect_hint()) {
539   OP *op = cUNOPo->op_first;
540   SV *sv;
541   const char *name = NULL;
542   STRLEN pos, len;
543   OPCODE type = (OPCODE) op->op_type;
544
545   switch (type) {
546    case OP_GV:
547    case OP_GVSV: {
548     GV *gv = cGVOPx_gv(op);
549     name = GvNAME(gv);
550     len  = GvNAMELEN(gv);
551     break;
552    }
553    default:
554     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
555      SV *nsv = cSVOPx_sv(op);
556      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
557       name = SvPV_const(nsv, len);
558     }
559   }
560   if (!name)
561    goto done;
562
563   sv = sv_2mortal(newSVpvn("$", 1));
564   sv_catpvn_nomg(sv, name, len);
565   if (!indirect_find(sv, PL_oldbufptr, &pos)) {
566    /* If it failed, retry without the current stash */
567    const char *stash = HvNAME_get(PL_curstash);
568    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
569
570    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
571        || name[stashlen] != ':' || name[stashlen+1] != ':') {
572     /* Failed again ? Try to remove main */
573     stash = "main";
574     stashlen = 4;
575     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
576         || name[stashlen] != ':' || name[stashlen+1] != ':')
577      goto done;
578    }
579
580    sv_setpvn(sv, "$", 1);
581    stashlen += 2;
582    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
583    if (!indirect_find(sv, PL_oldbufptr, &pos))
584     goto done;
585   }
586
587   o = indirect_old_ck_rv2sv(aTHX_ o);
588
589   indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
590   return o;
591  }
592
593 done:
594  o = indirect_old_ck_rv2sv(aTHX_ o);
595
596  indirect_map_delete(o);
597  return o;
598 }
599
600 /* ... ck_padany ........................................................... */
601
602 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
603
604 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
605  o = indirect_old_ck_padany(aTHX_ o);
606
607  if (indirect_hint()) {
608   SV *sv;
609   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
610
611   while (s < t && isSPACE(*s)) ++s;
612   if (*s == '$' && ++s <= t) {
613    while (s < t && isSPACE(*s)) ++s;
614    while (s < t && isSPACE(*t)) --t;
615    sv = sv_2mortal(newSVpvn("$", 1));
616    sv_catpvn_nomg(sv, s, t - s + 1);
617    indirect_map_store(o, s - SvPVX_const(PL_linestr),
618                          sv, CopLINE(&PL_compiling));
619    return o;
620   }
621  }
622
623  indirect_map_delete(o);
624  return o;
625 }
626
627 /* ... ck_scope ............................................................ */
628
629 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
630 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
631
632 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
633  OP *(*old_ck)(pTHX_ OP *) = 0;
634
635  switch (o->op_type) {
636   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
637   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
638  }
639  o = old_ck(aTHX_ o);
640
641  if (indirect_hint()) {
642   indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
643                         NULL, CopLINE(&PL_compiling));
644   return o;
645  }
646
647  indirect_map_delete(o);
648  return o;
649 }
650
651 /* We don't need to clean the map entries for leave ops because they can only
652  * be created by mutating from a lineseq. */
653
654 /* ... ck_method ........................................................... */
655
656 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
657
658 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
659  if (indirect_hint()) {
660   OP *op = cUNOPo->op_first;
661
662   /* Indirect method call is only possible when the method is a bareword, so
663    * don't trip up on $obj->$meth. */
664   if (op && op->op_type == OP_CONST) {
665    const indirect_op_info_t *oi = indirect_map_fetch(op);
666    STRLEN pos;
667    line_t line;
668    SV *sv;
669
670    if (!oi)
671     goto done;
672
673    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
674    pos  = oi->pos;
675    /* Keep the old line so that we really point to the first line of the
676     * expression. */
677    line = oi->line;
678
679    o = indirect_old_ck_method(aTHX_ o);
680    /* o may now be a method_named */
681
682    indirect_map_store(o, pos, sv, line);
683    return o;
684   }
685  }
686
687 done:
688  o = indirect_old_ck_method(aTHX_ o);
689
690  indirect_map_delete(o);
691  return o;
692 }
693
694 /* ... ck_method_named ..................................................... */
695
696 /* "use foo/no foo" compiles its call to import/unimport directly to a
697  * method_named op. */
698
699 STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
700
701 STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
702  if (indirect_hint()) {
703   STRLEN pos;
704   line_t line;
705   SV *sv;
706
707   sv = cSVOPo_sv;
708   if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
709    goto done;
710   sv = sv_mortalcopy(sv);
711
712   if (!indirect_find(sv, PL_oldbufptr, &pos))
713    goto done;
714   line = CopLINE(&PL_compiling);
715
716   o = indirect_old_ck_method_named(aTHX_ o);
717
718   indirect_map_store(o, pos, sv, line);
719   return o;
720  }
721
722 done:
723  o = indirect_old_ck_method_named(aTHX_ o);
724
725  indirect_map_delete(o);
726  return o;
727 }
728
729 /* ... ck_entersub ......................................................... */
730
731 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
732
733 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
734  SV *code = indirect_hint();
735
736  o = indirect_old_ck_entersub(aTHX_ o);
737
738  if (code) {
739   const indirect_op_info_t *moi, *ooi;
740   OP     *mop, *oop;
741   LISTOP *lop;
742
743   oop = o;
744   do {
745    lop = (LISTOP *) oop;
746    if (!(lop->op_flags & OPf_KIDS))
747     goto done;
748    oop = lop->op_first;
749   } while (oop->op_type != OP_PUSHMARK);
750   oop = oop->op_sibling;
751   mop = lop->op_last;
752
753   if (!oop)
754    goto done;
755
756   switch (oop->op_type) {
757    case OP_CONST:
758    case OP_RV2SV:
759    case OP_PADSV:
760    case OP_SCOPE:
761    case OP_LEAVE:
762     break;
763    default:
764     goto done;
765   }
766
767   if (mop->op_type == OP_METHOD)
768    mop = cUNOPx(mop)->op_first;
769   else if (mop->op_type != OP_METHOD_NAMED)
770    goto done;
771
772   moi = indirect_map_fetch(mop);
773   if (!moi)
774    goto done;
775
776   ooi = indirect_map_fetch(oop);
777   if (!ooi)
778    goto done;
779
780   /* When positions are identical, the method and the object must have the
781    * same name. But it also means that it is an indirect call, as "foo->foo"
782    * results in different positions. */
783   if (moi->pos <= ooi->pos) {
784    SV *file;
785    dSP;
786
787    ENTER;
788    SAVETMPS;
789
790 #ifdef USE_ITHREADS
791    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
792 #else
793    file = sv_mortalcopy(CopFILESV(&PL_compiling));
794 #endif
795
796    PUSHMARK(SP);
797    EXTEND(SP, 4);
798    mPUSHp(ooi->buf, ooi->len);
799    mPUSHp(moi->buf, moi->len);
800    PUSHs(file);
801    mPUSHu(moi->line);
802    PUTBACK;
803
804    call_sv(code, G_VOID);
805
806    PUTBACK;
807
808    FREETMPS;
809    LEAVE;
810   }
811  }
812
813 done:
814  return o;
815 }
816
817 STATIC U32 indirect_initialized = 0;
818
819 STATIC void indirect_teardown(pTHX_ void *root) {
820  if (!indirect_initialized)
821   return;
822
823 #if I_MULTIPLICITY
824  if (aTHX != root)
825   return;
826 #endif
827
828  {
829   dMY_CXT;
830   ptable_free(MY_CXT.map);
831 #if I_THREADSAFE
832   ptable_hints_free(MY_CXT.tbl);
833 #endif
834  }
835
836  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_old_ck_const);
837  indirect_old_ck_const        = 0;
838  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
839  indirect_old_ck_rv2sv        = 0;
840  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_old_ck_padany);
841  indirect_old_ck_padany       = 0;
842  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_old_ck_scope);
843  indirect_old_ck_scope        = 0;
844  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
845  indirect_old_ck_lineseq      = 0;
846
847  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_old_ck_method);
848  indirect_old_ck_method       = 0;
849  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_old_ck_method_named);
850  indirect_old_ck_method_named = 0;
851  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_old_ck_entersub);
852  indirect_old_ck_entersub     = 0;
853
854  indirect_initialized = 0;
855 }
856
857 STATIC void indirect_setup(pTHX) {
858 #define indirect_setup() indirect_setup(aTHX)
859  if (indirect_initialized)
860   return;
861
862  {
863   MY_CXT_INIT;
864 #if I_THREADSAFE
865   MY_CXT.tbl         = ptable_new();
866   MY_CXT.owner       = aTHX;
867 #endif
868   MY_CXT.map         = ptable_new();
869   MY_CXT.global_code = NULL;
870  }
871
872  indirect_old_ck_const        = PL_check[OP_CONST];
873  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_ck_const);
874  indirect_old_ck_rv2sv        = PL_check[OP_RV2SV];
875  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_ck_rv2sv);
876  indirect_old_ck_padany       = PL_check[OP_PADANY];
877  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_ck_padany);
878  indirect_old_ck_scope        = PL_check[OP_SCOPE];
879  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_ck_scope);
880  indirect_old_ck_lineseq      = PL_check[OP_LINESEQ];
881  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_ck_scope);
882
883  indirect_old_ck_method       = PL_check[OP_METHOD];
884  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_ck_method);
885  indirect_old_ck_method_named = PL_check[OP_METHOD_NAMED];
886  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_ck_method_named);
887  indirect_old_ck_entersub     = PL_check[OP_ENTERSUB];
888  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_ck_entersub);
889
890 #if I_MULTIPLICITY
891  call_atexit(indirect_teardown, aTHX);
892 #else
893  call_atexit(indirect_teardown, NULL);
894 #endif
895
896  indirect_initialized = 1;
897 }
898
899 STATIC U32 indirect_booted = 0;
900
901 /* --- XS ------------------------------------------------------------------ */
902
903 MODULE = indirect      PACKAGE = indirect
904
905 PROTOTYPES: ENABLE
906
907 BOOT:
908 {
909  if (!indirect_booted++) {
910   HV *stash;
911
912   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
913
914   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
915   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
916   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
917  }
918
919  indirect_setup();
920 }
921
922 #if I_THREADSAFE
923
924 void
925 CLONE(...)
926 PROTOTYPE: DISABLE
927 PREINIT:
928  ptable *t;
929  SV     *global_code_dup;
930 PPCODE:
931  {
932   my_cxt_t ud;
933   dMY_CXT;
934   ud.tbl   = t = ptable_new();
935   ud.owner = MY_CXT.owner;
936   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
937   global_code_dup = indirect_clone(MY_CXT.global_code, MY_CXT.owner);
938  }
939  {
940   MY_CXT_CLONE;
941   MY_CXT.map         = ptable_new();
942   MY_CXT.tbl         = t;
943   MY_CXT.owner       = aTHX;
944   MY_CXT.global_code = global_code_dup;
945  }
946  reap(3, indirect_thread_cleanup, NULL);
947  XSRETURN(0);
948
949 #endif
950
951 SV *
952 _tag(SV *value)
953 PROTOTYPE: $
954 CODE:
955  RETVAL = indirect_tag(value);
956 OUTPUT:
957  RETVAL
958
959 void
960 _global(SV *code)
961 PROTOTYPE: $
962 PPCODE:
963  if (!SvOK(code))
964   code = NULL;
965  else if (SvROK(code))
966   code = SvRV(code);
967  {
968   dMY_CXT;
969   SvREFCNT_dec(MY_CXT.global_code);
970   MY_CXT.global_code = SvREFCNT_inc(code);
971  }
972  XSRETURN(0);