]> git.vpit.fr Git - perl/modules/autovivification.git/blob - autovivification.xs
A crude warning test
[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  *oi = *val;
180
181 #ifdef USE_ITHREADS
182  MUTEX_UNLOCK(&a_op_map_mutex);
183 #endif
184
185  return val;
186 }
187
188 STATIC void a_map_delete(pTHX_ const OP *o) {
189 #define a_map_delete(O) a_map_delete(aTHX_ (O))
190 #ifdef USE_ITHREADS
191  MUTEX_LOCK(&a_op_map_mutex);
192 #endif
193
194  ptable_map_store(a_op_map, o, NULL);
195
196 #ifdef USE_ITHREADS
197  MUTEX_UNLOCK(&a_op_map_mutex);
198 #endif
199 }
200
201 STATIC void a_map_set_root(const OP *root, UV flags) {
202  a_op_info *oi;
203  const OP *o = root;
204
205 #ifdef USE_ITHREADS
206  MUTEX_LOCK(&a_op_map_mutex);
207 #endif
208
209  while (o) {
210   if (oi = ptable_fetch(a_op_map, o)) {
211    oi->root  = root;
212    oi->flags = flags;
213   }
214   if (!(o->op_flags & OPf_KIDS))
215    break;
216   switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
217    case OA_BASEOP:
218    case OA_UNOP:
219    case OA_BINOP:
220    case OA_BASEOP_OR_UNOP:
221     o = cUNOPo->op_first;
222     break;
223    case OA_LIST:
224    case OA_LISTOP:
225     o = cLISTOPo->op_last;
226     break;
227    default:
228     goto done;
229   }
230  }
231
232 done:
233 #ifdef USE_ITHREADS
234  MUTEX_UNLOCK(&a_op_map_mutex);
235 #endif
236
237  return;
238 }
239
240 /* ... Lightweight pp_defined() ............................................ */
241
242 STATIC bool a_defined(pTHX_ SV *sv) {
243 #define a_defined(S) a_defined(aTHX_ (S))
244  bool defined = FALSE;
245
246  switch (SvTYPE(sv)) {
247   case SVt_PVAV:
248    if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
249                       || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
250     defined = TRUE;
251    break;
252   case SVt_PVHV:
253    if (HvARRAY(sv) || SvGMAGICAL(sv)
254                    || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
255     defined = TRUE;
256    break;
257   default:
258    defined = SvOK(sv);
259  }
260
261  return defined;
262 }
263
264 /* --- PP functions -------------------------------------------------------- */
265
266 /* ... pp_rv2av ............................................................ */
267
268 STATIC OP *a_pp_rv2av(pTHX) {
269  a_op_info oi;
270  UV hint;
271  dSP;
272
273  a_map_fetch(PL_op, &oi);
274
275  if (PL_op != oi.root && !SvOK(TOPs)) {
276   /* We always need to push an empty array to fool the pp_aelem() that comes
277    * later. */
278   SV *av;
279   POPs;
280   av = sv_2mortal((SV *) newAV());
281   PUSHs(av);
282   RETURN;
283  }
284
285  return CALL_FPTR(oi.old_pp)(aTHX);
286 }
287
288 /* ... pp_rv2hv ............................................................ */
289
290 STATIC OP *a_pp_rv2hv(pTHX) {
291  a_op_info oi;
292  UV hint;
293  dSP;
294
295  a_map_fetch(PL_op, &oi);
296
297  if (PL_op != oi.root && !SvOK(TOPs)) {
298   if (oi.root->op_flags & OPf_MOD) {
299    SV *hv;
300    POPs;
301    hv = sv_2mortal((SV *) newHV());
302    PUSHs(hv);
303   }
304   RETURN;
305  }
306
307  return CALL_FPTR(oi.old_pp)(aTHX);
308 }
309
310 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
311
312 STATIC OP *a_pp_deref(pTHX) {
313  a_op_info oi;
314  UV flags;
315  dSP;
316
317  a_map_fetch(PL_op, &oi);
318  flags = oi.flags;
319
320  if (flags & A_HINT_DEREF) {
321   OP *o;
322   U8 old_private;
323
324 deref:
325   old_private       = PL_op->op_private;
326   PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
327   o = CALL_FPTR(oi.old_pp)(aTHX);
328   PL_op->op_private = old_private;
329
330   if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
331    SPAGAIN;
332    if (!SvOK(TOPs)) {
333     if (flags & A_HINT_STRICT)
334      croak("Reference vivification forbidden");
335     else if (flags & A_HINT_WARN)
336       warn("Reference was vivified");
337     else /* A_HINT_STORE */
338      croak("Can't vivify reference");
339    }
340   }
341
342   return o;
343  } else if (flags && (PL_op->op_private & OPpDEREF || PL_op == oi.root)) {
344   oi.flags = flags & A_HINT_NOTIFY;
345
346   if ((oi.root->op_flags & (OPf_MOD|OPf_REF)) != (OPf_MOD|OPf_REF)) {
347    if (flags & A_HINT_FETCH)
348     oi.flags |= (A_HINT_FETCH|A_HINT_DEREF);
349   } else if (flags & A_HINT_STORE)
350     oi.flags |= (A_HINT_STORE|A_HINT_DEREF);
351
352   if (PL_op == oi.root)
353    oi.flags &= ~A_HINT_DEREF;
354
355   /* We will need the updated flags value in the deref part */
356   flags = oi.flags;
357
358   if (flags & A_HINT_DEREF)
359    goto deref;
360
361   /* This op doesn't need to skip autovivification, so restore the original
362    * state. Be aware that another extension might have saved a_pp_deref as the
363    * ppaddr for this op, so restoring PL_op->op_ppaddr doesn't ensure that this
364    * function will never be called again. That's why we don't remove the op info
365    * from our map and we reset oi.flags to 0, so that it can still run correctly
366    * if required. */
367   oi.flags = 0;
368   PL_op->op_ppaddr = oi.old_pp;
369  }
370
371  return CALL_FPTR(oi.old_pp)(aTHX);
372 }
373
374 /* ... pp_root (exists,delete,keys,values) ................................. */
375
376 STATIC OP *a_pp_root_unop(pTHX) {
377  a_op_info oi;
378  dSP;
379
380  if (!a_defined(TOPs)) {
381   POPs;
382   /* Can only be reached by keys or values */
383   if (GIMME_V == G_SCALAR) {
384    dTARGET;
385    PUSHi(0);
386   }
387   RETURN;
388  }
389
390  a_map_fetch(PL_op, &oi);
391
392  return CALL_FPTR(oi.old_pp)(aTHX);
393 }
394
395 STATIC OP *a_pp_root_binop(pTHX) {
396  a_op_info oi;
397  dSP;
398
399  if (!a_defined(TOPm1s)) {
400   POPs;
401   POPs;
402   if (PL_op->op_type == OP_EXISTS)
403    RETPUSHNO;
404   else
405    RETPUSHUNDEF;
406  }
407
408  a_map_fetch(PL_op, &oi);
409
410  return CALL_FPTR(oi.old_pp)(aTHX);
411 }
412
413 /* --- Check functions ----------------------------------------------------- */
414
415 /* ... ck_pad{any,sv} ...................................................... */
416
417 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
418  * function, but are instead manually mutated from a PADANY. This is why we set
419  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
420  * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
421  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
422  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
423  * globally. */
424
425 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
426
427 STATIC void a_pp_padsv_save(void) {
428  if (a_pp_padsv_saved)
429   return;
430
431  a_pp_padsv_saved    = PL_ppaddr[OP_PADSV];
432  PL_ppaddr[OP_PADSV] = a_pp_deref;
433 }
434
435 STATIC void a_pp_padsv_restore(OP *o) {
436  if (!a_pp_padsv_saved)
437   return;
438
439  if (o->op_ppaddr == a_pp_deref)
440   o->op_ppaddr = a_pp_padsv_saved;
441
442  PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
443  a_pp_padsv_saved    = 0;
444 }
445
446 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
447
448 STATIC OP *a_ck_padany(pTHX_ OP *o) {
449  UV hint;
450
451  a_pp_padsv_restore(o);
452
453  o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
454
455  hint = a_hint();
456  if (hint & A_HINT_DO) {
457   a_pp_padsv_save();
458   a_map_store(o, a_pp_padsv_saved, hint);
459  } else
460   a_map_delete(o);
461
462  return o;
463 }
464
465 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
466
467 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
468  UV hint;
469
470  a_pp_padsv_restore(o);
471
472  o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
473
474  hint = a_hint();
475  if (hint & A_HINT_DO) {
476   a_map_store(o, o->op_ppaddr, hint);
477   o->op_ppaddr = a_pp_deref;
478  } else
479   a_map_delete(o);
480
481  return o;
482 }
483
484 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
485
486 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
487 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
488 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
489
490 STATIC OP *a_ck_deref(pTHX_ OP *o) {
491  OP * (*old_ck)(pTHX_ OP *o) = 0;
492  UV hint;
493
494  switch (o->op_type) {
495   case OP_AELEM: old_ck = a_old_ck_aelem; break;
496   case OP_HELEM: old_ck = a_old_ck_helem; break;
497   case OP_RV2SV: old_ck = a_old_ck_rv2sv; break;
498  }
499  o = CALL_FPTR(old_ck)(aTHX_ o);
500
501  hint = a_hint();
502  if (hint & A_HINT_DO) {
503   a_map_store(o, o->op_ppaddr, hint);
504   o->op_ppaddr = a_pp_deref;
505   a_map_set_root(o, hint);
506  } else
507   a_map_delete(o);
508
509  return o;
510 }
511
512 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
513
514 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
515 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
516
517 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
518  OP * (*old_ck)(pTHX_ OP *o) = 0;
519  OP * (*new_pp)(pTHX)        = 0;
520  UV hint;
521
522  switch (o->op_type) {
523   case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
524   case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv; break;
525  }
526  o = CALL_FPTR(old_ck)(aTHX_ o);
527
528  hint = a_hint();
529  if (hint & A_HINT_DO) {
530   if (!(hint & A_HINT_STRICT)) {
531    a_map_store(o, o->op_ppaddr, hint);
532    o->op_ppaddr = new_pp;
533   }
534   a_map_set_root(o, hint);
535  } else
536   a_map_delete(o);
537
538  return o;
539 }
540
541 /* ... ck_root (exists,delete,keys,values) ................................. */
542
543 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
544 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
545 STATIC OP *(*a_old_ck_keys)  (pTHX_ OP *) = 0;
546 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
547
548 STATIC OP *a_ck_root(pTHX_ OP *o) {
549  OP * (*old_ck)(pTHX_ OP *o) = 0;
550  OP * (*new_pp)(pTHX)        = 0;
551  bool enabled = FALSE;
552  UV hint = a_hint();
553
554  switch (o->op_type) {
555   case OP_EXISTS:
556    old_ck  = a_old_ck_exists;
557    new_pp  = a_pp_root_binop;
558    enabled = hint & A_HINT_EXISTS;
559    break;
560   case OP_DELETE:
561    old_ck  = a_old_ck_delete;
562    new_pp  = a_pp_root_binop;
563    enabled = hint & A_HINT_DELETE;
564    break;
565   case OP_KEYS:
566    old_ck  = a_old_ck_keys;
567    new_pp  = a_pp_root_unop;
568    enabled = hint & A_HINT_FETCH;
569    break;
570   case OP_VALUES:
571    old_ck  = a_old_ck_values;
572    new_pp  = a_pp_root_unop;
573    enabled = hint & A_HINT_FETCH;
574    break;
575  }
576  o = CALL_FPTR(old_ck)(aTHX_ o);
577
578  if (hint & A_HINT_DO) {
579   if (enabled) {
580    a_map_set_root(o, hint | A_HINT_DEREF);
581    a_map_store(o, o->op_ppaddr, hint);
582    o->op_ppaddr = new_pp;
583   } else {
584    a_map_set_root(o, 0);
585   }
586  } else
587   a_map_delete(o);
588
589  return o;
590 }
591
592 STATIC U32 a_initialized = 0;
593
594 /* --- XS ------------------------------------------------------------------ */
595
596 MODULE = autovivification      PACKAGE = autovivification
597
598 PROTOTYPES: ENABLE
599
600 BOOT: 
601 {                                    
602  if (!a_initialized++) {
603   HV *stash;
604
605   a_op_map = ptable_new();
606 #ifdef USE_ITHREADS
607   MUTEX_INIT(&a_op_map_mutex);
608 #endif
609
610   PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
611
612   a_old_ck_padany     = PL_check[OP_PADANY];
613   PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
614   a_old_ck_padsv      = PL_check[OP_PADSV];
615   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(a_ck_padsv);
616
617   a_old_ck_aelem      = PL_check[OP_AELEM];
618   PL_check[OP_AELEM]  = MEMBER_TO_FPTR(a_ck_deref);
619   a_old_ck_helem      = PL_check[OP_HELEM];
620   PL_check[OP_HELEM]  = MEMBER_TO_FPTR(a_ck_deref);
621   a_old_ck_rv2sv      = PL_check[OP_RV2SV];
622   PL_check[OP_RV2SV]  = MEMBER_TO_FPTR(a_ck_deref);
623
624   a_old_ck_rv2av      = PL_check[OP_RV2AV];
625   PL_check[OP_RV2AV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
626   a_old_ck_rv2hv      = PL_check[OP_RV2HV];
627   PL_check[OP_RV2HV]  = MEMBER_TO_FPTR(a_ck_rv2xv);
628
629   a_old_ck_exists     = PL_check[OP_EXISTS];
630   PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
631   a_old_ck_delete     = PL_check[OP_DELETE];
632   PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
633   a_old_ck_keys       = PL_check[OP_KEYS];
634   PL_check[OP_KEYS]   = MEMBER_TO_FPTR(a_ck_root);
635   a_old_ck_values     = PL_check[OP_VALUES];
636   PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
637
638   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
639   newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
640   newCONSTSUB(stash, "A_HINT_WARN",   newSVuv(A_HINT_WARN));
641   newCONSTSUB(stash, "A_HINT_FETCH",  newSVuv(A_HINT_FETCH));
642   newCONSTSUB(stash, "A_HINT_STORE",  newSVuv(A_HINT_STORE));
643   newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
644   newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
645   newCONSTSUB(stash, "A_HINT_MASK",   newSVuv(A_HINT_MASK));
646  }
647 }
648
649 SV *
650 _tag(SV *hint)
651 PROTOTYPE: $
652 CODE:
653  RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
654 OUTPUT:
655  RETVAL
656
657 SV *
658 _detag(SV *tag)
659 PROTOTYPE: $
660 CODE:
661  if (!SvOK(tag))
662   XSRETURN_UNDEF;
663  RETVAL = newSVuv(a_detag(tag));
664 OUTPUT:
665  RETVAL