]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Store the overriden pp_padsv into thread-local storage
[perl/modules/Lexical-Types.git] / Types.xs
1 /* This file is part of the Lexical-Types Perl module.
2  * See http://search.cpan.org/dist/Lexical-Types/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "Lexical::Types"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #define LT_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #if LT_HAS_PERL(5, 10, 0) || defined(PL_parser)
17 # ifndef PL_in_my_stash
18 #  define PL_in_my_stash PL_parser->in_my_stash
19 # endif
20 #else
21 # ifndef PL_in_my_stash
22 #  define PL_in_my_stash PL_Iin_my_stash
23 # endif
24 #endif
25
26 #ifndef LT_WORKAROUND_REQUIRE_PROPAGATION
27 # define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1)
28 #endif
29
30 #ifndef HvNAME_get
31 # define HvNAME_get(H) HvNAME(H)
32 #endif
33
34 #ifndef HvNAMELEN_get
35 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
36 #endif
37
38 #ifndef SvIS_FREED
39 # define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
40 #endif
41
42 /* ... Thread safety and multiplicity ...................................... */
43
44 #ifndef LT_MULTIPLICITY
45 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
46 #  define LT_MULTIPLICITY 1
47 # else
48 #  define LT_MULTIPLICITY 0
49 # endif
50 #endif
51 #if LT_MULTIPLICITY && !defined(tTHX)
52 # define tTHX PerlInterpreter*
53 #endif
54
55 #if LT_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))
56 # define LT_THREADSAFE 1
57 # ifndef MY_CXT_CLONE
58 #  define MY_CXT_CLONE \
59     dMY_CXT_SV;                                                      \
60     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
61     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
62     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
63 # endif
64 #else
65 # define LT_THREADSAFE 0
66 # undef  dMY_CXT
67 # define dMY_CXT      dNOOP
68 # undef  MY_CXT
69 # define MY_CXT       lt_globaldata
70 # undef  START_MY_CXT
71 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
72 # undef  MY_CXT_INIT
73 # define MY_CXT_INIT  NOOP
74 # undef  MY_CXT_CLONE
75 # define MY_CXT_CLONE NOOP
76 # undef  pMY_CXT
77 # define pMY_CXT
78 # undef  pMY_CXT_
79 # define pMY_CXT_
80 # undef  aMY_CXT
81 # define aMY_CXT
82 # undef  aMY_CXT_
83 # define aMY_CXT_
84 #endif
85
86 /* --- Helpers ------------------------------------------------------------- */
87
88 /* ... Thread-safe hints ................................................... */
89
90 /* If any of those is true, we need to store the hint in a global table. */
91
92 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
93
94 typedef struct {
95  SV *code;
96 #if LT_WORKAROUND_REQUIRE_PROPAGATION
97  UV  requires;
98 #endif
99 } lt_hint_t;
100
101 #define PTABLE_NAME        ptable_hints
102 #define PTABLE_VAL_FREE(V) { lt_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
103
104 #define pPTBL  pTHX
105 #define pPTBL_ pTHX_
106 #define aPTBL  aTHX
107 #define aPTBL_ aTHX_
108
109 #include "ptable.h"
110
111 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
112 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
113
114 #endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
115
116 /* ... Global data ......................................................... */
117
118 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
119
120 typedef struct {
121 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
122  ptable *tbl; /* It really is a ptable_hints */
123 #endif
124 #if LT_THREADSAFE
125  tTHX    owner;
126 #endif
127  OP *  (*pp_padsv_saved)(pTHX);
128 } my_cxt_t;
129
130 START_MY_CXT
131
132 /* ... Cloning global data ................................................. */
133
134 #if LT_THREADSAFE
135
136 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
137  my_cxt_t  *ud  = ud_;
138  lt_hint_t *h1 = ent->val;
139  lt_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
140
141  *h2 = *h1;
142
143  if (ud->owner != aTHX) {
144   SV *val = h1->code;
145   CLONE_PARAMS param;
146   AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
147   param.stashes    = stashes;
148   param.flags      = 0;
149   param.proto_perl = ud->owner;
150   h2->code = sv_dup(val, &param);
151   if (stashes) {
152    av_undef(stashes);
153    SvREFCNT_dec(stashes);
154   }
155  }
156
157  ptable_hints_store(ud->tbl, ent->key, h2);
158  SvREFCNT_inc(h2->code);
159 }
160
161 STATIC void lt_thread_cleanup(pTHX_ void *);
162
163 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
164  int *level = ud;
165
166  if (*level) {
167   *level = 0;
168   LEAVE;
169   SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
170   ENTER;
171  } else {
172   dMY_CXT;
173   PerlMemShared_free(level);
174   ptable_hints_free(MY_CXT.tbl);
175  }
176 }
177
178 #endif /* LT_THREADSAFE */
179
180 /* ... Hint tags ........................................................... */
181
182 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
183
184 STATIC SV *lt_tag(pTHX_ SV *value) {
185 #define lt_tag(V) lt_tag(aTHX_ (V))
186  lt_hint_t *h;
187  dMY_CXT;
188
189  value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
190
191  h = PerlMemShared_malloc(sizeof *h);
192  h->code = SvREFCNT_inc(value);
193
194 #if LT_WORKAROUND_REQUIRE_PROPAGATION
195  {
196   const PERL_SI *si;
197   UV             requires = 0;
198
199   for (si = PL_curstackinfo; si; si = si->si_prev) {
200    I32 cxix;
201
202    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
203     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
204
205     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
206      ++requires;
207    }
208   }
209
210   h->requires = requires;
211  }
212 #endif
213
214  /* We only need for the key to be an unique tag for looking up the value later.
215   * Allocated memory provides convenient unique identifiers, so that's why we
216   * use the value pointer as the key itself. */
217  ptable_hints_store(MY_CXT.tbl, value, h);
218
219  return newSVuv(PTR2UV(value));
220 }
221
222 STATIC SV *lt_detag(pTHX_ const SV *hint) {
223 #define lt_detag(H) lt_detag(aTHX_ (H))
224  lt_hint_t *h;
225  dMY_CXT;
226
227  if (!(hint && SvOK(hint) && SvIOK(hint)))
228   return NULL;
229
230  h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
231
232 #if LT_WORKAROUND_REQUIRE_PROPAGATION
233  {
234   const PERL_SI *si;
235   UV             requires = 0;
236
237   for (si = PL_curstackinfo; si; si = si->si_prev) {
238    I32 cxix;
239
240    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
241     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
242
243     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
244                                && ++requires > h->requires)
245      return NULL;
246    }
247   }
248  }
249 #endif
250
251  return h->code;
252 }
253
254 #else
255
256 STATIC SV *lt_tag(pTHX_ SV *value) {
257 #define lt_tag(V) lt_tag(aTHX_ (V))
258  UV tag = 0;
259
260  if (SvOK(value) && SvROK(value)) {
261   value = SvRV(value);
262   SvREFCNT_inc(value);
263   tag = PTR2UV(value);
264  }
265
266  return newSVuv(tag);
267 }
268
269 #define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
270
271 #endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
272
273 STATIC U32 lt_hash = 0;
274
275 STATIC SV *lt_hint(pTHX) {
276 #define lt_hint() lt_hint(aTHX)
277  SV *hint;
278 #if LT_HAS_PERL(5, 9, 5)
279  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
280                                        NULL,
281                                        __PACKAGE__, __PACKAGE_LEN__,
282                                        0,
283                                        lt_hash);
284 #else
285  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
286  if (!val)
287   return 0;
288  hint = *val;
289 #endif
290  return lt_detag(hint);
291 }
292
293 /* ... op => info map ...................................................... */
294
295 #define PTABLE_NAME        ptable_map
296 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
297
298 #include "ptable.h"
299
300 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
301 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
302
303 STATIC ptable *lt_op_map = NULL;
304
305 #ifdef USE_ITHREADS
306 STATIC perl_mutex lt_op_map_mutex;
307 #endif
308
309 typedef struct {
310  SV *orig_pkg;
311  SV *type_pkg;
312  SV *type_meth;
313  OP *(*pp_padsv)(pTHX);
314 } lt_op_info;
315
316 STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
317 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBLMS_ (O), (OP), (TP), (TM), (PP))
318  lt_op_info *oi;
319
320 #ifdef USE_ITHREADS
321  MUTEX_LOCK(&lt_op_map_mutex);
322 #endif
323
324  if (!(oi = ptable_fetch(lt_op_map, o))) {
325   oi = PerlMemShared_malloc(sizeof *oi);
326   ptable_map_store(lt_op_map, o, oi);
327  }
328
329  oi->orig_pkg  = orig_pkg;
330  oi->type_pkg  = type_pkg;
331  oi->type_meth = type_meth;
332  oi->pp_padsv  = pp_padsv;
333
334 #ifdef USE_ITHREADS
335  MUTEX_UNLOCK(&lt_op_map_mutex);
336 #endif
337 }
338
339 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
340  const lt_op_info *val;
341
342 #ifdef USE_ITHREADS
343  MUTEX_LOCK(&lt_op_map_mutex);
344 #endif
345
346  val = ptable_fetch(lt_op_map, o);
347  if (val) {
348   *oi = *val;
349   val = oi;
350  }
351
352 #ifdef USE_ITHREADS
353  MUTEX_UNLOCK(&lt_op_map_mutex);
354 #endif
355
356  return val;
357 }
358
359 STATIC void lt_map_delete(pTHX_ const OP *o) {
360 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
361 #ifdef USE_ITHREADS
362  MUTEX_LOCK(&lt_op_map_mutex);
363 #endif
364
365  ptable_map_store(lt_op_map, o, NULL);
366
367 #ifdef USE_ITHREADS
368  MUTEX_UNLOCK(&lt_op_map_mutex);
369 #endif
370 }
371
372 /* --- Hooks --------------------------------------------------------------- */
373
374 /* ... Our pp_padsv ........................................................ */
375
376 STATIC OP *lt_pp_padsv(pTHX) {
377  lt_op_info oi;
378
379  if ((PL_op->op_private & OPpLVAL_INTRO) && lt_map_fetch(PL_op, &oi)) {
380   PADOFFSET targ = PL_op->op_targ;
381   SV *sv         = PAD_SVl(targ);
382
383   if (sv) {
384    int items;
385    dSP;
386
387    ENTER;
388    SAVETMPS;
389
390    PUSHMARK(SP);
391    EXTEND(SP, 3);
392    PUSHs(oi.type_pkg);
393    PUSHs(sv);
394    PUSHs(oi.orig_pkg);
395    PUTBACK;
396
397    items = call_sv(oi.type_meth, G_ARRAY | G_METHOD);
398
399    SPAGAIN;
400    switch (items) {
401     case 0:
402      break;
403     case 1:
404      sv_setsv(sv, POPs);
405      break;
406     default:
407      croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
408    }
409    PUTBACK;
410
411    FREETMPS;
412    LEAVE;
413   }
414
415   return CALL_FPTR(oi.pp_padsv)(aTHX);
416  }
417
418  return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX);
419 }
420
421 STATIC void lt_pp_padsv_save(pMY_CXT) {
422 #define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT)
423  if (MY_CXT.pp_padsv_saved)
424   return;
425
426  MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV];
427  PL_ppaddr[OP_PADSV]   = lt_pp_padsv;
428 }
429
430 STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) {
431 #define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O))
432  OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved;
433
434  if (!saved)
435   return;
436
437  if (o->op_ppaddr == lt_pp_padsv)
438   o->op_ppaddr = saved;
439
440  PL_ppaddr[OP_PADSV]   = saved;
441  MY_CXT.pp_padsv_saved = 0;
442 }
443
444 /* ... Our ck_pad{any,sv} .................................................. */
445
446 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
447  * function, but are instead manually mutated from a PADANY. This is why we set
448  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
449  * their pp_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
450  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
451  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
452  * globally. */
453
454 STATIC SV *lt_default_meth = NULL;
455
456 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
457
458 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
459  HV *stash;
460  SV *code;
461  dMY_CXT;
462
463  lt_pp_padsv_restore(o);
464
465  o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o);
466
467  stash = PL_in_my_stash;
468  if (stash && (code = lt_hint())) {
469   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
470   SV *orig_meth = lt_default_meth;
471   SV *type_pkg  = NULL;
472   SV *type_meth = NULL;
473   int items;
474
475   dSP;
476
477   SvREADONLY_on(orig_pkg);
478
479   ENTER;
480   SAVETMPS;
481
482   PUSHMARK(SP);
483   EXTEND(SP, 2);
484   PUSHs(orig_pkg);
485   PUSHs(orig_meth);
486   PUTBACK;
487
488   items = call_sv(code, G_ARRAY);
489
490   SPAGAIN;
491   if (items > 2)
492    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
493   if (items == 0) {
494    SvREFCNT_dec(orig_pkg);
495    goto skip;
496   } else {
497    SV *rsv;
498    if (items > 1) {
499     rsv = POPs;
500     if (SvOK(rsv)) {
501      type_meth = newSVsv(rsv);
502      SvREADONLY_on(type_meth);
503     }
504    }
505    rsv = POPs;
506    if (SvOK(rsv)) {
507     type_pkg = newSVsv(rsv);
508     SvREADONLY_on(type_pkg);
509    }
510   }
511   PUTBACK;
512
513   FREETMPS;
514   LEAVE;
515
516   if (!type_pkg) {
517    type_pkg = orig_pkg;
518    SvREFCNT_inc(orig_pkg);
519   }
520
521   if (!type_meth) {
522    type_meth = orig_meth;
523    SvREFCNT_inc(orig_meth);
524   }
525
526   lt_pp_padsv_save();
527
528   lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved);
529  } else {
530 skip:
531   lt_map_delete(o);
532  }
533
534  return o;
535 }
536
537 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
538
539 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
540  dMY_CXT;
541
542  lt_pp_padsv_restore(o);
543
544  lt_map_delete(o);
545
546  return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
547 }
548
549 STATIC U32 lt_initialized = 0;
550
551 /* --- XS ------------------------------------------------------------------ */
552
553 MODULE = Lexical::Types      PACKAGE = Lexical::Types
554
555 PROTOTYPES: ENABLE
556
557 BOOT: 
558 {                                    
559  if (!lt_initialized++) {
560   HV *stash;
561
562   MY_CXT_INIT;
563 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
564   MY_CXT.tbl            = ptable_new();
565 #endif
566 #if LT_THREADSAFE
567   MY_CXT.owner          = aTHX;
568 #endif
569   MY_CXT.pp_padsv_saved = 0;
570
571   lt_op_map = ptable_new();
572 #ifdef USE_ITHREADS
573   MUTEX_INIT(&lt_op_map_mutex);
574 #endif
575
576   lt_default_meth = newSVpvn("TYPEDSCALAR", 11);
577   SvREADONLY_on(lt_default_meth);
578
579   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
580
581   lt_old_ck_padany    = PL_check[OP_PADANY];
582   PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
583   lt_old_ck_padsv     = PL_check[OP_PADSV];
584   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(lt_ck_padsv);
585
586   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
587   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
588  }
589 }
590
591 #if LT_THREADSAFE
592
593 void
594 CLONE(...)
595 PROTOTYPE: DISABLE
596 PREINIT:
597  ptable *t;
598  int    *level;
599 CODE:
600  {
601   my_cxt_t ud;
602   dMY_CXT;
603   ud.tbl   = t = ptable_new();
604   ud.owner = MY_CXT.owner;
605   ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud);
606  }
607  {
608   MY_CXT_CLONE;
609   MY_CXT.tbl            = t;
610   MY_CXT.owner          = aTHX;
611   MY_CXT.pp_padsv_saved = 0;
612  }
613  {
614   level = PerlMemShared_malloc(sizeof *level);
615   *level = 1;
616   LEAVE;
617   SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
618   ENTER;
619  }
620
621 #endif
622
623 SV *
624 _tag(SV *value)
625 PROTOTYPE: $
626 CODE:
627  RETVAL = lt_tag(value);
628 OUTPUT:
629  RETVAL