1 /* This file is part of the Lexical-Types Perl module.
2 * See http://search.cpan.org/dist/Lexical-Types/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "Lexical::Types"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
14 #define LT_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
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
21 # ifndef PL_in_my_stash
22 # define PL_in_my_stash PL_Iin_my_stash
26 #ifndef LT_WORKAROUND_REQUIRE_PROPAGATION
27 # define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1)
31 # define HvNAME_get(H) HvNAME(H)
35 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
38 #ifndef SvREFCNT_inc_simple_NN
39 # define SvREFCNT_inc_simple_NN SvREFCNT_inc
42 #ifndef ENTER_with_name
43 # define ENTER_with_name(N) ENTER
46 #ifndef LEAVE_with_name
47 # define LEAVE_with_name(N) LEAVE
50 /* ... Thread safety and multiplicity ...................................... */
52 #ifndef LT_MULTIPLICITY
53 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
54 # define LT_MULTIPLICITY 1
56 # define LT_MULTIPLICITY 0
59 #if LT_MULTIPLICITY && !defined(tTHX)
60 # define tTHX PerlInterpreter*
63 #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))
64 # define LT_THREADSAFE 1
66 # define MY_CXT_CLONE \
68 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
69 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
70 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
73 # define LT_THREADSAFE 0
75 # define dMY_CXT dNOOP
77 # define MY_CXT lt_globaldata
79 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
81 # define MY_CXT_INIT NOOP
83 # define MY_CXT_CLONE NOOP
94 /* --- Helpers ------------------------------------------------------------- */
96 /* ... Thread-safe hints ................................................... */
98 #if LT_WORKAROUND_REQUIRE_PROPAGATION
105 #define LT_HINT_STRUCT 1
107 #define LT_HINT_CODE(H) ((H)->code)
109 #define LT_HINT_FREE(H) { \
110 lt_hint_t *h = (H); \
111 SvREFCNT_dec(h->code); \
112 PerlMemShared_free(h); \
115 #else /* LT_WORKAROUND_REQUIRE_PROPAGATION */
117 typedef SV lt_hint_t;
119 #define LT_HINT_STRUCT 0
121 #define LT_HINT_CODE(H) (H)
123 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
125 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
129 #define PTABLE_NAME ptable_hints
130 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
139 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
140 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
142 #endif /* LT_THREADSAFE */
144 /* ... Global data ......................................................... */
146 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
150 ptable *tbl; /* It really is a ptable_hints */
154 OP * (*pp_padsv_saved)(pTHX);
159 /* ... Cloning global data ................................................. */
163 STATIC SV *lt_clone(pTHX_ SV *sv, tTHX owner) {
164 #define lt_clone(S, O) lt_clone(aTHX_ (S), (O))
169 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
172 param.stashes = stashes;
174 param.proto_perl = owner;
176 dupsv = sv_dup(sv, ¶m);
180 SvREFCNT_dec(stashes);
183 return SvREFCNT_inc(dupsv);
186 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
188 lt_hint_t *h1 = ent->val;
191 if (ud->owner == aTHX)
196 h2 = PerlMemShared_malloc(sizeof *h2);
197 h2->code = lt_clone(h1->code, ud->owner);
198 SvREFCNT_inc(h2->code);
199 #if LT_WORKAROUND_REQUIRE_PROPAGATION
200 h2->cxreq = h1->cxreq;
203 #else /* LT_HINT_STRUCT */
205 h2 = lt_clone(h1, ud->owner);
208 #endif /* !LT_HINT_STRUCT */
210 ptable_hints_store(ud->tbl, ent->key, h2);
213 STATIC void lt_thread_cleanup(pTHX_ void *);
215 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
221 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
225 PerlMemShared_free(level);
226 ptable_hints_free(MY_CXT.tbl);
230 #endif /* LT_THREADSAFE */
232 /* ... Hint tags ........................................................... */
234 #if LT_WORKAROUND_REQUIRE_PROPAGATION
235 STATIC IV lt_require_tag(pTHX) {
236 #define lt_require_tag() lt_require_tag(aTHX)
239 for (si = PL_curstackinfo; si; si = si->si_prev) {
242 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
243 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
245 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
252 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
254 STATIC SV *lt_tag(pTHX_ SV *value) {
255 #define lt_tag(V) lt_tag(aTHX_ (V))
262 if (SvTYPE(value) >= SVt_PVCV) {
264 SvREFCNT_inc_simple_NN(code);
269 h = PerlMemShared_malloc(sizeof *h);
271 # if LT_WORKAROUND_REQUIRE_PROPAGATION
272 h->cxreq = lt_require_tag();
273 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
274 #else /* LT_HINT_STRUCT */
276 #endif /* !LT_HINT_STRUCT */
279 /* We only need for the key to be an unique tag for looking up the value later.
280 * Allocated memory provides convenient unique identifiers, so that's why we
281 * use the hint as the key itself. */
282 ptable_hints_store(MY_CXT.tbl, h, h);
283 #endif /* LT_THREADSAFE */
285 return newSViv(PTR2IV(h));
288 STATIC SV *lt_detag(pTHX_ const SV *hint) {
289 #define lt_detag(H) lt_detag(aTHX_ (H))
293 if (!(hint && SvIOK(hint)))
296 h = INT2PTR(lt_hint_t *, SvIVX(hint));
298 h = ptable_fetch(MY_CXT.tbl, h);
299 #endif /* LT_THREADSAFE */
300 #if LT_WORKAROUND_REQUIRE_PROPAGATION
301 if (lt_require_tag() != h->cxreq)
303 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
305 return LT_HINT_CODE(h);
308 STATIC U32 lt_hash = 0;
310 STATIC SV *lt_hint(pTHX) {
311 #define lt_hint() lt_hint(aTHX)
313 #if LT_HAS_PERL(5, 9, 5)
314 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
316 __PACKAGE__, __PACKAGE_LEN__,
320 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
325 return lt_detag(hint);
328 /* ... op => info map ...................................................... */
330 #define PTABLE_NAME ptable_map
331 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
335 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
336 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
338 STATIC ptable *lt_op_map = NULL;
341 STATIC perl_mutex lt_op_map_mutex;
346 STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
348 #else /* MULTIPLICITY */
352 #endif /* !MULTIPLICITY */
353 OP *(*old_pp_padsv)(pTHX);
356 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp_padsv)(pTHX)) {
357 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
361 MUTEX_LOCK(<_op_map_mutex);
364 if (!(oi = ptable_fetch(lt_op_map, o))) {
365 oi = PerlMemShared_malloc(sizeof *oi);
366 ptable_map_store(lt_op_map, o, oi);
370 #else /* MULTIPLICITY */
372 SvREFCNT_dec(oi->orig_pkg);
373 SvREFCNT_dec(oi->type_pkg);
374 SvREFCNT_dec(oi->type_meth);
375 #endif /* !MULTIPLICITY */
380 STRLEN op_len = SvCUR(orig_pkg);
381 STRLEN tp_len = SvCUR(type_pkg);
382 STRLEN tm_len = SvCUR(type_meth);
383 STRLEN new_buf_size = op_len + tp_len + tm_len;
385 if (new_buf_size > oi->buf_size) {
386 PerlMemShared_free(oi->buf);
387 oi->buf = PerlMemShared_malloc(new_buf_size);
388 oi->buf_size = new_buf_size;
391 Copy(SvPVX(orig_pkg), buf, op_len, char);
393 Copy(SvPVX(type_pkg), buf, tp_len, char);
395 Copy(SvPVX(type_meth), buf, tm_len, char);
396 oi->orig_pkg_len = op_len;
397 oi->type_pkg_len = tp_len;
398 oi->type_meth_len = tm_len;
399 SvREFCNT_dec(orig_pkg);
400 SvREFCNT_dec(type_pkg);
401 SvREFCNT_dec(type_meth);
403 #else /* MULTIPLICITY */
404 oi->orig_pkg = orig_pkg;
405 oi->type_pkg = type_pkg;
406 oi->type_meth = type_meth;
407 #endif /* !MULTIPLICITY */
409 oi->old_pp_padsv = old_pp_padsv;
412 MUTEX_UNLOCK(<_op_map_mutex);
416 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
417 const lt_op_info *val;
420 MUTEX_LOCK(<_op_map_mutex);
423 val = ptable_fetch(lt_op_map, o);
430 MUTEX_UNLOCK(<_op_map_mutex);
436 STATIC void lt_map_delete(pTHX_ const OP *o) {
437 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
439 MUTEX_LOCK(<_op_map_mutex);
442 ptable_map_store(lt_op_map, o, NULL);
445 MUTEX_UNLOCK(<_op_map_mutex);
449 /* --- Hooks --------------------------------------------------------------- */
451 /* ... Our pp_padsv ........................................................ */
453 STATIC OP *lt_pp_padsv(pTHX) {
456 if ((PL_op->op_private & OPpLVAL_INTRO) && lt_map_fetch(PL_op, &oi)) {
457 PADOFFSET targ = PL_op->op_targ;
458 SV *sv = PAD_SVl(targ);
461 SV *orig_pkg, *type_pkg, *type_meth;
470 STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
472 orig_pkg = sv_2mortal(newSVpvn(buf, op_len));
473 SvREADONLY_on(orig_pkg);
475 type_pkg = sv_2mortal(newSVpvn(buf, tp_len));
476 SvREADONLY_on(type_pkg);
478 type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
479 SvREADONLY_on(type_meth);
481 #else /* MULTIPLICITY */
482 orig_pkg = oi.orig_pkg;
483 type_pkg = oi.type_pkg;
484 type_meth = oi.type_meth;
485 #endif /* !MULTIPLICITY */
494 items = call_sv(type_meth, G_ARRAY | G_METHOD);
504 croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
512 return CALL_FPTR(oi.old_pp_padsv)(aTHX);
515 return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX);
518 STATIC void lt_pp_padsv_save(pMY_CXT) {
519 #define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT)
520 if (MY_CXT.pp_padsv_saved)
523 MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV];
524 PL_ppaddr[OP_PADSV] = lt_pp_padsv;
527 STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) {
528 #define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O))
529 OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved;
534 if (o->op_ppaddr == lt_pp_padsv)
535 o->op_ppaddr = saved;
537 PL_ppaddr[OP_PADSV] = saved;
538 MY_CXT.pp_padsv_saved = 0;
541 /* ... Our ck_pad{any,sv} .................................................. */
543 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
544 * function, but are instead manually mutated from a PADANY. This is why we set
545 * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
546 * their pp_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
547 * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
548 * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
551 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
553 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
558 lt_pp_padsv_restore(o);
560 o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o);
562 stash = PL_in_my_stash;
563 if (stash && (code = lt_hint())) {
564 SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
565 SV *orig_meth = MY_CXT.default_meth;
567 SV *type_meth = NULL;
572 SvREADONLY_on(orig_pkg);
583 items = call_sv(code, G_ARRAY);
587 croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
589 SvREFCNT_dec(orig_pkg);
598 type_meth = newSVsv(rsv);
599 SvREADONLY_on(type_meth);
604 type_pkg = newSVsv(rsv);
605 SvREADONLY_on(type_pkg);
615 SvREFCNT_inc(orig_pkg);
619 type_meth = orig_meth;
620 SvREFCNT_inc(orig_meth);
625 lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved);
634 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
636 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
639 lt_pp_padsv_restore(o);
643 return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
646 STATIC U32 lt_initialized = 0;
648 STATIC void lt_teardown(pTHX_ void *root) {
660 ptable_hints_free(MY_CXT.tbl);
662 SvREFCNT_dec(MY_CXT.default_meth);
664 PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_old_ck_padany);
665 lt_old_ck_padany = 0;
666 PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_old_ck_padsv);
672 STATIC void lt_setup(pTHX) {
673 #define lt_setup() lt_setup(aTHX)
680 MY_CXT.tbl = ptable_new();
683 MY_CXT.pp_padsv_saved = 0;
684 MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
685 SvREADONLY_on(MY_CXT.default_meth);
688 lt_old_ck_padany = PL_check[OP_PADANY];
689 PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
690 lt_old_ck_padsv = PL_check[OP_PADSV];
691 PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_ck_padsv);
694 call_atexit(lt_teardown, aTHX);
696 call_atexit(lt_teardown, NULL);
702 STATIC U32 lt_booted = 0;
704 /* --- XS ------------------------------------------------------------------ */
706 MODULE = Lexical::Types PACKAGE = Lexical::Types
715 lt_op_map = ptable_new();
717 MUTEX_INIT(<_op_map_mutex);
720 PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
722 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
723 newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
737 SV *cloned_default_meth;
742 ud.tbl = t = ptable_new();
743 ud.owner = MY_CXT.owner;
744 ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud);
745 cloned_default_meth = lt_clone(MY_CXT.default_meth, MY_CXT.owner);
751 MY_CXT.pp_padsv_saved = 0;
752 MY_CXT.default_meth = cloned_default_meth;
755 level = PerlMemShared_malloc(sizeof *level);
757 LEAVE_with_name("sub");
758 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
759 ENTER_with_name("sub");
769 RETVAL = lt_tag(value);