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 sv_catpvn_nomg
39 # define sv_catpvn_nomg sv_catpvn
43 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
47 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
51 # define HvNAME_get(H) HvNAME(H)
55 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
58 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
60 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
61 # ifndef PL_lex_inwhat
62 # define PL_lex_inwhat PL_parser->lex_inwhat
65 # define PL_linestr PL_parser->linestr
68 # define PL_bufptr PL_parser->bufptr
71 # define PL_oldbufptr PL_parser->oldbufptr
74 # ifndef PL_lex_inwhat
75 # define PL_lex_inwhat PL_Ilex_inwhat
78 # define PL_linestr PL_Ilinestr
81 # define PL_bufptr PL_Ibufptr
84 # define PL_oldbufptr PL_Ioldbufptr
88 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
89 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
92 /* ... Thread safety and multiplicity ...................................... */
94 #ifndef I_MULTIPLICITY
95 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
96 # define I_MULTIPLICITY 1
98 # define I_MULTIPLICITY 0
101 #if I_MULTIPLICITY && !defined(tTHX)
102 # define tTHX PerlInterpreter*
105 #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))
106 # define I_THREADSAFE 1
107 # ifndef MY_CXT_CLONE
108 # define MY_CXT_CLONE \
110 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
111 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
112 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
115 # define I_THREADSAFE 0
117 # define dMY_CXT dNOOP
119 # define MY_CXT indirect_globaldata
121 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
123 # define MY_CXT_INIT NOOP
125 # define MY_CXT_CLONE NOOP
128 /* --- Helpers ------------------------------------------------------------- */
130 /* ... Thread-safe hints ................................................... */
132 /* If any of those are true, we need to store the hint in a global table. */
134 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
138 #if I_WORKAROUND_REQUIRE_PROPAGATION
143 #define PTABLE_NAME ptable_hints
145 #define PTABLE_VAL_FREE(V) \
146 { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
155 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
156 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
158 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
160 /* Define the op->str ptable here because we need to be able to clean it during
167 } indirect_op_info_t;
169 #define PTABLE_NAME ptable
170 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
179 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
180 #define ptable_clear(T) ptable_clear(aTHX_ (T))
181 #define ptable_free(T) ptable_free(aTHX_ (T))
183 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
186 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
187 ptable *tbl; /* It really is a ptable_hints */
200 STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
201 #define indirect_clone(S, O) indirect_clone(aTHX_ (S), (O))
206 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
209 param.stashes = stashes;
211 param.proto_perl = owner;
213 dupsv = sv_dup(sv, ¶m);
217 SvREFCNT_dec(stashes);
220 return SvREFCNT_inc(dupsv);
223 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
225 indirect_hint_t *h1 = ent->val;
226 indirect_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
230 if (ud->owner != aTHX)
231 h2->code = indirect_clone(h1->code, ud->owner);
233 ptable_hints_store(ud->tbl, ent->key, h2);
234 SvREFCNT_inc(h2->code);
237 STATIC void indirect_thread_cleanup(pTHX_ void *);
239 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
245 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
249 PerlMemShared_free(level);
250 ptable_free(MY_CXT.map);
251 ptable_hints_free(MY_CXT.tbl);
255 #endif /* I_THREADSAFE */
257 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
259 STATIC SV *indirect_tag(pTHX_ SV *value) {
260 #define indirect_tag(V) indirect_tag(aTHX_ (V))
264 value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
266 h = PerlMemShared_malloc(sizeof *h);
267 h->code = SvREFCNT_inc(value);
269 #if I_WORKAROUND_REQUIRE_PROPAGATION
274 for (si = PL_curstackinfo; si; si = si->si_prev) {
277 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
278 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
280 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
285 h->requires = requires;
289 /* We only need for the key to be an unique tag for looking up the value later.
290 * Allocated memory provides convenient unique identifiers, so that's why we
291 * use the value pointer as the key itself. */
292 ptable_hints_store(MY_CXT.tbl, value, h);
294 return newSVuv(PTR2UV(value));
297 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
298 #define indirect_detag(H) indirect_detag(aTHX_ (H))
302 if (!(hint && SvOK(hint) && SvIOK(hint)))
305 h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
307 #if I_WORKAROUND_REQUIRE_PROPAGATION
312 for (si = PL_curstackinfo; si; si = si->si_prev) {
315 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
316 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
318 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
319 && ++requires > h->requires)
331 STATIC SV *indirect_tag(pTHX_ SV *value) {
332 #define indirect_tag(V) indirect_tag(aTHX_ (V))
335 if (SvOK(value) && SvROK(value)) {
344 #define indirect_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
346 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
348 STATIC U32 indirect_hash = 0;
350 STATIC SV *indirect_hint(pTHX) {
351 #define indirect_hint() indirect_hint(aTHX)
353 #if I_HAS_PERL(5, 9, 5)
354 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
356 __PACKAGE__, __PACKAGE_LEN__,
360 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
366 return indirect_detag(hint);
369 /* ... op -> source position ............................................... */
371 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
372 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
373 indirect_op_info_t *oi;
378 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
379 * In this case the linestr has temporarly changed, but the old buffer should
380 * still be alive somewhere. */
382 if (!PL_lex_inwhat) {
383 const char *pl_linestr = SvPVX_const(PL_linestr);
384 if (MY_CXT.linestr != pl_linestr) {
385 ptable_clear(MY_CXT.map);
386 MY_CXT.linestr = pl_linestr;
390 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
391 Newx(oi, 1, indirect_op_info_t);
392 ptable_store(MY_CXT.map, o, oi);
397 s = SvPV_const(sv, len);
398 if (len > oi->size) {
400 Newx(oi->buf, len, char);
403 Copy(s, oi->buf, len, char);
408 STATIC const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
409 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
410 const indirect_op_info_t *val;
413 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
416 return ptable_fetch(MY_CXT.map, o);
419 STATIC void indirect_map_delete(pTHX_ const OP *o) {
420 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
423 ptable_store(MY_CXT.map, o, NULL);
426 /* --- Check functions ----------------------------------------------------- */
428 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
429 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
431 const char *p = NULL, *r = SvPV_const(sv, len);
433 if (len >= 1 && *r == '$') {
446 p = strstr(p + 1, r);
452 /* ... ck_const ............................................................ */
454 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
456 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
457 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
459 if (indirect_hint()) {
461 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
462 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
467 indirect_map_delete(o);
471 /* ... ck_rv2sv ............................................................ */
473 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
475 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
476 if (indirect_hint()) {
477 OP *op = cUNOPo->op_first;
479 const char *name = NULL, *s;
481 OPCODE type = (OPCODE) op->op_type;
486 GV *gv = cGVOPx_gv(op);
492 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
493 SV *nsv = cSVOPx_sv(op);
494 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
495 name = SvPV_const(nsv, len);
501 sv = sv_2mortal(newSVpvn("$", 1));
502 sv_catpvn_nomg(sv, name, len);
503 s = indirect_find(sv, PL_oldbufptr);
504 if (!s) { /* If it failed, retry without the current stash */
505 const char *stash = HvNAME_get(PL_curstash);
506 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
508 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
509 || name[stashlen] != ':' || name[stashlen+1] != ':') {
510 /* Failed again ? Try to remove main */
513 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
514 || name[stashlen] != ':' || name[stashlen+1] != ':')
518 sv_setpvn(sv, "$", 1);
520 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
521 s = indirect_find(sv, PL_oldbufptr);
526 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
527 indirect_map_store(o, s, sv);
532 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
534 indirect_map_delete(o);
538 /* ... ck_padany ........................................................... */
540 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
542 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
543 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
545 if (indirect_hint()) {
547 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
549 while (s < t && isSPACE(*s)) ++s;
550 if (*s == '$' && ++s <= t) {
551 while (s < t && isSPACE(*s)) ++s;
552 while (s < t && isSPACE(*t)) --t;
553 sv = sv_2mortal(newSVpvn("$", 1));
554 sv_catpvn_nomg(sv, s, t - s + 1);
555 indirect_map_store(o, s, sv);
560 indirect_map_delete(o);
564 /* ... ck_method ........................................................... */
566 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
568 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
569 if (indirect_hint()) {
570 OP *op = cUNOPo->op_first;
571 const indirect_op_info_t *oi = indirect_map_fetch(op);
572 const char *s = NULL;
575 if (oi && (s = oi->pos)) {
576 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
579 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
581 sv = sv_mortalcopy(sv);
582 s = indirect_find(sv, PL_oldbufptr);
585 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
586 /* o may now be a method_named */
588 indirect_map_store(o, s, sv);
593 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
595 indirect_map_delete(o);
599 /* ... ck_entersub ......................................................... */
601 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
603 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
604 SV *code = indirect_hint();
606 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
609 const indirect_op_info_t *moi, *ooi;
615 lop = (LISTOP *) oop;
616 if (!(lop->op_flags & OPf_KIDS))
619 } while (oop->op_type != OP_PUSHMARK);
620 oop = oop->op_sibling;
626 switch (oop->op_type) {
635 if (mop->op_type == OP_METHOD)
636 mop = cUNOPx(mop)->op_first;
637 else if (mop->op_type != OP_METHOD_NAMED)
640 moi = indirect_map_fetch(mop);
641 if (!(moi && moi->pos))
644 ooi = indirect_map_fetch(oop);
645 if (!(ooi && ooi->pos))
648 if (moi->pos < ooi->pos) {
657 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
659 file = sv_mortalcopy(CopFILESV(&PL_compiling));
661 line = CopLINE(&PL_compiling);
665 mPUSHp(ooi->buf, ooi->len);
666 mPUSHp(moi->buf, moi->len);
671 call_sv(code, G_VOID);
684 STATIC U32 indirect_initialized = 0;
686 /* --- XS ------------------------------------------------------------------ */
688 MODULE = indirect PACKAGE = indirect
694 if (!indirect_initialized++) {
698 MY_CXT.map = ptable_new();
699 MY_CXT.linestr = NULL;
700 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
701 MY_CXT.tbl = ptable_new();
707 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
709 indirect_old_ck_const = PL_check[OP_CONST];
710 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
711 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
712 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
713 indirect_old_ck_padany = PL_check[OP_PADANY];
714 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
715 indirect_old_ck_method = PL_check[OP_METHOD];
716 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
717 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
718 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
720 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
721 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
737 ud.tbl = t = ptable_new();
738 ud.owner = MY_CXT.owner;
739 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
743 MY_CXT.map = ptable_new();
744 MY_CXT.linestr = NULL;
749 level = PerlMemShared_malloc(sizeof *level);
752 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
762 RETVAL = indirect_tag(value);