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))
51 # define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
54 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
56 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
57 # ifndef PL_lex_inwhat
58 # define PL_lex_inwhat PL_parser->lex_inwhat
61 # define PL_linestr PL_parser->linestr
64 # define PL_bufptr PL_parser->bufptr
67 # define PL_oldbufptr PL_parser->oldbufptr
70 # ifndef PL_lex_inwhat
71 # define PL_lex_inwhat PL_Ilex_inwhat
74 # define PL_linestr PL_Ilinestr
77 # define PL_bufptr PL_Ibufptr
80 # define PL_oldbufptr PL_Ioldbufptr
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 /* ... Pointer table ....................................................... */
124 #define PTABLE_NAME ptable
125 #define PTABLE_VAL_FREE(V) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V)
134 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
135 #define ptable_clear(T) ptable_clear(aTHX_ (T))
136 #define ptable_free(T) ptable_free(aTHX_ (T))
138 /* ... Thread-safe hints ................................................... */
140 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
158 #endif /* I_THREADSAFE */
164 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
168 if (ud->owner != aTHX) {
170 AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
171 param.stashes = stashes;
173 param.proto_perl = ud->owner;
174 val = sv_dup(val, ¶m);
177 SvREFCNT_dec(stashes);
181 ptable_store(ud->tbl, ent->key, val);
185 STATIC void indirect_thread_cleanup(pTHX_ void *);
187 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
194 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
198 PerlMemShared_free(level);
199 ptable_free(MY_CXT.map);
200 ptable_free(MY_CXT.tbl);
204 STATIC SV *indirect_tag(pTHX_ SV *value) {
205 #define indirect_tag(V) indirect_tag(aTHX_ (V))
208 value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
209 /* We only need for the key to be an unique tag for looking up the value later.
210 * Allocated memory provides convenient unique identifiers, so that's why we
211 * use the value pointer as the key itself. */
212 ptable_store(MY_CXT.tbl, value, value);
215 return newSVuv(PTR2UV(value));
218 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
219 #define indirect_detag(H) indirect_detag(aTHX_ (H))
223 if (!hint || !SvOK(hint) || !SvIOK(hint))
226 tag = INT2PTR(void *, SvIVX(hint));
229 value = ptable_fetch(MY_CXT.tbl, tag);
237 STATIC SV *indirect_tag(pTHX_ SV *value) {
238 #define indirect_tag(V) indirect_tag(aTHX_ (V))
241 if (SvOK(value) && SvROK(value)) {
250 #define indirect_detag(H) INT2PTR(SV *, SvUVX(H))
252 #endif /* I_THREADSAFE */
254 STATIC U32 indirect_hash = 0;
256 STATIC SV *indirect_hint(pTHX) {
257 #define indirect_hint() indirect_hint(aTHX)
259 #if I_HAS_PERL(5, 9, 5)
260 id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
262 __PACKAGE__, __PACKAGE_LEN__,
266 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
272 return (id && SvOK(id)) ? id : NULL;
275 /* ... op -> source position ............................................... */
277 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
278 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
282 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
283 * In this case the linestr has temporarly changed, but the old buffer should
284 * still be alive somewhere. */
286 if (!PL_lex_inwhat) {
287 const char *pl_linestr = SvPVX_const(PL_linestr);
288 if (MY_CXT.linestr != pl_linestr) {
289 ptable_clear(MY_CXT.map);
290 MY_CXT.linestr = pl_linestr;
295 SvUPGRADE(val, SVt_PVIV);
296 SvUVX(val) = PTR2UV(src);
301 ptable_store(MY_CXT.map, o, val);
304 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
305 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
309 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
312 val = ptable_fetch(MY_CXT.map, o);
319 return INT2PTR(const char *, SvUVX(val));
322 /* --- Check functions ----------------------------------------------------- */
324 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
325 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
327 const char *p = NULL, *r = SvPV_const(sv, len);
329 if (len >= 1 && *r == '$') {
342 p = strstr(p + 1, r);
348 /* ... ck_const ............................................................ */
350 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
352 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
353 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
355 if (indirect_hint()) {
357 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
358 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
364 /* ... ck_rv2sv ............................................................ */
366 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
368 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
369 if (indirect_hint()) {
370 OP *op = cUNOPo->op_first;
372 const char *name = NULL, *s;
374 OPCODE type = (OPCODE) op->op_type;
379 GV *gv = cGVOPx_gv(op);
385 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
386 SV *nsv = cSVOPx_sv(op);
387 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
388 name = SvPV_const(nsv, len);
394 sv = sv_2mortal(newSVpvn("$", 1));
395 sv_catpvn_nomg(sv, name, len);
396 s = indirect_find(sv, PL_oldbufptr);
397 if (!s) { /* If it failed, retry without the current stash */
398 const char *stash = HvNAME_get(PL_curstash);
399 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
401 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
402 || name[stashlen] != ':' || name[stashlen+1] != ':') {
403 /* Failed again ? Try to remove main */
406 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
407 || name[stashlen] != ':' || name[stashlen+1] != ':')
411 sv_setpvn(sv, "$", 1);
413 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
414 s = indirect_find(sv, PL_oldbufptr);
419 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
420 indirect_map_store(o, s, sv);
425 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
428 /* ... ck_padany ........................................................... */
430 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
432 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
433 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
435 if (indirect_hint()) {
437 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
439 while (s < t && isSPACE(*s)) ++s;
440 if (*s == '$' && ++s <= t) {
441 while (s < t && isSPACE(*s)) ++s;
442 while (s < t && isSPACE(*t)) --t;
443 sv = sv_2mortal(newSVpvn("$", 1));
444 sv_catpvn_nomg(sv, s, t - s + 1);
445 indirect_map_store(o, s, sv);
452 /* ... ck_method ........................................................... */
454 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
456 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
457 if (indirect_hint()) {
458 OP *op = cUNOPo->op_first;
460 const char *s = indirect_map_fetch(op, &sv);
463 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
465 sv = sv_mortalcopy(sv);
466 s = indirect_find(sv, PL_oldbufptr);
468 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
469 /* o may now be a method_named */
470 indirect_map_store(o, s, sv);
475 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
478 /* ... ck_entersub ......................................................... */
480 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
482 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
483 SV *hint = indirect_hint();
485 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
488 const char *mpos, *opos;
489 SV *mnamesv, *onamesv;
495 lop = (LISTOP *) oop;
496 if (!(lop->op_flags & OPf_KIDS))
499 } while (oop->op_type != OP_PUSHMARK);
500 oop = oop->op_sibling;
503 if (mop->op_type == OP_METHOD)
504 mop = cUNOPx(mop)->op_first;
505 else if (mop->op_type != OP_METHOD_NAMED)
508 mpos = indirect_map_fetch(mop, &mnamesv);
512 opos = indirect_map_fetch(oop, &onamesv);
517 SV *code = indirect_detag(hint);
524 onamesv = sv_mortalcopy(onamesv);
525 mnamesv = sv_mortalcopy(mnamesv);
528 file = newSVpv(CopFILE(&PL_compiling), 0);
530 file = sv_mortalcopy(CopFILESV(&PL_compiling));
532 line = CopLINE(&PL_compiling);
545 call_sv(code, G_VOID);
559 STATIC U32 indirect_initialized = 0;
561 /* --- XS ------------------------------------------------------------------ */
563 MODULE = indirect PACKAGE = indirect
569 if (!indirect_initialized++) {
573 MY_CXT.map = ptable_new();
574 MY_CXT.linestr = NULL;
576 MY_CXT.tbl = ptable_new();
580 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
582 indirect_old_ck_const = PL_check[OP_CONST];
583 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
584 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
585 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
586 indirect_old_ck_padany = PL_check[OP_PADANY];
587 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
588 indirect_old_ck_method = PL_check[OP_METHOD];
589 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
590 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
591 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
593 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
594 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
610 ud.tbl = t = ptable_new();
611 ud.owner = MY_CXT.owner;
612 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
616 MY_CXT.map = ptable_new();
617 MY_CXT.linestr = NULL;
622 level = PerlMemShared_malloc(sizeof *level);
625 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
635 RETVAL = indirect_tag(value);