]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Add a link to Matt's blog post
[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 #ifndef ENTER_with_name
63 # define ENTER_with_name(N) ENTER
64 #endif
65
66 #ifndef LEAVE_with_name
67 # define LEAVE_with_name(N) LEAVE
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_lex_inwhat
74 #  define PL_lex_inwhat PL_parser->lex_inwhat
75 # endif
76 # ifndef PL_linestr
77 #  define PL_linestr PL_parser->linestr
78 # endif
79 # ifndef PL_bufptr
80 #  define PL_bufptr PL_parser->bufptr
81 # endif
82 # ifndef PL_oldbufptr
83 #  define PL_oldbufptr PL_parser->oldbufptr
84 # endif
85 #else
86 # ifndef PL_lex_inwhat
87 #  define PL_lex_inwhat PL_Ilex_inwhat
88 # endif
89 # ifndef PL_linestr
90 #  define PL_linestr PL_Ilinestr
91 # endif
92 # ifndef PL_bufptr
93 #  define PL_bufptr PL_Ibufptr
94 # endif
95 # ifndef PL_oldbufptr
96 #  define PL_oldbufptr PL_Ioldbufptr
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 /* --- Helpers ------------------------------------------------------------- */
146
147 /* ... Thread-safe hints ................................................... */
148
149 #if I_WORKAROUND_REQUIRE_PROPAGATION
150
151 typedef struct {
152  SV *code;
153  IV  cxreq;
154 } indirect_hint_t;
155
156 #define I_HINT_STRUCT 1
157
158 #define I_HINT_CODE(H) ((H)->code)
159
160 #define I_HINT_FREE(H) {   \
161  indirect_hint_t *h = (H); \
162  SvREFCNT_dec(h->code);    \
163  PerlMemShared_free(h);    \
164 }
165
166 #else  /*  I_WORKAROUND_REQUIRE_PROPAGATION */
167
168 typedef SV indirect_hint_t;
169
170 #define I_HINT_STRUCT 0
171
172 #define I_HINT_CODE(H) (H)
173
174 #define I_HINT_FREE(H) SvREFCNT_dec(H);
175
176 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
177
178 #if I_THREADSAFE
179
180 #define PTABLE_NAME        ptable_hints
181 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
182
183 #define pPTBL  pTHX
184 #define pPTBL_ pTHX_
185 #define aPTBL  aTHX
186 #define aPTBL_ aTHX_
187
188 #include "ptable.h"
189
190 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
191 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
192
193 #endif /* I_THREADSAFE */
194
195 /* Define the op->str ptable here because we need to be able to clean it during
196  * thread cleanup. */
197
198 typedef struct {
199  const char *pos;
200  char       *buf;
201  STRLEN      len, size;
202  line_t      line;
203 } indirect_op_info_t;
204
205 #define PTABLE_NAME        ptable
206 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
207
208 #define pPTBL  pTHX
209 #define pPTBL_ pTHX_
210 #define aPTBL  aTHX
211 #define aPTBL_ aTHX_
212
213 #include "ptable.h"
214
215 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
216 #define ptable_clear(T)       ptable_clear(aTHX_ (T))
217 #define ptable_free(T)        ptable_free(aTHX_ (T))
218
219 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
220
221 typedef struct {
222 #if I_THREADSAFE
223  ptable     *tbl; /* It really is a ptable_hints */
224  tTHX        owner;
225 #endif
226  ptable     *map;
227  const char *linestr;
228 } my_cxt_t;
229
230 START_MY_CXT
231
232 #if I_THREADSAFE
233
234 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
235 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
236  CLONE_PARAMS  param;
237  AV           *stashes = NULL;
238  SV           *dupsv;
239
240  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
241   stashes = newAV();
242
243  param.stashes    = stashes;
244  param.flags      = 0;
245  param.proto_perl = owner;
246
247  dupsv = sv_dup(sv, &param);
248
249  if (stashes) {
250   av_undef(stashes);
251   SvREFCNT_dec(stashes);
252  }
253
254  return SvREFCNT_inc(dupsv);
255 }
256
257 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
258  my_cxt_t        *ud = ud_;
259  indirect_hint_t *h1 = ent->val;
260  indirect_hint_t *h2;
261
262  if (ud->owner == aTHX)
263   return;
264
265 #if I_HINT_STRUCT
266
267  h2        = PerlMemShared_malloc(sizeof *h2);
268  h2->code  = indirect_clone(h1->code, ud->owner);
269  SvREFCNT_inc(h2->code);
270 #if I_WORKAROUND_REQUIRE_PROPAGATION
271  h2->cxreq = h1->cxreq;
272 #endif
273
274 #else  /*  I_HINT_STRUCT */
275
276  h2 = indirect_clone(h1, ud->owner);
277  SvREFCNT_inc(h2);
278
279 #endif /* !I_HINT_STRUCT */
280
281  ptable_hints_store(ud->tbl, ent->key, h2);
282 }
283
284 STATIC void indirect_thread_cleanup(pTHX_ void *);
285
286 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
287  int *level = ud;
288
289  if (*level) {
290   *level = 0;
291   LEAVE;
292   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
293   ENTER;
294  } else {
295   dMY_CXT;
296   PerlMemShared_free(level);
297   ptable_free(MY_CXT.map);
298   ptable_hints_free(MY_CXT.tbl);
299  }
300 }
301
302 #endif /* I_THREADSAFE */
303
304 #if I_WORKAROUND_REQUIRE_PROPAGATION
305 STATIC IV indirect_require_tag(pTHX) {
306 #define indirect_require_tag() indirect_require_tag(aTHX)
307  const PERL_SI *si;
308
309  for (si = PL_curstackinfo; si; si = si->si_prev) {
310   I32 cxix;
311
312   for (cxix = si->si_cxix; cxix >= 0; --cxix) {
313    const PERL_CONTEXT *cx = si->si_cxstack + cxix;
314
315    if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
316     return PTR2IV(cx);
317   }
318  }
319
320  return PTR2IV(NULL);
321 }
322 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
323
324 STATIC SV *indirect_tag(pTHX_ SV *value) {
325 #define indirect_tag(V) indirect_tag(aTHX_ (V))
326  indirect_hint_t *h;
327  SV *code = NULL;
328  dMY_CXT;
329
330  if (SvROK(value)) {
331   value = SvRV(value);
332   if (SvTYPE(value) >= SVt_PVCV) {
333    code = value;
334    SvREFCNT_inc_simple_NN(code);
335   }
336  }
337
338 #if I_HINT_STRUCT
339  h = PerlMemShared_malloc(sizeof *h);
340  h->code  = code;
341 # if I_WORKAROUND_REQUIRE_PROPAGATION
342  h->cxreq = indirect_require_tag();
343 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
344 #else  /*  I_HINT_STRUCT */
345  h = code;
346 #endif /* !I_HINT_STRUCT */
347
348 #if I_THREADSAFE
349  /* We only need for the key to be an unique tag for looking up the value later.
350   * Allocated memory provides convenient unique identifiers, so that's why we
351   * use the hint as the key itself. */
352  ptable_hints_store(MY_CXT.tbl, h, h);
353 #endif /* I_THREADSAFE */
354
355  return newSViv(PTR2IV(h));
356 }
357
358 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
359 #define indirect_detag(H) indirect_detag(aTHX_ (H))
360  indirect_hint_t *h;
361  dMY_CXT;
362
363  if (!(hint && SvIOK(hint)))
364   return NULL;
365
366  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
367 #if I_THREADSAFE
368  h = ptable_fetch(MY_CXT.tbl, h);
369 #endif /* I_THREADSAFE */
370
371 #if I_WORKAROUND_REQUIRE_PROPAGATION
372  if (indirect_require_tag() != h->cxreq)
373   return NULL;
374 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
375
376  return I_HINT_CODE(h);
377 }
378
379 STATIC U32 indirect_hash = 0;
380
381 STATIC SV *indirect_hint(pTHX) {
382 #define indirect_hint() indirect_hint(aTHX)
383  SV *hint;
384
385  if (IN_PERL_RUNTIME)
386   return NULL;
387
388 #if I_HAS_PERL(5, 9, 5)
389  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
390                                        NULL,
391                                        __PACKAGE__, __PACKAGE_LEN__,
392                                        0,
393                                        indirect_hash);
394 #else
395  {
396   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
397                                                                  indirect_hash);
398   if (!val)
399    return 0;
400   hint = *val;
401  }
402 #endif
403  return indirect_detag(hint);
404 }
405
406 /* ... op -> source position ............................................... */
407
408 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
409 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
410  indirect_op_info_t *oi;
411  const char *s;
412  STRLEN len;
413  dMY_CXT;
414
415  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
416   * In this case the linestr has temporarly changed, but the old buffer should
417   * still be alive somewhere. */
418
419  if (!PL_lex_inwhat) {
420   const char *pl_linestr = SvPVX_const(PL_linestr);
421   if (MY_CXT.linestr != pl_linestr) {
422    ptable_clear(MY_CXT.map);
423    MY_CXT.linestr = pl_linestr;
424   }
425  }
426
427  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
428   Newx(oi, 1, indirect_op_info_t);
429   ptable_store(MY_CXT.map, o, oi);
430   oi->buf  = NULL;
431   oi->size = 0;
432  }
433
434  if (sv) {
435   s = SvPV_const(sv, len);
436  } else {
437   s   = "{";
438   len = 1;
439  }
440
441  if (len > oi->size) {
442   Safefree(oi->buf);
443   Newx(oi->buf, len, char);
444   oi->size = len;
445  }
446  Copy(s, oi->buf, len, char);
447
448  oi->len  = len;
449  oi->pos  = src;
450  oi->line = line;
451 }
452
453 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
454 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
455  const indirect_op_info_t *val;
456  dMY_CXT;
457
458  if (MY_CXT.linestr != SvPVX_const(PL_linestr))
459   return NULL;
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_store(MY_CXT.map, o, NULL);
469 }
470
471 /* --- Check functions ----------------------------------------------------- */
472
473 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
474 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
475  STRLEN len;
476  const char *p = NULL, *r = SvPV_const(sv, len);
477
478  if (len >= 1 && *r == '$') {
479   ++r;
480   --len;
481   s = strchr(s, '$');
482   if (!s)
483    return NULL;
484  }
485
486  p = strstr(s, r);
487  while (p) {
488   p += len;
489   if (!isALNUM(*p))
490    break;
491   p = strstr(p + 1, r);
492  }
493
494  return p;
495 }
496
497 /* ... ck_const ............................................................ */
498
499 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
500
501 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
502  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
503
504  if (indirect_hint()) {
505   SV *sv = cSVOPo_sv;
506   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
507    const char *s = indirect_find(sv, PL_oldbufptr);
508    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
509    return o;
510   }
511  }
512
513  indirect_map_delete(o);
514  return o;
515 }
516
517 /* ... ck_rv2sv ............................................................ */
518
519 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
520
521 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
522  if (indirect_hint()) {
523   OP *op = cUNOPo->op_first;
524   SV *sv;
525   const char *name = NULL, *s;
526   STRLEN len;
527   OPCODE type = (OPCODE) op->op_type;
528
529   switch (type) {
530    case OP_GV:
531    case OP_GVSV: {
532     GV *gv = cGVOPx_gv(op);
533     name = GvNAME(gv);
534     len  = GvNAMELEN(gv);
535     break;
536    }
537    default:
538     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
539      SV *nsv = cSVOPx_sv(op);
540      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
541       name = SvPV_const(nsv, len);
542     }
543   }
544   if (!name)
545    goto done;
546
547   sv = sv_2mortal(newSVpvn("$", 1));
548   sv_catpvn_nomg(sv, name, len);
549   s = indirect_find(sv, PL_oldbufptr);
550   if (!s) { /* If it failed, retry without the current stash */
551    const char *stash = HvNAME_get(PL_curstash);
552    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
553
554    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
555        || name[stashlen] != ':' || name[stashlen+1] != ':') {
556     /* Failed again ? Try to remove main */
557     stash = "main";
558     stashlen = 4;
559     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
560         || name[stashlen] != ':' || name[stashlen+1] != ':')
561      goto done;
562    }
563
564    sv_setpvn(sv, "$", 1);
565    stashlen += 2;
566    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
567    s = indirect_find(sv, PL_oldbufptr);
568    if (!s)
569     goto done;
570   }
571
572   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
573   indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
574   return o;
575  }
576
577 done:
578  o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
579
580  indirect_map_delete(o);
581  return o;
582 }
583
584 /* ... ck_padany ........................................................... */
585
586 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
587
588 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
589  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
590
591  if (indirect_hint()) {
592   SV *sv;
593   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
594
595   while (s < t && isSPACE(*s)) ++s;
596   if (*s == '$' && ++s <= t) {
597    while (s < t && isSPACE(*s)) ++s;
598    while (s < t && isSPACE(*t)) --t;
599    sv = sv_2mortal(newSVpvn("$", 1));
600    sv_catpvn_nomg(sv, s, t - s + 1);
601    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
602    return o;
603   }
604  }
605
606  indirect_map_delete(o);
607  return o;
608 }
609
610 /* ... ck_scope ............................................................ */
611
612 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
613 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
614
615 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
616  OP *(*old_ck)(pTHX_ OP *) = 0;
617
618  switch (o->op_type) {
619   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
620   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
621  }
622  o = CALL_FPTR(old_ck)(aTHX_ o);
623
624  if (indirect_hint()) {
625   indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
626   return o;
627  }
628
629  indirect_map_delete(o);
630  return o;
631 }
632
633 /* We don't need to clean the map entries for leave ops because they can only
634  * be created by mutating from a lineseq. */
635
636 /* ... ck_method ........................................................... */
637
638 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
639
640 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
641  if (indirect_hint()) {
642   OP *op = cUNOPo->op_first;
643   const indirect_op_info_t *oi = indirect_map_fetch(op);
644   const char *s = NULL;
645   line_t line;
646   SV *sv;
647
648   if (oi && (s = oi->pos)) {
649    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
650    line = oi->line; /* Keep the old line so that we really point to the first */
651   } else {
652    sv = cSVOPx_sv(op);
653    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
654     goto done;
655    sv   = sv_mortalcopy(sv);
656    s    = indirect_find(sv, PL_oldbufptr);
657    line = CopLINE(&PL_compiling);
658   }
659
660   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
661   /* o may now be a method_named */
662
663   indirect_map_store(o, s, sv, line);
664   return o;
665  }
666
667 done:
668  o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
669
670  indirect_map_delete(o);
671  return o;
672 }
673
674 /* ... ck_entersub ......................................................... */
675
676 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
677  if (moi->pos > ooi->pos)
678   return 0;
679
680  if (moi->pos == ooi->pos)
681   return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
682
683  return 1;
684 }
685
686 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
687
688 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
689  SV *code = indirect_hint();
690
691  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
692
693  if (code) {
694   const indirect_op_info_t *moi, *ooi;
695   OP     *mop, *oop;
696   LISTOP *lop;
697
698   oop = o;
699   do {
700    lop = (LISTOP *) oop;
701    if (!(lop->op_flags & OPf_KIDS))
702     goto done;
703    oop = lop->op_first;
704   } while (oop->op_type != OP_PUSHMARK);
705   oop = oop->op_sibling;
706   mop = lop->op_last;
707
708   if (!oop)
709    goto done;
710
711   switch (oop->op_type) {
712    case OP_CONST:
713    case OP_RV2SV:
714    case OP_PADSV:
715    case OP_SCOPE:
716    case OP_LEAVE:
717     break;
718    default:
719     goto done;
720   }
721
722   if (mop->op_type == OP_METHOD)
723    mop = cUNOPx(mop)->op_first;
724   else if (mop->op_type != OP_METHOD_NAMED)
725    goto done;
726
727   moi = indirect_map_fetch(mop);
728   if (!(moi && moi->pos))
729    goto done;
730
731   ooi = indirect_map_fetch(oop);
732   if (!(ooi && ooi->pos))
733    goto done;
734
735   if (indirect_is_indirect(moi, ooi)) {
736    SV *file;
737    dSP;
738
739    ENTER;
740    SAVETMPS;
741
742 #ifdef USE_ITHREADS
743    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
744 #else
745    file = sv_mortalcopy(CopFILESV(&PL_compiling));
746 #endif
747
748    PUSHMARK(SP);
749    EXTEND(SP, 4);
750    mPUSHp(ooi->buf, ooi->len);
751    mPUSHp(moi->buf, moi->len);
752    PUSHs(file);
753    mPUSHu(moi->line);
754    PUTBACK;
755
756    call_sv(code, G_VOID);
757
758    PUTBACK;
759
760    FREETMPS;
761    LEAVE;
762   }
763  }
764
765 done:
766  return o;
767 }
768
769 STATIC U32 indirect_initialized = 0;
770
771 STATIC void indirect_teardown(pTHX_ void *root) {
772  dMY_CXT;
773
774  if (!indirect_initialized)
775   return;
776
777 #if I_MULTIPLICITY
778  if (aTHX != root)
779   return;
780 #endif
781
782  ptable_free(MY_CXT.map);
783 #if I_THREADSAFE
784  ptable_hints_free(MY_CXT.tbl);
785 #endif
786
787  PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_old_ck_const);
788  indirect_old_ck_const    = 0;
789  PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
790  indirect_old_ck_rv2sv    = 0;
791  PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_old_ck_padany);
792  indirect_old_ck_padany   = 0;
793  PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_old_ck_scope);
794  indirect_old_ck_scope    = 0;
795  PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
796  indirect_old_ck_lineseq  = 0;
797
798  PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_old_ck_method);
799  indirect_old_ck_method   = 0;
800  PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_old_ck_entersub);
801  indirect_old_ck_entersub = 0;
802
803  indirect_initialized = 0;
804 }
805
806 STATIC void indirect_setup(pTHX) {
807 #define indirect_setup() indirect_setup(aTHX)
808  if (indirect_initialized)
809   return;
810
811  MY_CXT_INIT;
812 #if I_THREADSAFE
813  MY_CXT.tbl     = ptable_new();
814  MY_CXT.owner   = aTHX;
815 #endif
816  MY_CXT.map     = ptable_new();
817  MY_CXT.linestr = NULL;
818
819  indirect_old_ck_const    = PL_check[OP_CONST];
820  PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
821  indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
822  PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
823  indirect_old_ck_padany   = PL_check[OP_PADANY];
824  PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
825  indirect_old_ck_scope    = PL_check[OP_SCOPE];
826  PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_ck_scope);
827  indirect_old_ck_lineseq  = PL_check[OP_LINESEQ];
828  PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_ck_scope);
829
830  indirect_old_ck_method   = PL_check[OP_METHOD];
831  PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
832  indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
833  PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
834
835 #if I_MULTIPLICITY
836  call_atexit(indirect_teardown, aTHX);
837 #else
838  call_atexit(indirect_teardown, NULL);
839 #endif
840
841  indirect_initialized = 1;
842 }
843
844 STATIC U32 indirect_booted = 0;
845
846 /* --- XS ------------------------------------------------------------------ */
847
848 MODULE = indirect      PACKAGE = indirect
849
850 PROTOTYPES: ENABLE
851
852 BOOT:
853 {
854  if (!indirect_booted++) {
855   HV *stash;
856
857   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
858
859   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
860   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
861   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
862  }
863
864  indirect_setup();
865 }
866
867 #if I_THREADSAFE
868
869 void
870 CLONE(...)
871 PROTOTYPE: DISABLE
872 PREINIT:
873  ptable *t;
874  int    *level;
875 CODE:
876  {
877   my_cxt_t ud;
878   dMY_CXT;
879   ud.tbl   = t = ptable_new();
880   ud.owner = MY_CXT.owner;
881   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
882  }
883  {
884   MY_CXT_CLONE;
885   MY_CXT.map     = ptable_new();
886   MY_CXT.linestr = NULL;
887   MY_CXT.tbl     = t;
888   MY_CXT.owner   = aTHX;
889  }
890  {
891   level = PerlMemShared_malloc(sizeof *level);
892   *level = 1;
893   LEAVE_with_name("sub");
894   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
895   ENTER_with_name("sub");
896  }
897
898 #endif
899
900 SV *
901 _tag(SV *value)
902 PROTOTYPE: $
903 CODE:
904  RETVAL = indirect_tag(value);
905 OUTPUT:
906  RETVAL