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 /* --- XS helpers ---------------------------------------------------------- */
11 #define XSH_PACKAGE "indirect"
18 /* ... op => source position map ........................................... */
28 #define PTABLE_NAME ptable
29 #define PTABLE_VAL_FREE(V) if (V) { indirect_op_info_t *oi = (V); XSH_LOCAL_FREE(oi->buf, oi->size, char); XSH_LOCAL_FREE(oi, 1, indirect_op_info_t); }
30 #define PTABLE_NEED_DELETE 1
31 #define PTABLE_NEED_WALK 0
33 #include "xsh/ptable.h"
35 /* XSH_LOCAL_FREE() always need aTHX */
36 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
37 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
38 #define ptable_clear(T) ptable_clear(aTHX_ (T))
39 #define ptable_free(T) ptable_free(aTHX_ (T))
41 /* ... Lexical hints ....................................................... */
43 #define XSH_HINTS_TYPE_SV 1
45 #include "xsh/hints.h"
47 /* ... Thread-local storage ................................................ */
54 #define XSH_THREADS_USER_CONTEXT 1
55 #define XSH_THREADS_USER_CLONE_NEEDS_DUP 1
56 #define XSH_THREADS_COMPILE_TIME_PROTECTION 1
60 static void xsh_user_clone(pTHX_ const xsh_user_cxt_t *old_cxt, xsh_user_cxt_t *new_cxt, CLONE_PARAMS *params) {
61 new_cxt->map = ptable_new(32);
62 new_cxt->global_code = xsh_dup_inc(old_cxt->global_code, params);
67 #endif /* XSH_THREADSAFE */
69 #include "xsh/threads.h"
71 /* ... Lexical hints, continued ............................................ */
73 static SV *indirect_hint(pTHX) {
74 #define indirect_hint() indirect_hint(aTHX)
77 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
82 hint = xsh_hints_fetch();
83 if (hint && SvOK(hint)) {
84 return xsh_hints_detag(hint);
87 if (xsh_is_loaded(&XSH_CXT))
88 return XSH_CXT.global_code;
94 /* --- Compatibility wrappers ---------------------------------------------- */
97 # define SvPV_const SvPV
100 #ifndef SvPV_nolen_const
101 # define SvPV_nolen_const SvPV_nolen
105 # define SvPVX_const SvPVX
108 #ifndef SvREFCNT_inc_simple_void_NN
109 # ifdef SvREFCNT_inc_simple_NN
110 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
112 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
116 #ifndef sv_catpvn_nomg
117 # define sv_catpvn_nomg sv_catpvn
121 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
125 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
129 # define HvNAME_get(H) HvNAME(H)
132 #ifndef HvNAMELEN_get
133 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
136 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
138 # define PL_linestr PL_parser->linestr
141 # define PL_bufptr PL_parser->bufptr
143 # ifndef PL_oldbufptr
144 # define PL_oldbufptr PL_parser->oldbufptr
146 # ifndef PL_lex_inwhat
147 # define PL_lex_inwhat PL_parser->lex_inwhat
149 # ifndef PL_multi_close
150 # define PL_multi_close PL_parser->multi_close
154 # define PL_linestr PL_Ilinestr
157 # define PL_bufptr PL_Ibufptr
159 # ifndef PL_oldbufptr
160 # define PL_oldbufptr PL_Ioldbufptr
162 # ifndef PL_lex_inwhat
163 # define PL_lex_inwhat PL_Ilex_inwhat
165 # ifndef PL_multi_close
166 # define PL_multi_close PL_Imulti_close
170 /* ... Safe version of call_sv() ........................................... */
172 static I32 indirect_call_sv(pTHX_ SV *sv, I32 flags) {
173 #define indirect_call_sv(S, F) indirect_call_sv(aTHX_ (S), (F))
175 PERL_CONTEXT saved_cx;
176 SV *saved_errsv = NULL;
179 if (IN_PERL_COMPILETIME && PL_errors)
180 sv_catsv(PL_errors, ERRSV);
182 saved_errsv = newSVsv(ERRSV);
186 cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
187 /* The last popped context will be reused by call_sv(), but our callers may
188 * still need its previous value. Back it up so that it isn't clobbered. */
189 saved_cx = cxstack[cxix];
191 ret = call_sv(sv, flags | G_EVAL);
193 cxstack[cxix] = saved_cx;
196 /* Discard the old ERRSV, and reuse the variable to temporarily store the
199 sv_setsv(saved_errsv, ERRSV);
201 saved_errsv = newSVsv(ERRSV);
203 /* Immediately flush all errors. */
204 if (IN_PERL_COMPILETIME) {
205 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
207 ++PL_parser->error_count;
208 #elif defined(PL_error_count)
214 sv_setsv(ERRSV, PL_errors);
215 SvCUR_set(PL_errors, 0);
218 sv_catsv(ERRSV, saved_errsv);
219 SvREFCNT_dec(saved_errsv);
221 } else if (saved_errsv) {
222 /* If IN_PERL_COMPILETIME && PL_errors, then the old ERRSV has already been
223 * added to PL_errors. Otherwise, just restore it to ERRSV, as if no eval
224 * block has ever been executed. */
225 sv_setsv(ERRSV, saved_errsv);
226 SvREFCNT_dec(saved_errsv);
232 /* --- Check functions ----------------------------------------------------- */
234 /* ... op => source position map, continued ................................ */
236 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
237 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
238 indirect_op_info_t *oi;
243 /* No need to check for XSH_CXT.map != NULL because this code path is always
244 * guarded by indirect_hint(). */
246 if (!(oi = ptable_fetch(XSH_CXT.map, o))) {
247 XSH_LOCAL_ALLOC(oi, 1, indirect_op_info_t);
248 ptable_store(XSH_CXT.map, o, oi);
254 s = SvPV_const(sv, len);
260 if (len > oi->size) {
261 XSH_LOCAL_REALLOC(oi->buf, oi->size, len, char);
264 Copy(s, oi->buf, len, char);
271 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
272 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
275 /* No need to check for XSH_CXT.map != NULL because this code path is always
276 * guarded by indirect_hint(). */
278 return ptable_fetch(XSH_CXT.map, o);
281 static void indirect_map_delete(pTHX_ const OP *o) {
282 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
285 if (xsh_is_loaded(&XSH_CXT) && XSH_CXT.map)
286 ptable_delete(XSH_CXT.map, o);
289 /* ... Heuristics for finding a string in the source buffer ................ */
291 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
292 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
293 STRLEN name_len, line_len;
294 const char *name, *name_end;
295 const char *line, *line_end;
298 line = SvPV_const(PL_linestr, line_len);
299 line_end = line + line_len;
301 name = SvPV_const(name_sv, name_len);
302 if (name_len >= 1 && *name == '$') {
305 while (line_bufptr < line_end && *line_bufptr != '$')
307 if (line_bufptr >= line_end)
310 name_end = name + name_len;
314 p = ninstr(p, line_end, name, name_end);
317 if (!isALNUM(p[name_len]))
319 /* p points to a word that has name as prefix, skip the rest of the word */
325 *name_pos = p - line;
330 /* ... ck_const ............................................................ */
332 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
334 static OP *indirect_ck_const(pTHX_ OP *o) {
335 o = indirect_old_ck_const(aTHX_ o);
337 if (indirect_hint()) {
340 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
344 bufptr = PL_multi_close == '<' ? PL_bufptr : PL_oldbufptr;
346 if (indirect_find(sv, bufptr, &pos)) {
349 /* If the constant is equal to the current package name, try to look for
350 * a "__PACKAGE__" coming before what we got. We only need to check this
351 * when we already had a match because __PACKAGE__ can only appear in
352 * direct method calls ("new __PACKAGE__" is a syntax error). */
355 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
356 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
358 SV *pkg = sv_newmortal();
359 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
361 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
367 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
373 indirect_map_delete(o);
377 /* ... ck_rv2sv ............................................................ */
379 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
381 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
382 if (indirect_hint()) {
383 OP *op = cUNOPo->op_first;
385 const char *name = NULL;
387 OPCODE type = (OPCODE) op->op_type;
392 GV *gv = cGVOPx_gv(op);
398 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
399 SV *nsv = cSVOPx_sv(op);
400 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
401 name = SvPV_const(nsv, len);
407 sv = sv_2mortal(newSVpvn("$", 1));
408 sv_catpvn_nomg(sv, name, len);
409 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
410 /* If it failed, retry without the current stash */
411 const char *stash = HvNAME_get(PL_curstash);
412 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
414 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
415 || name[stashlen] != ':' || name[stashlen+1] != ':') {
416 /* Failed again ? Try to remove main */
419 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
420 || name[stashlen] != ':' || name[stashlen+1] != ':')
424 sv_setpvn(sv, "$", 1);
426 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
427 if (!indirect_find(sv, PL_oldbufptr, &pos))
431 o = indirect_old_ck_rv2sv(aTHX_ o);
433 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
438 o = indirect_old_ck_rv2sv(aTHX_ o);
440 indirect_map_delete(o);
444 /* ... ck_padany ........................................................... */
446 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
448 static OP *indirect_ck_padany(pTHX_ OP *o) {
449 o = indirect_old_ck_padany(aTHX_ o);
451 if (indirect_hint()) {
453 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
455 while (s < t && isSPACE(*s)) ++s;
456 if (*s == '$' && ++s <= t) {
457 while (s < t && isSPACE(*s)) ++s;
458 while (s < t && isSPACE(*t)) --t;
459 sv = sv_2mortal(newSVpvn("$", 1));
460 sv_catpvn_nomg(sv, s, t - s + 1);
461 indirect_map_store(o, s - SvPVX_const(PL_linestr),
462 sv, CopLINE(&PL_compiling));
467 indirect_map_delete(o);
471 /* ... ck_scope ............................................................ */
473 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
474 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
476 static OP *indirect_ck_scope(pTHX_ OP *o) {
477 OP *(*old_ck)(pTHX_ OP *) = 0;
479 switch (o->op_type) {
480 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
481 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
485 if (indirect_hint()) {
486 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
487 NULL, CopLINE(&PL_compiling));
491 indirect_map_delete(o);
495 /* We don't need to clean the map entries for leave ops because they can only
496 * be created by mutating from a lineseq. */
498 /* ... ck_method ........................................................... */
500 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
502 static OP *indirect_ck_method(pTHX_ OP *o) {
503 if (indirect_hint()) {
504 OP *op = cUNOPo->op_first;
506 /* Indirect method call is only possible when the method is a bareword, so
507 * don't trip up on $obj->$meth. */
508 if (op && op->op_type == OP_CONST) {
509 const indirect_op_info_t *oi = indirect_map_fetch(op);
517 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
519 /* Keep the old line so that we really point to the first line of the
523 o = indirect_old_ck_method(aTHX_ o);
524 /* o may now be a method_named */
526 indirect_map_store(o, pos, sv, line);
532 o = indirect_old_ck_method(aTHX_ o);
534 indirect_map_delete(o);
538 /* ... ck_method_named ..................................................... */
540 /* "use foo/no foo" compiles its call to import/unimport directly to a
541 * method_named op. */
543 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
545 static OP *indirect_ck_method_named(pTHX_ OP *o) {
546 if (indirect_hint()) {
552 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
554 sv = sv_mortalcopy(sv);
556 if (!indirect_find(sv, PL_oldbufptr, &pos))
558 line = CopLINE(&PL_compiling);
560 o = indirect_old_ck_method_named(aTHX_ o);
562 indirect_map_store(o, pos, sv, line);
567 o = indirect_old_ck_method_named(aTHX_ o);
569 indirect_map_delete(o);
573 /* ... ck_entersub ......................................................... */
575 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
577 static OP *indirect_ck_entersub(pTHX_ OP *o) {
578 SV *code = indirect_hint();
580 o = indirect_old_ck_entersub(aTHX_ o);
583 const indirect_op_info_t *moi, *ooi;
589 lop = (LISTOP *) oop;
590 if (!(lop->op_flags & OPf_KIDS))
593 } while (oop->op_type != OP_PUSHMARK);
594 oop = OpSIBLING(oop);
600 switch (oop->op_type) {
611 if (mop->op_type == OP_METHOD)
612 mop = cUNOPx(mop)->op_first;
613 else if (mop->op_type != OP_METHOD_NAMED)
616 moi = indirect_map_fetch(mop);
620 ooi = indirect_map_fetch(oop);
624 /* When positions are identical, the method and the object must have the
625 * same name. But it also means that it is an indirect call, as "foo->foo"
626 * results in different positions. */
627 if ( moi->line < ooi->line
628 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
636 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
638 file = sv_mortalcopy(CopFILESV(&PL_compiling));
643 mPUSHp(ooi->buf, ooi->len);
644 mPUSHp(moi->buf, moi->len);
649 indirect_call_sv(code, G_VOID);
662 /* --- Module setup/teardown ----------------------------------------------- */
664 static void xsh_user_global_setup(pTHX) {
665 xsh_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
666 xsh_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
667 xsh_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
668 xsh_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
669 xsh_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
671 xsh_ck_replace(OP_METHOD, indirect_ck_method,
672 &indirect_old_ck_method);
673 xsh_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
674 &indirect_old_ck_method_named);
675 xsh_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
676 &indirect_old_ck_entersub);
681 static void xsh_user_local_setup(pTHX_ xsh_user_cxt_t *cxt) {
684 stash = gv_stashpvn(XSH_PACKAGE, XSH_PACKAGE_LEN, 1);
685 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(XSH_THREADSAFE));
686 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(XSH_FORKSAFE));
688 cxt->map = ptable_new(32);
689 cxt->global_code = NULL;
694 static void xsh_user_local_teardown(pTHX_ xsh_user_cxt_t *cxt) {
695 SvREFCNT_dec(cxt->global_code);
696 cxt->global_code = NULL;
698 ptable_free(cxt->map);
704 static void xsh_user_global_teardown(pTHX) {
705 xsh_ck_restore(OP_CONST, &indirect_old_ck_const);
706 xsh_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
707 xsh_ck_restore(OP_PADANY, &indirect_old_ck_padany);
708 xsh_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
709 xsh_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
711 xsh_ck_restore(OP_METHOD, &indirect_old_ck_method);
712 xsh_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
713 xsh_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
718 /* --- XS ------------------------------------------------------------------ */
720 MODULE = indirect PACKAGE = indirect
738 #endif /* XSH_THREADSAFE */
746 else if (SvROK(code))
748 RETVAL = xsh_hints_tag(code);
758 else if (SvROK(code))
762 SvREFCNT_dec(XSH_CXT.global_code);
763 XSH_CXT.global_code = SvREFCNT_inc(code);