]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
Don't vivify when aliasing
[perl/modules/autovivification.git] / autovivification.xs
1 /* This file is part of the autovivification Perl module.
2  * See http://search.cpan.org/dist/autovivification/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "autovivification"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
17 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
18 #endif
19
20 /* --- Helpers ------------------------------------------------------------- */
21
22 #if A_WORKAROUND_REQUIRE_PROPAGATION
23
24 typedef struct {
25  UV  bits;
26  I32 requires;
27 } a_hint_t;
28
29 STATIC SV *a_tag(pTHX_ UV bits) {
30 #define a_tag(B) a_tag(aTHX_ (B))
31  SV *tag;
32  a_hint_t h;
33
34  h.bits = bits;
35
36  {
37   const PERL_SI *si;
38   I32            requires = 0;
39
40   for (si = PL_curstackinfo; si; si = si->si_prev) {
41    I32 cxix;
42
43    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
44     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
45
46     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
47      ++requires;
48    }
49   }
50
51   h.requires = requires;
52  }
53
54  return newSVpvn((const char *) &h, sizeof h);
55 }
56
57 STATIC UV a_detag(pTHX_ const SV *hint) {
58 #define a_detag(H) a_detag(aTHX_ (H))
59  const a_hint_t *h;
60
61  if (!(hint && SvOK(hint)))
62   return 0;
63
64  h = (const a_hint_t *) SvPVX(hint);
65
66  {
67   const PERL_SI *si;
68   I32            requires = 0;
69
70   for (si = PL_curstackinfo; si; si = si->si_prev) {
71    I32 cxix;
72
73    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
74     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
75
76     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
77                                && ++requires > h->requires)
78      return 0;
79    }
80   }
81  }
82
83  return h->bits;
84 }
85
86 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
87
88 #define a_tag(B)   newSVuv(B)
89 #define a_detag(H) (((H) && SvOK(H)) ? SvUVX(H) : 0)
90
91 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
92
93 /* Used both for hints and op flags */
94 #define A_HINT_STRICT 1
95 #define A_HINT_WARN   2
96 #define A_HINT_FETCH  4
97 #define A_HINT_STORE  8
98 #define A_HINT_EXISTS 16
99 #define A_HINT_DELETE 32
100 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
101 #define A_HINT_DO     (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
102 #define A_HINT_MASK   (A_HINT_NOTIFY|A_HINT_DO)
103
104 /* Only used in op flags */
105 #define A_HINT_DEREF  64
106
107 STATIC U32 a_hash = 0;
108
109 STATIC UV a_hint(pTHX) {
110 #define a_hint() a_hint(aTHX)
111  const SV *hint;
112 #if A_HAS_PERL(5, 9, 5)
113  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
114                                        NULL,
115                                        __PACKAGE__, __PACKAGE_LEN__,
116                                        0,
117                                        a_hash);
118 #else
119  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
120  if (!val)
121   return 0;
122  hint = *val;
123 #endif
124  return a_detag(hint);
125 }
126
127 /* ... op => info map ...................................................... */
128
129 typedef struct {
130  OP *(*old_pp)(pTHX);
131  const OP *root;
132  UV flags;
133 } a_op_info;
134
135 #define PTABLE_NAME        ptable_map
136 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
137
138 #include "ptable.h"
139
140 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
141 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
142
143 STATIC ptable *a_op_map = NULL;
144
145 #ifdef USE_ITHREADS
146 STATIC perl_mutex a_op_map_mutex;
147 #endif
148
149 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), UV flags) {
150 #define a_map_store(O, PP, F) a_map_store(aPTBLMS_ (O), (PP), (F))
151  a_op_info *oi;
152
153 #ifdef USE_ITHREADS
154  MUTEX_LOCK(&a_op_map_mutex);
155 #endif
156
157  if (!(oi = ptable_fetch(a_op_map, o))) {
158   oi = PerlMemShared_malloc(sizeof *oi);
159   ptable_map_store(a_op_map, o, oi);
160  }
161
162  oi->old_pp = old_pp;
163  oi->root   = NULL;
164  oi->flags  = flags;
165
166 #ifdef USE_ITHREADS
167  MUTEX_UNLOCK(&a_op_map_mutex);
168 #endif
169 }
170
171 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
172  const a_op_info *val;
173
174 #ifdef USE_ITHREADS
175  MUTEX_LOCK(&a_op_map_mutex);
176 #endif
177
178  val = ptable_fetch(a_op_map, o);
179  if (val) {
180   *oi = *val;
181   val = oi;
182  } else
183   oi->old_pp = 0;
184
185 #ifdef USE_ITHREADS
186  MUTEX_UNLOCK(&a_op_map_mutex);
187 #endif
188
189  return val;
190 }
191
192 STATIC void a_map_delete(pTHX_ const OP *o) {
193 #define a_map_delete(O) a_map_delete(aTHX_ (O))
194 #ifdef USE_ITHREADS
195  MUTEX_LOCK(&a_op_map_mutex);
196 #endif
197
198  ptable_map_store(a_op_map, o, NULL);
199
200 #ifdef USE_ITHREADS
201  MUTEX_UNLOCK(&a_op_map_mutex);
202 #endif
203 }
204
205 STATIC void a_map_set_root(const OP *root, UV flags) {
206  a_op_info *oi;
207  const OP *o = root;
208
209 #ifdef USE_ITHREADS
210  MUTEX_LOCK(&a_op_map_mutex);
211 #endif
212
213  while (o) {
214   if (oi = ptable_fetch(a_op_map, o)) {
215    oi->root  = root;
216    oi->flags = flags;
217   }
218   if (!(o->op_flags & OPf_KIDS))
219    break;
220   o = cUNOPo->op_first;
221  }
222
223 #ifdef USE_ITHREADS
224  MUTEX_UNLOCK(&a_op_map_mutex);
225 #endif
226 }
227
228 /* ... Lightweight pp_defined() ............................................ */
229
230 STATIC bool a_defined(pTHX_ SV *sv) {
231 #define a_defined(S) a_defined(aTHX_ (S))
232  bool defined = FALSE;
233
234  switch (SvTYPE(sv)) {
235   case SVt_PVAV:
236    if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
237                       || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
238     defined = TRUE;
239    break;
240   case SVt_PVHV:
241    if (HvARRAY(sv) || SvGMAGICAL(sv)
242                    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
243     defined = TRUE;
244    break;
245   default:
246    defined = SvOK(sv);
247  }
248
249  return defined;
250 }
251
252 /* --- PP functions -------------------------------------------------------- */
253
254 /* ... pp_rv2av ............................................................ */
255
256 STATIC OP *a_pp_rv2av(pTHX) {
257  a_op_info oi;
258  UV hint;
259  dSP;
260
261  if (!SvOK(TOPs)) {
262   /* We always need to push an empty array to fool the pp_aelem() that comes
263    * later. */
264   SV *av;
265   POPs;
266   av = sv_2mortal((SV *) newAV());
267   PUSHs(av);
268   RETURN;
269  }
270
271  a_map_fetch(PL_op, &oi);
272
273  return CALL_FPTR(oi.old_pp)(aTHX);
274 }
275
276 /* ... pp_rv2hv ............................................................ */
277
278 STATIC OP *a_pp_rv2hv(pTHX) {
279  a_op_info oi;
280  UV hint;
281  dSP;
282
283  a_map_fetch(PL_op, &oi);
284
285  if (!SvOK(TOPs)) {
286   if (oi.root->op_flags & OPf_MOD) {
287    SV *hv;
288    POPs;
289    hv = sv_2mortal((SV *) newHV());
290    PUSHs(hv);
291   }
292   RETURN;
293  }
294
295  return CALL_FPTR(oi.old_pp)(aTHX);
296 }
297
298 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
299
300 STATIC const char a_msg_forbidden[]  = "Reference vivification forbidden";
301 STATIC const char a_msg_impossible[] = "Can't vivify reference";
302
303 STATIC OP *a_pp_deref(pTHX) {
304  a_op_info oi;
305  UV flags;
306  dSP;
307
308  a_map_fetch(PL_op, &oi);
309  flags = oi.flags;
310
311  if (flags & A_HINT_DEREF) {
312   OP *o;
313   U8 old_private;
314
315 deref:
316   old_private       = PL_op->op_private;
317   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
318   o = CALL_FPTR(oi.old_pp)(aTHX);
319   PL_op->op_private = old_private;
320
321   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
322    SPAGAIN;
323    if (!SvOK(TOPs)) {
324     if (flags & A_HINT_STRICT)
325      croak(a_msg_forbidden);
326     else if (flags & A_HINT_WARN)
327       warn(a_msg_forbidden);
328     else /* A_HINT_STORE */
329      croak(a_msg_impossible);
330    }
331   }
332
333   return o;
334  } else if (flags && (PL_op->op_private & OPpDEREF || PL_op == oi.root)) {
335   oi.flags = flags & A_HINT_NOTIFY;
336
337   if ((oi.root->op_flags & (OPf_MOD|OPf_REF)) != (OPf_MOD|OPf_REF)) {
338    if (flags & A_HINT_FETCH)
339     oi.flags |= (A_HINT_FETCH|A_HINT_DEREF);
340   } else if (flags & A_HINT_STORE)
341     oi.flags |= (A_HINT_STORE|A_HINT_DEREF);
342
343   if (PL_op == oi.root)
344    oi.flags &= ~A_HINT_DEREF;
345
346   /* We will need the updated flags value in the deref part */
347   flags = oi.flags;
348
349   if (flags & A_HINT_DEREF)
350    goto deref;
351
352   /* This op doesn't need to skip autovivification, so restore the original
353    * state. Be aware that another extension might have saved a_pp_deref as the
354    * ppaddr for this op, so restoring PL_op->op_ppaddr doesn't ensure that this
355    * function will never be called again. That's why we don't remove the op info
356    * from our map and we reset oi.flags to 0, so that it can still run correctly
357    * if required. */
358   oi.flags = 0;
359   PL_op->op_ppaddr = oi.old_pp;
360  }
361
362  return CALL_FPTR(oi.old_pp)(aTHX);
363 }
364
365 /* ... pp_root (exists,delete) ............................................. */
366
367 STATIC OP *a_pp_root(pTHX) {
368  a_op_info oi;
369  dSP;
370
371  if (!a_defined(TOPm1s)) {
372   POPs;
373   POPs;
374   if (PL_op->op_type == OP_EXISTS)
375    RETPUSHNO;
376   else
377    RETPUSHUNDEF;
378  }
379
380  a_map_fetch(PL_op, &oi);
381
382  return CALL_FPTR(oi.old_pp)(aTHX);
383 }
384
385 /* --- Check functions ----------------------------------------------------- */
386
387 /* ... ck_pad{any,sv} ...................................................... */
388
389 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
390  * function, but are instead manually mutated from a PADANY. This is why we set
391  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
392  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
393  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
394  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
395  * globally. */
396
397 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
398
399 STATIC void a_pp_padsv_save(void) {
400  if (a_pp_padsv_saved)
401   return;
402
403  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
404  PL_ppaddr[OP_PADSV] = a_pp_deref;
405 }
406
407 STATIC void a_pp_padsv_restore(OP *o) {
408  if (!a_pp_padsv_saved)
409   return;
410
411  if (o->op_ppaddr == a_pp_deref)
412   o->op_ppaddr = a_pp_padsv_saved;
413
414  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
415  a_pp_padsv_saved    = 0;
416 }
417
418 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
419
420 STATIC OP *a_ck_padany(pTHX_ OP *o) {
421  UV hint;
422
423  a_pp_padsv_restore(o);
424
425  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
426
427  hint = a_hint();
428  if (hint & A_HINT_DO) {
429   a_pp_padsv_save();
430   a_map_store(o, a_pp_padsv_saved, hint);
431  } else
432   a_map_delete(o);
433
434  return o;
435 }
436
437 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
438
439 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
440  UV hint;
441
442  a_pp_padsv_restore(o);
443
444  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
445
446  hint = a_hint();
447  if (hint & A_HINT_DO) {
448   a_map_store(o, o->op_ppaddr, hint);
449   o->op_ppaddr = a_pp_deref;
450  } else
451   a_map_delete(o);
452
453  return o;
454 }
455
456 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
457
458 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
459 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
460 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
461
462 STATIC OP *a_ck_deref(pTHX_ OP *o) {
463  OP * (*old_ck)(pTHX_ OP *o) = 0;
464  UV hint;
465
466  switch (o->op_type) {
467   case OP_AELEM: old_ck = a_old_ck_aelem; break;
468   case OP_HELEM: old_ck = a_old_ck_helem; break;
469   case OP_RV2SV: old_ck = a_old_ck_rv2sv; break;
470  }
471  o = CALL_FPTR(old_ck)(aTHX_ o);
472
473  hint = a_hint();
474  if (hint & A_HINT_DO) {
475   if (!(hint & A_HINT_STRICT) && o->op_flags & OPf_KIDS) {
476    OP *kid = cUNOPo->op_first;
477    switch (kid->op_type) {
478     case OP_RV2AV:
479      a_map_store(kid, kid->op_ppaddr, hint);
480      kid->op_ppaddr = a_pp_rv2av;
481      break;
482     case OP_RV2HV:
483      a_map_store(kid, kid->op_ppaddr, hint);
484      kid->op_ppaddr = a_pp_rv2hv;
485      break;
486    }
487   }
488   a_map_store(o, o->op_ppaddr, hint);
489   o->op_ppaddr = a_pp_deref;
490   a_map_set_root(o, hint);
491  } else
492   a_map_delete(o);
493
494  return o;
495 }
496
497 /* ... ck_root (exists,delete) ............................................. */
498
499 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
500 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
501
502 STATIC OP *a_ck_root(pTHX_ OP *o) {
503  OP * (*old_ck)(pTHX_ OP *o) = 0;
504  bool enabled = FALSE;
505  UV hint = a_hint();
506
507  switch (o->op_type) {
508   case OP_EXISTS:
509    old_ck  = a_old_ck_exists;
510    enabled = hint & A_HINT_EXISTS;
511    break;
512   case OP_DELETE:
513    old_ck  = a_old_ck_delete;
514    enabled = hint & A_HINT_DELETE;
515    break;
516  }
517  o = CALL_FPTR(old_ck)(aTHX_ o);
518
519  if (enabled) {
520   a_map_set_root(o, hint | A_HINT_DEREF);
521   a_map_store(o, o->op_ppaddr, hint);
522   o->op_ppaddr = a_pp_root;
523  } else {
524   a_map_set_root(o, 0);
525  }
526
527  return o;
528 }
529
530 STATIC U32 a_initialized = 0;
531
532 /* --- XS ------------------------------------------------------------------ */
533
534 MODULE = autovivification      PACKAGE = autovivification
535
536 PROTOTYPES: ENABLE
537
538 BOOT: 
539 {                                    
540  if (!a_initialized++) {
541   HV *stash;
542
543   a_op_map = ptable_new();
544 #ifdef USE_ITHREADS
545   MUTEX_INIT(&a_op_map_mutex);
546 #endif
547
548   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
549
550   a_old_ck_padany     = PL_check[OP_PADANY];
551   PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
552   a_old_ck_padsv      = PL_check[OP_PADSV];
553   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
554   a_old_ck_aelem      = PL_check[OP_AELEM];
555   PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
556   a_old_ck_helem      = PL_check[OP_HELEM];
557   PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
558   a_old_ck_rv2sv      = PL_check[OP_RV2SV];
559   PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
560   a_old_ck_exists     = PL_check[OP_EXISTS];
561   PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
562   a_old_ck_delete     = PL_check[OP_DELETE];
563   PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
564
565   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
566   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
567   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
568   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
569   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
570   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
571   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
572   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
573  }
574 }
575
576 SV *
577 _tag(SV *hint)
578 PROTOTYPE: $
579 CODE:
580  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
581 OUTPUT:
582  RETVAL
583
584 SV *
585 _detag(SV *tag)
586 PROTOTYPE: $
587 CODE:
588  if (!SvOK(tag))
589   XSRETURN_UNDEF;
590  RETVAL = newSVuv(a_detag(tag));
591 OUTPUT:
592  RETVAL