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