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)
65 # ifndef PL_lex_inwhat
66 # define PL_lex_inwhat PL_parser->lex_inwhat
69 # define PL_linestr PL_parser->linestr
72 # define PL_bufptr PL_parser->bufptr
75 # define PL_oldbufptr PL_parser->oldbufptr
78 # ifndef PL_lex_inwhat
79 # define PL_lex_inwhat PL_Ilex_inwhat
82 # define PL_linestr PL_Ilinestr
85 # define PL_bufptr PL_Ibufptr
88 # define PL_oldbufptr PL_Ioldbufptr
92 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
93 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
96 /* ... Thread safety and multiplicity ...................................... */
98 #ifndef I_MULTIPLICITY
99 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
100 # define I_MULTIPLICITY 1
102 # define I_MULTIPLICITY 0
105 #if I_MULTIPLICITY && !defined(tTHX)
106 # define tTHX PerlInterpreter*
109 #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))
110 # define I_THREADSAFE 1
111 # ifndef MY_CXT_CLONE
112 # define MY_CXT_CLONE \
114 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
115 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
116 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
119 # define I_THREADSAFE 0
121 # define dMY_CXT dNOOP
123 # define MY_CXT indirect_globaldata
125 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
127 # define MY_CXT_INIT NOOP
129 # define MY_CXT_CLONE NOOP
132 /* --- Helpers ------------------------------------------------------------- */
134 /* ... Thread-safe hints ................................................... */
136 /* If any of those are true, we need to store the hint in a global table. */
138 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
142 #if I_WORKAROUND_REQUIRE_PROPAGATION
147 #define PTABLE_NAME ptable_hints
149 #define PTABLE_VAL_FREE(V) \
150 { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
159 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
160 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
162 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
164 /* Define the op->str ptable here because we need to be able to clean it during
172 } indirect_op_info_t;
174 #define PTABLE_NAME ptable
175 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
184 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
185 #define ptable_clear(T) ptable_clear(aTHX_ (T))
186 #define ptable_free(T) ptable_free(aTHX_ (T))
188 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
191 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
192 ptable *tbl; /* It really is a ptable_hints */
205 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
206 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
211 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
214 param.stashes = stashes;
216 param.proto_perl = owner;
218 dupsv = sv_dup(sv, ¶m);
222 SvREFCNT_dec(stashes);
225 return SvREFCNT_inc(dupsv);
228 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
230 indirect_hint_t *h1 = ent->val;
231 indirect_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
235 if (ud->owner != aTHX)
236 h2->code = indirect_clone(h1->code, ud->owner);
238 ptable_hints_store(ud->tbl, ent->key, h2);
239 SvREFCNT_inc(h2->code);
242 STATIC void indirect_thread_cleanup(pTHX_ void *);
244 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
250 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
254 PerlMemShared_free(level);
255 ptable_free(MY_CXT.map);
256 ptable_hints_free(MY_CXT.tbl);
260 #endif /* I_THREADSAFE */
262 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
264 STATIC SV *indirect_tag(pTHX_ SV *value) {
265 #define indirect_tag(V) indirect_tag(aTHX_ (V))
272 if (SvTYPE(value) >= SVt_PVCV) {
274 if (CvANON(code) && !CvCLONED(code))
276 SvREFCNT_inc_simple_NN(code);
280 h = PerlMemShared_malloc(sizeof *h);
283 #if I_WORKAROUND_REQUIRE_PROPAGATION
288 for (si = PL_curstackinfo; si; si = si->si_prev) {
291 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
292 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
294 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
299 h->requires = requires;
303 /* We only need for the key to be an unique tag for looking up the value later.
304 * Allocated memory provides convenient unique identifiers, so that's why we
305 * use the value pointer as the key itself. */
306 ptable_hints_store(MY_CXT.tbl, value, h);
308 return newSViv(PTR2IV(value));
311 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
312 #define indirect_detag(H) indirect_detag(aTHX_ (H))
316 if (!(hint && SvIOK(hint)))
319 h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvIVX(hint)));
321 #if I_WORKAROUND_REQUIRE_PROPAGATION
326 for (si = PL_curstackinfo; si; si = si->si_prev) {
329 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
330 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
332 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
333 && ++requires > h->requires)
345 STATIC SV *indirect_tag(pTHX_ SV *value) {
346 #define indirect_tag(V) indirect_tag(aTHX_ (V))
351 SvREFCNT_inc_simple_NN(value);
358 #define indirect_detag(H) (((H) && SvIOK(H)) ? INT2PTR(SV *, SvIVX(H)) : NULL)
360 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
362 STATIC U32 indirect_hash = 0;
364 STATIC SV *indirect_hint(pTHX) {
365 #define indirect_hint() indirect_hint(aTHX)
371 #if I_HAS_PERL(5, 9, 5)
372 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
374 __PACKAGE__, __PACKAGE_LEN__,
379 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
386 return indirect_detag(hint);
389 /* ... op -> source position ............................................... */
391 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv, line_t line) {
392 #define indirect_map_store(O, S, N, L) indirect_map_store(aTHX_ (O), (S), (N), (L))
393 indirect_op_info_t *oi;
398 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
399 * In this case the linestr has temporarly changed, but the old buffer should
400 * still be alive somewhere. */
402 if (!PL_lex_inwhat) {
403 const char *pl_linestr = SvPVX_const(PL_linestr);
404 if (MY_CXT.linestr != pl_linestr) {
405 ptable_clear(MY_CXT.map);
406 MY_CXT.linestr = pl_linestr;
410 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
411 Newx(oi, 1, indirect_op_info_t);
412 ptable_store(MY_CXT.map, o, oi);
418 s = SvPV_const(sv, len);
424 if (len > oi->size) {
426 Newx(oi->buf, len, char);
429 Copy(s, oi->buf, len, char);
436 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
437 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
438 const indirect_op_info_t *val;
441 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
444 return ptable_fetch(MY_CXT.map, o);
447 STATIC void indirect_map_delete(pTHX_ const OP *o) {
448 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
451 ptable_store(MY_CXT.map, o, NULL);
454 /* --- Check functions ----------------------------------------------------- */
456 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
457 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
459 const char *p = NULL, *r = SvPV_const(sv, len);
461 if (len >= 1 && *r == '$') {
474 p = strstr(p + 1, r);
480 /* ... ck_const ............................................................ */
482 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
484 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
485 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
487 if (indirect_hint()) {
489 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
490 const char *s = indirect_find(sv, PL_oldbufptr);
491 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
496 indirect_map_delete(o);
500 /* ... ck_rv2sv ............................................................ */
502 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
504 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
505 if (indirect_hint()) {
506 OP *op = cUNOPo->op_first;
508 const char *name = NULL, *s;
510 OPCODE type = (OPCODE) op->op_type;
515 GV *gv = cGVOPx_gv(op);
521 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
522 SV *nsv = cSVOPx_sv(op);
523 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
524 name = SvPV_const(nsv, len);
530 sv = sv_2mortal(newSVpvn("$", 1));
531 sv_catpvn_nomg(sv, name, len);
532 s = indirect_find(sv, PL_oldbufptr);
533 if (!s) { /* If it failed, retry without the current stash */
534 const char *stash = HvNAME_get(PL_curstash);
535 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
537 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
538 || name[stashlen] != ':' || name[stashlen+1] != ':') {
539 /* Failed again ? Try to remove main */
542 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
543 || name[stashlen] != ':' || name[stashlen+1] != ':')
547 sv_setpvn(sv, "$", 1);
549 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
550 s = indirect_find(sv, PL_oldbufptr);
555 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
556 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
561 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
563 indirect_map_delete(o);
567 /* ... ck_padany ........................................................... */
569 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
571 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
572 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
574 if (indirect_hint()) {
576 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
578 while (s < t && isSPACE(*s)) ++s;
579 if (*s == '$' && ++s <= t) {
580 while (s < t && isSPACE(*s)) ++s;
581 while (s < t && isSPACE(*t)) --t;
582 sv = sv_2mortal(newSVpvn("$", 1));
583 sv_catpvn_nomg(sv, s, t - s + 1);
584 indirect_map_store(o, s, sv, CopLINE(&PL_compiling));
589 indirect_map_delete(o);
593 /* ... ck_scope ............................................................ */
595 STATIC OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
596 STATIC OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
598 STATIC OP *indirect_ck_scope(pTHX_ OP *o) {
599 OP *(*old_ck)(pTHX_ OP *) = 0;
601 switch (o->op_type) {
602 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
603 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
605 o = CALL_FPTR(old_ck)(aTHX_ o);
607 if (indirect_hint()) {
608 indirect_map_store(o, PL_oldbufptr, NULL, CopLINE(&PL_compiling));
612 indirect_map_delete(o);
616 /* We don't need to clean the map entries for leave ops because they can only
617 * be created by mutating from a lineseq. */
619 /* ... ck_method ........................................................... */
621 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
623 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
624 if (indirect_hint()) {
625 OP *op = cUNOPo->op_first;
626 const indirect_op_info_t *oi = indirect_map_fetch(op);
627 const char *s = NULL;
631 if (oi && (s = oi->pos)) {
632 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
633 line = oi->line; /* Keep the old line so that we really point to the first */
636 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
638 sv = sv_mortalcopy(sv);
639 s = indirect_find(sv, PL_oldbufptr);
640 line = CopLINE(&PL_compiling);
643 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
644 /* o may now be a method_named */
646 indirect_map_store(o, s, sv, line);
651 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
653 indirect_map_delete(o);
657 /* ... ck_entersub ......................................................... */
659 STATIC int indirect_is_indirect(const indirect_op_info_t *moi, const indirect_op_info_t *ooi) {
660 if (moi->pos > ooi->pos)
663 if (moi->pos == ooi->pos)
664 return moi->len == ooi->len && !memcmp(moi->buf, ooi->buf, moi->len);
669 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
671 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
672 SV *code = indirect_hint();
674 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
677 const indirect_op_info_t *moi, *ooi;
683 lop = (LISTOP *) oop;
684 if (!(lop->op_flags & OPf_KIDS))
687 } while (oop->op_type != OP_PUSHMARK);
688 oop = oop->op_sibling;
694 switch (oop->op_type) {
705 if (mop->op_type == OP_METHOD)
706 mop = cUNOPx(mop)->op_first;
707 else if (mop->op_type != OP_METHOD_NAMED)
710 moi = indirect_map_fetch(mop);
711 if (!(moi && moi->pos))
714 ooi = indirect_map_fetch(oop);
715 if (!(ooi && ooi->pos))
718 if (indirect_is_indirect(moi, ooi)) {
726 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
728 file = sv_mortalcopy(CopFILESV(&PL_compiling));
733 mPUSHp(ooi->buf, ooi->len);
734 mPUSHp(moi->buf, moi->len);
739 call_sv(code, G_VOID);
752 STATIC U32 indirect_initialized = 0;
754 /* --- XS ------------------------------------------------------------------ */
756 MODULE = indirect PACKAGE = indirect
762 if (!indirect_initialized++) {
766 MY_CXT.map = ptable_new();
767 MY_CXT.linestr = NULL;
768 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
769 MY_CXT.tbl = ptable_new();
775 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
777 indirect_old_ck_const = PL_check[OP_CONST];
778 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
779 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
780 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
781 indirect_old_ck_padany = PL_check[OP_PADANY];
782 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
783 indirect_old_ck_scope = PL_check[OP_SCOPE];
784 PL_check[OP_SCOPE] = MEMBER_TO_FPTR(indirect_ck_scope);
785 indirect_old_ck_lineseq = PL_check[OP_LINESEQ];
786 PL_check[OP_LINESEQ] = MEMBER_TO_FPTR(indirect_ck_scope);
788 indirect_old_ck_method = PL_check[OP_METHOD];
789 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
790 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
791 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
793 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
794 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
810 ud.tbl = t = ptable_new();
811 ud.owner = MY_CXT.owner;
812 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
816 MY_CXT.map = ptable_new();
817 MY_CXT.linestr = NULL;
822 level = PerlMemShared_malloc(sizeof *level);
825 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
835 RETVAL = indirect_tag(value);