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