]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Make names stored in the op map readonly
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #ifndef NOOP
15 # define NOOP
16 #endif
17
18 #ifndef dNOOP
19 # define dNOOP
20 #endif
21
22 #ifndef SvPV_const
23 # define SvPV_const SvPV
24 #endif
25
26 #ifndef SvPV_nolen_const
27 # define SvPV_nolen_const SvPV_nolen
28 #endif
29
30 #ifndef SvPVX_const
31 # define SvPVX_const SvPVX
32 #endif
33
34 #ifndef sv_catpvn_nomg
35 # define sv_catpvn_nomg sv_catpvn
36 #endif
37
38 #ifndef HvNAME_get
39 # define HvNAME_get(H) HvNAME(H)
40 #endif
41
42 #ifndef HvNAMELEN_get
43 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
44 #endif
45
46 #ifndef SvIS_FREED
47 # define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
48 #endif
49
50 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
51
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
55 # endif
56 # ifndef PL_linestr
57 #  define PL_linestr PL_parser->linestr
58 # endif
59 # ifndef PL_bufptr
60 #  define PL_bufptr PL_parser->bufptr
61 # endif
62 # ifndef PL_oldbufptr
63 #  define PL_oldbufptr PL_parser->oldbufptr
64 # endif
65 #else
66 # ifndef PL_lex_inwhat
67 #  define PL_lex_inwhat PL_Ilex_inwhat
68 # endif
69 # ifndef PL_linestr
70 #  define PL_linestr PL_Ilinestr
71 # endif
72 # ifndef PL_bufptr
73 #  define PL_bufptr PL_Ibufptr
74 # endif
75 # ifndef PL_oldbufptr
76 #  define PL_oldbufptr PL_Ioldbufptr
77 # endif
78 #endif
79
80 /* ... Thread safety and multiplicity ...................................... */
81
82 #ifndef I_MULTIPLICITY
83 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
84 #  define I_MULTIPLICITY 1
85 # else
86 #  define I_MULTIPLICITY 0
87 # endif
88 #endif
89 #if I_MULTIPLICITY && !defined(tTHX)
90 # define tTHX PerlInterpreter*
91 #endif
92
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
95 # ifndef MY_CXT_CLONE
96 #  define MY_CXT_CLONE \
97     dMY_CXT_SV;                                                      \
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))
101 # endif
102 #else
103 # define I_THREADSAFE 0
104 # undef  dMY_CXT
105 # define dMY_CXT      dNOOP
106 # undef  MY_CXT
107 # define MY_CXT       indirect_globaldata
108 # undef  START_MY_CXT
109 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
110 # undef  MY_CXT_INIT
111 # define MY_CXT_INIT  NOOP
112 # undef  MY_CXT_CLONE
113 # define MY_CXT_CLONE NOOP
114 #endif
115
116 /* --- Helpers ------------------------------------------------------------- */
117
118 /* ... Pointer table ....................................................... */
119
120 #define PTABLE_NAME        ptable
121 #define PTABLE_VAL_FREE(V) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V)
122
123 #define pPTBL  pTHX
124 #define pPTBL_ pTHX_
125 #define aPTBL  aTHX
126 #define aPTBL_ aTHX_
127
128 #include "ptable.h"
129
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))
133
134 /* ... Thread-safe hints ................................................... */
135
136 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
137
138 #if I_THREADSAFE
139
140 typedef struct {
141  ptable     *tbl;
142  ptable     *map;
143  tTHX        owner;
144  const char *linestr;
145 } my_cxt_t;
146
147 #else
148
149 typedef struct {
150  ptable     *map;
151  const char *linestr;
152 } my_cxt_t;
153
154 #endif /* I_THREADSAFE */
155
156 START_MY_CXT
157
158 #if I_THREADSAFE
159
160 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
161  my_cxt_t *ud  = ud_;
162  SV       *val = ent->val;
163
164  if (ud->owner != aTHX) {
165   CLONE_PARAMS param;
166   AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
167   param.stashes    = stashes;
168   param.flags      = 0;
169   param.proto_perl = ud->owner;
170   val = sv_dup(val, &param);
171   if (stashes) {
172    av_undef(stashes);
173    SvREFCNT_dec(stashes);
174   }
175  }
176
177  ptable_store(ud->tbl, ent->key, val);
178  SvREFCNT_inc(val);
179 }
180
181 STATIC void indirect_thread_cleanup(pTHX_ void *);
182
183 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
184  int *level = ud;
185  SV  *id;
186
187  if (*level) {
188   *level = 0;
189   LEAVE;
190   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
191   ENTER;
192  } else {
193   dMY_CXT;
194   PerlMemShared_free(level);
195   ptable_free(MY_CXT.map);
196   ptable_free(MY_CXT.tbl);
197  }
198 }
199
200 STATIC SV *indirect_tag(pTHX_ SV *value) {
201 #define indirect_tag(V) indirect_tag(aTHX_ (V))
202  dMY_CXT;
203
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);
209  SvREFCNT_inc(value);
210
211  return newSVuv(PTR2UV(value));
212 }
213
214 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
215 #define indirect_detag(H) indirect_detag(aTHX_ (H))
216  void *tag;
217  SV   *value;
218
219  if (!hint || !SvOK(hint) || !SvIOK(hint))
220   croak("Wrong hint");
221
222  tag = INT2PTR(void *, SvIVX(hint));
223  {
224   dMY_CXT;
225   value = ptable_fetch(MY_CXT.tbl, tag);
226  }
227
228  return value;
229 }
230
231 #else
232
233 STATIC SV *indirect_tag(pTHX_ SV *value) {
234 #define indirect_tag(V) indirect_tag(aTHX_ (V))
235  UV tag = 0;
236
237  if (SvOK(value) && SvROK(value)) {
238   value = SvRV(value);
239   SvREFCNT_inc(value);
240   tag = PTR2UV(value);
241  }
242
243  return newSVuv(tag);
244 }
245
246 #define indirect_detag(H) INT2PTR(SV *, SvUVX(H))
247
248 #endif /* I_THREADSAFE */
249
250 STATIC U32 indirect_hash = 0;
251
252 STATIC SV *indirect_hint(pTHX) {
253 #define indirect_hint() indirect_hint(aTHX)
254  SV *id;
255 #if I_HAS_PERL(5, 10, 0)
256  id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
257                                      NULL,
258                                      __PACKAGE__, __PACKAGE_LEN__,
259                                      0,
260                                      indirect_hash);
261 #else
262  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
263                                                                  indirect_hash);
264  if (!val)
265   return 0;
266  id = *val;
267 #endif
268  return (id && SvOK(id)) ? id : NULL;
269 }
270
271 /* ... op -> source position ............................................... */
272
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))
275  dMY_CXT;
276  SV *val;
277
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. */
281
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;
287   }
288  }
289
290  val = newSVsv(sv);
291  SvUPGRADE(val, SVt_PVIV);
292  SvUVX(val) = PTR2UV(src);
293  SvIOK_on(val);
294  SvIsUV_on(val);
295  SvREADONLY_on(val);
296
297  ptable_store(MY_CXT.map, o, val);
298 }
299
300 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
301 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
302  dMY_CXT;
303  SV *val;
304
305  if (MY_CXT.linestr != SvPVX_const(PL_linestr))
306   return NULL;
307
308  val = ptable_fetch(MY_CXT.map, o);
309  if (!val) {
310   *name = NULL;
311   return NULL;
312  }
313
314  *name = val;
315  return INT2PTR(const char *, SvUVX(val));
316 }
317
318 /* --- Check functions ----------------------------------------------------- */
319
320 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
321 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
322  STRLEN len;
323  const char *p = NULL, *r = SvPV_const(sv, len);
324
325  if (len >= 1 && *r == '$') {
326   ++r;
327   --len;
328   s = strchr(s, '$');
329   if (!s)
330    return NULL;
331  }
332
333  p = strstr(s, r);
334  while (p) {
335   p += len;
336   if (!isALNUM(*p))
337    break;
338   p = strstr(p + 1, r);
339  }
340
341  return p;
342 }
343
344 /* ... ck_const ............................................................ */
345
346 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
347
348 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
349  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
350
351  if (indirect_hint()) {
352   SV *sv = cSVOPo_sv;
353   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
354    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
355  }
356
357  return o;
358 }
359
360 /* ... ck_rv2sv ............................................................ */
361
362 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
363
364 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
365  if (indirect_hint()) {
366   OP *op = cUNOPo->op_first;
367   SV *sv;
368   const char *name = NULL, *s;
369   STRLEN len;
370   OPCODE type = (OPCODE) op->op_type;
371
372   switch (type) {
373    case OP_GV:
374    case OP_GVSV: {
375     GV *gv = cGVOPx_gv(op);
376     name = GvNAME(gv);
377     len  = GvNAMELEN(gv);
378     break;
379    }
380    default:
381     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
382      SV *nsv = cSVOPx_sv(op);
383      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
384       name = SvPV_const(nsv, len);
385     }
386   }
387   if (!name)
388    goto done;
389
390   sv = sv_2mortal(newSVpvn("$", 1));
391   sv_catpvn_nomg(sv, name, len);
392   s = indirect_find(sv, PL_oldbufptr);
393   if (!s) { /* If it failed, retry without the current stash */
394    const char *stash = HvNAME_get(PL_curstash);
395    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
396
397    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
398        || name[stashlen] != ':' || name[stashlen+1] != ':') {
399     /* Failed again ? Try to remove main */
400     stash = "main";
401     stashlen = 4;
402     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
403         || name[stashlen] != ':' || name[stashlen+1] != ':')
404      goto done;
405    }
406
407    sv_setpvn(sv, "$", 1);
408    stashlen += 2;
409    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
410    s = indirect_find(sv, PL_oldbufptr);
411    if (!s)
412     goto done;
413   }
414
415   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
416   indirect_map_store(o, s, sv);
417   return o;
418  }
419
420 done:
421  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
422 }
423
424 /* ... ck_padany ........................................................... */
425
426 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
427
428 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
429  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
430
431  if (indirect_hint()) {
432   SV *sv;
433   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
434
435   while (s < t && isSPACE(*s)) ++s;
436   if (*s == '$' && ++s <= t) {
437    while (s < t && isSPACE(*s)) ++s;
438    while (s < t && isSPACE(*t)) --t;
439    sv = sv_2mortal(newSVpvn("$", 1));
440    sv_catpvn_nomg(sv, s, t - s + 1);
441    indirect_map_store(o, s, sv);
442   }
443  }
444
445  return o;
446 }
447
448 /* ... ck_method ........................................................... */
449
450 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
451
452 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
453  if (indirect_hint()) {
454   OP *op = cUNOPo->op_first;
455   SV *sv;
456   const char *s = indirect_map_fetch(op, &sv);
457   if (!s) {
458    sv = cSVOPx_sv(op);
459    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
460     goto done;
461    sv = sv_mortalcopy(sv);
462    s  = indirect_find(sv, PL_oldbufptr);
463   }
464   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
465   /* o may now be a method_named */
466   indirect_map_store(o, s, sv);
467   return o;
468  }
469
470 done:
471  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
472 }
473
474 /* ... ck_entersub ......................................................... */
475
476 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
477
478 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
479  SV *hint = indirect_hint();
480
481  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
482
483  if (hint) {
484   const char *mpos, *opos;
485   SV *mnamesv, *onamesv;
486   OP *mop, *oop;
487   LISTOP *lop;
488
489   oop = o;
490   do {
491    lop = (LISTOP *) oop;
492    if (!(lop->op_flags & OPf_KIDS))
493     goto done;
494    oop = lop->op_first;
495   } while (oop->op_type != OP_PUSHMARK);
496   oop = oop->op_sibling;
497   mop = lop->op_last;
498
499   if (mop->op_type == OP_METHOD)
500    mop = cUNOPx(mop)->op_first;
501   else if (mop->op_type != OP_METHOD_NAMED)
502    goto done;
503
504   mpos = indirect_map_fetch(mop, &mnamesv);
505   if (!mpos)
506    goto done;
507
508   opos = indirect_map_fetch(oop, &onamesv);
509   if (!opos)
510    goto done;
511
512   if (mpos < opos) {
513    SV *code = indirect_detag(hint);
514
515    if (hint) {
516     dSP;
517
518     ENTER;
519     SAVETMPS;
520
521     PUSHMARK(SP);
522     EXTEND(SP, 2);
523     PUSHs(onamesv);
524     PUSHs(mnamesv);
525     PUTBACK;
526
527     call_sv(code, G_VOID);
528
529     PUTBACK;
530
531     FREETMPS;
532     LEAVE;
533    }
534   }
535  }
536
537 done:
538  return o;
539 }
540
541 STATIC U32 indirect_initialized = 0;
542
543 /* --- XS ------------------------------------------------------------------ */
544
545 MODULE = indirect      PACKAGE = indirect
546
547 PROTOTYPES: ENABLE
548
549 BOOT:
550 {
551  if (!indirect_initialized++) {
552   HV *stash;
553
554   MY_CXT_INIT;
555   MY_CXT.map     = ptable_new();
556   MY_CXT.linestr = NULL;
557 #if I_THREADSAFE
558   MY_CXT.tbl     = ptable_new();
559   MY_CXT.owner   = aTHX;
560 #endif
561
562   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
563
564   indirect_old_ck_const    = PL_check[OP_CONST];
565   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
566   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
567   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
568   indirect_old_ck_padany   = PL_check[OP_PADANY];
569   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
570   indirect_old_ck_method   = PL_check[OP_METHOD];
571   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
572   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
573   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
574
575   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
576   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
577  }
578 }
579
580 #if I_THREADSAFE
581
582 void
583 CLONE(...)
584 PROTOTYPE: DISABLE
585 PREINIT:
586  ptable *t;
587  int    *level;
588 CODE:
589  {
590   my_cxt_t ud;
591   dMY_CXT;
592   ud.tbl   = t = ptable_new();
593   ud.owner = MY_CXT.owner;
594   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
595  }
596  {
597   MY_CXT_CLONE;
598   MY_CXT.map     = ptable_new();
599   MY_CXT.linestr = NULL;
600   MY_CXT.tbl     = t;
601   MY_CXT.owner   = aTHX;
602  }
603  {
604   level = PerlMemShared_malloc(sizeof *level);
605   *level = 1;
606   LEAVE;
607   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
608   ENTER;
609  }
610
611 #endif
612
613 SV *
614 _tag(SV *value)
615 PROTOTYPE: $
616 CODE:
617  RETVAL = indirect_tag(value);
618 OUTPUT:
619  RETVAL