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 /* ... Thread safety and multiplicity ...................................... */
44 #ifndef LT_MULTIPLICITY
45 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
46 # define LT_MULTIPLICITY 1
48 # define LT_MULTIPLICITY 0
51 #if LT_MULTIPLICITY && !defined(tTHX)
52 # define tTHX PerlInterpreter*
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
58 # define MY_CXT_CLONE \
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))
65 # define LT_THREADSAFE 0
67 # define dMY_CXT dNOOP
69 # define MY_CXT lt_globaldata
71 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
73 # define MY_CXT_INIT NOOP
75 # define MY_CXT_CLONE NOOP
86 /* --- Helpers ------------------------------------------------------------- */
88 /* ... Thread-safe hints ................................................... */
90 #if LT_WORKAROUND_REQUIRE_PROPAGATION
97 #define LT_HINT_STRUCT 1
99 #define LT_HINT_CODE(H) ((H)->code)
101 #define LT_HINT_FREE(H) { \
102 lt_hint_t *h = (H); \
103 SvREFCNT_dec(h->code); \
104 PerlMemShared_free(h); \
107 #else /* LT_WORKAROUND_REQUIRE_PROPAGATION */
109 typedef SV lt_hint_t;
111 #define LT_HINT_STRUCT 0
113 #define LT_HINT_CODE(H) (H)
115 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
117 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
121 #define PTABLE_NAME ptable_hints
122 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
131 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
132 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
134 #endif /* LT_THREADSAFE */
136 /* ... Global data ......................................................... */
138 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
142 ptable *tbl; /* It really is a ptable_hints */
146 OP * (*pp_padsv_saved)(pTHX);
151 /* ... Cloning global data ................................................. */
155 STATIC SV *lt_clone(pTHX_ SV *sv, tTHX owner) {
156 #define lt_clone(S, O) lt_clone(aTHX_ (S), (O))
161 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
164 param.stashes = stashes;
166 param.proto_perl = owner;
168 dupsv = sv_dup(sv, ¶m);
172 SvREFCNT_dec(stashes);
175 return SvREFCNT_inc(dupsv);
178 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
180 lt_hint_t *h1 = ent->val;
183 if (ud->owner == aTHX)
188 h2 = PerlMemShared_malloc(sizeof *h2);
189 h2->code = lt_clone(h1->code, ud->owner);
190 SvREFCNT_inc(h2->code);
191 #if LT_WORKAROUND_REQUIRE_PROPAGATION
192 h2->requires = h1->requires;
195 #else /* LT_HINT_STRUCT */
197 h2 = lt_clone(h1, ud->owner);
200 #endif /* !LT_HINT_STRUCT */
202 ptable_hints_store(ud->tbl, ent->key, h2);
205 STATIC void lt_thread_cleanup(pTHX_ void *);
207 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
213 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
217 PerlMemShared_free(level);
218 ptable_hints_free(MY_CXT.tbl);
222 #endif /* LT_THREADSAFE */
224 /* ... Hint tags ........................................................... */
226 STATIC SV *lt_tag(pTHX_ SV *value) {
227 #define lt_tag(V) lt_tag(aTHX_ (V))
234 if (SvTYPE(value) >= SVt_PVCV) {
236 SvREFCNT_inc_simple_NN(code);
241 h = PerlMemShared_malloc(sizeof *h);
244 #if LT_WORKAROUND_REQUIRE_PROPAGATION
249 for (si = PL_curstackinfo; si; si = si->si_prev) {
252 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
253 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
255 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
260 h->requires = requires;
262 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
264 #else /* LT_HINT_STRUCT */
266 #endif /* !LT_HINT_STRUCT */
269 /* We only need for the key to be an unique tag for looking up the value later.
270 * Allocated memory provides convenient unique identifiers, so that's why we
271 * use the hint as the key itself. */
272 ptable_hints_store(MY_CXT.tbl, h, h);
273 #endif /* LT_THREADSAFE */
275 return newSViv(PTR2IV(h));
278 STATIC SV *lt_detag(pTHX_ const SV *hint) {
279 #define lt_detag(H) lt_detag(aTHX_ (H))
283 if (!(hint && SvIOK(hint)))
286 h = INT2PTR(lt_hint_t *, SvIVX(hint));
288 h = ptable_fetch(MY_CXT.tbl, h);
289 #endif /* LT_THREADSAFE */
291 #if LT_WORKAROUND_REQUIRE_PROPAGATION
296 for (si = PL_curstackinfo; si; si = si->si_prev) {
299 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
300 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
302 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
303 && ++requires > h->requires)
308 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
310 return LT_HINT_CODE(h);
313 STATIC U32 lt_hash = 0;
315 STATIC SV *lt_hint(pTHX) {
316 #define lt_hint() lt_hint(aTHX)
318 #if LT_HAS_PERL(5, 9, 5)
319 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
321 __PACKAGE__, __PACKAGE_LEN__,
325 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
330 return lt_detag(hint);
333 /* ... op => info map ...................................................... */
335 #define PTABLE_NAME ptable_map
336 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
340 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
341 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
343 STATIC ptable *lt_op_map = NULL;
346 STATIC perl_mutex lt_op_map_mutex;
351 STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
353 #else /* MULTIPLICITY */
357 #endif /* !MULTIPLICITY */
358 OP *(*pp_padsv)(pTHX);
361 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
362 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
366 MUTEX_LOCK(<_op_map_mutex);
369 if (!(oi = ptable_fetch(lt_op_map, o))) {
370 oi = PerlMemShared_malloc(sizeof *oi);
371 ptable_map_store(lt_op_map, o, oi);
375 #else /* MULTIPLICITY */
377 SvREFCNT_dec(oi->orig_pkg);
378 SvREFCNT_dec(oi->type_pkg);
379 SvREFCNT_dec(oi->type_meth);
380 #endif /* !MULTIPLICITY */
385 STRLEN op_len = SvCUR(orig_pkg);
386 STRLEN tp_len = SvCUR(type_pkg);
387 STRLEN tm_len = SvCUR(type_meth);
388 STRLEN new_buf_size = op_len + tp_len + tm_len;
390 if (new_buf_size > oi->buf_size) {
391 PerlMemShared_free(oi->buf);
392 oi->buf = PerlMemShared_malloc(new_buf_size);
393 oi->buf_size = new_buf_size;
396 Copy(SvPVX(orig_pkg), buf, op_len, char);
398 Copy(SvPVX(type_pkg), buf, tp_len, char);
400 Copy(SvPVX(type_meth), buf, tm_len, char);
401 oi->orig_pkg_len = op_len;
402 oi->type_pkg_len = tp_len;
403 oi->type_meth_len = tm_len;
404 SvREFCNT_dec(orig_pkg);
405 SvREFCNT_dec(type_pkg);
406 SvREFCNT_dec(type_meth);
408 #else /* MULTIPLICITY */
409 oi->orig_pkg = orig_pkg;
410 oi->type_pkg = type_pkg;
411 oi->type_meth = type_meth;
412 #endif /* !MULTIPLICITY */
414 oi->pp_padsv = pp_padsv;
417 MUTEX_UNLOCK(<_op_map_mutex);
421 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
422 const lt_op_info *val;
425 MUTEX_LOCK(<_op_map_mutex);
428 val = ptable_fetch(lt_op_map, o);
435 MUTEX_UNLOCK(<_op_map_mutex);
441 STATIC void lt_map_delete(pTHX_ const OP *o) {
442 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
444 MUTEX_LOCK(<_op_map_mutex);
447 ptable_map_store(lt_op_map, o, NULL);
450 MUTEX_UNLOCK(<_op_map_mutex);
454 /* --- Hooks --------------------------------------------------------------- */
456 /* ... Our pp_padsv ........................................................ */
458 STATIC OP *lt_pp_padsv(pTHX) {
461 if ((PL_op->op_private & OPpLVAL_INTRO) && lt_map_fetch(PL_op, &oi)) {
462 PADOFFSET targ = PL_op->op_targ;
463 SV *sv = PAD_SVl(targ);
466 SV *orig_pkg, *type_pkg, *type_meth;
475 STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
477 orig_pkg = sv_2mortal(newSVpvn(buf, op_len));
478 SvREADONLY_on(orig_pkg);
480 type_pkg = sv_2mortal(newSVpvn(buf, tp_len));
481 SvREADONLY_on(type_pkg);
483 type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
484 SvREADONLY_on(type_meth);
486 #else /* MULTIPLICITY */
487 orig_pkg = oi.orig_pkg;
488 type_pkg = oi.type_pkg;
489 type_meth = oi.type_meth;
490 #endif /* !MULTIPLICITY */
499 items = call_sv(type_meth, G_ARRAY | G_METHOD);
509 croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
517 return CALL_FPTR(oi.pp_padsv)(aTHX);
520 return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX);
523 STATIC void lt_pp_padsv_save(pMY_CXT) {
524 #define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT)
525 if (MY_CXT.pp_padsv_saved)
528 MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV];
529 PL_ppaddr[OP_PADSV] = lt_pp_padsv;
532 STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) {
533 #define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O))
534 OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved;
539 if (o->op_ppaddr == lt_pp_padsv)
540 o->op_ppaddr = saved;
542 PL_ppaddr[OP_PADSV] = saved;
543 MY_CXT.pp_padsv_saved = 0;
546 /* ... Our ck_pad{any,sv} .................................................. */
548 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
549 * function, but are instead manually mutated from a PADANY. This is why we set
550 * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
551 * their pp_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
552 * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
553 * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
556 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
558 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
563 lt_pp_padsv_restore(o);
565 o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o);
567 stash = PL_in_my_stash;
568 if (stash && (code = lt_hint())) {
569 SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
570 SV *orig_meth = MY_CXT.default_meth;
572 SV *type_meth = NULL;
577 SvREADONLY_on(orig_pkg);
588 items = call_sv(code, G_ARRAY);
592 croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
594 SvREFCNT_dec(orig_pkg);
601 type_meth = newSVsv(rsv);
602 SvREADONLY_on(type_meth);
607 type_pkg = newSVsv(rsv);
608 SvREADONLY_on(type_pkg);
618 SvREFCNT_inc(orig_pkg);
622 type_meth = orig_meth;
623 SvREFCNT_inc(orig_meth);
628 lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved);
637 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
639 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
642 lt_pp_padsv_restore(o);
646 return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
649 STATIC U32 lt_initialized = 0;
651 /* --- XS ------------------------------------------------------------------ */
653 MODULE = Lexical::Types PACKAGE = Lexical::Types
659 if (!lt_initialized++) {
664 MY_CXT.tbl = ptable_new();
667 MY_CXT.pp_padsv_saved = 0;
668 MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
669 SvREADONLY_on(MY_CXT.default_meth);
671 lt_op_map = ptable_new();
673 MUTEX_INIT(<_op_map_mutex);
676 PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
678 lt_old_ck_padany = PL_check[OP_PADANY];
679 PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
680 lt_old_ck_padsv = PL_check[OP_PADSV];
681 PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_ck_padsv);
683 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
684 newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
696 SV *cloned_default_meth;
701 ud.tbl = t = ptable_new();
702 ud.owner = MY_CXT.owner;
703 ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud);
704 cloned_default_meth = lt_clone(MY_CXT.default_meth, MY_CXT.owner);
710 MY_CXT.pp_padsv_saved = 0;
711 MY_CXT.default_meth = cloned_default_meth;
714 level = PerlMemShared_malloc(sizeof *level);
717 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
727 RETVAL = lt_tag(value);