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"
17 /* ... op => source position map ........................................... */
27 #define PTABLE_NAME ptable
28 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
29 #define PTABLE_NEED_DELETE 1
30 #define PTABLE_NEED_WALK 0
32 #include "xsh/ptable.h"
34 /* Safefree() always need aTHX */
35 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
36 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
37 #define ptable_clear(T) ptable_clear(aTHX_ (T))
38 #define ptable_free(T) ptable_free(aTHX_ (T))
40 /* ... Lexical hints ....................................................... */
42 #define XSH_HINTS_TYPE_SV 1
44 #include "xsh/hints.h"
46 /* ... Thread-local storage ................................................ */
53 #define XSH_THREADS_USER_CONTEXT 1
54 #define XSH_THREADS_USER_CLONE_NEEDS_DUP 1
55 #define XSH_THREADS_COMPILE_TIME_PROTECTION 1
59 static void xsh_user_clone(pTHX_ const xsh_user_cxt_t *old_cxt, xsh_user_cxt_t *new_cxt, CLONE_PARAMS *params) {
60 new_cxt->map = ptable_new(32);
61 new_cxt->global_code = xsh_dup_inc(old_cxt->global_code, params);
66 #endif /* XSH_THREADSAFE */
68 #include "xsh/threads.h"
70 /* ... Lexical hints, continued ............................................ */
72 static SV *indirect_hint(pTHX) {
73 #define indirect_hint() indirect_hint(aTHX)
76 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
81 hint = xsh_hints_fetch();
82 if (hint && SvOK(hint)) {
83 return xsh_hints_detag(hint);
86 if (xsh_is_loaded(&XSH_CXT))
87 return XSH_CXT.global_code;
93 /* --- Compatibility wrappers ---------------------------------------------- */
96 # define Newx(v, n, c) New(0, v, n, c)
100 # define SvPV_const SvPV
103 #ifndef SvPV_nolen_const
104 # define SvPV_nolen_const SvPV_nolen
108 # define SvPVX_const SvPVX
111 #ifndef SvREFCNT_inc_simple_void_NN
112 # ifdef SvREFCNT_inc_simple_NN
113 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
115 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
119 #ifndef sv_catpvn_nomg
120 # define sv_catpvn_nomg sv_catpvn
124 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
128 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
132 # define HvNAME_get(H) HvNAME(H)
135 #ifndef HvNAMELEN_get
136 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
139 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
141 # define PL_linestr PL_parser->linestr
144 # define PL_bufptr PL_parser->bufptr
146 # ifndef PL_oldbufptr
147 # define PL_oldbufptr PL_parser->oldbufptr
149 # ifndef PL_lex_inwhat
150 # define PL_lex_inwhat PL_parser->lex_inwhat
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
167 /* ... Safe version of call_sv() ........................................... */
169 static I32 indirect_call_sv(pTHX_ SV *sv, I32 flags) {
170 #define indirect_call_sv(S, F) indirect_call_sv(aTHX_ (S), (F))
172 PERL_CONTEXT saved_cx;
173 SV *saved_errsv = NULL;
176 if (IN_PERL_COMPILETIME && PL_errors)
177 sv_catsv(PL_errors, ERRSV);
179 saved_errsv = newSVsv(ERRSV);
183 cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
184 /* The last popped context will be reused by call_sv(), but our callers may
185 * still need its previous value. Back it up so that it isn't clobbered. */
186 saved_cx = cxstack[cxix];
188 ret = call_sv(sv, flags | G_EVAL);
190 cxstack[cxix] = saved_cx;
193 /* Discard the old ERRSV, and reuse the variable to temporarily store the
196 sv_setsv(saved_errsv, ERRSV);
198 saved_errsv = newSVsv(ERRSV);
200 /* Immediately flush all errors. */
201 if (IN_PERL_COMPILETIME) {
202 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
204 ++PL_parser->error_count;
205 #elif defined(PL_error_count)
211 sv_setsv(ERRSV, PL_errors);
212 SvCUR_set(PL_errors, 0);
215 sv_catsv(ERRSV, saved_errsv);
216 SvREFCNT_dec(saved_errsv);
218 } else if (saved_errsv) {
219 /* If IN_PERL_COMPILETIME && PL_errors, then the old ERRSV has already been
220 * added to PL_errors. Otherwise, just restore it to ERRSV, as if no eval
221 * block has ever been executed. */
222 sv_setsv(ERRSV, saved_errsv);
223 SvREFCNT_dec(saved_errsv);
229 /* --- Check functions ----------------------------------------------------- */
231 /* ... op => source position map, continued ................................ */
233 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
234 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
235 indirect_op_info_t *oi;
240 /* No need to check for XSH_CXT.map != NULL because this code path is always
241 * guarded by indirect_hint(). */
243 if (!(oi = ptable_fetch(XSH_CXT.map, o))) {
244 Newx(oi, 1, indirect_op_info_t);
245 ptable_store(XSH_CXT.map, o, oi);
251 s = SvPV_const(sv, len);
257 if (len > oi->size) {
259 Newx(oi->buf, len, char);
262 Copy(s, oi->buf, len, char);
269 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
270 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
273 /* No need to check for XSH_CXT.map != NULL because this code path is always
274 * guarded by indirect_hint(). */
276 return ptable_fetch(XSH_CXT.map, o);
279 static void indirect_map_delete(pTHX_ const OP *o) {
280 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
283 if (xsh_is_loaded(&XSH_CXT) && XSH_CXT.map)
284 ptable_delete(XSH_CXT.map, o);
287 /* ... Heuristics for finding a string in the source buffer ................ */
289 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
290 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
291 STRLEN name_len, line_len;
292 const char *name, *name_end;
293 const char *line, *line_end;
296 line = SvPV_const(PL_linestr, line_len);
297 line_end = line + line_len;
299 name = SvPV_const(name_sv, name_len);
300 if (name_len >= 1 && *name == '$') {
303 while (line_bufptr < line_end && *line_bufptr != '$')
305 if (line_bufptr >= line_end)
308 name_end = name + name_len;
312 p = ninstr(p, line_end, name, name_end);
315 if (!isALNUM(p[name_len]))
317 /* p points to a word that has name as prefix, skip the rest of the word */
323 *name_pos = p - line;
328 /* ... ck_const ............................................................ */
330 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
332 static OP *indirect_ck_const(pTHX_ OP *o) {
333 o = indirect_old_ck_const(aTHX_ o);
335 if (indirect_hint()) {
338 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
341 if (indirect_find(sv, PL_oldbufptr, &pos)) {
344 /* If the constant is equal to the current package name, try to look for
345 * a "__PACKAGE__" coming before what we got. We only need to check this
346 * when we already had a match because __PACKAGE__ can only appear in
347 * direct method calls ("new __PACKAGE__" is a syntax error). */
350 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
351 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
353 SV *pkg = sv_newmortal();
354 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
356 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
362 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
368 indirect_map_delete(o);
372 /* ... ck_rv2sv ............................................................ */
374 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
376 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
377 if (indirect_hint()) {
378 OP *op = cUNOPo->op_first;
380 const char *name = NULL;
382 OPCODE type = (OPCODE) op->op_type;
387 GV *gv = cGVOPx_gv(op);
393 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
394 SV *nsv = cSVOPx_sv(op);
395 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
396 name = SvPV_const(nsv, len);
402 sv = sv_2mortal(newSVpvn("$", 1));
403 sv_catpvn_nomg(sv, name, len);
404 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
405 /* If it failed, retry without the current stash */
406 const char *stash = HvNAME_get(PL_curstash);
407 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
409 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
410 || name[stashlen] != ':' || name[stashlen+1] != ':') {
411 /* Failed again ? Try to remove main */
414 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
415 || name[stashlen] != ':' || name[stashlen+1] != ':')
419 sv_setpvn(sv, "$", 1);
421 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
422 if (!indirect_find(sv, PL_oldbufptr, &pos))
426 o = indirect_old_ck_rv2sv(aTHX_ o);
428 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
433 o = indirect_old_ck_rv2sv(aTHX_ o);
435 indirect_map_delete(o);
439 /* ... ck_padany ........................................................... */
441 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
443 static OP *indirect_ck_padany(pTHX_ OP *o) {
444 o = indirect_old_ck_padany(aTHX_ o);
446 if (indirect_hint()) {
448 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
450 while (s < t && isSPACE(*s)) ++s;
451 if (*s == '$' && ++s <= t) {
452 while (s < t && isSPACE(*s)) ++s;
453 while (s < t && isSPACE(*t)) --t;
454 sv = sv_2mortal(newSVpvn("$", 1));
455 sv_catpvn_nomg(sv, s, t - s + 1);
456 indirect_map_store(o, s - SvPVX_const(PL_linestr),
457 sv, CopLINE(&PL_compiling));
462 indirect_map_delete(o);
466 /* ... ck_scope ............................................................ */
468 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
469 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
471 static OP *indirect_ck_scope(pTHX_ OP *o) {
472 OP *(*old_ck)(pTHX_ OP *) = 0;
474 switch (o->op_type) {
475 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
476 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
480 if (indirect_hint()) {
481 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
482 NULL, CopLINE(&PL_compiling));
486 indirect_map_delete(o);
490 /* We don't need to clean the map entries for leave ops because they can only
491 * be created by mutating from a lineseq. */
493 /* ... ck_method ........................................................... */
495 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
497 static OP *indirect_ck_method(pTHX_ OP *o) {
498 if (indirect_hint()) {
499 OP *op = cUNOPo->op_first;
501 /* Indirect method call is only possible when the method is a bareword, so
502 * don't trip up on $obj->$meth. */
503 if (op && op->op_type == OP_CONST) {
504 const indirect_op_info_t *oi = indirect_map_fetch(op);
512 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
514 /* Keep the old line so that we really point to the first line of the
518 o = indirect_old_ck_method(aTHX_ o);
519 /* o may now be a method_named */
521 indirect_map_store(o, pos, sv, line);
527 o = indirect_old_ck_method(aTHX_ o);
529 indirect_map_delete(o);
533 /* ... ck_method_named ..................................................... */
535 /* "use foo/no foo" compiles its call to import/unimport directly to a
536 * method_named op. */
538 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
540 static OP *indirect_ck_method_named(pTHX_ OP *o) {
541 if (indirect_hint()) {
547 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
549 sv = sv_mortalcopy(sv);
551 if (!indirect_find(sv, PL_oldbufptr, &pos))
553 line = CopLINE(&PL_compiling);
555 o = indirect_old_ck_method_named(aTHX_ o);
557 indirect_map_store(o, pos, sv, line);
562 o = indirect_old_ck_method_named(aTHX_ o);
564 indirect_map_delete(o);
568 /* ... ck_entersub ......................................................... */
570 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
572 static OP *indirect_ck_entersub(pTHX_ OP *o) {
573 SV *code = indirect_hint();
575 o = indirect_old_ck_entersub(aTHX_ o);
578 const indirect_op_info_t *moi, *ooi;
584 lop = (LISTOP *) oop;
585 if (!(lop->op_flags & OPf_KIDS))
588 } while (oop->op_type != OP_PUSHMARK);
589 oop = OpSIBLING(oop);
595 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 moi = indirect_map_fetch(mop);
615 ooi = indirect_map_fetch(oop);
619 /* When positions are identical, the method and the object must have the
620 * same name. But it also means that it is an indirect call, as "foo->foo"
621 * results in different positions. */
622 if ( moi->line < ooi->line
623 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
631 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
633 file = sv_mortalcopy(CopFILESV(&PL_compiling));
638 mPUSHp(ooi->buf, ooi->len);
639 mPUSHp(moi->buf, moi->len);
644 indirect_call_sv(code, G_VOID);
657 /* --- Module setup/teardown ----------------------------------------------- */
659 static void xsh_user_global_setup(pTHX) {
660 xsh_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
661 xsh_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
662 xsh_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
663 xsh_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
664 xsh_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
666 xsh_ck_replace(OP_METHOD, indirect_ck_method,
667 &indirect_old_ck_method);
668 xsh_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
669 &indirect_old_ck_method_named);
670 xsh_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
671 &indirect_old_ck_entersub);
676 static void xsh_user_local_setup(pTHX_ xsh_user_cxt_t *cxt) {
679 stash = gv_stashpvn(XSH_PACKAGE, XSH_PACKAGE_LEN, 1);
680 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(XSH_THREADSAFE));
681 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(XSH_FORKSAFE));
683 cxt->map = ptable_new(32);
684 cxt->global_code = NULL;
689 static void xsh_user_local_teardown(pTHX_ xsh_user_cxt_t *cxt) {
690 SvREFCNT_dec(cxt->global_code);
691 cxt->global_code = NULL;
693 ptable_free(cxt->map);
699 static void xsh_user_global_teardown(pTHX) {
700 xsh_ck_restore(OP_CONST, &indirect_old_ck_const);
701 xsh_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
702 xsh_ck_restore(OP_PADANY, &indirect_old_ck_padany);
703 xsh_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
704 xsh_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
706 xsh_ck_restore(OP_METHOD, &indirect_old_ck_method);
707 xsh_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
708 xsh_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
713 /* --- XS ------------------------------------------------------------------ */
715 MODULE = indirect PACKAGE = indirect
733 #endif /* XSH_THREADSAFE */
741 else if (SvROK(code))
743 RETVAL = xsh_hints_tag(code);
753 else if (SvROK(code))
757 SvREFCNT_dec(XSH_CXT.global_code);
758 XSH_CXT.global_code = SvREFCNT_inc(code);