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