]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Factor the cloning logic in a new lt_clone() function
[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 SV *lt_clone(pTHX_ SV *sv, tTHX owner) {
137 #define lt_clone(S, O) lt_clone(aTHX_ (S), (O))
138  CLONE_PARAMS  param;
139  AV           *stashes = NULL;
140  SV           *dupsv;
141
142  if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
143   stashes = newAV();
144
145  param.stashes    = stashes;
146  param.flags      = 0;
147  param.proto_perl = owner;
148
149  dupsv = sv_dup(sv, &param);
150
151  if (stashes) {
152   av_undef(stashes);
153   SvREFCNT_dec(stashes);
154  }
155
156  return dupsv;
157 }
158
159 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
160  my_cxt_t  *ud  = ud_;
161  lt_hint_t *h1 = ent->val;
162  lt_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
163
164  *h2 = *h1;
165
166  if (ud->owner != aTHX)
167   h2->code = lt_clone(h1->code, ud->owner);
168
169  ptable_hints_store(ud->tbl, ent->key, h2);
170  SvREFCNT_inc(h2->code);
171 }
172
173 STATIC void lt_thread_cleanup(pTHX_ void *);
174
175 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
176  int *level = ud;
177
178  if (*level) {
179   *level = 0;
180   LEAVE;
181   SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
182   ENTER;
183  } else {
184   dMY_CXT;
185   PerlMemShared_free(level);
186   ptable_hints_free(MY_CXT.tbl);
187  }
188 }
189
190 #endif /* LT_THREADSAFE */
191
192 /* ... Hint tags ........................................................... */
193
194 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
195
196 STATIC SV *lt_tag(pTHX_ SV *value) {
197 #define lt_tag(V) lt_tag(aTHX_ (V))
198  lt_hint_t *h;
199  dMY_CXT;
200
201  value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
202
203  h = PerlMemShared_malloc(sizeof *h);
204  h->code = SvREFCNT_inc(value);
205
206 #if LT_WORKAROUND_REQUIRE_PROPAGATION
207  {
208   const PERL_SI *si;
209   UV             requires = 0;
210
211   for (si = PL_curstackinfo; si; si = si->si_prev) {
212    I32 cxix;
213
214    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
215     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
216
217     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
218      ++requires;
219    }
220   }
221
222   h->requires = requires;
223  }
224 #endif
225
226  /* We only need for the key to be an unique tag for looking up the value later.
227   * Allocated memory provides convenient unique identifiers, so that's why we
228   * use the value pointer as the key itself. */
229  ptable_hints_store(MY_CXT.tbl, value, h);
230
231  return newSVuv(PTR2UV(value));
232 }
233
234 STATIC SV *lt_detag(pTHX_ const SV *hint) {
235 #define lt_detag(H) lt_detag(aTHX_ (H))
236  lt_hint_t *h;
237  dMY_CXT;
238
239  if (!(hint && SvOK(hint) && SvIOK(hint)))
240   return NULL;
241
242  h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
243
244 #if LT_WORKAROUND_REQUIRE_PROPAGATION
245  {
246   const PERL_SI *si;
247   UV             requires = 0;
248
249   for (si = PL_curstackinfo; si; si = si->si_prev) {
250    I32 cxix;
251
252    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
253     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
254
255     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
256                                && ++requires > h->requires)
257      return NULL;
258    }
259   }
260  }
261 #endif
262
263  return h->code;
264 }
265
266 #else
267
268 STATIC SV *lt_tag(pTHX_ SV *value) {
269 #define lt_tag(V) lt_tag(aTHX_ (V))
270  UV tag = 0;
271
272  if (SvOK(value) && SvROK(value)) {
273   value = SvRV(value);
274   SvREFCNT_inc(value);
275   tag = PTR2UV(value);
276  }
277
278  return newSVuv(tag);
279 }
280
281 #define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
282
283 #endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
284
285 STATIC U32 lt_hash = 0;
286
287 STATIC SV *lt_hint(pTHX) {
288 #define lt_hint() lt_hint(aTHX)
289  SV *hint;
290 #if LT_HAS_PERL(5, 9, 5)
291  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
292                                        NULL,
293                                        __PACKAGE__, __PACKAGE_LEN__,
294                                        0,
295                                        lt_hash);
296 #else
297  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
298  if (!val)
299   return 0;
300  hint = *val;
301 #endif
302  return lt_detag(hint);
303 }
304
305 /* ... op => info map ...................................................... */
306
307 #define PTABLE_NAME        ptable_map
308 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
309
310 #include "ptable.h"
311
312 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
313 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
314
315 STATIC ptable *lt_op_map = NULL;
316
317 #ifdef USE_ITHREADS
318 STATIC perl_mutex lt_op_map_mutex;
319 #endif
320
321 typedef struct {
322  SV *orig_pkg;
323  SV *type_pkg;
324  SV *type_meth;
325  OP *(*pp_padsv)(pTHX);
326 } lt_op_info;
327
328 STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
329 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBLMS_ (O), (OP), (TP), (TM), (PP))
330  lt_op_info *oi;
331
332 #ifdef USE_ITHREADS
333  MUTEX_LOCK(&lt_op_map_mutex);
334 #endif
335
336  if (!(oi = ptable_fetch(lt_op_map, o))) {
337   oi = PerlMemShared_malloc(sizeof *oi);
338   ptable_map_store(lt_op_map, o, oi);
339  }
340
341  oi->orig_pkg  = orig_pkg;
342  oi->type_pkg  = type_pkg;
343  oi->type_meth = type_meth;
344  oi->pp_padsv  = pp_padsv;
345
346 #ifdef USE_ITHREADS
347  MUTEX_UNLOCK(&lt_op_map_mutex);
348 #endif
349 }
350
351 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
352  const lt_op_info *val;
353
354 #ifdef USE_ITHREADS
355  MUTEX_LOCK(&lt_op_map_mutex);
356 #endif
357
358  val = ptable_fetch(lt_op_map, o);
359  if (val) {
360   *oi = *val;
361   val = oi;
362  }
363
364 #ifdef USE_ITHREADS
365  MUTEX_UNLOCK(&lt_op_map_mutex);
366 #endif
367
368  return val;
369 }
370
371 STATIC void lt_map_delete(pTHX_ const OP *o) {
372 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
373 #ifdef USE_ITHREADS
374  MUTEX_LOCK(&lt_op_map_mutex);
375 #endif
376
377  ptable_map_store(lt_op_map, o, NULL);
378
379 #ifdef USE_ITHREADS
380  MUTEX_UNLOCK(&lt_op_map_mutex);
381 #endif
382 }
383
384 /* --- Hooks --------------------------------------------------------------- */
385
386 /* ... Our pp_padsv ........................................................ */
387
388 STATIC OP *lt_pp_padsv(pTHX) {
389  lt_op_info oi;
390
391  if ((PL_op->op_private & OPpLVAL_INTRO) && lt_map_fetch(PL_op, &oi)) {
392   PADOFFSET targ = PL_op->op_targ;
393   SV *sv         = PAD_SVl(targ);
394
395   if (sv) {
396    int items;
397    dSP;
398
399    ENTER;
400    SAVETMPS;
401
402    PUSHMARK(SP);
403    EXTEND(SP, 3);
404    PUSHs(oi.type_pkg);
405    PUSHs(sv);
406    PUSHs(oi.orig_pkg);
407    PUTBACK;
408
409    items = call_sv(oi.type_meth, G_ARRAY | G_METHOD);
410
411    SPAGAIN;
412    switch (items) {
413     case 0:
414      break;
415     case 1:
416      sv_setsv(sv, POPs);
417      break;
418     default:
419      croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
420    }
421    PUTBACK;
422
423    FREETMPS;
424    LEAVE;
425   }
426
427   return CALL_FPTR(oi.pp_padsv)(aTHX);
428  }
429
430  return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX);
431 }
432
433 STATIC void lt_pp_padsv_save(pMY_CXT) {
434 #define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT)
435  if (MY_CXT.pp_padsv_saved)
436   return;
437
438  MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV];
439  PL_ppaddr[OP_PADSV]   = lt_pp_padsv;
440 }
441
442 STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) {
443 #define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O))
444  OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved;
445
446  if (!saved)
447   return;
448
449  if (o->op_ppaddr == lt_pp_padsv)
450   o->op_ppaddr = saved;
451
452  PL_ppaddr[OP_PADSV]   = saved;
453  MY_CXT.pp_padsv_saved = 0;
454 }
455
456 /* ... Our ck_pad{any,sv} .................................................. */
457
458 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
459  * function, but are instead manually mutated from a PADANY. This is why we set
460  * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
461  * their pp_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
462  * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
463  * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
464  * globally. */
465
466 STATIC SV *lt_default_meth = NULL;
467
468 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
469
470 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
471  HV *stash;
472  SV *code;
473  dMY_CXT;
474
475  lt_pp_padsv_restore(o);
476
477  o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o);
478
479  stash = PL_in_my_stash;
480  if (stash && (code = lt_hint())) {
481   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
482   SV *orig_meth = lt_default_meth;
483   SV *type_pkg  = NULL;
484   SV *type_meth = NULL;
485   int items;
486
487   dSP;
488
489   SvREADONLY_on(orig_pkg);
490
491   ENTER;
492   SAVETMPS;
493
494   PUSHMARK(SP);
495   EXTEND(SP, 2);
496   PUSHs(orig_pkg);
497   PUSHs(orig_meth);
498   PUTBACK;
499
500   items = call_sv(code, G_ARRAY);
501
502   SPAGAIN;
503   if (items > 2)
504    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
505   if (items == 0) {
506    SvREFCNT_dec(orig_pkg);
507    goto skip;
508   } else {
509    SV *rsv;
510    if (items > 1) {
511     rsv = POPs;
512     if (SvOK(rsv)) {
513      type_meth = newSVsv(rsv);
514      SvREADONLY_on(type_meth);
515     }
516    }
517    rsv = POPs;
518    if (SvOK(rsv)) {
519     type_pkg = newSVsv(rsv);
520     SvREADONLY_on(type_pkg);
521    }
522   }
523   PUTBACK;
524
525   FREETMPS;
526   LEAVE;
527
528   if (!type_pkg) {
529    type_pkg = orig_pkg;
530    SvREFCNT_inc(orig_pkg);
531   }
532
533   if (!type_meth) {
534    type_meth = orig_meth;
535    SvREFCNT_inc(orig_meth);
536   }
537
538   lt_pp_padsv_save();
539
540   lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved);
541  } else {
542 skip:
543   lt_map_delete(o);
544  }
545
546  return o;
547 }
548
549 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
550
551 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
552  dMY_CXT;
553
554  lt_pp_padsv_restore(o);
555
556  lt_map_delete(o);
557
558  return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
559 }
560
561 STATIC U32 lt_initialized = 0;
562
563 /* --- XS ------------------------------------------------------------------ */
564
565 MODULE = Lexical::Types      PACKAGE = Lexical::Types
566
567 PROTOTYPES: ENABLE
568
569 BOOT: 
570 {                                    
571  if (!lt_initialized++) {
572   HV *stash;
573
574   MY_CXT_INIT;
575 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
576   MY_CXT.tbl            = ptable_new();
577 #endif
578 #if LT_THREADSAFE
579   MY_CXT.owner          = aTHX;
580 #endif
581   MY_CXT.pp_padsv_saved = 0;
582
583   lt_op_map = ptable_new();
584 #ifdef USE_ITHREADS
585   MUTEX_INIT(&lt_op_map_mutex);
586 #endif
587
588   lt_default_meth = newSVpvn("TYPEDSCALAR", 11);
589   SvREADONLY_on(lt_default_meth);
590
591   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
592
593   lt_old_ck_padany    = PL_check[OP_PADANY];
594   PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
595   lt_old_ck_padsv     = PL_check[OP_PADSV];
596   PL_check[OP_PADSV]  = MEMBER_TO_FPTR(lt_ck_padsv);
597
598   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
599   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
600  }
601 }
602
603 #if LT_THREADSAFE
604
605 void
606 CLONE(...)
607 PROTOTYPE: DISABLE
608 PREINIT:
609  ptable *t;
610  int    *level;
611 CODE:
612  {
613   my_cxt_t ud;
614   dMY_CXT;
615   ud.tbl   = t = ptable_new();
616   ud.owner = MY_CXT.owner;
617   ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud);
618  }
619  {
620   MY_CXT_CLONE;
621   MY_CXT.tbl            = t;
622   MY_CXT.owner          = aTHX;
623   MY_CXT.pp_padsv_saved = 0;
624  }
625  {
626   level = PerlMemShared_malloc(sizeof *level);
627   *level = 1;
628   LEAVE;
629   SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
630   ENTER;
631  }
632
633 #endif
634
635 SV *
636 _tag(SV *value)
637 PROTOTYPE: $
638 CODE:
639  RETVAL = lt_tag(value);
640 OUTPUT:
641  RETVAL