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 SvPV_const SvPV
26 #ifndef SvPV_nolen_const
27 # define SvPV_nolen_const SvPV_nolen
31 # define SvPVX_const SvPVX
34 #ifndef sv_catpvn_nomg
35 # define sv_catpvn_nomg sv_catpvn
39 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
43 # define HvNAME_get(H) HvNAME(H)
47 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
50 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
52 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
53 # ifndef PL_lex_inwhat
54 # define PL_lex_inwhat PL_parser->lex_inwhat
57 # define PL_linestr PL_parser->linestr
60 # define PL_bufptr PL_parser->bufptr
63 # define PL_oldbufptr PL_parser->oldbufptr
66 # ifndef PL_lex_inwhat
67 # define PL_lex_inwhat PL_Ilex_inwhat
70 # define PL_linestr PL_Ilinestr
73 # define PL_bufptr PL_Ibufptr
76 # define PL_oldbufptr PL_Ioldbufptr
80 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
81 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
84 /* ... Thread safety and multiplicity ...................................... */
86 #ifndef I_MULTIPLICITY
87 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
88 # define I_MULTIPLICITY 1
90 # define I_MULTIPLICITY 0
93 #if I_MULTIPLICITY && !defined(tTHX)
94 # define tTHX PerlInterpreter*
97 #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))
98 # define I_THREADSAFE 1
100 # define MY_CXT_CLONE \
102 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
103 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
104 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
107 # define I_THREADSAFE 0
109 # define dMY_CXT dNOOP
111 # define MY_CXT indirect_globaldata
113 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
115 # define MY_CXT_INIT NOOP
117 # define MY_CXT_CLONE NOOP
120 /* --- Helpers ------------------------------------------------------------- */
122 /* ... Thread-safe hints ................................................... */
124 /* If any of those are true, we need to store the hint in a global table. */
126 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
130 #if I_WORKAROUND_REQUIRE_PROPAGATION
135 #define PTABLE_NAME ptable_hints
137 #if I_WORKAROUND_REQUIRE_PROPAGATION
138 # define PTABLE_VAL_FREE(V) \
139 { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
141 # define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
151 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
152 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
154 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
156 /* Define the op->str ptable here because we need to be able to clean it during
159 #define PTABLE_NAME ptable
160 #define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
169 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
170 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
171 #define ptable_clear(T) ptable_clear(aTHX_ (T))
172 #define ptable_free(T) ptable_free(aTHX_ (T))
174 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
177 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
178 ptable *tbl; /* It really is a ptable_hints */
191 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
193 indirect_hint_t *h1 = ent->val;
194 indirect_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
198 if (ud->owner != aTHX) {
201 AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
202 param.stashes = stashes;
204 param.proto_perl = ud->owner;
205 h2->code = sv_dup(val, ¶m);
208 SvREFCNT_dec(stashes);
212 ptable_hints_store(ud->tbl, ent->key, h2);
213 SvREFCNT_inc(h2->code);
216 STATIC void indirect_thread_cleanup(pTHX_ void *);
218 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
224 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
228 PerlMemShared_free(level);
229 ptable_free(MY_CXT.map);
230 ptable_hints_free(MY_CXT.tbl);
234 #endif /* I_THREADSAFE */
236 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
238 STATIC SV *indirect_tag(pTHX_ SV *value) {
239 #define indirect_tag(V) indirect_tag(aTHX_ (V))
243 value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
245 h = PerlMemShared_malloc(sizeof *h);
246 h->code = SvREFCNT_inc(value);
248 #if I_WORKAROUND_REQUIRE_PROPAGATION
253 for (si = PL_curstackinfo; si; si = si->si_prev) {
256 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
257 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
259 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
264 h->requires = requires;
268 /* We only need for the key to be an unique tag for looking up the value later.
269 * Allocated memory provides convenient unique identifiers, so that's why we
270 * use the value pointer as the key itself. */
271 ptable_hints_store(MY_CXT.tbl, value, h);
273 return newSVuv(PTR2UV(value));
276 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
277 #define indirect_detag(H) indirect_detag(aTHX_ (H))
281 if (!(hint && SvOK(hint) && SvIOK(hint)))
284 h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
286 #if I_WORKAROUND_REQUIRE_PROPAGATION
291 for (si = PL_curstackinfo; si; si = si->si_prev) {
294 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
295 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
297 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
298 && ++requires > h->requires)
310 STATIC SV *indirect_tag(pTHX_ SV *value) {
311 #define indirect_tag(V) indirect_tag(aTHX_ (V))
314 if (SvOK(value) && SvROK(value)) {
323 #define indirect_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
325 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
327 STATIC U32 indirect_hash = 0;
329 STATIC SV *indirect_hint(pTHX) {
330 #define indirect_hint() indirect_hint(aTHX)
332 #if I_HAS_PERL(5, 9, 5)
333 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
335 __PACKAGE__, __PACKAGE_LEN__,
339 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
345 return indirect_detag(hint);
348 /* ... op -> source position ............................................... */
350 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
351 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
355 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
356 * In this case the linestr has temporarly changed, but the old buffer should
357 * still be alive somewhere. */
359 if (!PL_lex_inwhat) {
360 const char *pl_linestr = SvPVX_const(PL_linestr);
361 if (MY_CXT.linestr != pl_linestr) {
362 ptable_clear(MY_CXT.map);
363 MY_CXT.linestr = pl_linestr;
368 SvUPGRADE(val, SVt_PVIV);
369 SvUVX(val) = PTR2UV(src);
374 ptable_store(MY_CXT.map, o, val);
377 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
378 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
382 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
385 val = ptable_fetch(MY_CXT.map, o);
392 return INT2PTR(const char *, SvUVX(val));
395 STATIC void indirect_map_delete(pTHX_ const OP *o) {
396 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
399 ptable_delete(MY_CXT.map, o);
402 /* --- Check functions ----------------------------------------------------- */
404 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
405 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
407 const char *p = NULL, *r = SvPV_const(sv, len);
409 if (len >= 1 && *r == '$') {
422 p = strstr(p + 1, r);
428 /* ... ck_const ............................................................ */
430 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
432 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
433 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
435 if (indirect_hint()) {
437 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
438 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
443 indirect_map_delete(o);
447 /* ... ck_rv2sv ............................................................ */
449 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
451 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
452 if (indirect_hint()) {
453 OP *op = cUNOPo->op_first;
455 const char *name = NULL, *s;
457 OPCODE type = (OPCODE) op->op_type;
462 GV *gv = cGVOPx_gv(op);
468 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
469 SV *nsv = cSVOPx_sv(op);
470 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
471 name = SvPV_const(nsv, len);
477 sv = sv_2mortal(newSVpvn("$", 1));
478 sv_catpvn_nomg(sv, name, len);
479 s = indirect_find(sv, PL_oldbufptr);
480 if (!s) { /* If it failed, retry without the current stash */
481 const char *stash = HvNAME_get(PL_curstash);
482 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
484 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
485 || name[stashlen] != ':' || name[stashlen+1] != ':') {
486 /* Failed again ? Try to remove main */
489 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
490 || name[stashlen] != ':' || name[stashlen+1] != ':')
494 sv_setpvn(sv, "$", 1);
496 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
497 s = indirect_find(sv, PL_oldbufptr);
502 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
503 indirect_map_store(o, s, sv);
508 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
510 indirect_map_delete(o);
514 /* ... ck_padany ........................................................... */
516 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
518 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
519 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
521 if (indirect_hint()) {
523 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
525 while (s < t && isSPACE(*s)) ++s;
526 if (*s == '$' && ++s <= t) {
527 while (s < t && isSPACE(*s)) ++s;
528 while (s < t && isSPACE(*t)) --t;
529 sv = sv_2mortal(newSVpvn("$", 1));
530 sv_catpvn_nomg(sv, s, t - s + 1);
531 indirect_map_store(o, s, sv);
536 indirect_map_delete(o);
540 /* ... ck_method ........................................................... */
542 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
544 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
545 if (indirect_hint()) {
546 OP *op = cUNOPo->op_first;
548 const char *s = indirect_map_fetch(op, &sv);
551 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
553 sv = sv_mortalcopy(sv);
554 s = indirect_find(sv, PL_oldbufptr);
556 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
557 /* o may now be a method_named */
558 indirect_map_store(o, s, sv);
563 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
565 indirect_map_delete(o);
569 /* ... ck_entersub ......................................................... */
571 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
573 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
574 SV *code = indirect_hint();
576 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
579 const char *mpos, *opos;
580 SV *mnamesv, *onamesv;
586 lop = (LISTOP *) oop;
587 if (!(lop->op_flags & OPf_KIDS))
590 } while (oop->op_type != OP_PUSHMARK);
591 oop = oop->op_sibling;
597 switch (oop->op_type) {
606 if (mop->op_type == OP_METHOD)
607 mop = cUNOPx(mop)->op_first;
608 else if (mop->op_type != OP_METHOD_NAMED)
611 mpos = indirect_map_fetch(mop, &mnamesv);
615 opos = indirect_map_fetch(oop, &onamesv);
627 onamesv = sv_mortalcopy(onamesv);
628 mnamesv = sv_mortalcopy(mnamesv);
631 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
633 file = sv_mortalcopy(CopFILESV(&PL_compiling));
635 line = CopLINE(&PL_compiling);
645 call_sv(code, G_VOID);
658 STATIC U32 indirect_initialized = 0;
660 /* --- XS ------------------------------------------------------------------ */
662 MODULE = indirect PACKAGE = indirect
668 if (!indirect_initialized++) {
672 MY_CXT.map = ptable_new();
673 MY_CXT.linestr = NULL;
674 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
675 MY_CXT.tbl = ptable_new();
681 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
683 indirect_old_ck_const = PL_check[OP_CONST];
684 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
685 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
686 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
687 indirect_old_ck_padany = PL_check[OP_PADANY];
688 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
689 indirect_old_ck_method = PL_check[OP_METHOD];
690 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
691 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
692 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
694 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
695 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
711 ud.tbl = t = ptable_new();
712 ud.owner = MY_CXT.owner;
713 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
717 MY_CXT.map = ptable_new();
718 MY_CXT.linestr = NULL;
723 level = PerlMemShared_malloc(sizeof *level);
726 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
736 RETVAL = indirect_tag(value);