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))
39 # define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
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 any of those is true, we need to store the hint in a global table. */
92 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
96 #if LT_WORKAROUND_REQUIRE_PROPAGATION
101 #define PTABLE_NAME ptable_hints
102 #define PTABLE_VAL_FREE(V) { lt_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
111 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
112 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
114 #endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
116 /* ... Global data ......................................................... */
118 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
121 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
122 ptable *tbl; /* It really is a ptable_hints */
127 OP * (*pp_padsv_saved)(pTHX);
132 /* ... Cloning global data ................................................. */
136 STATIC SV *lt_clone(pTHX_ SV *sv, tTHX owner) {
137 #define lt_clone(S, O) lt_clone(aTHX_ (S), (O))
142 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
145 param.stashes = stashes;
147 param.proto_perl = owner;
149 dupsv = sv_dup(sv, ¶m);
153 SvREFCNT_dec(stashes);
156 return SvREFCNT_inc(dupsv);
159 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
161 lt_hint_t *h1 = ent->val;
162 lt_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
166 if (ud->owner != aTHX)
167 h2->code = lt_clone(h1->code, ud->owner);
169 ptable_hints_store(ud->tbl, ent->key, h2);
170 SvREFCNT_inc(h2->code);
173 STATIC void lt_thread_cleanup(pTHX_ void *);
175 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
181 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
185 PerlMemShared_free(level);
186 ptable_hints_free(MY_CXT.tbl);
190 #endif /* LT_THREADSAFE */
192 /* ... Hint tags ........................................................... */
194 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
196 STATIC SV *lt_tag(pTHX_ SV *value) {
197 #define lt_tag(V) lt_tag(aTHX_ (V))
201 value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
203 h = PerlMemShared_malloc(sizeof *h);
204 h->code = SvREFCNT_inc(value);
206 #if LT_WORKAROUND_REQUIRE_PROPAGATION
211 for (si = PL_curstackinfo; si; si = si->si_prev) {
214 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
215 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
217 if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
222 h->requires = requires;
226 /* We only need for the key to be an unique tag for looking up the value later.
227 * Allocated memory provides convenient unique identifiers, so that's why we
228 * use the value pointer as the key itself. */
229 ptable_hints_store(MY_CXT.tbl, value, h);
231 return newSVuv(PTR2UV(value));
234 STATIC SV *lt_detag(pTHX_ const SV *hint) {
235 #define lt_detag(H) lt_detag(aTHX_ (H))
239 if (!(hint && SvOK(hint) && SvIOK(hint)))
242 h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
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
256 && ++requires > h->requires)
268 STATIC SV *lt_tag(pTHX_ SV *value) {
269 #define lt_tag(V) lt_tag(aTHX_ (V))
272 if (SvOK(value) && SvROK(value)) {
281 #define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
283 #endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
285 STATIC U32 lt_hash = 0;
287 STATIC SV *lt_hint(pTHX) {
288 #define lt_hint() lt_hint(aTHX)
290 #if LT_HAS_PERL(5, 9, 5)
291 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
293 __PACKAGE__, __PACKAGE_LEN__,
297 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
302 return lt_detag(hint);
305 /* ... op => info map ...................................................... */
307 #define PTABLE_NAME ptable_map
308 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
312 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
313 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
315 STATIC ptable *lt_op_map = NULL;
318 STATIC perl_mutex lt_op_map_mutex;
323 STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
325 #else /* MULTIPLICITY */
329 #endif /* !MULTIPLICITY */
330 OP *(*pp_padsv)(pTHX);
333 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
334 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
338 MUTEX_LOCK(<_op_map_mutex);
341 if (!(oi = ptable_fetch(lt_op_map, o))) {
342 oi = PerlMemShared_malloc(sizeof *oi);
343 ptable_map_store(lt_op_map, o, oi);
347 #else /* MULTIPLICITY */
349 SvREFCNT_dec(oi->orig_pkg);
350 SvREFCNT_dec(oi->type_pkg);
351 SvREFCNT_dec(oi->type_meth);
352 #endif /* !MULTIPLICITY */
357 STRLEN op_len = SvCUR(orig_pkg);
358 STRLEN tp_len = SvCUR(type_pkg);
359 STRLEN tm_len = SvCUR(type_meth);
360 STRLEN new_buf_size = op_len + tp_len + tm_len;
362 if (new_buf_size > oi->buf_size) {
363 PerlMemShared_free(oi->buf);
364 oi->buf = PerlMemShared_malloc(new_buf_size);
365 oi->buf_size = new_buf_size;
368 Copy(SvPVX(orig_pkg), buf, op_len, char);
370 Copy(SvPVX(type_pkg), buf, tp_len, char);
372 Copy(SvPVX(type_meth), buf, tm_len, char);
373 oi->orig_pkg_len = op_len;
374 oi->type_pkg_len = tp_len;
375 oi->type_meth_len = tm_len;
376 SvREFCNT_dec(orig_pkg);
377 SvREFCNT_dec(type_pkg);
378 SvREFCNT_dec(type_meth);
380 #else /* MULTIPLICITY */
381 oi->orig_pkg = orig_pkg;
382 oi->type_pkg = type_pkg;
383 oi->type_meth = type_meth;
384 #endif /* !MULTIPLICITY */
386 oi->pp_padsv = pp_padsv;
389 MUTEX_UNLOCK(<_op_map_mutex);
393 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
394 const lt_op_info *val;
397 MUTEX_LOCK(<_op_map_mutex);
400 val = ptable_fetch(lt_op_map, o);
407 MUTEX_UNLOCK(<_op_map_mutex);
413 STATIC void lt_map_delete(pTHX_ const OP *o) {
414 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
416 MUTEX_LOCK(<_op_map_mutex);
419 ptable_map_store(lt_op_map, o, NULL);
422 MUTEX_UNLOCK(<_op_map_mutex);
426 /* --- Hooks --------------------------------------------------------------- */
428 /* ... Our pp_padsv ........................................................ */
430 STATIC OP *lt_pp_padsv(pTHX) {
433 if ((PL_op->op_private & OPpLVAL_INTRO) && lt_map_fetch(PL_op, &oi)) {
434 PADOFFSET targ = PL_op->op_targ;
435 SV *sv = PAD_SVl(targ);
438 SV *orig_pkg, *type_pkg, *type_meth;
447 STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
449 orig_pkg = sv_2mortal(newSVpvn(buf, op_len));
450 SvREADONLY_on(orig_pkg);
452 type_pkg = sv_2mortal(newSVpvn(buf, tp_len));
453 SvREADONLY_on(type_pkg);
455 type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
456 SvREADONLY_on(type_meth);
458 #else /* MULTIPLICITY */
459 orig_pkg = oi.orig_pkg;
460 type_pkg = oi.type_pkg;
461 type_meth = oi.type_meth;
462 #endif /* !MULTIPLICITY */
471 items = call_sv(type_meth, G_ARRAY | G_METHOD);
481 croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
489 return CALL_FPTR(oi.pp_padsv)(aTHX);
492 return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX);
495 STATIC void lt_pp_padsv_save(pMY_CXT) {
496 #define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT)
497 if (MY_CXT.pp_padsv_saved)
500 MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV];
501 PL_ppaddr[OP_PADSV] = lt_pp_padsv;
504 STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) {
505 #define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O))
506 OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved;
511 if (o->op_ppaddr == lt_pp_padsv)
512 o->op_ppaddr = saved;
514 PL_ppaddr[OP_PADSV] = saved;
515 MY_CXT.pp_padsv_saved = 0;
518 /* ... Our ck_pad{any,sv} .................................................. */
520 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
521 * function, but are instead manually mutated from a PADANY. This is why we set
522 * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
523 * their pp_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
524 * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
525 * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
528 STATIC SV *lt_default_meth = NULL;
530 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
532 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
537 lt_pp_padsv_restore(o);
539 o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o);
541 stash = PL_in_my_stash;
542 if (stash && (code = lt_hint())) {
543 SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
544 SV *orig_meth = lt_default_meth;
546 SV *type_meth = NULL;
551 SvREADONLY_on(orig_pkg);
562 items = call_sv(code, G_ARRAY);
566 croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
568 SvREFCNT_dec(orig_pkg);
575 type_meth = newSVsv(rsv);
576 SvREADONLY_on(type_meth);
581 type_pkg = newSVsv(rsv);
582 SvREADONLY_on(type_pkg);
592 SvREFCNT_inc(orig_pkg);
596 type_meth = orig_meth;
597 SvREFCNT_inc(orig_meth);
602 lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved);
611 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
613 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
616 lt_pp_padsv_restore(o);
620 return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
623 STATIC U32 lt_initialized = 0;
625 /* --- XS ------------------------------------------------------------------ */
627 MODULE = Lexical::Types PACKAGE = Lexical::Types
633 if (!lt_initialized++) {
637 #if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
638 MY_CXT.tbl = ptable_new();
643 MY_CXT.pp_padsv_saved = 0;
645 lt_op_map = ptable_new();
647 MUTEX_INIT(<_op_map_mutex);
650 lt_default_meth = newSVpvn("TYPEDSCALAR", 11);
651 SvREADONLY_on(lt_default_meth);
653 PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
655 lt_old_ck_padany = PL_check[OP_PADANY];
656 PL_check[OP_PADANY] = MEMBER_TO_FPTR(lt_ck_padany);
657 lt_old_ck_padsv = PL_check[OP_PADSV];
658 PL_check[OP_PADSV] = MEMBER_TO_FPTR(lt_ck_padsv);
660 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
661 newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
677 ud.tbl = t = ptable_new();
678 ud.owner = MY_CXT.owner;
679 ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud);
685 MY_CXT.pp_padsv_saved = 0;
688 level = PerlMemShared_malloc(sizeof *level);
691 SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
701 RETVAL = lt_tag(value);