1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
23 # define Newx(v, n, c) New(0, v, n, c)
27 # define SvPV_const SvPV
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
35 # define SvPVX_const SvPVX
38 #ifndef SvREFCNT_inc_simple_void_NN
39 # ifdef SvREFCNT_inc_simple_NN
40 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
42 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
46 #ifndef sv_catpvn_nomg
47 # define sv_catpvn_nomg sv_catpvn
51 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
55 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
59 # define HvNAME_get(H) HvNAME(H)
63 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
68 # define OpSIBLING(O) OP_SIBLING(O)
70 # define OpSIBLING(O) ((O)->op_sibling)
74 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
76 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
78 # define PL_linestr PL_parser->linestr
81 # define PL_bufptr PL_parser->bufptr
84 # define PL_oldbufptr PL_parser->oldbufptr
86 # ifndef PL_lex_inwhat
87 # define PL_lex_inwhat PL_parser->lex_inwhat
91 # define PL_linestr PL_Ilinestr
94 # define PL_bufptr PL_Ibufptr
97 # define PL_oldbufptr PL_Ioldbufptr
99 # ifndef PL_lex_inwhat
100 # define PL_lex_inwhat PL_Ilex_inwhat
104 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
105 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
108 /* ... Thread safety and multiplicity ...................................... */
110 /* Safe unless stated otherwise in Makefile.PL */
112 # define I_FORKSAFE 1
115 #ifndef I_MULTIPLICITY
116 # if defined(MULTIPLICITY)
117 # define I_MULTIPLICITY 1
119 # define I_MULTIPLICITY 0
123 # ifndef PERL_IMPLICIT_CONTEXT
124 # error MULTIPLICITY builds must set PERL_IMPLICIT_CONTEXT
127 #if I_MULTIPLICITY && !defined(tTHX)
128 # define tTHX PerlInterpreter*
131 #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))
132 # define I_THREADSAFE 1
133 # ifndef MY_CXT_CLONE
134 # define MY_CXT_CLONE \
136 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
137 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
138 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
141 # define I_THREADSAFE 0
143 # define dMY_CXT dNOOP
145 # define MY_CXT indirect_globaldata
147 # define START_MY_CXT static my_cxt_t MY_CXT;
149 # define MY_CXT_INIT NOOP
151 # define MY_CXT_CLONE NOOP
155 /* We must use preexistent global mutexes or we will never be able to destroy
157 # if I_HAS_PERL(5, 9, 3)
158 # define I_LOADED_LOCK MUTEX_LOCK(&PL_my_ctx_mutex)
159 # define I_LOADED_UNLOCK MUTEX_UNLOCK(&PL_my_ctx_mutex)
161 # define I_LOADED_LOCK OP_REFCNT_LOCK
162 # define I_LOADED_UNLOCK OP_REFCNT_UNLOCK
165 # define I_LOADED_LOCK NOOP
166 # define I_LOADED_UNLOCK NOOP
169 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
170 # define I_CHECK_LOCK OP_CHECK_MUTEX_LOCK
171 # define I_CHECK_UNLOCK OP_CHECK_MUTEX_UNLOCK
172 #elif I_HAS_PERL(5, 9, 3)
173 # define I_CHECK_LOCK OP_REFCNT_LOCK
174 # define I_CHECK_UNLOCK OP_REFCNT_UNLOCK
176 /* Before perl 5.9.3, indirect_ck_*() calls are already protected by the
177 * I_LOADED mutex, which falls back to the OP_REFCNT mutex. Make sure we don't
179 # define I_CHECK_LOCK NOOP
180 # define I_CHECK_UNLOCK NOOP
183 typedef OP *(*indirect_ck_t)(pTHX_ OP *);
185 #ifdef wrap_op_checker
187 # define indirect_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
191 static void indirect_ck_replace(pTHX_ OPCODE type, indirect_ck_t new_ck, indirect_ck_t *old_ck_p) {
192 #define indirect_ck_replace(T, NC, OCP) indirect_ck_replace(aTHX_ (T), (NC), (OCP))
195 *old_ck_p = PL_check[type];
196 PL_check[type] = new_ck;
203 static void indirect_ck_restore(pTHX_ OPCODE type, indirect_ck_t *old_ck_p) {
204 #define indirect_ck_restore(T, OCP) indirect_ck_restore(aTHX_ (T), (OCP))
207 PL_check[type] = *old_ck_p;
213 /* --- Helpers ------------------------------------------------------------- */
215 /* ... Check if the module is loaded ....................................... */
217 static I32 indirect_loaded = 0;
221 #define PTABLE_NAME ptable_loaded
222 #define PTABLE_VAL_FREE(V) NOOP
226 #define ptable_loaded_store(T, K, V) ptable_loaded_store(aPTBLMS_ (T), (K), (V))
227 #define ptable_loaded_delete(T, K) ptable_loaded_delete(aPTBLMS_ (T), (K))
228 #define ptable_loaded_free(T) ptable_loaded_free(aPTBLMS_ (T))
230 static ptable *indirect_loaded_cxts = NULL;
232 static int indirect_is_loaded(pTHX_ void *cxt) {
233 #define indirect_is_loaded(C) indirect_is_loaded(aTHX_ (C))
237 if (indirect_loaded_cxts && ptable_fetch(indirect_loaded_cxts, cxt))
244 static int indirect_set_loaded_locked(pTHX_ void *cxt) {
245 #define indirect_set_loaded_locked(C) indirect_set_loaded_locked(aTHX_ (C))
246 int global_setup = 0;
248 if (indirect_loaded <= 0) {
249 assert(indirect_loaded == 0);
250 assert(!indirect_loaded_cxts);
251 indirect_loaded_cxts = ptable_new();
255 assert(indirect_loaded_cxts);
256 ptable_loaded_store(indirect_loaded_cxts, cxt, cxt);
261 static int indirect_clear_loaded_locked(pTHX_ void *cxt) {
262 #define indirect_clear_loaded_locked(C) indirect_clear_loaded_locked(aTHX_ (C))
263 int global_teardown = 0;
265 if (indirect_loaded > 1) {
266 assert(indirect_loaded_cxts);
267 ptable_loaded_delete(indirect_loaded_cxts, cxt);
269 } else if (indirect_loaded_cxts) {
270 assert(indirect_loaded == 1);
271 ptable_loaded_free(indirect_loaded_cxts);
272 indirect_loaded_cxts = NULL;
277 return global_teardown;
282 #define indirect_is_loaded(C) (indirect_loaded > 0)
283 #define indirect_set_loaded_locked(C) ((indirect_loaded++ <= 0) ? 1 : 0)
284 #define indirect_clear_loaded_locked(C) ((--indirect_loaded <= 0) ? 1 : 0)
288 /* ... Thread-safe hints ................................................... */
290 #if I_WORKAROUND_REQUIRE_PROPAGATION
297 #define I_HINT_STRUCT 1
299 #define I_HINT_CODE(H) ((H)->code)
301 #define I_HINT_FREE(H) { \
302 indirect_hint_t *h = (H); \
303 SvREFCNT_dec(h->code); \
304 PerlMemShared_free(h); \
307 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
309 typedef SV indirect_hint_t;
311 #define I_HINT_STRUCT 0
313 #define I_HINT_CODE(H) (H)
315 #define I_HINT_FREE(H) SvREFCNT_dec(H);
317 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
321 #define PTABLE_NAME ptable_hints
322 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
331 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
332 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
334 #endif /* I_THREADSAFE */
336 /* Define the op->str ptable here because we need to be able to clean it during
345 } indirect_op_info_t;
347 #define PTABLE_NAME ptable
348 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
357 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
358 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
359 #define ptable_clear(T) ptable_clear(aTHX_ (T))
360 #define ptable_free(T) ptable_free(aTHX_ (T))
362 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
366 ptable *tbl; /* It really is a ptable_hints */
379 #if I_HAS_PERL(5, 13, 2)
380 CLONE_PARAMS *params;
384 } indirect_ptable_clone_ud;
386 #if I_HAS_PERL(5, 13, 2)
387 # define indirect_ptable_clone_ud_init(U, T, O) \
389 (U).params = Perl_clone_params_new((O), aTHX)
390 # define indirect_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
391 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
393 # define indirect_ptable_clone_ud_init(U, T, O) \
395 (U).params.stashes = newAV(); \
396 (U).params.flags = 0; \
397 (U).params.proto_perl = (O)
398 # define indirect_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
399 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
402 static void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
403 indirect_ptable_clone_ud *ud = ud_;
404 indirect_hint_t *h1 = ent->val;
409 h2 = PerlMemShared_malloc(sizeof *h2);
410 h2->code = indirect_dup_inc(h1->code, ud);
411 #if I_WORKAROUND_REQUIRE_PROPAGATION
412 h2->require_tag = PTR2IV(indirect_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
415 #else /* I_HINT_STRUCT */
417 h2 = indirect_dup_inc(h1, ud);
419 #endif /* !I_HINT_STRUCT */
421 ptable_hints_store(ud->tbl, ent->key, h2);
424 static void indirect_thread_cleanup(pTHX_ void *ud) {
428 global_teardown = indirect_clear_loaded_locked(&MY_CXT);
429 assert(!global_teardown);
431 SvREFCNT_dec(MY_CXT.global_code);
432 MY_CXT.global_code = NULL;
434 ptable_free(MY_CXT.map);
437 ptable_hints_free(MY_CXT.tbl);
441 static int indirect_endav_free(pTHX_ SV *sv, MAGIC *mg) {
442 SAVEDESTRUCTOR_X(indirect_thread_cleanup, NULL);
447 static MGVTBL indirect_endav_vtbl = {
464 #endif /* I_THREADSAFE */
466 #if I_WORKAROUND_REQUIRE_PROPAGATION
467 static IV indirect_require_tag(pTHX) {
468 #define indirect_require_tag() indirect_require_tag(aTHX)
469 const CV *cv, *outside;
474 /* If for some reason the pragma is operational at run-time, try to discover
475 * the current cv in use. */
478 for (si = PL_curstackinfo; si; si = si->si_prev) {
481 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
482 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
484 switch (CxTYPE(cx)) {
487 /* The propagation workaround is only needed up to 5.10.0 and at that
488 * time format and sub contexts were still identical. And even later the
489 * cv members offsets should have been kept the same. */
491 goto get_enclosing_cv;
493 cv = cx->blk_eval.cv;
494 goto get_enclosing_cv;
505 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
510 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
512 static SV *indirect_tag(pTHX_ SV *value) {
513 #define indirect_tag(V) indirect_tag(aTHX_ (V))
521 #endif /* I_THREADSAFE */
525 if (SvTYPE(value) >= SVt_PVCV) {
527 SvREFCNT_inc_simple_void_NN(code);
532 h = PerlMemShared_malloc(sizeof *h);
534 # if I_WORKAROUND_REQUIRE_PROPAGATION
535 h->require_tag = indirect_require_tag();
536 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
537 #else /* I_HINT_STRUCT */
539 #endif /* !I_HINT_STRUCT */
542 /* We only need for the key to be an unique tag for looking up the value later
543 * Allocated memory provides convenient unique identifiers, so that's why we
544 * use the hint as the key itself. */
545 ptable_hints_store(MY_CXT.tbl, h, h);
546 #endif /* I_THREADSAFE */
548 return newSViv(PTR2IV(h));
551 static SV *indirect_detag(pTHX_ const SV *hint) {
552 #define indirect_detag(H) indirect_detag(aTHX_ (H))
554 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
561 #endif /* I_THREADSAFE */
563 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
565 h = ptable_fetch(MY_CXT.tbl, h);
566 #endif /* I_THREADSAFE */
568 #if I_WORKAROUND_REQUIRE_PROPAGATION
569 if (indirect_require_tag() != h->require_tag)
570 return MY_CXT.global_code;
571 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
573 return I_HINT_CODE(h);
576 static VOL U32 indirect_hash = 0;
578 static SV *indirect_hint(pTHX) {
579 #define indirect_hint() indirect_hint(aTHX)
585 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
590 #ifdef cop_hints_fetch_pvn
591 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
593 #elif I_HAS_PERL(5, 9, 5)
594 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
596 __PACKAGE__, __PACKAGE_LEN__,
601 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
607 if (hint && SvIOK(hint)) {
608 return indirect_detag(hint);
611 if (indirect_is_loaded(&MY_CXT))
612 return MY_CXT.global_code;
618 /* ... op -> source position ............................................... */
620 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
621 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
622 indirect_op_info_t *oi;
627 /* No need to check for MY_CXT.map != NULL because this code path is always
628 * guarded by indirect_hint(). */
630 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
631 Newx(oi, 1, indirect_op_info_t);
632 ptable_store(MY_CXT.map, o, oi);
638 s = SvPV_const(sv, len);
644 if (len > oi->size) {
646 Newx(oi->buf, len, char);
649 Copy(s, oi->buf, len, char);
656 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
657 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
660 /* No need to check for MY_CXT.map != NULL because this code path is always
661 * guarded by indirect_hint(). */
663 return ptable_fetch(MY_CXT.map, o);
666 static void indirect_map_delete(pTHX_ const OP *o) {
667 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
670 if (indirect_is_loaded(&MY_CXT) && MY_CXT.map)
671 ptable_delete(MY_CXT.map, o);
674 /* --- Check functions ----------------------------------------------------- */
676 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
677 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
678 STRLEN name_len, line_len;
679 const char *name, *name_end;
680 const char *line, *line_end;
683 line = SvPV_const(PL_linestr, line_len);
684 line_end = line + line_len;
686 name = SvPV_const(name_sv, name_len);
687 if (name_len >= 1 && *name == '$') {
690 while (line_bufptr < line_end && *line_bufptr != '$')
692 if (line_bufptr >= line_end)
695 name_end = name + name_len;
699 p = ninstr(p, line_end, name, name_end);
702 if (!isALNUM(p[name_len]))
704 /* p points to a word that has name as prefix, skip the rest of the word */
710 *name_pos = p - line;
715 /* ... ck_const ............................................................ */
717 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
719 static OP *indirect_ck_const(pTHX_ OP *o) {
720 o = indirect_old_ck_const(aTHX_ o);
722 if (indirect_hint()) {
725 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
728 if (indirect_find(sv, PL_oldbufptr, &pos)) {
731 /* If the constant is equal to the current package name, try to look for
732 * a "__PACKAGE__" coming before what we got. We only need to check this
733 * when we already had a match because __PACKAGE__ can only appear in
734 * direct method calls ("new __PACKAGE__" is a syntax error). */
737 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
738 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
740 SV *pkg = sv_newmortal();
741 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
743 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
749 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
755 indirect_map_delete(o);
759 /* ... ck_rv2sv ............................................................ */
761 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
763 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
764 if (indirect_hint()) {
765 OP *op = cUNOPo->op_first;
767 const char *name = NULL;
769 OPCODE type = (OPCODE) op->op_type;
774 GV *gv = cGVOPx_gv(op);
780 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
781 SV *nsv = cSVOPx_sv(op);
782 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
783 name = SvPV_const(nsv, len);
789 sv = sv_2mortal(newSVpvn("$", 1));
790 sv_catpvn_nomg(sv, name, len);
791 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
792 /* If it failed, retry without the current stash */
793 const char *stash = HvNAME_get(PL_curstash);
794 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
796 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
797 || name[stashlen] != ':' || name[stashlen+1] != ':') {
798 /* Failed again ? Try to remove main */
801 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
802 || name[stashlen] != ':' || name[stashlen+1] != ':')
806 sv_setpvn(sv, "$", 1);
808 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
809 if (!indirect_find(sv, PL_oldbufptr, &pos))
813 o = indirect_old_ck_rv2sv(aTHX_ o);
815 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
820 o = indirect_old_ck_rv2sv(aTHX_ o);
822 indirect_map_delete(o);
826 /* ... ck_padany ........................................................... */
828 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
830 static OP *indirect_ck_padany(pTHX_ OP *o) {
831 o = indirect_old_ck_padany(aTHX_ o);
833 if (indirect_hint()) {
835 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
837 while (s < t && isSPACE(*s)) ++s;
838 if (*s == '$' && ++s <= t) {
839 while (s < t && isSPACE(*s)) ++s;
840 while (s < t && isSPACE(*t)) --t;
841 sv = sv_2mortal(newSVpvn("$", 1));
842 sv_catpvn_nomg(sv, s, t - s + 1);
843 indirect_map_store(o, s - SvPVX_const(PL_linestr),
844 sv, CopLINE(&PL_compiling));
849 indirect_map_delete(o);
853 /* ... ck_scope ............................................................ */
855 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
856 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
858 static OP *indirect_ck_scope(pTHX_ OP *o) {
859 OP *(*old_ck)(pTHX_ OP *) = 0;
861 switch (o->op_type) {
862 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
863 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
867 if (indirect_hint()) {
868 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
869 NULL, CopLINE(&PL_compiling));
873 indirect_map_delete(o);
877 /* We don't need to clean the map entries for leave ops because they can only
878 * be created by mutating from a lineseq. */
880 /* ... ck_method ........................................................... */
882 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
884 static OP *indirect_ck_method(pTHX_ OP *o) {
885 if (indirect_hint()) {
886 OP *op = cUNOPo->op_first;
888 /* Indirect method call is only possible when the method is a bareword, so
889 * don't trip up on $obj->$meth. */
890 if (op && op->op_type == OP_CONST) {
891 const indirect_op_info_t *oi = indirect_map_fetch(op);
899 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
901 /* Keep the old line so that we really point to the first line of the
905 o = indirect_old_ck_method(aTHX_ o);
906 /* o may now be a method_named */
908 indirect_map_store(o, pos, sv, line);
914 o = indirect_old_ck_method(aTHX_ o);
916 indirect_map_delete(o);
920 /* ... ck_method_named ..................................................... */
922 /* "use foo/no foo" compiles its call to import/unimport directly to a
923 * method_named op. */
925 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
927 static OP *indirect_ck_method_named(pTHX_ OP *o) {
928 if (indirect_hint()) {
934 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
936 sv = sv_mortalcopy(sv);
938 if (!indirect_find(sv, PL_oldbufptr, &pos))
940 line = CopLINE(&PL_compiling);
942 o = indirect_old_ck_method_named(aTHX_ o);
944 indirect_map_store(o, pos, sv, line);
949 o = indirect_old_ck_method_named(aTHX_ o);
951 indirect_map_delete(o);
955 /* ... ck_entersub ......................................................... */
957 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
959 static OP *indirect_ck_entersub(pTHX_ OP *o) {
960 SV *code = indirect_hint();
962 o = indirect_old_ck_entersub(aTHX_ o);
965 const indirect_op_info_t *moi, *ooi;
971 lop = (LISTOP *) oop;
972 if (!(lop->op_flags & OPf_KIDS))
975 } while (oop->op_type != OP_PUSHMARK);
976 oop = OpSIBLING(oop);
982 switch (oop->op_type) {
993 if (mop->op_type == OP_METHOD)
994 mop = cUNOPx(mop)->op_first;
995 else if (mop->op_type != OP_METHOD_NAMED)
998 moi = indirect_map_fetch(mop);
1002 ooi = indirect_map_fetch(oop);
1006 /* When positions are identical, the method and the object must have the
1007 * same name. But it also means that it is an indirect call, as "foo->foo"
1008 * results in different positions. */
1009 if ( moi->line < ooi->line
1010 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
1018 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
1020 file = sv_mortalcopy(CopFILESV(&PL_compiling));
1025 mPUSHp(ooi->buf, ooi->len);
1026 mPUSHp(moi->buf, moi->len);
1031 call_sv(code, G_VOID);
1044 /* --- Module setup/teardown ----------------------------------------------- */
1046 static void indirect_teardown(pTHX_ void *interp) {
1056 if (indirect_clear_loaded_locked(&MY_CXT)) {
1057 indirect_ck_restore(OP_CONST, &indirect_old_ck_const);
1058 indirect_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
1059 indirect_ck_restore(OP_PADANY, &indirect_old_ck_padany);
1060 indirect_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
1061 indirect_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
1063 indirect_ck_restore(OP_METHOD, &indirect_old_ck_method);
1064 indirect_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
1065 indirect_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
1070 ptable_free(MY_CXT.map);
1074 ptable_hints_free(MY_CXT.tbl);
1081 static void indirect_setup(pTHX) {
1082 #define indirect_setup() indirect_setup(aTHX)
1083 MY_CXT_INIT; /* Takes/release PL_my_ctx_mutex */
1087 if (indirect_set_loaded_locked(&MY_CXT)) {
1088 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
1090 indirect_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
1091 indirect_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
1092 indirect_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
1093 indirect_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
1094 indirect_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
1096 indirect_ck_replace(OP_METHOD, indirect_ck_method,
1097 &indirect_old_ck_method);
1098 indirect_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
1099 &indirect_old_ck_method_named);
1100 indirect_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
1101 &indirect_old_ck_entersub);
1109 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1110 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
1111 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
1114 MY_CXT.tbl = ptable_new();
1115 MY_CXT.owner = aTHX;
1118 MY_CXT.map = ptable_new();
1119 MY_CXT.global_code = NULL;
1123 call_atexit(indirect_teardown, aTHX);
1125 call_atexit(indirect_teardown, NULL);
1131 /* --- XS ------------------------------------------------------------------ */
1133 MODULE = indirect PACKAGE = indirect
1149 SV *global_code_dup;
1153 indirect_ptable_clone_ud ud;
1156 indirect_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1157 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
1158 global_code_dup = indirect_dup_inc(MY_CXT.global_code, &ud);
1159 indirect_ptable_clone_ud_deinit(ud);
1163 MY_CXT.map = ptable_new();
1165 MY_CXT.owner = aTHX;
1166 MY_CXT.global_code = global_code_dup;
1170 global_setup = indirect_set_loaded_locked(&MY_CXT);
1171 assert(!global_setup);
1175 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1181 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1183 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &indirect_endav_vtbl, NULL, 0);
1188 _THREAD_CLEANUP(...)
1191 indirect_thread_cleanup(aTHX_ NULL);
1200 RETVAL = indirect_tag(value);
1210 else if (SvROK(code))
1214 SvREFCNT_dec(MY_CXT.global_code);
1215 MY_CXT.global_code = SvREFCNT_inc(code);