]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Move the run-time initializer call into a new lt_op_info_call() helper
[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 #include "reap.h"
263
264 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
265  dMY_CXT;
266
267  ptable_hints_free(MY_CXT.tbl);
268  ptable_seen_free(MY_CXT.seen);
269 }
270
271 #endif /* LT_THREADSAFE */
272
273 /* ... Hint tags ........................................................... */
274
275 #if LT_WORKAROUND_REQUIRE_PROPAGATION
276
277 STATIC IV lt_require_tag(pTHX) {
278 #define lt_require_tag() lt_require_tag(aTHX)
279  const CV *cv, *outside;
280
281  cv = PL_compcv;
282
283  if (!cv) {
284   /* If for some reason the pragma is operational at run-time, try to discover
285    * the current cv in use. */
286   const PERL_SI *si;
287
288   for (si = PL_curstackinfo; si; si = si->si_prev) {
289    I32 cxix;
290
291    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
292     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
293
294     switch (CxTYPE(cx)) {
295      case CXt_SUB:
296      case CXt_FORMAT:
297       /* The propagation workaround is only needed up to 5.10.0 and at that
298        * time format and sub contexts were still identical. And even later the
299        * cv members offsets should have been kept the same. */
300       cv = cx->blk_sub.cv;
301       goto get_enclosing_cv;
302      case CXt_EVAL:
303       cv = cx->blk_eval.cv;
304       goto get_enclosing_cv;
305      default:
306       break;
307     }
308    }
309   }
310
311   cv = PL_main_cv;
312  }
313
314 get_enclosing_cv:
315  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
316   cv = outside;
317
318  return PTR2IV(cv);
319 }
320
321 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
322
323 STATIC SV *lt_tag(pTHX_ SV *value) {
324 #define lt_tag(V) lt_tag(aTHX_ (V))
325  lt_hint_t *h;
326  SV *code = NULL;
327
328  if (SvROK(value)) {
329   value = SvRV(value);
330   if (SvTYPE(value) >= SVt_PVCV) {
331    code = value;
332    SvREFCNT_inc_simple_void_NN(code);
333   }
334  }
335
336 #if LT_HINT_STRUCT
337  h = PerlMemShared_malloc(sizeof *h);
338  h->code        = code;
339 # if LT_WORKAROUND_REQUIRE_PROPAGATION
340  h->require_tag = lt_require_tag();
341 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
342 #else  /*  LT_HINT_STRUCT */
343  h = code;
344 #endif /* !LT_HINT_STRUCT */
345
346 #if LT_THREADSAFE
347  {
348   dMY_CXT;
349   /* We only need for the key to be an unique tag for looking up the value later
350    * Allocated memory provides convenient unique identifiers, so that's why we
351    * use the hint as the key itself. */
352   ptable_hints_store(MY_CXT.tbl, h, h);
353  }
354 #endif /* LT_THREADSAFE */
355
356  return newSViv(PTR2IV(h));
357 }
358
359 STATIC SV *lt_detag(pTHX_ const SV *hint) {
360 #define lt_detag(H) lt_detag(aTHX_ (H))
361  lt_hint_t *h;
362 #if LT_THREADSAFE
363  dMY_CXT;
364 #endif
365
366  if (!(hint && SvIOK(hint)))
367   return NULL;
368
369  h = INT2PTR(lt_hint_t *, SvIVX(hint));
370 #if LT_THREADSAFE
371  h = ptable_fetch(MY_CXT.tbl, h);
372 #endif /* LT_THREADSAFE */
373 #if LT_WORKAROUND_REQUIRE_PROPAGATION
374  if (lt_require_tag() != h->require_tag)
375   return NULL;
376 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
377
378  return LT_HINT_CODE(h);
379 }
380
381 STATIC U32 lt_hash = 0;
382
383 STATIC SV *lt_hint(pTHX) {
384 #define lt_hint() lt_hint(aTHX)
385  SV *hint;
386 #ifdef cop_hints_fetch_pvn
387  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
388 #elif LT_HAS_PERL(5, 9, 5)
389  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
390                                        NULL,
391                                        __PACKAGE__, __PACKAGE_LEN__,
392                                        0,
393                                        lt_hash);
394 #else
395  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
396  if (!val)
397   return 0;
398  hint = *val;
399 #endif
400  return lt_detag(hint);
401 }
402
403 /* ... op => info map ...................................................... */
404
405 #define PTABLE_NAME        ptable_map
406 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
407
408 #include "ptable.h"
409
410 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
411 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
412 #define ptable_map_delete(T, K)   ptable_map_delete(aPTBLMS_ (T), (K))
413
414 STATIC ptable *lt_op_map = NULL;
415
416 #ifdef USE_ITHREADS
417
418 STATIC perl_mutex lt_op_map_mutex;
419
420 #define LT_LOCK(M)   MUTEX_LOCK(M)
421 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
422
423 #else /* USE_ITHREADS */
424
425 #define LT_LOCK(M)
426 #define LT_UNLOCK(M)
427
428 #endif /* !USE_ITHREADS */
429
430 typedef struct {
431 #ifdef MULTIPLICITY
432  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
433  char *buf;
434 #else /* MULTIPLICITY */
435  SV *orig_pkg;
436  SV *type_pkg;
437  SV *type_meth;
438 #endif /* !MULTIPLICITY */
439  OP *(*old_pp)(pTHX);
440 } lt_op_info;
441
442 STATIC void lt_op_info_call(pTHX_ const lt_op_info *oi, SV *sv) {
443 #define lt_op_info_call(O, S) lt_op_info_call(aTHX_ (O), (S))
444  SV *orig_pkg, *type_pkg, *type_meth;
445  int items;
446  dSP;
447
448  ENTER;
449  SAVETMPS;
450
451 #ifdef MULTIPLICITY
452  {
453   STRLEN op_len = oi->orig_pkg_len, tp_len = oi->type_pkg_len;
454   char *buf = oi->buf;
455   orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
456   SvREADONLY_on(orig_pkg);
457   buf      += op_len;
458   type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
459   SvREADONLY_on(type_pkg);
460   buf      += tp_len;
461   type_meth = sv_2mortal(newSVpvn(buf, oi->type_meth_len));
462   SvREADONLY_on(type_meth);
463  }
464 #else /* MULTIPLICITY */
465  orig_pkg  = oi->orig_pkg;
466  type_pkg  = oi->type_pkg;
467  type_meth = oi->type_meth;
468 #endif /* !MULTIPLICITY */
469
470  PUSHMARK(SP);
471  EXTEND(SP, 3);
472  PUSHs(type_pkg);
473  PUSHs(sv);
474  PUSHs(orig_pkg);
475  PUTBACK;
476
477  items = call_sv(type_meth, G_ARRAY | G_METHOD);
478
479  SPAGAIN;
480  switch (items) {
481   case 0:
482    break;
483   case 1:
484    sv_setsv(sv, POPs);
485    break;
486   default:
487    croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
488  }
489  PUTBACK;
490
491  FREETMPS;
492  LEAVE;
493
494  return;
495 }
496
497 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp)(pTHX)) {
498 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
499  lt_op_info *oi;
500
501  LT_LOCK(&lt_op_map_mutex);
502
503  if (!(oi = ptable_fetch(lt_op_map, o))) {
504   oi = PerlMemShared_malloc(sizeof *oi);
505   ptable_map_store(lt_op_map, o, oi);
506 #ifdef MULTIPLICITY
507   oi->buf      = NULL;
508   oi->buf_size = 0;
509 #else /* MULTIPLICITY */
510  } else {
511   SvREFCNT_dec(oi->orig_pkg);
512   SvREFCNT_dec(oi->type_pkg);
513   SvREFCNT_dec(oi->type_meth);
514 #endif /* !MULTIPLICITY */
515  }
516
517 #ifdef MULTIPLICITY
518  {
519   STRLEN op_len       = SvCUR(orig_pkg);
520   STRLEN tp_len       = SvCUR(type_pkg);
521   STRLEN tm_len       = SvCUR(type_meth);
522   STRLEN new_buf_size = op_len + tp_len + tm_len;
523   char *buf;
524   if (new_buf_size > oi->buf_size) {
525    PerlMemShared_free(oi->buf);
526    oi->buf      = PerlMemShared_malloc(new_buf_size);
527    oi->buf_size = new_buf_size;
528   }
529   buf  = oi->buf;
530   Copy(SvPVX(orig_pkg),  buf, op_len, char);
531   buf += op_len;
532   Copy(SvPVX(type_pkg),  buf, tp_len, char);
533   buf += tp_len;
534   Copy(SvPVX(type_meth), buf, tm_len, char);
535   oi->orig_pkg_len  = op_len;
536   oi->type_pkg_len  = tp_len;
537   oi->type_meth_len = tm_len;
538   SvREFCNT_dec(orig_pkg);
539   SvREFCNT_dec(type_pkg);
540   SvREFCNT_dec(type_meth);
541  }
542 #else /* MULTIPLICITY */
543  oi->orig_pkg  = orig_pkg;
544  oi->type_pkg  = type_pkg;
545  oi->type_meth = type_meth;
546 #endif /* !MULTIPLICITY */
547
548  oi->old_pp = old_pp;
549
550  LT_UNLOCK(&lt_op_map_mutex);
551 }
552
553 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
554  const lt_op_info *val;
555
556  LT_LOCK(&lt_op_map_mutex);
557
558  val = ptable_fetch(lt_op_map, o);
559  if (val) {
560   *oi = *val;
561   val = oi;
562  }
563
564  LT_UNLOCK(&lt_op_map_mutex);
565
566  return val;
567 }
568
569 STATIC void lt_map_delete(pTHX_ const OP *o) {
570 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
571  LT_LOCK(&lt_op_map_mutex);
572
573  ptable_map_delete(lt_op_map, o);
574
575  LT_UNLOCK(&lt_op_map_mutex);
576 }
577
578 /* --- Hooks --------------------------------------------------------------- */
579
580 /* ... Our pp_padsv ........................................................ */
581
582 STATIC OP *lt_pp_padsv(pTHX) {
583  lt_op_info oi;
584
585  if (lt_map_fetch(PL_op, &oi)) {
586   dTARGET;
587   lt_op_info_call(&oi, TARG);
588   return oi.old_pp(aTHX);
589  }
590
591  return PL_op->op_ppaddr(aTHX);
592 }
593
594 /* ... Our ck_pad{any,sv} .................................................. */
595
596 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
597  * function, but are instead manually mutated from a padany. So we store
598  * the op entry in the op map in the padany check function, and we set their
599  * op_ppaddr member in our peephole optimizer replacement below. */
600
601 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
602
603 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
604  HV *stash;
605  SV *code;
606
607  o = lt_old_ck_padany(aTHX_ o);
608
609  stash = PL_in_my_stash;
610  if (stash && (code = lt_hint())) {
611   dMY_CXT;
612   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
613   SV *orig_meth = MY_CXT.default_meth;
614   SV *type_pkg  = NULL;
615   SV *type_meth = NULL;
616   int items;
617
618   dSP;
619
620   SvREADONLY_on(orig_pkg);
621
622   ENTER;
623   SAVETMPS;
624
625   PUSHMARK(SP);
626   EXTEND(SP, 2);
627   PUSHs(orig_pkg);
628   PUSHs(orig_meth);
629   PUTBACK;
630
631   items = call_sv(code, G_ARRAY);
632
633   SPAGAIN;
634   if (items > 2)
635    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
636   if (items == 0) {
637    SvREFCNT_dec(orig_pkg);
638    FREETMPS;
639    LEAVE;
640    goto skip;
641   } else {
642    SV *rsv;
643    if (items > 1) {
644     rsv = POPs;
645     if (SvOK(rsv)) {
646      type_meth = newSVsv(rsv);
647      SvREADONLY_on(type_meth);
648     }
649    }
650    rsv = POPs;
651    if (SvOK(rsv)) {
652     type_pkg = newSVsv(rsv);
653     SvREADONLY_on(type_pkg);
654    }
655   }
656   PUTBACK;
657
658   FREETMPS;
659   LEAVE;
660
661   if (!type_pkg) {
662    type_pkg = orig_pkg;
663    SvREFCNT_inc_simple_void_NN(orig_pkg);
664   }
665
666   if (!type_meth) {
667    type_meth = orig_meth;
668    SvREFCNT_inc_simple_void_NN(orig_meth);
669   }
670
671   lt_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
672  } else {
673 skip:
674   lt_map_delete(o);
675  }
676
677  return o;
678 }
679
680 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
681
682 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
683  lt_map_delete(o);
684
685  return lt_old_ck_padsv(aTHX_ o);
686 }
687
688 /* ... Our peephole optimizer .............................................. */
689
690 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
691
692 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
693 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
694  for (; o; o = o->op_next) {
695   lt_op_info *oi = NULL;
696
697   if (ptable_fetch(seen, o))
698    break;
699   ptable_seen_store(seen, o, o);
700
701   switch (o->op_type) {
702    case OP_PADSV:
703     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
704      LT_LOCK(&lt_op_map_mutex);
705      oi = ptable_fetch(lt_op_map, o);
706      if (oi) {
707       oi->old_pp   = o->op_ppaddr;
708       o->op_ppaddr = lt_pp_padsv;
709      }
710      LT_UNLOCK(&lt_op_map_mutex);
711     }
712     break;
713 #if !LT_HAS_RPEEP
714    case OP_MAPWHILE:
715    case OP_GREPWHILE:
716    case OP_AND:
717    case OP_OR:
718    case OP_ANDASSIGN:
719    case OP_ORASSIGN:
720    case OP_COND_EXPR:
721    case OP_RANGE:
722 # if LT_HAS_PERL(5, 10, 0)
723    case OP_ONCE:
724    case OP_DOR:
725    case OP_DORASSIGN:
726 # endif
727     lt_peep_rec(cLOGOPo->op_other);
728     break;
729    case OP_ENTERLOOP:
730    case OP_ENTERITER:
731     lt_peep_rec(cLOOPo->op_redoop);
732     lt_peep_rec(cLOOPo->op_nextop);
733     lt_peep_rec(cLOOPo->op_lastop);
734     break;
735 # if LT_HAS_PERL(5, 9, 5)
736    case OP_SUBST:
737     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
738     break;
739 # else
740    case OP_QR:
741    case OP_MATCH:
742    case OP_SUBST:
743     lt_peep_rec(cPMOPo->op_pmreplstart);
744     break;
745 # endif
746 #endif /* !LT_HAS_RPEEP */
747    default:
748     break;
749   }
750  }
751 }
752
753 STATIC void lt_peep(pTHX_ OP *o) {
754  dMY_CXT;
755  ptable *seen = MY_CXT.seen;
756
757  lt_old_peep(aTHX_ o);
758
759  ptable_seen_clear(seen);
760  lt_peep_rec(o);
761  ptable_seen_clear(seen);
762 }
763
764 /* --- Interpreter setup/teardown ------------------------------------------ */
765
766
767 STATIC U32 lt_initialized = 0;
768
769 STATIC void lt_teardown(pTHX_ void *root) {
770  if (!lt_initialized)
771   return;
772
773 #if LT_MULTIPLICITY
774  if (aTHX != root)
775   return;
776 #endif
777
778  {
779   dMY_CXT;
780 #if LT_THREADSAFE
781   ptable_hints_free(MY_CXT.tbl);
782 #endif
783   ptable_seen_free(MY_CXT.seen);
784   SvREFCNT_dec(MY_CXT.default_meth);
785  }
786
787  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
788  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
789
790 #if LT_HAS_RPEEP
791  PL_rpeepp   = lt_old_peep;
792 #else
793  PL_peepp    = lt_old_peep;
794 #endif
795  lt_old_peep = 0;
796
797  lt_initialized = 0;
798 }
799
800 STATIC void lt_setup(pTHX) {
801 #define lt_setup() lt_setup(aTHX)
802  if (lt_initialized)
803   return;
804
805  {
806   MY_CXT_INIT;
807 #if LT_THREADSAFE
808   MY_CXT.tbl          = ptable_new();
809   MY_CXT.owner        = aTHX;
810 #endif
811   MY_CXT.seen         = ptable_new();
812   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
813   SvREADONLY_on(MY_CXT.default_meth);
814  }
815
816  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
817  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
818
819 #if LT_HAS_RPEEP
820  lt_old_peep = PL_rpeepp;
821  PL_rpeepp   = lt_peep;
822 #else
823  lt_old_peep = PL_peepp;
824  PL_peepp    = lt_peep;
825 #endif
826
827 #if LT_MULTIPLICITY
828  call_atexit(lt_teardown, aTHX);
829 #else
830  call_atexit(lt_teardown, NULL);
831 #endif
832
833  lt_initialized = 1;
834 }
835
836 STATIC U32 lt_booted = 0;
837
838 /* --- XS ------------------------------------------------------------------ */
839
840 MODULE = Lexical::Types      PACKAGE = Lexical::Types
841
842 PROTOTYPES: ENABLE
843
844 BOOT:
845 {
846  if (!lt_booted++) {
847   HV *stash;
848
849   lt_op_map = ptable_new();
850 #ifdef USE_ITHREADS
851   MUTEX_INIT(&lt_op_map_mutex);
852 #endif
853
854   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
855
856   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
857   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
858   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
859  }
860
861  lt_setup();
862 }
863
864 #if LT_THREADSAFE
865
866 void
867 CLONE(...)
868 PROTOTYPE: DISABLE
869 PREINIT:
870  ptable *t;
871  ptable *s;
872  SV     *cloned_default_meth;
873 PPCODE:
874  {
875   {
876    lt_ptable_clone_ud ud;
877    dMY_CXT;
878
879    t = ptable_new();
880    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
881    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
882    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
883    lt_ptable_clone_ud_deinit(ud);
884   }
885   s = ptable_new();
886  }
887  {
888   MY_CXT_CLONE;
889   MY_CXT.tbl          = t;
890   MY_CXT.owner        = aTHX;
891   MY_CXT.seen         = s;
892   MY_CXT.default_meth = cloned_default_meth;
893  }
894  reap(3, lt_thread_cleanup, NULL);
895  XSRETURN(0);
896
897 #endif
898
899 SV *
900 _tag(SV *value)
901 PROTOTYPE: $
902 CODE:
903  RETVAL = lt_tag(value);
904 OUTPUT:
905  RETVAL