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