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 HvNAME_get(H) HvNAME(H)
43 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
47 # define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
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 /* ... Thread safety and multiplicity ...................................... */
82 #ifndef I_MULTIPLICITY
83 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
84 # define I_MULTIPLICITY 1
86 # define I_MULTIPLICITY 0
89 #if I_MULTIPLICITY && !defined(tTHX)
90 # define tTHX PerlInterpreter*
93 #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))
94 # define I_THREADSAFE 1
96 # define MY_CXT_CLONE \
98 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
99 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
100 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
103 # define I_THREADSAFE 0
105 # define dMY_CXT dNOOP
107 # define MY_CXT indirect_globaldata
109 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
111 # define MY_CXT_INIT NOOP
113 # define MY_CXT_CLONE NOOP
116 /* --- Helpers ------------------------------------------------------------- */
118 /* ... Pointer table ....................................................... */
120 #define PTABLE_NAME ptable
121 #define PTABLE_VAL_FREE(V) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V)
130 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
131 #define ptable_clear(T) ptable_clear(aTHX_ (T))
132 #define ptable_free(T) ptable_free(aTHX_ (T))
134 /* ... Thread-safe hints ................................................... */
136 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
154 #endif /* I_THREADSAFE */
160 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
164 if (ud->owner != aTHX) {
166 AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
167 param.stashes = stashes;
169 param.proto_perl = ud->owner;
170 val = sv_dup(val, ¶m);
173 SvREFCNT_dec(stashes);
177 ptable_store(ud->tbl, ent->key, val);
181 STATIC void indirect_thread_cleanup(pTHX_ void *);
183 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
190 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
194 PerlMemShared_free(level);
195 ptable_free(MY_CXT.map);
196 ptable_free(MY_CXT.tbl);
200 STATIC SV *indirect_tag(pTHX_ SV *value) {
201 #define indirect_tag(V) indirect_tag(aTHX_ (V))
204 value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
205 /* We only need for the key to be an unique tag for looking up the value later.
206 * Allocated memory provides convenient unique identifiers, so that's why we
207 * use the value pointer as the key itself. */
208 ptable_store(MY_CXT.tbl, value, value);
211 return newSVuv(PTR2UV(value));
214 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
215 #define indirect_detag(H) indirect_detag(aTHX_ (H))
219 if (!hint || !SvOK(hint) || !SvIOK(hint))
222 tag = INT2PTR(void *, SvIVX(hint));
225 value = ptable_fetch(MY_CXT.tbl, tag);
233 STATIC SV *indirect_tag(pTHX_ SV *value) {
234 #define indirect_tag(V) indirect_tag(aTHX_ (V))
237 if (SvOK(value) && SvROK(value)) {
246 #define indirect_detag(H) INT2PTR(SV *, SvUVX(H))
248 #endif /* I_THREADSAFE */
250 STATIC U32 indirect_hash = 0;
252 STATIC SV *indirect_hint(pTHX) {
253 #define indirect_hint() indirect_hint(aTHX)
255 #if I_HAS_PERL(5, 10, 0)
256 id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
258 __PACKAGE__, __PACKAGE_LEN__,
262 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
268 return (id && SvOK(id)) ? id : NULL;
271 /* ... op -> source position ............................................... */
273 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
274 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
278 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
279 * In this case the linestr has temporarly changed, but the old buffer should
280 * still be alive somewhere. */
282 if (!PL_lex_inwhat) {
283 const char *pl_linestr = SvPVX_const(PL_linestr);
284 if (MY_CXT.linestr != pl_linestr) {
285 ptable_clear(MY_CXT.map);
286 MY_CXT.linestr = pl_linestr;
291 SvUPGRADE(val, SVt_PVIV);
292 SvUVX(val) = PTR2UV(src);
296 ptable_store(MY_CXT.map, o, val);
299 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
300 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
304 if (MY_CXT.linestr != SvPVX_const(PL_linestr))
307 val = ptable_fetch(MY_CXT.map, o);
314 return INT2PTR(const char *, SvUVX(val));
317 /* --- Check functions ----------------------------------------------------- */
319 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
320 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
322 const char *p = NULL, *r = SvPV_const(sv, len);
324 if (len >= 1 && *r == '$') {
337 p = strstr(p + 1, r);
343 /* ... ck_const ............................................................ */
345 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
347 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
348 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
350 if (indirect_hint()) {
352 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
353 indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
359 /* ... ck_rv2sv ............................................................ */
361 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
363 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
364 if (indirect_hint()) {
365 OP *op = cUNOPo->op_first;
367 const char *name = NULL, *s;
369 OPCODE type = (OPCODE) op->op_type;
374 GV *gv = cGVOPx_gv(op);
380 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
381 SV *nsv = cSVOPx_sv(op);
382 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
383 name = SvPV_const(nsv, len);
389 sv = sv_2mortal(newSVpvn("$", 1));
390 sv_catpvn_nomg(sv, name, len);
391 s = indirect_find(sv, PL_oldbufptr);
392 if (!s) { /* If it failed, retry without the current stash */
393 const char *stash = HvNAME_get(PL_curstash);
394 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
396 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
397 || name[stashlen] != ':' || name[stashlen+1] != ':') {
398 /* Failed again ? Try to remove main */
401 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
402 || name[stashlen] != ':' || name[stashlen+1] != ':')
406 sv_setpvn(sv, "$", 1);
408 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
409 s = indirect_find(sv, PL_oldbufptr);
414 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
415 indirect_map_store(o, s, sv);
420 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
423 /* ... ck_padany ........................................................... */
425 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
427 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
428 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
430 if (indirect_hint()) {
432 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
434 while (s < t && isSPACE(*s)) ++s;
435 if (*s == '$' && ++s <= t) {
436 while (s < t && isSPACE(*s)) ++s;
437 while (s < t && isSPACE(*t)) --t;
438 sv = sv_2mortal(newSVpvn("$", 1));
439 sv_catpvn_nomg(sv, s, t - s + 1);
440 indirect_map_store(o, s, sv);
447 /* ... ck_method ........................................................... */
449 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
451 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
452 if (indirect_hint()) {
453 OP *op = cUNOPo->op_first;
455 const char *s = indirect_map_fetch(op, &sv);
458 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
460 sv = sv_mortalcopy(sv);
461 s = indirect_find(sv, PL_oldbufptr);
463 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
464 /* o may now be a method_named */
465 indirect_map_store(o, s, sv);
470 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
473 /* ... ck_entersub ......................................................... */
475 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
477 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
478 SV *hint = indirect_hint();
480 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
483 const char *mpos, *opos;
484 SV *mnamesv, *onamesv;
490 lop = (LISTOP *) oop;
491 if (!(lop->op_flags & OPf_KIDS))
494 } while (oop->op_type != OP_PUSHMARK);
495 oop = oop->op_sibling;
498 if (mop->op_type == OP_METHOD)
499 mop = cUNOPx(mop)->op_first;
500 else if (mop->op_type != OP_METHOD_NAMED)
503 mpos = indirect_map_fetch(mop, &mnamesv);
507 opos = indirect_map_fetch(oop, &onamesv);
512 SV *code = indirect_detag(hint);
526 call_sv(code, G_VOID);
540 STATIC U32 indirect_initialized = 0;
542 /* --- XS ------------------------------------------------------------------ */
544 MODULE = indirect PACKAGE = indirect
550 if (!indirect_initialized++) {
554 MY_CXT.map = ptable_new();
555 MY_CXT.linestr = NULL;
557 MY_CXT.tbl = ptable_new();
561 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
563 indirect_old_ck_const = PL_check[OP_CONST];
564 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
565 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
566 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
567 indirect_old_ck_padany = PL_check[OP_PADANY];
568 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
569 indirect_old_ck_method = PL_check[OP_METHOD];
570 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
571 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
572 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);
574 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
575 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
591 ud.tbl = t = ptable_new();
592 ud.owner = MY_CXT.owner;
593 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
597 MY_CXT.map = ptable_new();
598 MY_CXT.linestr = NULL;
603 level = PerlMemShared_malloc(sizeof *level);
606 SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
616 RETVAL = indirect_tag(value);