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