]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Do nothing after that the thread local storage has been freed
[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 LT_HAS_RPEEP
31 # define LT_HAS_RPEEP LT_HAS_PERL(5, 13, 5)
32 #endif
33
34 #ifndef HvNAME_get
35 # define HvNAME_get(H) HvNAME(H)
36 #endif
37
38 #ifndef HvNAMELEN_get
39 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
40 #endif
41
42 #ifndef SvREFCNT_inc_simple_void_NN
43 # define SvREFCNT_inc_simple_void_NN(S) ((void) SvREFCNT_inc(S))
44 #endif
45
46 /* ... Thread safety and multiplicity ...................................... */
47
48 /* Safe unless stated otherwise in Makefile.PL */
49 #ifndef LT_FORKSAFE
50 # define LT_FORKSAFE 1
51 #endif
52
53 #ifndef LT_MULTIPLICITY
54 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
55 #  define LT_MULTIPLICITY 1
56 # else
57 #  define LT_MULTIPLICITY 0
58 # endif
59 #endif
60
61 #ifndef tTHX
62 # define tTHX PerlInterpreter*
63 #endif
64
65 #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))
66 # define LT_THREADSAFE 1
67 # ifndef MY_CXT_CLONE
68 #  define MY_CXT_CLONE \
69     dMY_CXT_SV;                                                      \
70     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
71     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
72     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
73 # endif
74 #else
75 # define LT_THREADSAFE 0
76 # undef  dMY_CXT
77 # define dMY_CXT      dNOOP
78 # undef  MY_CXT
79 # define MY_CXT       lt_globaldata
80 # undef  START_MY_CXT
81 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
82 # undef  MY_CXT_INIT
83 # define MY_CXT_INIT  NOOP
84 # undef  MY_CXT_CLONE
85 # define MY_CXT_CLONE NOOP
86 # undef  pMY_CXT
87 # define pMY_CXT
88 # undef  pMY_CXT_
89 # define pMY_CXT_
90 # undef  aMY_CXT
91 # define aMY_CXT
92 # undef  aMY_CXT_
93 # define aMY_CXT_
94 #endif
95
96 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
97 # define LT_CHECK_MUTEX_LOCK   OP_CHECK_MUTEX_LOCK
98 # define LT_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
99 #else
100 # define LT_CHECK_MUTEX_LOCK   OP_REFCNT_LOCK
101 # define LT_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
102 #endif
103
104 typedef OP *(*lt_ck_t)(pTHX_ OP *);
105
106 #ifdef wrap_op_checker
107
108 # define lt_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
109
110 #else
111
112 STATIC void lt_ck_replace(pTHX_ OPCODE type, lt_ck_t new_ck, lt_ck_t *old_ck_p){
113 #define lt_ck_replace(T, NC, OCP) lt_ck_replace(aTHX_ (T), (NC), (OCP))
114  LT_CHECK_MUTEX_LOCK;
115  if (!*old_ck_p) {
116   *old_ck_p      = PL_check[type];
117   PL_check[type] = new_ck;
118  }
119  LT_CHECK_MUTEX_UNLOCK;
120 }
121
122 #endif
123
124 STATIC void lt_ck_restore(pTHX_ OPCODE type, lt_ck_t *old_ck_p) {
125 #define lt_ck_restore(T, OCP) lt_ck_restore(aTHX_ (T), (OCP))
126  LT_CHECK_MUTEX_LOCK;
127  if (*old_ck_p) {
128   PL_check[type] = *old_ck_p;
129   *old_ck_p      = 0;
130  }
131  LT_CHECK_MUTEX_UNLOCK;
132 }
133
134 /* --- Helpers ------------------------------------------------------------- */
135
136 /* ... Thread-safe hints ................................................... */
137
138 #if LT_WORKAROUND_REQUIRE_PROPAGATION
139
140 typedef struct {
141  SV *code;
142  IV  require_tag;
143 } lt_hint_t;
144
145 #define LT_HINT_STRUCT 1
146
147 #define LT_HINT_CODE(H) ((H)->code)
148
149 #define LT_HINT_FREE(H) { \
150  lt_hint_t *h = (H);      \
151  SvREFCNT_dec(h->code);   \
152  PerlMemShared_free(h);   \
153 }
154
155 #else  /*  LT_WORKAROUND_REQUIRE_PROPAGATION */
156
157 typedef SV lt_hint_t;
158
159 #define LT_HINT_STRUCT 0
160
161 #define LT_HINT_CODE(H) (H)
162
163 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
164
165 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
166
167 #if LT_THREADSAFE
168
169 #define PTABLE_NAME        ptable_hints
170 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
171
172 #define pPTBL  pTHX
173 #define pPTBL_ pTHX_
174 #define aPTBL  aTHX
175 #define aPTBL_ aTHX_
176
177 #include "ptable.h"
178
179 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
180 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
181
182 #endif /* LT_THREADSAFE */
183
184 /* ... "Seen" pointer table ................................................ */
185
186 #define PTABLE_NAME        ptable_seen
187 #define PTABLE_VAL_FREE(V) NOOP
188
189 #include "ptable.h"
190
191 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
192 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
193 #define ptable_seen_clear(T)       ptable_seen_clear(aPTBLMS_ (T))
194 #define ptable_seen_free(T)        ptable_seen_free(aPTBLMS_ (T))
195
196 /* ... Global data ......................................................... */
197
198 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
199
200 typedef struct {
201 #if LT_THREADSAFE
202  ptable *tbl; /* It really is a ptable_hints */
203  tTHX    owner;
204 #endif
205  ptable *seen; /* It really is a ptable_seen */
206  SV     *default_meth;
207 } my_cxt_t;
208
209 START_MY_CXT
210
211 /* ... Cloning global data ................................................. */
212
213 #if LT_THREADSAFE
214
215 typedef struct {
216  ptable *tbl;
217 #if LT_HAS_PERL(5, 13, 2)
218  CLONE_PARAMS *params;
219 #else
220  CLONE_PARAMS params;
221 #endif
222 } lt_ptable_clone_ud;
223
224 #if LT_HAS_PERL(5, 13, 2)
225 # define lt_ptable_clone_ud_init(U, T, O) \
226    (U).tbl    = (T); \
227    (U).params = Perl_clone_params_new((O), aTHX)
228 # define lt_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
229 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), (U)->params))
230 #else
231 # define lt_ptable_clone_ud_init(U, T, O) \
232    (U).tbl               = (T);     \
233    (U).params.stashes    = newAV(); \
234    (U).params.flags      = 0;       \
235    (U).params.proto_perl = (O)
236 # define lt_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
237 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), &((U)->params)))
238 #endif
239
240 STATIC void lt_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
241  lt_ptable_clone_ud *ud = ud_;
242  lt_hint_t *h1 = ent->val;
243  lt_hint_t *h2;
244
245 #if LT_HINT_STRUCT
246
247  h2              = PerlMemShared_malloc(sizeof *h2);
248  h2->code        = lt_dup_inc(h1->code, ud);
249 #if LT_WORKAROUND_REQUIRE_PROPAGATION
250  h2->require_tag = PTR2IV(lt_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
251 #endif
252
253 #else /*   LT_HINT_STRUCT */
254
255  h2 = lt_dup_inc(h1, ud);
256
257 #endif /* !LT_HINT_STRUCT */
258
259  ptable_hints_store(ud->tbl, ent->key, h2);
260 }
261
262 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
263  dMY_CXT;
264
265  ptable_hints_free(MY_CXT.tbl);
266  MY_CXT.tbl          = NULL;
267  ptable_seen_free(MY_CXT.seen);
268  MY_CXT.seen         = NULL;
269  SvREFCNT_dec(MY_CXT.default_meth);
270  MY_CXT.default_meth = NULL;
271 }
272
273 STATIC int lt_endav_free(pTHX_ SV *sv, MAGIC *mg) {
274  SAVEDESTRUCTOR_X(lt_thread_cleanup, NULL);
275
276  return 0;
277 }
278
279 STATIC MGVTBL lt_endav_vtbl = {
280  0,
281  0,
282  0,
283  0,
284  lt_endav_free
285 #if MGf_COPY
286  , 0
287 #endif
288 #if MGf_DUP
289  , 0
290 #endif
291 #if MGf_LOCAL
292  , 0
293 #endif
294 };
295
296 #endif /* LT_THREADSAFE */
297
298 /* ... Hint tags ........................................................... */
299
300 #if LT_WORKAROUND_REQUIRE_PROPAGATION
301
302 STATIC IV lt_require_tag(pTHX) {
303 #define lt_require_tag() lt_require_tag(aTHX)
304  const CV *cv, *outside;
305
306  cv = PL_compcv;
307
308  if (!cv) {
309   /* If for some reason the pragma is operational at run-time, try to discover
310    * the current cv in use. */
311   const PERL_SI *si;
312
313   for (si = PL_curstackinfo; si; si = si->si_prev) {
314    I32 cxix;
315
316    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
317     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
318
319     switch (CxTYPE(cx)) {
320      case CXt_SUB:
321      case CXt_FORMAT:
322       /* The propagation workaround is only needed up to 5.10.0 and at that
323        * time format and sub contexts were still identical. And even later the
324        * cv members offsets should have been kept the same. */
325       cv = cx->blk_sub.cv;
326       goto get_enclosing_cv;
327      case CXt_EVAL:
328       cv = cx->blk_eval.cv;
329       goto get_enclosing_cv;
330      default:
331       break;
332     }
333    }
334   }
335
336   cv = PL_main_cv;
337  }
338
339 get_enclosing_cv:
340  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
341   cv = outside;
342
343  return PTR2IV(cv);
344 }
345
346 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
347
348 STATIC SV *lt_tag(pTHX_ SV *value) {
349 #define lt_tag(V) lt_tag(aTHX_ (V))
350  lt_hint_t *h;
351  SV        *code = NULL;
352 #if LT_THREADSAFE
353  dMY_CXT;
354
355  if (!MY_CXT.tbl)
356   return newSViv(0);
357 #endif /* LT_THREADSAFE */
358
359  if (SvROK(value)) {
360   value = SvRV(value);
361   if (SvTYPE(value) >= SVt_PVCV) {
362    code = value;
363    SvREFCNT_inc_simple_void_NN(code);
364   }
365  }
366
367 #if LT_HINT_STRUCT
368  h = PerlMemShared_malloc(sizeof *h);
369  h->code        = code;
370 # if LT_WORKAROUND_REQUIRE_PROPAGATION
371  h->require_tag = lt_require_tag();
372 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
373 #else  /*  LT_HINT_STRUCT */
374  h = code;
375 #endif /* !LT_HINT_STRUCT */
376
377 #if LT_THREADSAFE
378  /* We only need for the key to be an unique tag for looking up the value later
379   * Allocated memory provides convenient unique identifiers, so that's why we
380   * use the hint as the key itself. */
381  ptable_hints_store(MY_CXT.tbl, h, h);
382 #endif /* LT_THREADSAFE */
383
384  return newSViv(PTR2IV(h));
385 }
386
387 STATIC SV *lt_detag(pTHX_ const SV *hint) {
388 #define lt_detag(H) lt_detag(aTHX_ (H))
389  lt_hint_t *h;
390 #if LT_THREADSAFE
391  dMY_CXT;
392
393  if (!MY_CXT.tbl)
394   return NULL;
395 #endif /* LT_THREADSAFE */
396
397  if (!(hint && SvIOK(hint)))
398   return NULL;
399
400  h = INT2PTR(lt_hint_t *, SvIVX(hint));
401 #if LT_THREADSAFE
402  h = ptable_fetch(MY_CXT.tbl, h);
403 #endif /* LT_THREADSAFE */
404 #if LT_WORKAROUND_REQUIRE_PROPAGATION
405  if (lt_require_tag() != h->require_tag)
406   return NULL;
407 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
408
409  return LT_HINT_CODE(h);
410 }
411
412 STATIC U32 lt_hash = 0;
413
414 STATIC SV *lt_hint(pTHX) {
415 #define lt_hint() lt_hint(aTHX)
416  SV *hint;
417 #ifdef cop_hints_fetch_pvn
418  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
419 #elif LT_HAS_PERL(5, 9, 5)
420  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
421                                        NULL,
422                                        __PACKAGE__, __PACKAGE_LEN__,
423                                        0,
424                                        lt_hash);
425 #else
426  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
427  if (!val)
428   return 0;
429  hint = *val;
430 #endif
431  return lt_detag(hint);
432 }
433
434 /* ... op => info map ...................................................... */
435
436 #define PTABLE_NAME        ptable_map
437 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
438
439 #include "ptable.h"
440
441 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
442 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
443 #define ptable_map_delete(T, K)   ptable_map_delete(aPTBLMS_ (T), (K))
444
445 #ifdef USE_ITHREADS
446
447 STATIC perl_mutex lt_op_map_mutex;
448
449 #define LT_LOCK(M)   MUTEX_LOCK(M)
450 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
451
452 #else /* USE_ITHREADS */
453
454 #define LT_LOCK(M)
455 #define LT_UNLOCK(M)
456
457 #endif /* !USE_ITHREADS */
458
459 STATIC ptable *lt_op_padxv_map = NULL;
460
461 typedef struct {
462  OP *(*old_pp)(pTHX);
463 #ifdef MULTIPLICITY
464  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
465  char *buf;
466 #else /* MULTIPLICITY */
467  SV *orig_pkg;
468  SV *type_pkg;
469  SV *type_meth;
470 #endif /* !MULTIPLICITY */
471 } lt_op_padxv_info;
472
473 STATIC void lt_op_padxv_info_call(pTHX_ const lt_op_padxv_info *oi, SV *sv) {
474 #define lt_op_padxv_info_call(O, S) lt_op_padxv_info_call(aTHX_ (O), (S))
475  SV *orig_pkg, *type_pkg, *type_meth;
476  int items;
477  dSP;
478
479  ENTER;
480  SAVETMPS;
481
482 #ifdef MULTIPLICITY
483  {
484   STRLEN op_len = oi->orig_pkg_len, tp_len = oi->type_pkg_len;
485   char *buf = oi->buf;
486   orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
487   SvREADONLY_on(orig_pkg);
488   buf      += op_len;
489   type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
490   SvREADONLY_on(type_pkg);
491   buf      += tp_len;
492   type_meth = sv_2mortal(newSVpvn(buf, oi->type_meth_len));
493   SvREADONLY_on(type_meth);
494  }
495 #else /* MULTIPLICITY */
496  orig_pkg  = oi->orig_pkg;
497  type_pkg  = oi->type_pkg;
498  type_meth = oi->type_meth;
499 #endif /* !MULTIPLICITY */
500
501  PUSHMARK(SP);
502  EXTEND(SP, 3);
503  PUSHs(type_pkg);
504  PUSHs(sv);
505  PUSHs(orig_pkg);
506  PUTBACK;
507
508  items = call_sv(type_meth, G_ARRAY | G_METHOD);
509
510  SPAGAIN;
511  switch (items) {
512   case 0:
513    break;
514   case 1:
515    sv_setsv(sv, POPs);
516    break;
517   default:
518    croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
519  }
520  PUTBACK;
521
522  FREETMPS;
523  LEAVE;
524
525  return;
526 }
527
528 STATIC void lt_padxv_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp)(pTHX)) {
529 #define lt_padxv_map_store(O, OP, TP, TM, PP) lt_padxv_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
530  lt_op_padxv_info *oi;
531
532  LT_LOCK(&lt_op_map_mutex);
533
534  if (!(oi = ptable_fetch(lt_op_padxv_map, o))) {
535   oi = PerlMemShared_malloc(sizeof *oi);
536   ptable_map_store(lt_op_padxv_map, o, oi);
537 #ifdef MULTIPLICITY
538   oi->buf      = NULL;
539   oi->buf_size = 0;
540 #else /* MULTIPLICITY */
541  } else {
542   SvREFCNT_dec(oi->orig_pkg);
543   SvREFCNT_dec(oi->type_pkg);
544   SvREFCNT_dec(oi->type_meth);
545 #endif /* !MULTIPLICITY */
546  }
547
548 #ifdef MULTIPLICITY
549  {
550   STRLEN op_len       = SvCUR(orig_pkg);
551   STRLEN tp_len       = SvCUR(type_pkg);
552   STRLEN tm_len       = SvCUR(type_meth);
553   STRLEN new_buf_size = op_len + tp_len + tm_len;
554   char *buf;
555   if (new_buf_size > oi->buf_size) {
556    PerlMemShared_free(oi->buf);
557    oi->buf      = PerlMemShared_malloc(new_buf_size);
558    oi->buf_size = new_buf_size;
559   }
560   buf  = oi->buf;
561   Copy(SvPVX(orig_pkg),  buf, op_len, char);
562   buf += op_len;
563   Copy(SvPVX(type_pkg),  buf, tp_len, char);
564   buf += tp_len;
565   Copy(SvPVX(type_meth), buf, tm_len, char);
566   oi->orig_pkg_len  = op_len;
567   oi->type_pkg_len  = tp_len;
568   oi->type_meth_len = tm_len;
569   SvREFCNT_dec(orig_pkg);
570   SvREFCNT_dec(type_pkg);
571   SvREFCNT_dec(type_meth);
572  }
573 #else /* MULTIPLICITY */
574  oi->orig_pkg  = orig_pkg;
575  oi->type_pkg  = type_pkg;
576  oi->type_meth = type_meth;
577 #endif /* !MULTIPLICITY */
578
579  oi->old_pp = old_pp;
580
581  LT_UNLOCK(&lt_op_map_mutex);
582 }
583
584 STATIC const lt_op_padxv_info *lt_padxv_map_fetch(const OP *o, lt_op_padxv_info *oi) {
585  const lt_op_padxv_info *val;
586
587  LT_LOCK(&lt_op_map_mutex);
588
589  val = ptable_fetch(lt_op_padxv_map, o);
590  if (val) {
591   *oi = *val;
592   val = oi;
593  }
594
595  LT_UNLOCK(&lt_op_map_mutex);
596
597  return val;
598 }
599
600 #if LT_HAS_PERL(5, 17, 6)
601
602 STATIC ptable *lt_op_padrange_map = NULL;
603
604 typedef struct {
605  OP *(*old_pp)(pTHX);
606  const OP *padxv_start;
607 } lt_op_padrange_info;
608
609 STATIC void lt_padrange_map_store(pTHX_ const OP *o, const OP *s, OP *(*old_pp)(pTHX)) {
610 #define lt_padrange_map_store(O, S, PP) lt_padrange_map_store(aTHX_ (O), (S), (PP))
611  lt_op_padrange_info *oi;
612
613  LT_LOCK(&lt_op_map_mutex);
614
615  if (!(oi = ptable_fetch(lt_op_padrange_map, o))) {
616   oi = PerlMemShared_malloc(sizeof *oi);
617   ptable_map_store(lt_op_padrange_map, o, oi);
618  }
619
620  oi->old_pp      = old_pp;
621  oi->padxv_start = s;
622
623  LT_UNLOCK(&lt_op_map_mutex);
624 }
625
626 STATIC const lt_op_padrange_info *lt_padrange_map_fetch(const OP *o, lt_op_padrange_info *oi) {
627  const lt_op_padrange_info *val;
628
629  LT_LOCK(&lt_op_map_mutex);
630
631  val = ptable_fetch(lt_op_padrange_map, o);
632  if (val) {
633   *oi = *val;
634   val = oi;
635  }
636
637  LT_UNLOCK(&lt_op_map_mutex);
638
639  return val;
640 }
641
642 #endif
643
644 STATIC void lt_map_delete(pTHX_ const OP *o) {
645 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
646  LT_LOCK(&lt_op_map_mutex);
647
648  ptable_map_delete(lt_op_padxv_map,    o);
649 #if LT_HAS_PERL(5, 17, 6)
650  ptable_map_delete(lt_op_padrange_map, o);
651 #endif
652
653  LT_UNLOCK(&lt_op_map_mutex);
654 }
655
656 /* --- Hooks --------------------------------------------------------------- */
657
658 /* ... Our pp_padsv ........................................................ */
659
660 STATIC OP *lt_pp_padsv(pTHX) {
661  lt_op_padxv_info oi;
662
663  if (lt_padxv_map_fetch(PL_op, &oi)) {
664   dTARGET;
665   lt_op_padxv_info_call(&oi, TARG);
666   return oi.old_pp(aTHX);
667  }
668
669  return PL_op->op_ppaddr(aTHX);
670 }
671
672 /* ... Our pp_padrange (on perl 5.17.6 and above) .......................... */
673
674 #if LT_HAS_PERL(5, 17, 6)
675
676 STATIC OP *lt_pp_padrange(pTHX) {
677  lt_op_padrange_info roi;
678
679  if (lt_padrange_map_fetch(PL_op, &roi)) {
680   PADOFFSET i, base, count;
681   const OP *p;
682
683   base  = PL_op->op_targ;
684   count = PL_op->op_private & OPpPADRANGE_COUNTMASK;
685
686   for (i = 0, p = roi.padxv_start; i < count && p; ++i, p = p->op_sibling) {
687    lt_op_padxv_info oi;
688    if (p->op_type == OP_PADSV && lt_padxv_map_fetch(p, &oi))
689     lt_op_padxv_info_call(&oi, PAD_SV(base + i));
690   }
691
692   return roi.old_pp(aTHX);
693  }
694
695  return PL_op->op_ppaddr(aTHX);
696 }
697
698 #endif
699
700 /* ... Our ck_pad{any,sv} .................................................. */
701
702 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
703  * function, but are instead manually mutated from a padany. So we store
704  * the op entry in the op map in the padany check function, and we set their
705  * op_ppaddr member in our peephole optimizer replacement below. */
706
707 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
708
709 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
710  HV *stash;
711  SV *code;
712
713  o = lt_old_ck_padany(aTHX_ o);
714
715  stash = PL_in_my_stash;
716  if (stash && (code = lt_hint())) {
717   dMY_CXT;
718   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
719   SV *orig_meth = MY_CXT.default_meth; /* Guarded by lt_hint() */
720   SV *type_pkg  = NULL;
721   SV *type_meth = NULL;
722   int items;
723
724   dSP;
725
726   SvREADONLY_on(orig_pkg);
727
728   ENTER;
729   SAVETMPS;
730
731   PUSHMARK(SP);
732   EXTEND(SP, 2);
733   PUSHs(orig_pkg);
734   PUSHs(orig_meth);
735   PUTBACK;
736
737   items = call_sv(code, G_ARRAY);
738
739   SPAGAIN;
740   if (items > 2)
741    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
742   if (items == 0) {
743    SvREFCNT_dec(orig_pkg);
744    FREETMPS;
745    LEAVE;
746    goto skip;
747   } else {
748    SV *rsv;
749    if (items > 1) {
750     rsv = POPs;
751     if (SvOK(rsv)) {
752      type_meth = newSVsv(rsv);
753      SvREADONLY_on(type_meth);
754     }
755    }
756    rsv = POPs;
757    if (SvOK(rsv)) {
758     type_pkg = newSVsv(rsv);
759     SvREADONLY_on(type_pkg);
760    }
761   }
762   PUTBACK;
763
764   FREETMPS;
765   LEAVE;
766
767   if (!type_pkg) {
768    type_pkg = orig_pkg;
769    SvREFCNT_inc_simple_void_NN(orig_pkg);
770   }
771
772   if (!type_meth) {
773    type_meth = orig_meth;
774    SvREFCNT_inc_simple_void_NN(orig_meth);
775   }
776
777   lt_padxv_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
778  } else {
779 skip:
780   lt_map_delete(o);
781  }
782
783  return o;
784 }
785
786 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
787
788 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
789  lt_map_delete(o);
790
791  return lt_old_ck_padsv(aTHX_ o);
792 }
793
794 /* ... Our peephole optimizer .............................................. */
795
796 #if LT_HAS_PERL(5, 17, 6)
797
798 STATIC int lt_maybe_padrange_setup(pTHX_ OP *o, const OP *start) {
799 #define lt_maybe_padrange_setup(O, S) lt_maybe_padrange_setup(aTHX_ (O), (S))
800  PADOFFSET i, count;
801  const OP *p;
802
803  count = o->op_private & OPpPADRANGE_COUNTMASK;
804
805  for (i = 0, p = start; i < count && p; ++i, p = p->op_sibling) {
806   if (p->op_type == OP_PADSV) {
807    /* In a padrange sequence, either all lexicals are typed, or none are.
808     * Thus we can stop at the first padsv op. However, note that these
809     * lexicals can need to call different methods in different packages. */
810    LT_LOCK(&lt_op_map_mutex);
811    if (ptable_fetch(lt_op_padxv_map, p)) {
812     LT_UNLOCK(&lt_op_map_mutex);
813     lt_padrange_map_store(o, start, o->op_ppaddr);
814     o->op_ppaddr = lt_pp_padrange;
815    } else {
816     LT_UNLOCK(&lt_op_map_mutex);
817    }
818    return 1;
819   }
820  }
821
822  return 0;
823 }
824
825 #endif
826
827 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
828
829 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
830 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
831  for (; o; o = o->op_next) {
832   if (ptable_fetch(seen, o))
833    break;
834   ptable_seen_store(seen, o, o);
835
836   switch (o->op_type) {
837    case OP_PADSV:
838     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
839      lt_op_padxv_info *oi;
840      LT_LOCK(&lt_op_map_mutex);
841      oi = ptable_fetch(lt_op_padxv_map, o);
842      if (oi) {
843       oi->old_pp   = o->op_ppaddr;
844       o->op_ppaddr = lt_pp_padsv;
845      }
846      LT_UNLOCK(&lt_op_map_mutex);
847     }
848     break;
849 #if LT_HAS_PERL(5, 17, 6)
850    case OP_PADRANGE:
851     /* We deal with special padrange ops later, in the aassign op they belong
852      * to. */
853     if (o->op_ppaddr != lt_pp_padrange && o->op_private & OPpLVAL_INTRO
854                                        && !(o->op_flags & OPf_SPECIAL)) {
855      /* A padrange op is guaranteed to have previously been a pushmark.
856       * Moreover, for non-special padrange ops (i.e. that aren't for
857       * my (...) = @_), the original padxv ops are its siblings. */
858      lt_maybe_padrange_setup(o, o->op_sibling);
859     }
860     break;
861    case OP_AASSIGN: {
862     OP *op;
863     if (cBINOPo->op_first && cBINOPo->op_first->op_flags & OPf_KIDS
864                           && (op = cUNOPx(cBINOPo->op_first)->op_first)
865                           && op->op_type == OP_PADRANGE
866                           && op->op_ppaddr != lt_pp_padrange
867                           && op->op_private & OPpLVAL_INTRO
868                           && op->op_flags & OPf_SPECIAL) {
869      const OP *start = cUNOPx(cBINOPo->op_last)->op_first;
870      if (start->op_type == OP_PUSHMARK)
871       start = start->op_sibling;
872      lt_maybe_padrange_setup(op, start);
873     }
874     break;
875    }
876 #endif
877 #if !LT_HAS_RPEEP
878    case OP_MAPWHILE:
879    case OP_GREPWHILE:
880    case OP_AND:
881    case OP_OR:
882    case OP_ANDASSIGN:
883    case OP_ORASSIGN:
884    case OP_COND_EXPR:
885    case OP_RANGE:
886 # if LT_HAS_PERL(5, 10, 0)
887    case OP_ONCE:
888    case OP_DOR:
889    case OP_DORASSIGN:
890 # endif
891     lt_peep_rec(cLOGOPo->op_other);
892     break;
893    case OP_ENTERLOOP:
894    case OP_ENTERITER:
895     lt_peep_rec(cLOOPo->op_redoop);
896     lt_peep_rec(cLOOPo->op_nextop);
897     lt_peep_rec(cLOOPo->op_lastop);
898     break;
899 # if LT_HAS_PERL(5, 9, 5)
900    case OP_SUBST:
901     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
902     break;
903 # else
904    case OP_QR:
905    case OP_MATCH:
906    case OP_SUBST:
907     lt_peep_rec(cPMOPo->op_pmreplstart);
908     break;
909 # endif
910 #endif /* !LT_HAS_RPEEP */
911    default:
912     break;
913   }
914  }
915 }
916
917 STATIC void lt_peep(pTHX_ OP *o) {
918  dMY_CXT;
919  ptable *seen = MY_CXT.seen;
920
921  lt_old_peep(aTHX_ o);
922
923  if (seen) {
924   ptable_seen_clear(seen);
925   lt_peep_rec(o);
926   ptable_seen_clear(seen);
927  }
928 }
929
930 /* --- Interpreter setup/teardown ------------------------------------------ */
931
932
933 STATIC U32 lt_initialized = 0;
934
935 STATIC void lt_teardown(pTHX_ void *root) {
936  if (!lt_initialized)
937   return;
938
939 #if LT_MULTIPLICITY
940  if (aTHX != root)
941   return;
942 #endif
943
944  {
945   dMY_CXT;
946 #if LT_THREADSAFE
947   ptable_hints_free(MY_CXT.tbl);
948   MY_CXT.tbl          = NULL;
949 #endif
950   ptable_seen_free(MY_CXT.seen);
951   MY_CXT.seen         = NULL;
952   SvREFCNT_dec(MY_CXT.default_meth);
953   MY_CXT.default_meth = NULL;
954  }
955
956  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
957  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
958
959 #if LT_HAS_RPEEP
960  PL_rpeepp   = lt_old_peep;
961 #else
962  PL_peepp    = lt_old_peep;
963 #endif
964  lt_old_peep = 0;
965
966  lt_initialized = 0;
967 }
968
969 STATIC void lt_setup(pTHX) {
970 #define lt_setup() lt_setup(aTHX)
971  if (lt_initialized)
972   return;
973
974  {
975   MY_CXT_INIT;
976 #if LT_THREADSAFE
977   MY_CXT.tbl          = ptable_new();
978   MY_CXT.owner        = aTHX;
979 #endif
980   MY_CXT.seen         = ptable_new();
981   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
982   SvREADONLY_on(MY_CXT.default_meth);
983  }
984
985  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
986  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
987
988 #if LT_HAS_RPEEP
989  lt_old_peep = PL_rpeepp;
990  PL_rpeepp   = lt_peep;
991 #else
992  lt_old_peep = PL_peepp;
993  PL_peepp    = lt_peep;
994 #endif
995
996 #if LT_MULTIPLICITY
997  call_atexit(lt_teardown, aTHX);
998 #else
999  call_atexit(lt_teardown, NULL);
1000 #endif
1001
1002  lt_initialized = 1;
1003 }
1004
1005 STATIC U32 lt_booted = 0;
1006
1007 /* --- XS ------------------------------------------------------------------ */
1008
1009 MODULE = Lexical::Types      PACKAGE = Lexical::Types
1010
1011 PROTOTYPES: ENABLE
1012
1013 BOOT:
1014 {
1015  if (!lt_booted++) {
1016   HV *stash;
1017
1018   lt_op_padxv_map    = ptable_new();
1019 #if LT_HAS_PERL(5, 17, 6)
1020   lt_op_padrange_map = ptable_new();
1021 #endif
1022 #ifdef USE_ITHREADS
1023   MUTEX_INIT(&lt_op_map_mutex);
1024 #endif
1025
1026   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
1027
1028   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1029   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
1030   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
1031  }
1032
1033  lt_setup();
1034 }
1035
1036 #if LT_THREADSAFE
1037
1038 void
1039 CLONE(...)
1040 PROTOTYPE: DISABLE
1041 PREINIT:
1042  ptable *t;
1043  ptable *s;
1044  SV     *cloned_default_meth;
1045  GV     *gv;
1046 PPCODE:
1047  {
1048   {
1049    lt_ptable_clone_ud ud;
1050    dMY_CXT;
1051
1052    t = ptable_new();
1053    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1054    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
1055    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
1056    lt_ptable_clone_ud_deinit(ud);
1057   }
1058   s = ptable_new();
1059  }
1060  {
1061   MY_CXT_CLONE;
1062   MY_CXT.tbl          = t;
1063   MY_CXT.owner        = aTHX;
1064   MY_CXT.seen         = s;
1065   MY_CXT.default_meth = cloned_default_meth;
1066  }
1067  gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1068  if (gv) {
1069   CV *cv = GvCV(gv);
1070   if (!PL_endav)
1071    PL_endav = newAV();
1072   SvREFCNT_inc(cv);
1073   if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1074    SvREFCNT_dec(cv);
1075   sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &lt_endav_vtbl, NULL, 0);
1076  }
1077  XSRETURN(0);
1078
1079 void
1080 _THREAD_CLEANUP(...)
1081 PROTOTYPE: DISABLE
1082 PPCODE:
1083  lt_thread_cleanup(aTHX_ NULL);
1084  XSRETURN(0);
1085
1086 #endif
1087
1088 SV *
1089 _tag(SV *value)
1090 PROTOTYPE: $
1091 CODE:
1092  RETVAL = lt_tag(value);
1093 OUTPUT:
1094  RETVAL