]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Encode the README file in UTF-8
[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, 10, 1)
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_delete(T, K)   ptable_delete(aTHX_ (T), (K))
204 #define ptable_clear(T)       ptable_clear(aTHX_ (T))
205 #define ptable_free(T)        ptable_free(aTHX_ (T))
206
207 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
208
209 typedef struct {
210 #if I_THREADSAFE
211  ptable *tbl; /* It really is a ptable_hints */
212  tTHX    owner;
213 #endif
214  ptable *map;
215 } my_cxt_t;
216
217 START_MY_CXT
218
219 #if I_THREADSAFE
220
221 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
222 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
223  CLONE_PARAMS  param;
224  AV           *stashes = NULL;
225  SV           *dupsv;
226
227  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
228   stashes = newAV();
229
230  param.stashes    = stashes;
231  param.flags      = 0;
232  param.proto_perl = owner;
233
234  dupsv = sv_dup(sv, &param);
235
236  if (stashes) {
237   av_undef(stashes);
238   SvREFCNT_dec(stashes);
239  }
240
241  return SvREFCNT_inc(dupsv);
242 }
243
244 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
245  my_cxt_t        *ud = ud_;
246  indirect_hint_t *h1 = ent->val;
247  indirect_hint_t *h2;
248
249  if (ud->owner == aTHX)
250   return;
251
252 #if I_HINT_STRUCT
253
254  h2       = PerlMemShared_malloc(sizeof *h2);
255  h2->code = indirect_clone(h1->code, ud->owner);
256  SvREFCNT_inc(h2->code);
257 #if I_WORKAROUND_REQUIRE_PROPAGATION
258  h2->require_tag = PTR2IV(indirect_clone(INT2PTR(SV *, h1->require_tag),
259                                          ud->owner));
260 #endif
261
262 #else  /*  I_HINT_STRUCT */
263
264  h2 = indirect_clone(h1, ud->owner);
265  SvREFCNT_inc(h2);
266
267 #endif /* !I_HINT_STRUCT */
268
269  ptable_hints_store(ud->tbl, ent->key, h2);
270 }
271
272 #include "reap.h"
273
274 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
275  dMY_CXT;
276
277  ptable_free(MY_CXT.map);
278  ptable_hints_free(MY_CXT.tbl);
279 }
280
281 #endif /* I_THREADSAFE */
282
283 #if I_WORKAROUND_REQUIRE_PROPAGATION
284 STATIC IV indirect_require_tag(pTHX) {
285 #define indirect_require_tag() indirect_require_tag(aTHX)
286  const CV *cv, *outside;
287
288  cv = PL_compcv;
289
290  if (!cv) {
291   /* If for some reason the pragma is operational at run-time, try to discover
292    * the current cv in use. */
293   const PERL_SI *si;
294
295   for (si = PL_curstackinfo; si; si = si->si_prev) {
296    I32 cxix;
297
298    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
299     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
300
301     switch (CxTYPE(cx)) {
302      case CXt_SUB:
303      case CXt_FORMAT:
304       /* The propagation workaround is only needed up to 5.10.0 and at that
305        * time format and sub contexts were still identical. And even later the
306        * cv members offsets should have been kept the same. */
307       cv = cx->blk_sub.cv;
308       goto get_enclosing_cv;
309      case CXt_EVAL:
310       cv = cx->blk_eval.cv;
311       goto get_enclosing_cv;
312      default:
313       break;
314     }
315    }
316   }
317
318   cv = PL_main_cv;
319  }
320
321 get_enclosing_cv:
322  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
323   cv = outside;
324
325  return PTR2IV(cv);
326 }
327 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
328
329 STATIC SV *indirect_tag(pTHX_ SV *value) {
330 #define indirect_tag(V) indirect_tag(aTHX_ (V))
331  indirect_hint_t *h;
332  SV *code = NULL;
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  {
354   dMY_CXT;
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  }
360 #endif /* I_THREADSAFE */
361
362  return newSViv(PTR2IV(h));
363 }
364
365 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
366 #define indirect_detag(H) indirect_detag(aTHX_ (H))
367  indirect_hint_t *h;
368
369  if (!(hint && SvIOK(hint)))
370   return NULL;
371
372  h = INT2PTR(indirect_hint_t *, SvIVX(hint));
373 #if I_THREADSAFE
374  {
375   dMY_CXT;
376   h = ptable_fetch(MY_CXT.tbl, h);
377  }
378 #endif /* I_THREADSAFE */
379
380 #if I_WORKAROUND_REQUIRE_PROPAGATION
381  if (indirect_require_tag() != h->require_tag)
382   return NULL;
383 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
384
385  return I_HINT_CODE(h);
386 }
387
388 STATIC U32 indirect_hash = 0;
389
390 STATIC SV *indirect_hint(pTHX) {
391 #define indirect_hint() indirect_hint(aTHX)
392  SV *hint;
393
394  if (IN_PERL_RUNTIME)
395   return NULL;
396
397 #ifdef cop_hints_fetch_pvn
398  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
399                                                               indirect_hash, 0);
400 #elif I_HAS_PERL(5, 9, 5)
401  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
402                                        NULL,
403                                        __PACKAGE__, __PACKAGE_LEN__,
404                                        0,
405                                        indirect_hash);
406 #else
407  {
408   SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
409   if (!val)
410    return 0;
411   hint = *val;
412  }
413 #endif
414
415  return indirect_detag(hint);
416 }
417
418 /* ... op -> source position ............................................... */
419
420 STATIC void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
421 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
422  indirect_op_info_t *oi;
423  const char *s;
424  STRLEN len;
425  dMY_CXT;
426
427  if (!(oi = ptable_fetch(MY_CXT.map, o))) {
428   Newx(oi, 1, indirect_op_info_t);
429   ptable_store(MY_CXT.map, o, oi);
430   oi->buf  = NULL;
431   oi->size = 0;
432  }
433
434  if (sv) {
435   s = SvPV_const(sv, len);
436  } else {
437   s   = "{";
438   len = 1;
439  }
440
441  if (len > oi->size) {
442   Safefree(oi->buf);
443   Newx(oi->buf, len, char);
444   oi->size = len;
445  }
446  Copy(s, oi->buf, len, char);
447
448  oi->len  = len;
449  oi->pos  = pos;
450  oi->line = line;
451 }
452
453 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
454 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
455  dMY_CXT;
456
457  return ptable_fetch(MY_CXT.map, o);
458 }
459
460 STATIC void indirect_map_delete(pTHX_ const OP *o) {
461 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
462  dMY_CXT;
463
464  ptable_delete(MY_CXT.map, o);
465 }
466
467 /* --- Check functions ----------------------------------------------------- */
468
469 STATIC int indirect_find(pTHX_ SV *sv, const char *s, STRLEN *pos) {
470 #define indirect_find(N, S, P) indirect_find(aTHX_ (N), (S), (P))
471  STRLEN len;
472  const char *p, *r = SvPV_const(sv, len);
473
474  if (len >= 1 && *r == '$') {
475   ++r;
476   --len;
477   s = strchr(s, '$');
478   if (!s)
479    return 0;
480  }
481
482  p = s;
483  while (1) {
484   p = strstr(p, r);
485   if (!p)
486    return 0;
487   if (!isALNUM(p[len]))
488    break;
489   /* p points to a word that has r as prefix, skip the rest of the word */
490   p += len + 1;
491   while (isALNUM(*p))
492    ++p;
493  }
494
495  *pos = p - SvPVX_const(PL_linestr);
496
497  return 1;
498 }
499
500 /* ... ck_const ............................................................ */
501
502 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
503
504 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
505  o = indirect_old_ck_const(aTHX_ o);
506
507  if (indirect_hint()) {
508   SV *sv = cSVOPo_sv;
509
510   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
511    STRLEN pos;
512
513    if (indirect_find(sv, PL_oldbufptr, &pos)) {
514     indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
515     return o;
516    }
517   }
518  }
519
520  indirect_map_delete(o);
521  return o;
522 }
523
524 /* ... ck_rv2sv ............................................................ */
525
526 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
527
528 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
529  if (indirect_hint()) {
530   OP *op = cUNOPo->op_first;
531   SV *sv;
532   const char *name = NULL;
533   STRLEN pos, len;
534   OPCODE type = (OPCODE) op->op_type;
535
536   switch (type) {
537    case OP_GV:
538    case OP_GVSV: {
539     GV *gv = cGVOPx_gv(op);
540     name = GvNAME(gv);
541     len  = GvNAMELEN(gv);
542     break;
543    }
544    default:
545     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
546      SV *nsv = cSVOPx_sv(op);
547      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
548       name = SvPV_const(nsv, len);
549     }
550   }
551   if (!name)
552    goto done;
553
554   sv = sv_2mortal(newSVpvn("$", 1));
555   sv_catpvn_nomg(sv, name, len);
556   if (!indirect_find(sv, PL_oldbufptr, &pos)) {
557    /* If it failed, retry without the current stash */
558    const char *stash = HvNAME_get(PL_curstash);
559    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
560
561    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
562        || name[stashlen] != ':' || name[stashlen+1] != ':') {
563     /* Failed again ? Try to remove main */
564     stash = "main";
565     stashlen = 4;
566     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
567         || name[stashlen] != ':' || name[stashlen+1] != ':')
568      goto done;
569    }
570
571    sv_setpvn(sv, "$", 1);
572    stashlen += 2;
573    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
574    if (!indirect_find(sv, PL_oldbufptr, &pos))
575     goto done;
576   }
577
578   o = indirect_old_ck_rv2sv(aTHX_ o);
579
580   indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
581   return o;
582  }
583
584 done:
585  o = indirect_old_ck_rv2sv(aTHX_ o);
586
587  indirect_map_delete(o);
588  return o;
589 }
590
591 /* ... ck_padany ........................................................... */
592
593 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
594
595 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
596  o = indirect_old_ck_padany(aTHX_ o);
597
598  if (indirect_hint()) {
599   SV *sv;
600   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
601
602   while (s < t && isSPACE(*s)) ++s;
603   if (*s == '$' && ++s <= t) {
604    while (s < t && isSPACE(*s)) ++s;
605    while (s < t && isSPACE(*t)) --t;
606    sv = sv_2mortal(newSVpvn("$", 1));
607    sv_catpvn_nomg(sv, s, t - s + 1);
608    indirect_map_store(o, s - SvPVX_const(PL_linestr),
609                          sv, CopLINE(&PL_compiling));
610    return o;
611   }
612  }
613
614  indirect_map_delete(o);
615  return o;
616 }
617
618 /* ... ck_scope ............................................................ */
619
620 STATIC OP *(*indirect_old_ck_scope)  (pTHX_ OP *) = 0;
621 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
622
623 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
624  OP *(*old_ck)(pTHX_ OP *) = 0;
625
626  switch (o->op_type) {
627   case OP_SCOPE:   old_ck = indirect_old_ck_scope;   break;
628   case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
629  }
630  o = old_ck(aTHX_ o);
631
632  if (indirect_hint()) {
633   indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
634                         NULL, CopLINE(&PL_compiling));
635   return o;
636  }
637
638  indirect_map_delete(o);
639  return o;
640 }
641
642 /* We don't need to clean the map entries for leave ops because they can only
643  * be created by mutating from a lineseq. */
644
645 /* ... ck_method ........................................................... */
646
647 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
648
649 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
650  if (indirect_hint()) {
651   OP *op = cUNOPo->op_first;
652
653   /* Indirect method call is only possible when the method is a bareword, so
654    * don't trip up on $obj->$meth. */
655   if (op && op->op_type == OP_CONST) {
656    const indirect_op_info_t *oi = indirect_map_fetch(op);
657    STRLEN pos;
658    line_t line;
659    SV *sv;
660
661    if (!oi)
662     goto done;
663
664    sv   = sv_2mortal(newSVpvn(oi->buf, oi->len));
665    pos  = oi->pos;
666    /* Keep the old line so that we really point to the first line of the
667     * expression. */
668    line = oi->line;
669
670    o = indirect_old_ck_method(aTHX_ o);
671    /* o may now be a method_named */
672
673    indirect_map_store(o, pos, sv, line);
674    return o;
675   }
676  }
677
678 done:
679  o = indirect_old_ck_method(aTHX_ o);
680
681  indirect_map_delete(o);
682  return o;
683 }
684
685 /* ... ck_method_named ..................................................... */
686
687 /* "use foo/no foo" compiles its call to import/unimport directly to a
688  * method_named op. */
689
690 STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
691
692 STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
693  if (indirect_hint()) {
694   STRLEN pos;
695   line_t line;
696   SV *sv;
697
698   sv = cSVOPo_sv;
699   if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
700    goto done;
701   sv = sv_mortalcopy(sv);
702
703   if (!indirect_find(sv, PL_oldbufptr, &pos))
704    goto done;
705   line = CopLINE(&PL_compiling);
706
707   o = indirect_old_ck_method_named(aTHX_ o);
708
709   indirect_map_store(o, pos, sv, line);
710   return o;
711  }
712
713 done:
714  o = indirect_old_ck_method_named(aTHX_ o);
715
716  indirect_map_delete(o);
717  return o;
718 }
719
720 /* ... ck_entersub ......................................................... */
721
722 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
723
724 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
725  SV *code = indirect_hint();
726
727  o = indirect_old_ck_entersub(aTHX_ o);
728
729  if (code) {
730   const indirect_op_info_t *moi, *ooi;
731   OP     *mop, *oop;
732   LISTOP *lop;
733
734   oop = o;
735   do {
736    lop = (LISTOP *) oop;
737    if (!(lop->op_flags & OPf_KIDS))
738     goto done;
739    oop = lop->op_first;
740   } while (oop->op_type != OP_PUSHMARK);
741   oop = oop->op_sibling;
742   mop = lop->op_last;
743
744   if (!oop)
745    goto done;
746
747   switch (oop->op_type) {
748    case OP_CONST:
749    case OP_RV2SV:
750    case OP_PADSV:
751    case OP_SCOPE:
752    case OP_LEAVE:
753     break;
754    default:
755     goto done;
756   }
757
758   if (mop->op_type == OP_METHOD)
759    mop = cUNOPx(mop)->op_first;
760   else if (mop->op_type != OP_METHOD_NAMED)
761    goto done;
762
763   moi = indirect_map_fetch(mop);
764   if (!moi)
765    goto done;
766
767   ooi = indirect_map_fetch(oop);
768   if (!ooi)
769    goto done;
770
771   /* When positions are identical, the method and the object must have the
772    * same name. But it also means that it is an indirect call, as "foo->foo"
773    * results in different positions. */
774   if (moi->pos <= ooi->pos) {
775    SV *file;
776    dSP;
777
778    ENTER;
779    SAVETMPS;
780
781 #ifdef USE_ITHREADS
782    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
783 #else
784    file = sv_mortalcopy(CopFILESV(&PL_compiling));
785 #endif
786
787    PUSHMARK(SP);
788    EXTEND(SP, 4);
789    mPUSHp(ooi->buf, ooi->len);
790    mPUSHp(moi->buf, moi->len);
791    PUSHs(file);
792    mPUSHu(moi->line);
793    PUTBACK;
794
795    call_sv(code, G_VOID);
796
797    PUTBACK;
798
799    FREETMPS;
800    LEAVE;
801   }
802  }
803
804 done:
805  return o;
806 }
807
808 STATIC U32 indirect_initialized = 0;
809
810 STATIC void indirect_teardown(pTHX_ void *root) {
811  if (!indirect_initialized)
812   return;
813
814 #if I_MULTIPLICITY
815  if (aTHX != root)
816   return;
817 #endif
818
819  {
820   dMY_CXT;
821   ptable_free(MY_CXT.map);
822 #if I_THREADSAFE
823   ptable_hints_free(MY_CXT.tbl);
824 #endif
825  }
826
827  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_old_ck_const);
828  indirect_old_ck_const        = 0;
829  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_old_ck_rv2sv);
830  indirect_old_ck_rv2sv        = 0;
831  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_old_ck_padany);
832  indirect_old_ck_padany       = 0;
833  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_old_ck_scope);
834  indirect_old_ck_scope        = 0;
835  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_old_ck_lineseq);
836  indirect_old_ck_lineseq      = 0;
837
838  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_old_ck_method);
839  indirect_old_ck_method       = 0;
840  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_old_ck_method_named);
841  indirect_old_ck_method_named = 0;
842  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_old_ck_entersub);
843  indirect_old_ck_entersub     = 0;
844
845  indirect_initialized = 0;
846 }
847
848 STATIC void indirect_setup(pTHX) {
849 #define indirect_setup() indirect_setup(aTHX)
850  if (indirect_initialized)
851   return;
852
853  {
854   MY_CXT_INIT;
855 #if I_THREADSAFE
856   MY_CXT.tbl   = ptable_new();
857   MY_CXT.owner = aTHX;
858 #endif
859   MY_CXT.map   = ptable_new();
860  }
861
862  indirect_old_ck_const        = PL_check[OP_CONST];
863  PL_check[OP_CONST]           = MEMBER_TO_FPTR(indirect_ck_const);
864  indirect_old_ck_rv2sv        = PL_check[OP_RV2SV];
865  PL_check[OP_RV2SV]           = MEMBER_TO_FPTR(indirect_ck_rv2sv);
866  indirect_old_ck_padany       = PL_check[OP_PADANY];
867  PL_check[OP_PADANY]          = MEMBER_TO_FPTR(indirect_ck_padany);
868  indirect_old_ck_scope        = PL_check[OP_SCOPE];
869  PL_check[OP_SCOPE]           = MEMBER_TO_FPTR(indirect_ck_scope);
870  indirect_old_ck_lineseq      = PL_check[OP_LINESEQ];
871  PL_check[OP_LINESEQ]         = MEMBER_TO_FPTR(indirect_ck_scope);
872
873  indirect_old_ck_method       = PL_check[OP_METHOD];
874  PL_check[OP_METHOD]          = MEMBER_TO_FPTR(indirect_ck_method);
875  indirect_old_ck_method_named = PL_check[OP_METHOD_NAMED];
876  PL_check[OP_METHOD_NAMED]    = MEMBER_TO_FPTR(indirect_ck_method_named);
877  indirect_old_ck_entersub     = PL_check[OP_ENTERSUB];
878  PL_check[OP_ENTERSUB]        = MEMBER_TO_FPTR(indirect_ck_entersub);
879
880 #if I_MULTIPLICITY
881  call_atexit(indirect_teardown, aTHX);
882 #else
883  call_atexit(indirect_teardown, NULL);
884 #endif
885
886  indirect_initialized = 1;
887 }
888
889 STATIC U32 indirect_booted = 0;
890
891 /* --- XS ------------------------------------------------------------------ */
892
893 MODULE = indirect      PACKAGE = indirect
894
895 PROTOTYPES: ENABLE
896
897 BOOT:
898 {
899  if (!indirect_booted++) {
900   HV *stash;
901
902   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
903
904   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
905   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
906   newCONSTSUB(stash, "I_FORKSAFE",   newSVuv(I_FORKSAFE));
907  }
908
909  indirect_setup();
910 }
911
912 #if I_THREADSAFE
913
914 void
915 CLONE(...)
916 PROTOTYPE: DISABLE
917 PREINIT:
918  ptable *t;
919 PPCODE:
920  {
921   my_cxt_t ud;
922   dMY_CXT;
923   ud.tbl   = t = ptable_new();
924   ud.owner = MY_CXT.owner;
925   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
926  }
927  {
928   MY_CXT_CLONE;
929   MY_CXT.map   = ptable_new();
930   MY_CXT.tbl   = t;
931   MY_CXT.owner = aTHX;
932  }
933  reap(3, indirect_thread_cleanup, NULL);
934  XSRETURN(0);
935
936 #endif
937
938 SV *
939 _tag(SV *value)
940 PROTOTYPE: $
941 CODE:
942  RETVAL = indirect_tag(value);
943 OUTPUT:
944  RETVAL