4091cf31b0729b4ffc58457d6e4d8d01ded5b8f7
[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 #ifdef cop_hints_fetch_pvn
398  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
399                                                               indirect_hash, 0);
400 #elif I_HAS_PERL(5, 9, 5)
401  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
402                                        NULL,
403                                        __PACKAGE__, __PACKAGE_LEN__,
404                                        0,
405                                        indirect_hash);
406 #else
407  {
408   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
409   if (val)
410    hint = *val;
411  }
412 #endif
413
414  if (hint && SvIOK(hint))
415   return indirect_detag(hint);
416  else {
417   dMY_CXT;
418   return MY_CXT.global_code;
419  }
420 }
421
422 /* ... op -> source position ............................................... */
423
424 STATIC void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
425 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
426  indirect_op_info_t *oi;
427  const char *s;
428  STRLEN len;
429  dMY_CXT;
430
431  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
432   Newx(oi, 1, indirect_op_info_t);
433   ptable_store(MY_CXT.map, o, oi);
434   oi->buf  = NULL;
435   oi->size = 0;
436  }
437
438  if (sv) {
439   s = SvPV_const(sv, len);
440  } else {
441   s   = "{";
442   len = 1;
443  }
444
445  if (len > oi->size) {
446   Safefree(oi->buf);
447   Newx(oi->buf, len, char);
448   oi->size = len;
449  }
450  Copy(s, oi->buf, len, char);
451
452  oi->len  = len;
453  oi->pos  = pos;
454  oi->line = line;
455 }
456
457 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
458 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
459  dMY_CXT;
460
461  return ptable_fetch(MY_CXT.map, o);
462 }
463
464 STATIC void indirect_map_delete(pTHX_ const OP *o) {
465 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
466  dMY_CXT;
467
468  ptable_delete(MY_CXT.map, o);
469 }
470
471 /* --- Check functions ----------------------------------------------------- */
472
473 STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
474 #define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
475  STRLEN len;
476  const char *p, *r = SvPV_const(sv, len);
477
478  if (len >= 1 && *r == '$') {
479   ++r;
480   --len;
481   s = strchr(s, '$');
482   if (!s)
483    return 0;
484  }
485
486  p = s;
487  while (1) {
488   p = strstr(p, r);
489   if (!p)
490    return 0;
491   if (!isALNUM(p[len]))
492    break;
493   /* p points to a word that has r as prefix, skip the rest of the word */
494   p += len + 1;
495   while (isALNUM(*p))
496    ++p;
497  }
498
499  *pos = p - SvPVX_const(PL_linestr);
500
501  return 1;
502 }
503
504 /* ... ck_const ............................................................ */
505
506 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
507
508 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
509  o = indirect_old_ck_const(aTHX_ o);
510
511  if (indirect_hint()) {
512   SV *sv = cSVOPo_sv;
513
514   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
515    STRLEN pos;
516
517    if (indirect_find(sv, PL_oldbufptr, &pos)) {
518     indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
519     return o;
520    }
521   }
522  }
523
524  indirect_map_delete(o);
525  return o;
526 }
527
528 /* ... ck_rv2sv ............................................................ */
529
530 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
531
532 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
533  if (indirect_hint()) {
534   OP *op = cUNOPo->op_first;
535   SV *sv;
536   const char *name = NULL;
537   STRLEN pos, len;
538   OPCODE type = (OPCODE) op->op_type;
539
540   switch (type) {
541    case OP_GV:
542    case OP_GVSV: {
543     GV *gv = cGVOPx_gv(op);
544     name = GvNAME(gv);
545     len  = GvNAMELEN(gv);
546     break;
547    }
548    default:
549     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
550      SV *nsv = cSVOPx_sv(op);
551      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
552       name = SvPV_const(nsv, len);
553     }
554   }
555   if (!name)
556    goto done;
557
558   sv = sv_2mortal(newSVpvn("$", 1));
559   sv_catpvn_nomg(sv, name, len);
560   if (!indirect_find(sv, PL_oldbufptr, &pos)) {
561    /* If it failed, retry without the current stash */
562    const char *stash = HvNAME_get(PL_curstash);
563    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
564
565    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
566        || name[stashlen] != ':' || name[stashlen+1] != ':') {
567     /* Failed again ? Try to remove main */
568     stash = "main";
569     stashlen = 4;
570     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
571         || name[stashlen] != ':' || name[stashlen+1] != ':')
572      goto done;
573    }
574
575    sv_setpvn(sv, "$", 1);
576    stashlen += 2;
577    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
578    if (!indirect_find(sv, PL_oldbufptr, &pos))
579     goto done;
580   }
581
582   o = indirect_old_ck_rv2sv(aTHX_ o);
583
584   indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
585   return o;
586  }
587
588 done:
589  o = indirect_old_ck_rv2sv(aTHX_ o);
590
591  indirect_map_delete(o);
592  return o;
593 }
594
595 /* ... ck_padany ........................................................... */
596
597 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
598
599 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
600  o = indirect_old_ck_padany(aTHX_ o);
601
602  if (indirect_hint()) {
603   SV *sv;
604   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
605
606   while (s < t && isSPACE(*s)) ++s;
607   if (*s == '$' && ++s <= t) {
608    while (s < t && isSPACE(*s)) ++s;
609    while (s < t && isSPACE(*t)) --t;
610    sv = sv_2mortal(newSVpvn("$", 1));
611    sv_catpvn_nomg(sv, s, t - s + 1);
612    indirect_map_store(o, s - SvPVX_const(PL_linestr),
613                          sv, CopLINE(&PL_compiling));
614    return o;
615   }
616  }
617
618  indirect_map_delete(o);
619  return o;
620 }
621
622 /* ... ck_scope ............................................................ */
623
624 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
625 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
626
627 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
628  OP *(*old_ck)(pTHX_ OP *) = 0;
629
630  switch (o->op_type) {
631   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
632   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
633  }
634  o = old_ck(aTHX_ o);
635
636  if (indirect_hint()) {
637   indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
638                         NULL, CopLINE(&PL_compiling));
639   return o;
640  }
641
642  indirect_map_delete(o);
643  return o;
644 }
645
646 /* We don't need to clean the map entries for leave ops because they can only
647  * be created by mutating from a lineseq. */
648
649 /* ... ck_method ........................................................... */
650
651 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
652
653 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
654  if (indirect_hint()) {
655   OP *op = cUNOPo->op_first;
656
657   /* Indirect method call is only possible when the method is a bareword, so
658    * don't trip up on $obj->$meth. */
659   if (op && op->op_type == OP_CONST) {
660    const indirect_op_info_t *oi = indirect_map_fetch(op);
661    STRLEN pos;
662    line_t line;
663    SV *sv;
664
665    if (!oi)
666     goto done;
667
668    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
669    pos  = oi->pos;
670    /* Keep the old line so that we really point to the first line of the
671     * expression. */
672    line = oi->line;
673
674    o = indirect_old_ck_method(aTHX_ o);
675    /* o may now be a method_named */
676
677    indirect_map_store(o, pos, sv, line);
678    return o;
679   }
680  }
681
682 done:
683  o = indirect_old_ck_method(aTHX_ o);
684
685  indirect_map_delete(o);
686  return o;
687 }
688
689 /* ... ck_method_named ..................................................... */
690
691 /* "use foo/no foo" compiles its call to import/unimport directly to a
692  * method_named op. */
693
694 STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
695
696 STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
697  if (indirect_hint()) {
698   STRLEN pos;
699   line_t line;
700   SV *sv;
701
702   sv = cSVOPo_sv;
703   if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
704    goto done;
705   sv = sv_mortalcopy(sv);
706
707   if (!indirect_find(sv, PL_oldbufptr, &pos))
708    goto done;
709   line = CopLINE(&PL_compiling);
710
711   o = indirect_old_ck_method_named(aTHX_ o);
712
713   indirect_map_store(o, pos, sv, line);
714   return o;
715  }
716
717 done:
718  o = indirect_old_ck_method_named(aTHX_ o);
719
720  indirect_map_delete(o);
721  return o;
722 }
723
724 /* ... ck_entersub ......................................................... */
725
726 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
727
728 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
729  SV *code = indirect_hint();
730
731  o = indirect_old_ck_entersub(aTHX_ o);
732
733  if (code) {
734   const indirect_op_info_t *moi, *ooi;
735   OP     *mop, *oop;
736   LISTOP *lop;
737
738   oop = o;
739   do {
740    lop = (LISTOP *) oop;
741    if (!(lop->op_flags & OPf_KIDS))
742     goto done;
743    oop = lop->op_first;
744   } while (oop->op_type != OP_PUSHMARK);
745   oop = oop->op_sibling;
746   mop = lop->op_last;
747
748   if (!oop)
749    goto done;
750
751   switch (oop->op_type) {
752    case OP_CONST:
753    case OP_RV2SV:
754    case OP_PADSV:
755    case OP_SCOPE:
756    case OP_LEAVE:
757     break;
758    default:
759     goto done;
760   }
761
762   if (mop->op_type == OP_METHOD)
763    mop = cUNOPx(mop)->op_first;
764   else if (mop->op_type != OP_METHOD_NAMED)
765    goto done;
766
767   moi = indirect_map_fetch(mop);
768   if (!moi)
769    goto done;
770
771   ooi = indirect_map_fetch(oop);
772   if (!ooi)
773    goto done;
774
775   /* When positions are identical, the method and the object must have the
776    * same name. But it also means that it is an indirect call, as "foo->foo"
777    * results in different positions. */
778   if (moi->pos <= ooi->pos) {
779    SV *file;
780    dSP;
781
782    ENTER;
783    SAVETMPS;
784
785 #ifdef USE_ITHREADS
786    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
787 #else
788    file = sv_mortalcopy(CopFILESV(&PL_compiling));
789 #endif
790
791    PUSHMARK(SP);
792    EXTEND(SP, 4);
793    mPUSHp(ooi->buf, ooi->len);
794    mPUSHp(moi->buf, moi->len);
795    PUSHs(file);
796    mPUSHu(moi->line);
797    PUTBACK;
798
799    call_sv(code, G_VOID);
800
801    PUTBACK;
802
803    FREETMPS;
804    LEAVE;
805   }
806  }
807
808 done:
809  return o;
810 }
811
812 STATIC U32 indirect_initialized = 0;
813
814 STATIC void indirect_teardown(pTHX_ void *root) {
815  if (!indirect_initialized)
816   return;
817
818 #if I_MULTIPLICITY
819  if (aTHX != root)
820   return;
821 #endif
822
823  {
824   dMY_CXT;
825   ptable_free(MY_CXT.map);
826 #if I_THREADSAFE
827   ptable_hints_free(MY_CXT.tbl);
828 #endif
829  }
830
831  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_old_ck_const);
832  indirect_old_ck_const        = 0;
833  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
834  indirect_old_ck_rv2sv        = 0;
835  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_old_ck_padany);
836  indirect_old_ck_padany       = 0;
837  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_old_ck_scope);
838  indirect_old_ck_scope        = 0;
839  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
840  indirect_old_ck_lineseq      = 0;
841
842  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_old_ck_method);
843  indirect_old_ck_method       = 0;
844  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_old_ck_method_named);
845  indirect_old_ck_method_named = 0;
846  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_old_ck_entersub);
847  indirect_old_ck_entersub     = 0;
848
849  indirect_initialized = 0;
850 }
851
852 STATIC void indirect_setup(pTHX) {
853 #define indirect_setup() indirect_setup(aTHX)
854  if (indirect_initialized)
855   return;
856
857  {
858   MY_CXT_INIT;
859 #if I_THREADSAFE
860   MY_CXT.tbl         = ptable_new();
861   MY_CXT.owner       = aTHX;
862 #endif
863   MY_CXT.map         = ptable_new();
864   MY_CXT.global_code = NULL;
865  }
866
867  indirect_old_ck_const        = PL_check[OP_CONST];
868  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_ck_const);
869  indirect_old_ck_rv2sv        = PL_check[OP_RV2SV];
870  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_ck_rv2sv);
871  indirect_old_ck_padany       = PL_check[OP_PADANY];
872  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_ck_padany);
873  indirect_old_ck_scope        = PL_check[OP_SCOPE];
874  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_ck_scope);
875  indirect_old_ck_lineseq      = PL_check[OP_LINESEQ];
876  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_ck_scope);
877
878  indirect_old_ck_method       = PL_check[OP_METHOD];
879  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_ck_method);
880  indirect_old_ck_method_named = PL_check[OP_METHOD_NAMED];
881  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_ck_method_named);
882  indirect_old_ck_entersub     = PL_check[OP_ENTERSUB];
883  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_ck_entersub);
884
885 #if I_MULTIPLICITY
886  call_atexit(indirect_teardown, aTHX);
887 #else
888  call_atexit(indirect_teardown, NULL);
889 #endif
890
891  indirect_initialized = 1;
892 }
893
894 STATIC U32 indirect_booted = 0;
895
896 /* --- XS ------------------------------------------------------------------ */
897
898 MODULE = indirect      PACKAGE = indirect
899
900 PROTOTYPES: ENABLE
901
902 BOOT:
903 {
904  if (!indirect_booted++) {
905   HV *stash;
906
907   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
908
909   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
910   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
911   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
912  }
913
914  indirect_setup();
915 }
916
917 #if I_THREADSAFE
918
919 void
920 CLONE(...)
921 PROTOTYPE: DISABLE
922 PREINIT:
923  ptable *t;
924  SV     *global_code_dup;
925 PPCODE:
926  {
927   my_cxt_t ud;
928   dMY_CXT;
929   ud.tbl   = t = ptable_new();
930   ud.owner = MY_CXT.owner;
931   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
932   global_code_dup = indirect_clone(MY_CXT.global_code, MY_CXT.owner);
933  }
934  {
935   MY_CXT_CLONE;
936   MY_CXT.map         = ptable_new();
937   MY_CXT.tbl         = t;
938   MY_CXT.owner       = aTHX;
939   MY_CXT.global_code = global_code_dup;
940  }
941  reap(3, indirect_thread_cleanup, NULL);
942  XSRETURN(0);
943
944 #endif
945
946 SV *
947 _tag(SV *value)
948 PROTOTYPE: $
949 CODE:
950  RETVAL = indirect_tag(value);
951 OUTPUT:
952  RETVAL
953
954 void
955 _global(SV *code)
956 PROTOTYPE: $
957 PPCODE:
958  if (!SvOK(code))
959   code = NULL;
960  else if (SvROK(code))
961   code = SvRV(code);
962  {
963   dMY_CXT;
964   SvREFCNT_dec(MY_CXT.global_code);
965   MY_CXT.global_code = SvREFCNT_inc(code);
966  }
967  XSRETURN(0);