]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Use TARG in lt_pp_padsv
[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_NN
43 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
44 #endif
45
46 /* ... Thread safety and multiplicity ...................................... */
47
48 #ifndef LT_MULTIPLICITY
49 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
50 #  define LT_MULTIPLICITY 1
51 # else
52 #  define LT_MULTIPLICITY 0
53 # endif
54 #endif
55
56 #ifndef tTHX
57 # define tTHX PerlInterpreter*
58 #endif
59
60 #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))
61 # define LT_THREADSAFE 1
62 # ifndef MY_CXT_CLONE
63 #  define MY_CXT_CLONE \
64     dMY_CXT_SV;                                                      \
65     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
66     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
67     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
68 # endif
69 #else
70 # define LT_THREADSAFE 0
71 # undef  dMY_CXT
72 # define dMY_CXT      dNOOP
73 # undef  MY_CXT
74 # define MY_CXT       lt_globaldata
75 # undef  START_MY_CXT
76 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
77 # undef  MY_CXT_INIT
78 # define MY_CXT_INIT  NOOP
79 # undef  MY_CXT_CLONE
80 # define MY_CXT_CLONE NOOP
81 # undef  pMY_CXT
82 # define pMY_CXT
83 # undef  pMY_CXT_
84 # define pMY_CXT_
85 # undef  aMY_CXT
86 # define aMY_CXT
87 # undef  aMY_CXT_
88 # define aMY_CXT_
89 #endif
90
91 /* --- Helpers ------------------------------------------------------------- */
92
93 /* ... Thread-safe hints ................................................... */
94
95 #if LT_WORKAROUND_REQUIRE_PROPAGATION
96
97 typedef struct {
98  SV *code;
99  IV  require_tag;
100 } lt_hint_t;
101
102 #define LT_HINT_STRUCT 1
103
104 #define LT_HINT_CODE(H) ((H)->code)
105
106 #define LT_HINT_FREE(H) { \
107  lt_hint_t *h = (H);      \
108  SvREFCNT_dec(h->code);   \
109  PerlMemShared_free(h);   \
110 }
111
112 #else  /*  LT_WORKAROUND_REQUIRE_PROPAGATION */
113
114 typedef SV lt_hint_t;
115
116 #define LT_HINT_STRUCT 0
117
118 #define LT_HINT_CODE(H) (H)
119
120 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
121
122 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
123
124 #if LT_THREADSAFE
125
126 #define PTABLE_NAME        ptable_hints
127 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
128
129 #define pPTBL  pTHX
130 #define pPTBL_ pTHX_
131 #define aPTBL  aTHX
132 #define aPTBL_ aTHX_
133
134 #include "ptable.h"
135
136 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
137 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
138
139 #endif /* LT_THREADSAFE */
140
141 /* ... "Seen" pointer table ................................................ */
142
143 #if !LT_HAS_RPEEP
144
145 #define PTABLE_NAME        ptable_seen
146 #define PTABLE_VAL_FREE(V) NOOP
147
148 #include "ptable.h"
149
150 #endif /* !LT_HAS_RPEEP */
151
152 /* ... Global data ......................................................... */
153
154 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
155
156 typedef struct {
157 #if LT_THREADSAFE
158  ptable *tbl; /* It really is a ptable_hints */
159  tTHX    owner;
160 #endif
161 #if !LT_HAS_RPEEP
162  ptable *seen; /* It really is a ptable_seen */
163 #endif
164  SV     *default_meth;
165 } my_cxt_t;
166
167 START_MY_CXT
168
169 /* ... Cloning global data ................................................. */
170
171 #if LT_THREADSAFE
172
173 typedef struct {
174  ptable *tbl;
175 #if LT_HAS_PERL(5, 13, 2)
176  CLONE_PARAMS *params;
177 #else
178  CLONE_PARAMS params;
179 #endif
180 } lt_ptable_clone_ud;
181
182 #if LT_HAS_PERL(5, 13, 2)
183 # define lt_ptable_clone_ud_init(U, T, O) \
184    (U).tbl    = (T); \
185    (U).params = Perl_clone_params_new((O), aTHX)
186 # define lt_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
187 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), (U)->params))
188 #else
189 # define lt_ptable_clone_ud_init(U, T, O) \
190    (U).tbl               = (T);     \
191    (U).params.stashes    = newAV(); \
192    (U).params.flags      = 0;       \
193    (U).params.proto_perl = (O)
194 # define lt_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
195 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), &((U)->params)))
196 #endif
197
198 STATIC void lt_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
199  lt_ptable_clone_ud *ud = ud_;
200  lt_hint_t *h1 = ent->val;
201  lt_hint_t *h2;
202
203 #if LT_HINT_STRUCT
204
205  h2              = PerlMemShared_malloc(sizeof *h2);
206  h2->code        = lt_dup_inc(h1->code, ud);
207 #if LT_WORKAROUND_REQUIRE_PROPAGATION
208  h2->require_tag = PTR2IV(lt_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
209 #endif
210
211 #else /*   LT_HINT_STRUCT */
212
213  h2 = lt_dup_inc(h1, ud);
214
215 #endif /* !LT_HINT_STRUCT */
216
217  ptable_hints_store(ud->tbl, ent->key, h2);
218 }
219
220 #include "reap.h"
221
222 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
223  dMY_CXT;
224
225  ptable_hints_free(MY_CXT.tbl);
226 #if !LT_HAS_RPEEP
227  ptable_seen_free(MY_CXT.seen);
228 #endif /* !LT_HAS_RPEEP */
229 }
230
231 #endif /* LT_THREADSAFE */
232
233 /* ... Hint tags ........................................................... */
234
235 #if LT_WORKAROUND_REQUIRE_PROPAGATION
236
237 STATIC IV lt_require_tag(pTHX) {
238 #define lt_require_tag() lt_require_tag(aTHX)
239  const CV *cv, *outside;
240
241  cv = PL_compcv;
242
243  if (!cv) {
244   /* If for some reason the pragma is operational at run-time, try to discover
245    * the current cv in use. */
246   const PERL_SI *si;
247
248   for (si = PL_curstackinfo; si; si = si->si_prev) {
249    I32 cxix;
250
251    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
252     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
253
254     switch (CxTYPE(cx)) {
255      case CXt_SUB:
256      case CXt_FORMAT:
257       /* The propagation workaround is only needed up to 5.10.0 and at that
258        * time format and sub contexts were still identical. And even later the
259        * cv members offsets should have been kept the same. */
260       cv = cx->blk_sub.cv;
261       goto get_enclosing_cv;
262      case CXt_EVAL:
263       cv = cx->blk_eval.cv;
264       goto get_enclosing_cv;
265      default:
266       break;
267     }
268    }
269   }
270
271   cv = PL_main_cv;
272  }
273
274 get_enclosing_cv:
275  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
276   cv = outside;
277
278  return PTR2IV(cv);
279 }
280
281 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
282
283 STATIC SV *lt_tag(pTHX_ SV *value) {
284 #define lt_tag(V) lt_tag(aTHX_ (V))
285  lt_hint_t *h;
286  SV *code = NULL;
287
288  if (SvROK(value)) {
289   value = SvRV(value);
290   if (SvTYPE(value) >= SVt_PVCV) {
291    code = value;
292    SvREFCNT_inc_simple_NN(code);
293   }
294  }
295
296 #if LT_HINT_STRUCT
297  h = PerlMemShared_malloc(sizeof *h);
298  h->code        = code;
299 # if LT_WORKAROUND_REQUIRE_PROPAGATION
300  h->require_tag = lt_require_tag();
301 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
302 #else  /*  LT_HINT_STRUCT */
303  h = code;
304 #endif /* !LT_HINT_STRUCT */
305
306 #if LT_THREADSAFE
307  {
308   dMY_CXT;
309   /* We only need for the key to be an unique tag for looking up the value later
310    * Allocated memory provides convenient unique identifiers, so that's why we
311    * use the hint as the key itself. */
312   ptable_hints_store(MY_CXT.tbl, h, h);
313  }
314 #endif /* LT_THREADSAFE */
315
316  return newSViv(PTR2IV(h));
317 }
318
319 STATIC SV *lt_detag(pTHX_ const SV *hint) {
320 #define lt_detag(H) lt_detag(aTHX_ (H))
321  lt_hint_t *h;
322 #if LT_THREADSAFE
323  dMY_CXT;
324 #endif
325
326  if (!(hint && SvIOK(hint)))
327   return NULL;
328
329  h = INT2PTR(lt_hint_t *, SvIVX(hint));
330 #if LT_THREADSAFE
331  h = ptable_fetch(MY_CXT.tbl, h);
332 #endif /* LT_THREADSAFE */
333 #if LT_WORKAROUND_REQUIRE_PROPAGATION
334  if (lt_require_tag() != h->require_tag)
335   return NULL;
336 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
337
338  return LT_HINT_CODE(h);
339 }
340
341 STATIC U32 lt_hash = 0;
342
343 STATIC SV *lt_hint(pTHX) {
344 #define lt_hint() lt_hint(aTHX)
345  SV *hint;
346 #ifdef cop_hints_fetch_pvn
347  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
348 #elif LT_HAS_PERL(5, 9, 5)
349  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
350                                        NULL,
351                                        __PACKAGE__, __PACKAGE_LEN__,
352                                        0,
353                                        lt_hash);
354 #else
355  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
356  if (!val)
357   return 0;
358  hint = *val;
359 #endif
360  return lt_detag(hint);
361 }
362
363 /* ... op => info map ...................................................... */
364
365 #define PTABLE_NAME        ptable_map
366 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
367
368 #include "ptable.h"
369
370 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
371 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
372
373 STATIC ptable *lt_op_map = NULL;
374
375 #ifdef USE_ITHREADS
376
377 STATIC perl_mutex lt_op_map_mutex;
378
379 #define LT_LOCK(M)   MUTEX_LOCK(M)
380 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
381
382 #else /* USE_ITHREADS */
383
384 #define LT_LOCK(M)
385 #define LT_UNLOCK(M)
386
387 #endif /* !USE_ITHREADS */
388
389 typedef struct {
390 #ifdef MULTIPLICITY
391  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
392  char *buf;
393 #else /* MULTIPLICITY */
394  SV *orig_pkg;
395  SV *type_pkg;
396  SV *type_meth;
397 #endif /* !MULTIPLICITY */
398  OP *(*old_pp_padsv)(pTHX);
399 } lt_op_info;
400
401 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp_padsv)(pTHX)) {
402 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
403  lt_op_info *oi;
404
405  LT_LOCK(&lt_op_map_mutex);
406
407  if (!(oi = ptable_fetch(lt_op_map, o))) {
408   oi = PerlMemShared_malloc(sizeof *oi);
409   ptable_map_store(lt_op_map, o, oi);
410 #ifdef MULTIPLICITY
411   oi->buf      = NULL;
412   oi->buf_size = 0;
413 #else /* MULTIPLICITY */
414  } else {
415   SvREFCNT_dec(oi->orig_pkg);
416   SvREFCNT_dec(oi->type_pkg);
417   SvREFCNT_dec(oi->type_meth);
418 #endif /* !MULTIPLICITY */
419  }
420
421 #ifdef MULTIPLICITY
422  {
423   STRLEN op_len       = SvCUR(orig_pkg);
424   STRLEN tp_len       = SvCUR(type_pkg);
425   STRLEN tm_len       = SvCUR(type_meth);
426   STRLEN new_buf_size = op_len + tp_len + tm_len;
427   char *buf;
428   if (new_buf_size > oi->buf_size) {
429    PerlMemShared_free(oi->buf);
430    oi->buf      = PerlMemShared_malloc(new_buf_size);
431    oi->buf_size = new_buf_size;
432   }
433   buf  = oi->buf;
434   Copy(SvPVX(orig_pkg),  buf, op_len, char);
435   buf += op_len;
436   Copy(SvPVX(type_pkg),  buf, tp_len, char);
437   buf += tp_len;
438   Copy(SvPVX(type_meth), buf, tm_len, char);
439   oi->orig_pkg_len  = op_len;
440   oi->type_pkg_len  = tp_len;
441   oi->type_meth_len = tm_len;
442   SvREFCNT_dec(orig_pkg);
443   SvREFCNT_dec(type_pkg);
444   SvREFCNT_dec(type_meth);
445  }
446 #else /* MULTIPLICITY */
447  oi->orig_pkg  = orig_pkg;
448  oi->type_pkg  = type_pkg;
449  oi->type_meth = type_meth;
450 #endif /* !MULTIPLICITY */
451
452  oi->old_pp_padsv = old_pp_padsv;
453
454  LT_UNLOCK(&lt_op_map_mutex);
455 }
456
457 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
458  const lt_op_info *val;
459
460  LT_LOCK(&lt_op_map_mutex);
461
462  val = ptable_fetch(lt_op_map, o);
463  if (val) {
464   *oi = *val;
465   val = oi;
466  }
467
468  LT_UNLOCK(&lt_op_map_mutex);
469
470  return val;
471 }
472
473 STATIC void lt_map_delete(pTHX_ const OP *o) {
474 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
475  LT_LOCK(&lt_op_map_mutex);
476
477  ptable_map_delete(lt_op_map, o);
478
479  LT_UNLOCK(&lt_op_map_mutex);
480 }
481
482 /* --- Hooks --------------------------------------------------------------- */
483
484 /* ... Our pp_padsv ........................................................ */
485
486 STATIC OP *lt_pp_padsv(pTHX) {
487  lt_op_info oi;
488
489  if (lt_map_fetch(PL_op, &oi)) {
490   SV *orig_pkg, *type_pkg, *type_meth;
491   int items;
492   dSP;
493   dTARGET;
494
495   ENTER;
496   SAVETMPS;
497
498 #ifdef MULTIPLICITY
499   {
500    STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
501    char *buf = oi.buf;
502    orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
503    SvREADONLY_on(orig_pkg);
504    buf      += op_len;
505    type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
506    SvREADONLY_on(type_pkg);
507    buf      += tp_len;
508    type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
509    SvREADONLY_on(type_meth);
510   }
511 #else /* MULTIPLICITY */
512   orig_pkg  = oi.orig_pkg;
513   type_pkg  = oi.type_pkg;
514   type_meth = oi.type_meth;
515 #endif /* !MULTIPLICITY */
516
517   PUSHMARK(SP);
518   EXTEND(SP, 3);
519   PUSHs(type_pkg);
520   PUSHTARG;
521   PUSHs(orig_pkg);
522   PUTBACK;
523
524   items = call_sv(type_meth, G_ARRAY | G_METHOD);
525
526   SPAGAIN;
527   switch (items) {
528    case 0:
529     break;
530    case 1:
531     sv_setsv(TARG, POPs);
532     break;
533    default:
534     croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
535   }
536   PUTBACK;
537
538   FREETMPS;
539   LEAVE;
540
541   return oi.old_pp_padsv(aTHX);
542  }
543
544  return PL_op->op_ppaddr(aTHX);
545 }
546
547 /* ... Our ck_pad{any,sv} .................................................. */
548
549 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
550  * function, but are instead manually mutated from a padany. So we store
551  * the op entry in the op map in the padany check function, and we set their
552  * op_ppaddr member in our peephole optimizer replacement below. */
553
554 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
555
556 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
557  HV *stash;
558  SV *code;
559
560  o = lt_old_ck_padany(aTHX_ o);
561
562  stash = PL_in_my_stash;
563  if (stash && (code = lt_hint())) {
564   dMY_CXT;
565   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
566   SV *orig_meth = MY_CXT.default_meth;
567   SV *type_pkg  = NULL;
568   SV *type_meth = NULL;
569   int items;
570
571   dSP;
572
573   SvREADONLY_on(orig_pkg);
574
575   ENTER;
576   SAVETMPS;
577
578   PUSHMARK(SP);
579   EXTEND(SP, 2);
580   PUSHs(orig_pkg);
581   PUSHs(orig_meth);
582   PUTBACK;
583
584   items = call_sv(code, G_ARRAY);
585
586   SPAGAIN;
587   if (items > 2)
588    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
589   if (items == 0) {
590    SvREFCNT_dec(orig_pkg);
591    FREETMPS;
592    LEAVE;
593    goto skip;
594   } else {
595    SV *rsv;
596    if (items > 1) {
597     rsv = POPs;
598     if (SvOK(rsv)) {
599      type_meth = newSVsv(rsv);
600      SvREADONLY_on(type_meth);
601     }
602    }
603    rsv = POPs;
604    if (SvOK(rsv)) {
605     type_pkg = newSVsv(rsv);
606     SvREADONLY_on(type_pkg);
607    }
608   }
609   PUTBACK;
610
611   FREETMPS;
612   LEAVE;
613
614   if (!type_pkg) {
615    type_pkg = orig_pkg;
616    SvREFCNT_inc(orig_pkg);
617   }
618
619   if (!type_meth) {
620    type_meth = orig_meth;
621    SvREFCNT_inc(orig_meth);
622   }
623
624   lt_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
625  } else {
626 skip:
627   lt_map_delete(o);
628  }
629
630  return o;
631 }
632
633 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
634
635 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
636  lt_map_delete(o);
637
638  return lt_old_ck_padsv(aTHX_ o);
639 }
640
641 /* ... Our peephole optimizer .............................................. */
642
643 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
644
645 #if !LT_HAS_RPEEP
646 # define LT_PEEP_REC_PROTO STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen)
647 #else /* !LT_HAS_RPEEP */
648 # define LT_PEEP_REC_PROTO STATIC void lt_peep_rec(pTHX_ OP *o)
649 #endif /* LT_HAS_RPEEP */
650
651 LT_PEEP_REC_PROTO;
652 LT_PEEP_REC_PROTO {
653 #if !LT_HAS_RPEEP
654 # define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
655 #else /* !LT_HAS_RPEEP */
656 # define lt_peep_rec(O) lt_peep_rec(aTHX_ (O))
657 #endif /* LT_HAS_RPEEP */
658
659 #if !LT_HAS_RPEEP
660  if (ptable_fetch(seen, o))
661   return;
662 #endif
663
664  for (; o; o = o->op_next) {
665   lt_op_info *oi = NULL;
666
667 #if !LT_HAS_RPEEP
668   ptable_seen_store(seen, o, o);
669 #endif
670   switch (o->op_type) {
671    case OP_PADSV:
672     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
673      LT_LOCK(&lt_op_map_mutex);
674      oi = ptable_fetch(lt_op_map, o);
675      if (oi) {
676       oi->old_pp_padsv = o->op_ppaddr;
677       o->op_ppaddr     = lt_pp_padsv;
678      }
679      LT_UNLOCK(&lt_op_map_mutex);
680     }
681     break;
682 #if !LT_HAS_RPEEP
683    case OP_MAPWHILE:
684    case OP_GREPWHILE:
685    case OP_AND:
686    case OP_OR:
687    case OP_ANDASSIGN:
688    case OP_ORASSIGN:
689    case OP_COND_EXPR:
690    case OP_RANGE:
691 # if LT_HAS_PERL(5, 10, 0)
692    case OP_ONCE:
693    case OP_DOR:
694    case OP_DORASSIGN:
695 # endif
696     lt_peep_rec(cLOGOPo->op_other);
697     break;
698    case OP_ENTERLOOP:
699    case OP_ENTERITER:
700     lt_peep_rec(cLOOPo->op_redoop);
701     lt_peep_rec(cLOOPo->op_nextop);
702     lt_peep_rec(cLOOPo->op_lastop);
703     break;
704 # if LT_HAS_PERL(5, 9, 5)
705    case OP_SUBST:
706     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
707     break;
708 # else
709    case OP_QR:
710    case OP_MATCH:
711    case OP_SUBST:
712     lt_peep_rec(cPMOPo->op_pmreplstart);
713     break;
714 # endif
715 #endif /* !LT_HAS_RPEEP */
716    default:
717     break;
718   }
719  }
720 }
721
722 STATIC void lt_peep(pTHX_ OP *o) {
723 #if !LT_HAS_RPEEP
724  dMY_CXT;
725  ptable *seen = MY_CXT.seen;
726
727  ptable_seen_clear(seen);
728 #endif /* !LT_HAS_RPEEP */
729
730  lt_old_peep(aTHX_ o);
731  lt_peep_rec(o);
732 }
733
734 /* --- Interpreter setup/teardown ------------------------------------------ */
735
736
737 STATIC U32 lt_initialized = 0;
738
739 STATIC void lt_teardown(pTHX_ void *root) {
740  if (!lt_initialized)
741   return;
742
743 #if LT_MULTIPLICITY
744  if (aTHX != root)
745   return;
746 #endif
747
748  {
749   dMY_CXT;
750 #if LT_THREADSAFE
751   ptable_hints_free(MY_CXT.tbl);
752 #endif
753 #if !LT_HAS_RPEEP
754   ptable_seen_free(MY_CXT.seen);
755 #endif
756   SvREFCNT_dec(MY_CXT.default_meth);
757  }
758
759  PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_old_ck_padany);
760  lt_old_ck_padany    = 0;
761  PL_check[OP_PADSV]  = MEMBER_TO_FPTR(lt_old_ck_padsv);
762  lt_old_ck_padsv     = 0;
763
764 #if LT_HAS_RPEEP
765  PL_rpeepp   = lt_old_peep;
766 #else
767  PL_peepp    = lt_old_peep;
768 #endif
769  lt_old_peep = 0;
770
771  lt_initialized = 0;
772 }
773
774 STATIC void lt_setup(pTHX) {
775 #define lt_setup() lt_setup(aTHX)
776  if (lt_initialized)
777   return;
778
779  {
780   MY_CXT_INIT;
781 #if LT_THREADSAFE
782   MY_CXT.tbl          = ptable_new();
783   MY_CXT.owner        = aTHX;
784 #endif
785 #if !LT_HAS_RPEEP
786   MY_CXT.seen         = ptable_new();
787 #endif
788   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
789   SvREADONLY_on(MY_CXT.default_meth);
790  }
791
792  lt_old_ck_padany    = PL_check[OP_PADANY];
793  PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
794  lt_old_ck_padsv     = PL_check[OP_PADSV];
795  PL_check[OP_PADSV]  = MEMBER_TO_FPTR(lt_ck_padsv);
796
797 #if LT_HAS_RPEEP
798  lt_old_peep = PL_rpeepp;
799  PL_rpeepp   = lt_peep;
800 #else
801  lt_old_peep = PL_peepp;
802  PL_peepp    = lt_peep;
803 #endif
804
805 #if LT_MULTIPLICITY
806  call_atexit(lt_teardown, aTHX);
807 #else
808  call_atexit(lt_teardown, NULL);
809 #endif
810
811  lt_initialized = 1;
812 }
813
814 STATIC U32 lt_booted = 0;
815
816 /* --- XS ------------------------------------------------------------------ */
817
818 MODULE = Lexical::Types      PACKAGE = Lexical::Types
819
820 PROTOTYPES: ENABLE
821
822 BOOT:
823 {
824  if (!lt_booted++) {
825   HV *stash;
826
827   lt_op_map = ptable_new();
828 #ifdef USE_ITHREADS
829   MUTEX_INIT(&lt_op_map_mutex);
830 #endif
831
832   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
833
834   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
835   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
836  }
837
838  lt_setup();
839 }
840
841 #if LT_THREADSAFE
842
843 void
844 CLONE(...)
845 PROTOTYPE: DISABLE
846 PREINIT:
847  ptable *t;
848 #if !LT_HAS_RPEEP
849  ptable *s;
850 #endif
851  SV     *cloned_default_meth;
852 PPCODE:
853  {
854   {
855    lt_ptable_clone_ud ud;
856    dMY_CXT;
857
858    t = ptable_new();
859    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
860    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
861    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
862    lt_ptable_clone_ud_deinit(ud);
863   }
864 #if !LT_HAS_RPEEP
865   s = ptable_new();
866 #endif
867  }
868  {
869   MY_CXT_CLONE;
870   MY_CXT.tbl          = t;
871   MY_CXT.owner        = aTHX;
872 #if !LT_HAS_RPEEP
873   MY_CXT.seen         = s;
874 #endif
875   MY_CXT.default_meth = cloned_default_meth;
876  }
877  reap(3, lt_thread_cleanup, NULL);
878  XSRETURN(0);
879
880 #endif
881
882 SV *
883 _tag(SV *value)
884 PROTOTYPE: $
885 CODE:
886  RETVAL = lt_tag(value);
887 OUTPUT:
888  RETVAL