]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Kwalitee test overhaul
[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  I32  requires;
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->requires = h1->requires;
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 STATIC SV *indirect_tag(pTHX_ SV *value) {
297 #define indirect_tag(V) indirect_tag(aTHX_ (V))
298  indirect_hint_t *h;
299  SV *code = NULL;
300  dMY_CXT;
301
302  if (SvROK(value)) {
303   value = SvRV(value);
304   if (SvTYPE(value) >= SVt_PVCV) {
305    code = value;
306    SvREFCNT_inc_simple_NN(code);
307   }
308  }
309
310 #if I_HINT_STRUCT
311  h = PerlMemShared_malloc(sizeof *h);
312  h->code = code;
313
314 #if I_WORKAROUND_REQUIRE_PROPAGATION
315  {
316   const PERL_SI *si;
317   I32            requires = 0;
318
319   for (si = PL_curstackinfo; si; si = si->si_prev) {
320    I32 cxix;
321
322    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
323     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
324
325     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
326      ++requires;
327    }
328   }
329
330   h->requires = requires;
331  }
332 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
333
334 #else  /*  I_HINT_STRUCT */
335  h = code;
336 #endif /* !I_HINT_STRUCT */
337
338 #if I_THREADSAFE
339  /* We only need for the key to be an unique tag for looking up the value later.
340   * Allocated memory provides convenient unique identifiers, so that's why we
341   * use the hint as the key itself. */
342  ptable_hints_store(MY_CXT.tbl, h, h);
343 #endif /* I_THREADSAFE */
344
345  return newSViv(PTR2IV(h));
346 }
347
348 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
349 #define indirect_detag(H) indirect_detag(aTHX_ (H))
350  indirect_hint_t *h;
351  dMY_CXT;
352
353  if (!(hint && SvIOK(hint)))
354   return NULL;
355
356  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
357 #if I_THREADSAFE
358  h = ptable_fetch(MY_CXT.tbl, h);
359 #endif /* I_THREADSAFE */
360
361 #if I_WORKAROUND_REQUIRE_PROPAGATION
362  {
363   const PERL_SI *si;
364   I32            requires = 0;
365
366   for (si = PL_curstackinfo; si; si = si->si_prev) {
367    I32 cxix;
368
369    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
370     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
371
372     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
373                                && ++requires > h->requires)
374      return NULL;
375    }
376   }
377  }
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 *hint;
388
389  if (IN_PERL_RUNTIME)
390   return NULL;
391
392 #if I_HAS_PERL(5, 9, 5)
393  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
394                                        NULL,
395                                        __PACKAGE__, __PACKAGE_LEN__,
396                                        0,
397                                        indirect_hash);
398 #else
399  {
400   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
401                                                                  indirect_hash);
402   if (!val)
403    return 0;
404   hint = *val;
405  }
406 #endif
407  return indirect_detag(hint);
408 }
409
410 /* ... op -> source position ............................................... */
411
412 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
413 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
414  indirect_op_info_t *oi;
415  const char *s;
416  STRLEN len;
417  dMY_CXT;
418
419  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
420   * In this case the linestr has temporarly changed, but the old buffer should
421   * still be alive somewhere. */
422
423  if (!PL_lex_inwhat) {
424   const char *pl_linestr = SvPVX_const(PL_linestr);
425   if (MY_CXT.linestr != pl_linestr) {
426    ptable_clear(MY_CXT.map);
427    MY_CXT.linestr = pl_linestr;
428   }
429  }
430
431  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
432   Newx(oi, 1, indirect_op_info_t);
433   ptable_store(MY_CXT.map, o, oi);
434   oi->buf  = NULL;
435   oi->size = 0;
436  }
437
438  if (sv) {
439   s = SvPV_const(sv, len);
440  } else {
441   s   = "{";
442   len = 1;
443  }
444
445  if (len > oi->size) {
446   Safefree(oi->buf);
447   Newx(oi->buf, len, char);
448   oi->size = len;
449  }
450  Copy(s, oi->buf, len, char);
451
452  oi->len  = len;
453  oi->pos  = src;
454  oi->line = line;
455 }
456
457 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
458 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
459  const indirect_op_info_t *val;
460  dMY_CXT;
461
462  if (MY_CXT.linestr != SvPVX_const(PL_linestr))
463   return NULL;
464
465  return ptable_fetch(MY_CXT.map, o);
466 }
467
468 STATIC void indirect_map_delete(pTHX_ const OP *o) {
469 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
470  dMY_CXT;
471
472  ptable_store(MY_CXT.map, o, NULL);
473 }
474
475 /* --- Check functions ----------------------------------------------------- */
476
477 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
478 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
479  STRLEN len;
480  const char *p = NULL, *r = SvPV_const(sv, len);
481
482  if (len >= 1 && *r == '$') {
483   ++r;
484   --len;
485   s = strchr(s, '$');
486   if (!s)
487    return NULL;
488  }
489
490  p = strstr(s, r);
491  while (p) {
492   p += len;
493   if (!isALNUM(*p))
494    break;
495   p = strstr(p + 1, r);
496  }
497
498  return p;
499 }
500
501 /* ... ck_const ............................................................ */
502
503 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
504
505 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
506  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
507
508  if (indirect_hint()) {
509   SV *sv = cSVOPo_sv;
510   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
511    const char *s = indirect_find(sv, PL_oldbufptr);
512    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
513    return o;
514   }
515  }
516
517  indirect_map_delete(o);
518  return o;
519 }
520
521 /* ... ck_rv2sv ............................................................ */
522
523 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
524
525 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
526  if (indirect_hint()) {
527   OP *op = cUNOPo->op_first;
528   SV *sv;
529   const char *name = NULL, *s;
530   STRLEN len;
531   OPCODE type = (OPCODE) op->op_type;
532
533   switch (type) {
534    case OP_GV:
535    case OP_GVSV: {
536     GV *gv = cGVOPx_gv(op);
537     name = GvNAME(gv);
538     len  = GvNAMELEN(gv);
539     break;
540    }
541    default:
542     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
543      SV *nsv = cSVOPx_sv(op);
544      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
545       name = SvPV_const(nsv, len);
546     }
547   }
548   if (!name)
549    goto done;
550
551   sv = sv_2mortal(newSVpvn("$", 1));
552   sv_catpvn_nomg(sv, name, len);
553   s = indirect_find(sv, PL_oldbufptr);
554   if (!s) { /* If it failed, retry without the current stash */
555    const char *stash = HvNAME_get(PL_curstash);
556    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
557
558    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
559        || name[stashlen] != ':' || name[stashlen+1] != ':') {
560     /* Failed again ? Try to remove main */
561     stash = "main";
562     stashlen = 4;
563     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
564         || name[stashlen] != ':' || name[stashlen+1] != ':')
565      goto done;
566    }
567
568    sv_setpvn(sv, "$", 1);
569    stashlen += 2;
570    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
571    s = indirect_find(sv, PL_oldbufptr);
572    if (!s)
573     goto done;
574   }
575
576   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
577   indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
578   return o;
579  }
580
581 done:
582  o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
583
584  indirect_map_delete(o);
585  return o;
586 }
587
588 /* ... ck_padany ........................................................... */
589
590 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
591
592 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
593  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
594
595  if (indirect_hint()) {
596   SV *sv;
597   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
598
599   while (s < t && isSPACE(*s)) ++s;
600   if (*s == '$' && ++s <= t) {
601    while (s < t && isSPACE(*s)) ++s;
602    while (s < t && isSPACE(*t)) --t;
603    sv = sv_2mortal(newSVpvn("$", 1));
604    sv_catpvn_nomg(sv, s, t - s + 1);
605    indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
606    return o;
607   }
608  }
609
610  indirect_map_delete(o);
611  return o;
612 }
613
614 /* ... ck_scope ............................................................ */
615
616 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
617 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
618
619 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
620  OP *(*old_ck)(pTHX_ OP *) = 0;
621
622  switch (o->op_type) {
623   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
624   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
625  }
626  o = CALL_FPTR(old_ck)(aTHX_ o);
627
628  if (indirect_hint()) {
629   indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
630   return o;
631  }
632
633  indirect_map_delete(o);
634  return o;
635 }
636
637 /* We don't need to clean the map entries for leave ops because they can only
638  * be created by mutating from a lineseq. */
639
640 /* ... ck_method ........................................................... */
641
642 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
643
644 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
645  if (indirect_hint()) {
646   OP *op = cUNOPo->op_first;
647   const indirect_op_info_t *oi = indirect_map_fetch(op);
648   const char *s = NULL;
649   line_t line;
650   SV *sv;
651
652   if (oi && (s = oi->pos)) {
653    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
654    line = oi->line; /* Keep the old line so that we really point to the first */
655   } else {
656    sv = cSVOPx_sv(op);
657    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
658     goto done;
659    sv   = sv_mortalcopy(sv);
660    s    = indirect_find(sv, PL_oldbufptr);
661    line = CopLINE(&PL_compiling);
662   }
663
664   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
665   /* o may now be a method_named */
666
667   indirect_map_store(o, s, sv, line);
668   return o;
669  }
670
671 done:
672  o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
673
674  indirect_map_delete(o);
675  return o;
676 }
677
678 /* ... ck_entersub ......................................................... */
679
680 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
681  if (moi->pos > ooi->pos)
682   return 0;
683
684  if (moi->pos == ooi->pos)
685   return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
686
687  return 1;
688 }
689
690 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
691
692 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
693  SV *code = indirect_hint();
694
695  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
696
697  if (code) {
698   const indirect_op_info_t *moi, *ooi;
699   OP     *mop, *oop;
700   LISTOP *lop;
701
702   oop = o;
703   do {
704    lop = (LISTOP *) oop;
705    if (!(lop->op_flags & OPf_KIDS))
706     goto done;
707    oop = lop->op_first;
708   } while (oop->op_type != OP_PUSHMARK);
709   oop = oop->op_sibling;
710   mop = lop->op_last;
711
712   if (!oop)
713    goto done;
714
715   switch (oop->op_type) {
716    case OP_CONST:
717    case OP_RV2SV:
718    case OP_PADSV:
719    case OP_SCOPE:
720    case OP_LEAVE:
721     break;
722    default:
723     goto done;
724   }
725
726   if (mop->op_type == OP_METHOD)
727    mop = cUNOPx(mop)->op_first;
728   else if (mop->op_type != OP_METHOD_NAMED)
729    goto done;
730
731   moi = indirect_map_fetch(mop);
732   if (!(moi && moi->pos))
733    goto done;
734
735   ooi = indirect_map_fetch(oop);
736   if (!(ooi && ooi->pos))
737    goto done;
738
739   if (indirect_is_indirect(moi, ooi)) {
740    SV *file;
741    dSP;
742
743    ENTER;
744    SAVETMPS;
745
746 #ifdef USE_ITHREADS
747    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
748 #else
749    file = sv_mortalcopy(CopFILESV(&PL_compiling));
750 #endif
751
752    PUSHMARK(SP);
753    EXTEND(SP, 4);
754    mPUSHp(ooi->buf, ooi->len);
755    mPUSHp(moi->buf, moi->len);
756    PUSHs(file);
757    mPUSHu(moi->line);
758    PUTBACK;
759
760    call_sv(code, G_VOID);
761
762    PUTBACK;
763
764    FREETMPS;
765    LEAVE;
766   }
767  }
768
769 done:
770  return o;
771 }
772
773 STATIC U32 indirect_initialized = 0;
774
775 STATIC void indirect_teardown(pTHX_ void *root) {
776  dMY_CXT;
777
778  if (!indirect_initialized)
779   return;
780
781 #if I_MULTIPLICITY
782  if (aTHX != root)
783   return;
784 #endif
785
786  ptable_free(MY_CXT.map);
787 #if I_THREADSAFE
788  ptable_hints_free(MY_CXT.tbl);
789 #endif
790
791  PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_old_ck_const);
792  indirect_old_ck_const    = 0;
793  PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
794  indirect_old_ck_rv2sv    = 0;
795  PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_old_ck_padany);
796  indirect_old_ck_padany   = 0;
797  PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_old_ck_scope);
798  indirect_old_ck_scope    = 0;
799  PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
800  indirect_old_ck_lineseq  = 0;
801
802  PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_old_ck_method);
803  indirect_old_ck_method   = 0;
804  PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_old_ck_entersub);
805  indirect_old_ck_entersub = 0;
806
807  indirect_initialized = 0;
808 }
809
810 STATIC void indirect_setup(pTHX) {
811 #define indirect_setup() indirect_setup(aTHX)
812  if (indirect_initialized)
813   return;
814
815  MY_CXT_INIT;
816 #if I_THREADSAFE
817  MY_CXT.tbl     = ptable_new();
818  MY_CXT.owner   = aTHX;
819 #endif
820  MY_CXT.map     = ptable_new();
821  MY_CXT.linestr = NULL;
822
823  indirect_old_ck_const    = PL_check[OP_CONST];
824  PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
825  indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
826  PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
827  indirect_old_ck_padany   = PL_check[OP_PADANY];
828  PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
829  indirect_old_ck_scope    = PL_check[OP_SCOPE];
830  PL_check[OP_SCOPE]       = MEMBER_TO_FPTR(indirect_ck_scope);
831  indirect_old_ck_lineseq  = PL_check[OP_LINESEQ];
832  PL_check[OP_LINESEQ]     = MEMBER_TO_FPTR(indirect_ck_scope);
833
834  indirect_old_ck_method   = PL_check[OP_METHOD];
835  PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
836  indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
837  PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
838
839 #if I_MULTIPLICITY
840  call_atexit(indirect_teardown, aTHX);
841 #else
842  call_atexit(indirect_teardown, NULL);
843 #endif
844
845  indirect_initialized = 1;
846 }
847
848 STATIC U32 indirect_booted = 0;
849
850 /* --- XS ------------------------------------------------------------------ */
851
852 MODULE = indirect      PACKAGE = indirect
853
854 PROTOTYPES: ENABLE
855
856 BOOT:
857 {
858  if (!indirect_booted++) {
859   HV *stash;
860
861   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
862
863   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
864   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
865   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
866  }
867
868  indirect_setup();
869 }
870
871 #if I_THREADSAFE
872
873 void
874 CLONE(...)
875 PROTOTYPE: DISABLE
876 PREINIT:
877  ptable *t;
878  int    *level;
879 CODE:
880  {
881   my_cxt_t ud;
882   dMY_CXT;
883   ud.tbl   = t = ptable_new();
884   ud.owner = MY_CXT.owner;
885   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
886  }
887  {
888   MY_CXT_CLONE;
889   MY_CXT.map     = ptable_new();
890   MY_CXT.linestr = NULL;
891   MY_CXT.tbl     = t;
892   MY_CXT.owner   = aTHX;
893  }
894  {
895   level = PerlMemShared_malloc(sizeof *level);
896   *level = 1;
897   LEAVE;
898   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
899   ENTER;
900  }
901
902 #endif
903
904 SV *
905 _tag(SV *value)
906 PROTOTYPE: $
907 CODE:
908  RETVAL = indirect_tag(value);
909 OUTPUT:
910  RETVAL