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_NN
39 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
42 #ifndef sv_catpvn_nomg
43 # define sv_catpvn_nomg sv_catpvn
47 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
51 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
55 # define HvNAME_get(H) HvNAME(H)
59 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
62 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
64 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
66 # define PL_linestr PL_parser->linestr
69 # define PL_bufptr PL_parser->bufptr
72 # define PL_oldbufptr PL_parser->oldbufptr
76 # define PL_linestr PL_Ilinestr
79 # define PL_bufptr PL_Ibufptr
82 # define PL_oldbufptr PL_Ioldbufptr
86 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
87 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
90 /* ... Thread safety and multiplicity ...................................... */
92 /* Safe unless stated otherwise in Makefile.PL */
97 #ifndef I_MULTIPLICITY
98 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
99 # define I_MULTIPLICITY 1
101 # define I_MULTIPLICITY 0
104 #if I_MULTIPLICITY && !defined(tTHX)
105 # define tTHX PerlInterpreter*
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 \
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))
118 # define I_THREADSAFE 0
120 # define dMY_CXT dNOOP
122 # define MY_CXT indirect_globaldata
124 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
126 # define MY_CXT_INIT NOOP
128 # define MY_CXT_CLONE NOOP
131 /* --- Helpers ------------------------------------------------------------- */
133 /* ... Thread-safe hints ................................................... */
135 #if I_WORKAROUND_REQUIRE_PROPAGATION
142 #define I_HINT_STRUCT 1
144 #define I_HINT_CODE(H) ((H)->code)
146 #define I_HINT_FREE(H) { \
147 indirect_hint_t *h = (H); \
148 SvREFCNT_dec(h->code); \
149 PerlMemShared_free(h); \
152 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
154 typedef SV indirect_hint_t;
156 #define I_HINT_STRUCT 0
158 #define I_HINT_CODE(H) (H)
160 #define I_HINT_FREE(H) SvREFCNT_dec(H);
162 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
166 #define PTABLE_NAME ptable_hints
167 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
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))
179 #endif /* I_THREADSAFE */
181 /* Define the op->str ptable here because we need to be able to clean it during
190 } indirect_op_info_t;
192 #define PTABLE_NAME ptable
193 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
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))
207 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
211 ptable *tbl; /* It really is a ptable_hints */
221 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
222 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
227 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
230 param.stashes = stashes;
232 param.proto_perl = owner;
234 dupsv = sv_dup(sv, ¶m);
238 SvREFCNT_dec(stashes);
241 return SvREFCNT_inc(dupsv);
244 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
246 indirect_hint_t *h1 = ent->val;
249 if (ud->owner == aTHX)
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),
262 #else /* I_HINT_STRUCT */
264 h2 = indirect_clone(h1, ud->owner);
267 #endif /* !I_HINT_STRUCT */
269 ptable_hints_store(ud->tbl, ent->key, h2);
274 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
277 ptable_free(MY_CXT.map);
278 ptable_hints_free(MY_CXT.tbl);
281 #endif /* I_THREADSAFE */
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;
291 /* If for some reason the pragma is operational at run-time, try to discover
292 * the current cv in use. */
295 for (si = PL_curstackinfo; si; si = si->si_prev) {
298 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
299 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
301 switch (CxTYPE(cx)) {
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. */
308 goto get_enclosing_cv;
310 cv = cx->blk_eval.cv;
311 goto get_enclosing_cv;
322 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
327 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
329 STATIC SV *indirect_tag(pTHX_ SV *value) {
330 #define indirect_tag(V) indirect_tag(aTHX_ (V))
336 if (SvTYPE(value) >= SVt_PVCV) {
338 SvREFCNT_inc_simple_NN(code);
343 h = PerlMemShared_malloc(sizeof *h);
345 # if I_WORKAROUND_REQUIRE_PROPAGATION
346 h->require_tag = indirect_require_tag();
347 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
348 #else /* I_HINT_STRUCT */
350 #endif /* !I_HINT_STRUCT */
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);
360 #endif /* I_THREADSAFE */
362 return newSViv(PTR2IV(h));
365 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
366 #define indirect_detag(H) indirect_detag(aTHX_ (H))
369 if (!(hint && SvIOK(hint)))
372 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
376 h = ptable_fetch(MY_CXT.tbl, h);
378 #endif /* I_THREADSAFE */
380 #if I_WORKAROUND_REQUIRE_PROPAGATION
381 if (indirect_require_tag() != h->require_tag)
383 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
385 return I_HINT_CODE(h);
388 STATIC U32 indirect_hash = 0;
390 STATIC SV *indirect_hint(pTHX) {
391 #define indirect_hint() indirect_hint(aTHX)
397 #ifdef cop_hints_fetch_pvn
398 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
400 #elif I_HAS_PERL(5, 9, 5)
401 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
403 __PACKAGE__, __PACKAGE_LEN__,
408 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
415 return indirect_detag(hint);
418 /* ... op -> source position ............................................... */
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;
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);
435 s = SvPV_const(sv, len);
441 if (len > oi->size) {
443 Newx(oi->buf, len, char);
446 Copy(s, oi->buf, len, char);
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))
457 return ptable_fetch(MY_CXT.map, o);
460 STATIC void indirect_map_delete(pTHX_ const OP *o) {
461 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
464 ptable_delete(MY_CXT.map, o);
467 /* --- Check functions ----------------------------------------------------- */
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))
472 const char *p, *r = SvPV_const(sv, len);
474 if (len >= 1 && *r == '$') {
487 if (!isALNUM(p[len]))
489 /* p points to a word that has r as prefix, skip the rest of the word */
495 *pos = p - SvPVX_const(PL_linestr);
500 /* ... ck_const ............................................................ */
502 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
504 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
505 o = indirect_old_ck_const(aTHX_ o);
507 if (indirect_hint()) {
510 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
513 if (indirect_find(sv, PL_oldbufptr, &pos)) {
514 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
520 indirect_map_delete(o);
524 /* ... ck_rv2sv ............................................................ */
526 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
528 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
529 if (indirect_hint()) {
530 OP *op = cUNOPo->op_first;
532 const char *name = NULL;
534 OPCODE type = (OPCODE) op->op_type;
539 GV *gv = cGVOPx_gv(op);
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);
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);
561 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
562 || name[stashlen] != ':' || name[stashlen+1] != ':') {
563 /* Failed again ? Try to remove main */
566 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
567 || name[stashlen] != ':' || name[stashlen+1] != ':')
571 sv_setpvn(sv, "$", 1);
573 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
574 if (!indirect_find(sv, PL_oldbufptr, &pos))
578 o = indirect_old_ck_rv2sv(aTHX_ o);
580 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
585 o = indirect_old_ck_rv2sv(aTHX_ o);
587 indirect_map_delete(o);
591 /* ... ck_padany ........................................................... */
593 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
595 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
596 o = indirect_old_ck_padany(aTHX_ o);
598 if (indirect_hint()) {
600 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
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));
614 indirect_map_delete(o);
618 /* ... ck_scope ............................................................ */
620 STATIC OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
621 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
623 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
624 OP *(*old_ck)(pTHX_ OP *) = 0;
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;
632 if (indirect_hint()) {
633 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
634 NULL, CopLINE(&PL_compiling));
638 indirect_map_delete(o);
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. */
645 /* ... ck_method ........................................................... */
647 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
649 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
650 if (indirect_hint()) {
651 OP *op = cUNOPo->op_first;
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);
664 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
666 /* Keep the old line so that we really point to the first line of the
670 o = indirect_old_ck_method(aTHX_ o);
671 /* o may now be a method_named */
673 indirect_map_store(o, pos, sv, line);
679 o = indirect_old_ck_method(aTHX_ o);
681 indirect_map_delete(o);
685 /* ... ck_method_named ..................................................... */
687 /* "use foo/no foo" compiles its call to import/unimport directly to a
688 * method_named op. */
690 STATIC OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
692 STATIC OP *indirect_ck_method_named(pTHX_ OP *o) {
693 if (indirect_hint()) {
699 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
701 sv = sv_mortalcopy(sv);
703 if (!indirect_find(sv, PL_oldbufptr, &pos))
705 line = CopLINE(&PL_compiling);
707 o = indirect_old_ck_method_named(aTHX_ o);
709 indirect_map_store(o, pos, sv, line);
714 o = indirect_old_ck_method_named(aTHX_ o);
716 indirect_map_delete(o);
720 /* ... ck_entersub ......................................................... */
722 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
724 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
725 SV *code = indirect_hint();
727 o = indirect_old_ck_entersub(aTHX_ o);
730 const indirect_op_info_t *moi, *ooi;
736 lop = (LISTOP *) oop;
737 if (!(lop->op_flags & OPf_KIDS))
740 } while (oop->op_type != OP_PUSHMARK);
741 oop = oop->op_sibling;
747 switch (oop->op_type) {
758 if (mop->op_type == OP_METHOD)
759 mop = cUNOPx(mop)->op_first;
760 else if (mop->op_type != OP_METHOD_NAMED)
763 moi = indirect_map_fetch(mop);
767 ooi = indirect_map_fetch(oop);
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) {
782 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
784 file = sv_mortalcopy(CopFILESV(&PL_compiling));
789 mPUSHp(ooi->buf, ooi->len);
790 mPUSHp(moi->buf, moi->len);
795 call_sv(code, G_VOID);
808 STATIC U32 indirect_initialized = 0;
810 STATIC void indirect_teardown(pTHX_ void *root) {
811 if (!indirect_initialized)
821 ptable_free(MY_CXT.map);
823 ptable_hints_free(MY_CXT.tbl);
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;
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;
845 indirect_initialized = 0;
848 STATIC void indirect_setup(pTHX) {
849 #define indirect_setup() indirect_setup(aTHX)
850 if (indirect_initialized)
856 MY_CXT.tbl = ptable_new();
859 MY_CXT.map = ptable_new();
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);
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);
881 call_atexit(indirect_teardown, aTHX);
883 call_atexit(indirect_teardown, NULL);
886 indirect_initialized = 1;
889 STATIC U32 indirect_booted = 0;
891 /* --- XS ------------------------------------------------------------------ */
893 MODULE = indirect PACKAGE = indirect
899 if (!indirect_booted++) {
902 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
904 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
905 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
906 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
923 ud.tbl = t = ptable_new();
924 ud.owner = MY_CXT.owner;
925 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
929 MY_CXT.map = ptable_new();
933 reap(3, indirect_thread_cleanup, NULL);
942 RETVAL = indirect_tag(value);