From: Vincent Pit Date: Tue, 30 Jun 2009 22:14:56 +0000 (+0200) Subject: Work around the hints propagation in requires on perl <= 5.10.0 X-Git-Tag: v0.07~2 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FLexical-Types.git;a=commitdiff_plain;h=28f52395e07f990ec77ca6b87352811f2a9c5f79 Work around the hints propagation in requires on perl <= 5.10.0 --- diff --git a/MANIFEST b/MANIFEST index 89831d7..f14794d 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,6 +14,7 @@ t/12-integrate.t t/13-padsv.t t/14-ro.t t/15-constants.t +t/16-scope.t t/20-object.t t/21-tie.t t/22-magic.t @@ -23,3 +24,5 @@ t/91-pod.t t/92-pod-coverage.t t/95-portability-files.t t/99-kwalitee.t +t/lib/Lexical/Types/TestRequired1.pm +t/lib/Lexical/Types/TestRequired2.pm diff --git a/Types.xs b/Types.xs index bcf5fa1..221319f 100644 --- a/Types.xs +++ b/Types.xs @@ -23,6 +23,10 @@ # endif #endif +#ifndef LT_WORKAROUND_REQUIRE_PROPAGATION +# define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1) +#endif + #ifndef HvNAME_get # define HvNAME_get(H) HvNAME(H) #endif @@ -59,16 +63,35 @@ # endif #else # define LT_THREADSAFE 0 +# undef dMY_CXT +# define dMY_CXT dNOOP +# undef MY_CXT +# define MY_CXT lt_globaldata +# undef START_MY_CXT +# define START_MY_CXT STATIC my_cxt_t MY_CXT; +# undef MY_CXT_INIT +# define MY_CXT_INIT NOOP +# undef MY_CXT_CLONE +# define MY_CXT_CLONE NOOP #endif /* --- Helpers ------------------------------------------------------------- */ /* ... Thread-safe hints ................................................... */ -#if LT_THREADSAFE +/* If any of those is true, we need to store the hint in a global table. */ + +#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION + +typedef struct { + SV *code; +#if LT_WORKAROUND_REQUIRE_PROPAGATION + UV requires; +#endif +} lt_hint_t; #define PTABLE_NAME ptable_hints -#define PTABLE_VAL_FREE(V) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V) +#define PTABLE_VAL_FREE(V) { lt_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); } #define pPTBL pTHX #define pPTBL_ pTHX_ @@ -83,31 +106,39 @@ #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION typedef struct { - ptable *tbl; + ptable *tbl; /* It really is a ptable_hints */ +#if LT_THREADSAFE tTHX owner; +#endif } my_cxt_t; START_MY_CXT +#if LT_THREADSAFE + STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) { - my_cxt_t *ud = ud_; - SV *val = ent->val; + my_cxt_t *ud = ud_; + lt_hint_t *h1 = ent->val; + lt_hint_t *h2 = PerlMemShared_malloc(sizeof *h2); + + *h2 = *h1; if (ud->owner != aTHX) { + SV *val = h1->code; CLONE_PARAMS param; AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL; param.stashes = stashes; param.flags = 0; param.proto_perl = ud->owner; - val = sv_dup(val, ¶m); + h2->code = sv_dup(val, ¶m); if (stashes) { av_undef(stashes); SvREFCNT_dec(stashes); } } - ptable_hints_store(ud->tbl, ent->key, val); - SvREFCNT_inc(val); + ptable_hints_store(ud->tbl, ent->key, h2); + SvREFCNT_inc(h2->code); } STATIC void lt_thread_cleanup(pTHX_ void *); @@ -127,35 +158,76 @@ STATIC void lt_thread_cleanup(pTHX_ void *ud) { } } +#endif /* LT_THREADSAFE */ + STATIC SV *lt_tag(pTHX_ SV *value) { #define lt_tag(V) lt_tag(aTHX_ (V)) + lt_hint_t *h; dMY_CXT; value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL; + + h = PerlMemShared_malloc(sizeof *h); + h->code = SvREFCNT_inc(value); + +#if LT_WORKAROUND_REQUIRE_PROPAGATION + { + const PERL_SI *si; + UV requires = 0; + + for (si = PL_curstackinfo; si; si = si->si_prev) { + I32 cxix; + + for (cxix = si->si_cxix; cxix >= 0; --cxix) { + const PERL_CONTEXT *cx = si->si_cxstack + cxix; + + if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE) + ++requires; + } + } + + h->requires = requires; + } +#endif + /* We only need for the key to be an unique tag for looking up the value later. * Allocated memory provides convenient unique identifiers, so that's why we * use the value pointer as the key itself. */ - ptable_hints_store(MY_CXT.tbl, value, value); - SvREFCNT_inc(value); + ptable_hints_store(MY_CXT.tbl, value, h); return newSVuv(PTR2UV(value)); } STATIC SV *lt_detag(pTHX_ const SV *hint) { #define lt_detag(H) lt_detag(aTHX_ (H)) - void *tag; - SV *value; + lt_hint_t *h; + dMY_CXT; + + if (!(hint && SvOK(hint) && SvIOK(hint))) + return NULL; - if (!hint || !SvOK(hint) || !SvIOK(hint)) - croak("Wrong hint"); + h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint))); - tag = INT2PTR(void *, SvIVX(hint)); +#if LT_WORKAROUND_REQUIRE_PROPAGATION { - dMY_CXT; - value = ptable_fetch(MY_CXT.tbl, tag); + const PERL_SI *si; + UV requires = 0; + + for (si = PL_curstackinfo; si; si = si->si_prev) { + I32 cxix; + + for (cxix = si->si_cxix; cxix >= 0; --cxix) { + const PERL_CONTEXT *cx = si->si_cxstack + cxix; + + if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE + && ++requires > h->requires) + return NULL; + } + } } +#endif - return value; + return h->code; } #else @@ -173,9 +245,9 @@ STATIC SV *lt_tag(pTHX_ SV *value) { return newSVuv(tag); } -#define lt_detag(H) INT2PTR(SV *, SvUVX(H)) +#define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL) -#endif /* LT_THREADSAFE */ +#endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */ STATIC U32 lt_hash = 0; @@ -194,7 +266,7 @@ STATIC SV *lt_hint(pTHX) { return 0; hint = *val; #endif - return (hint && SvOK(hint)) ? hint : NULL; + return lt_detag(hint); } /* ... op => info map ...................................................... */ @@ -362,63 +434,60 @@ STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0; STATIC OP *lt_ck_padany(pTHX_ OP *o) { HV *stash; - SV *hint; + SV *code; lt_pp_padsv_restore(o); o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o); stash = PL_in_my_stash; - if (stash && (hint = lt_hint())) { + if (stash && (code = lt_hint())) { SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash)); SV *orig_meth = lt_default_meth; SV *type_pkg = NULL; SV *type_meth = NULL; - SV *code = lt_detag(hint); - - SvREADONLY_on(orig_pkg); + int items; - if (code) { - int items; - dSP; - - ENTER; - SAVETMPS; - - PUSHMARK(SP); - EXTEND(SP, 2); - PUSHs(orig_pkg); - PUSHs(orig_meth); - PUTBACK; + dSP; - items = call_sv(code, G_ARRAY); + SvREADONLY_on(orig_pkg); - SPAGAIN; - if (items > 2) - croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); - if (items == 0) { - SvREFCNT_dec(orig_pkg); - goto skip; - } else { - SV *rsv; - if (items > 1) { - rsv = POPs; - if (SvOK(rsv)) { - type_meth = newSVsv(rsv); - SvREADONLY_on(type_meth); - } - } + ENTER; + SAVETMPS; + + PUSHMARK(SP); + EXTEND(SP, 2); + PUSHs(orig_pkg); + PUSHs(orig_meth); + PUTBACK; + + items = call_sv(code, G_ARRAY); + + SPAGAIN; + if (items > 2) + croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); + if (items == 0) { + SvREFCNT_dec(orig_pkg); + goto skip; + } else { + SV *rsv; + if (items > 1) { rsv = POPs; if (SvOK(rsv)) { - type_pkg = newSVsv(rsv); - SvREADONLY_on(type_pkg); + type_meth = newSVsv(rsv); + SvREADONLY_on(type_meth); } } - PUTBACK; - - FREETMPS; - LEAVE; + rsv = POPs; + if (SvOK(rsv)) { + type_pkg = newSVsv(rsv); + SvREADONLY_on(type_pkg); + } } + PUTBACK; + + FREETMPS; + LEAVE; if (!type_pkg) { type_pkg = orig_pkg; @@ -463,9 +532,11 @@ BOOT: { if (!lt_initialized++) { HV *stash; -#if LT_THREADSAFE +#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION MY_CXT_INIT; MY_CXT.tbl = ptable_new(); +#endif +#if LT_THREADSAFE MY_CXT.owner = aTHX; #endif diff --git a/lib/Lexical/Types.pm b/lib/Lexical/Types.pm index e2cc200..14f2699 100644 --- a/lib/Lexical/Types.pm +++ b/lib/Lexical/Types.pm @@ -155,7 +155,7 @@ sub import { croak "Invalid $r reference for 'as'"; } } else { - $hint = _tag(0); + $hint = _tag(sub { @_ }); } $^H |= 0x020000; diff --git a/t/16-scope.t b/t/16-scope.t new file mode 100644 index 0000000..712bec2 --- /dev/null +++ b/t/16-scope.t @@ -0,0 +1,22 @@ +#!perl -T + +use strict; +use warnings; + +use Test::More tests => (1 + 2) + (1 + 4); + +sub Int::TYPEDSCALAR { join ':', (caller 0)[1, 2] } + +our ($x, $y, $z, $t); + +use lib 't/lib'; + +{ + eval 'use Lexical::Types; use Lexical::Types::TestRequired1'; + is $@, '', 'first require test didn\'t croak prematurely'; +} + +{ + eval 'use Lexical::Types; use Lexical::Types::TestRequired2'; + is $@, '', 'second require test didn\'t croak prematurely'; +} diff --git a/t/lib/Lexical/Types/TestRequired1.pm b/t/lib/Lexical/Types/TestRequired1.pm new file mode 100644 index 0000000..a54e325 --- /dev/null +++ b/t/lib/Lexical/Types/TestRequired1.pm @@ -0,0 +1,11 @@ +package Lexical::Types::TestRequired1; + +my Int $x; +Test::More::is($x, undef, 'pragma not in use in require'); + +eval q{ + my Int $y; + Test::More::is($y, undef, 'pragma not in use in eval in require'); +}; + +1; diff --git a/t/lib/Lexical/Types/TestRequired2.pm b/t/lib/Lexical/Types/TestRequired2.pm new file mode 100644 index 0000000..eb15065 --- /dev/null +++ b/t/lib/Lexical/Types/TestRequired2.pm @@ -0,0 +1,25 @@ +package Lexical::Types::TestRequired2; + +use Lexical::Types; + +BEGIN { + delete $INC{'Lexical/Types/TestRequired1.pm'}; +} + +use lib 't/lib'; +use Lexical::Types::TestRequired1; + +my Int $x; +Test::More::is($x, 't/lib/Lexical/Types/TestRequired2.pm:' . (__LINE__-1), 'pragma in use in require'); + +eval q! + my Int $y; + my $desc = 'pragma in use in eval in require'; + if ($] < 5.009005) { + Test::More::is($y, undef, $desc); + } else { + Test::More::like($y, qr/^\(eval +\d+\):2$/, $desc); + } +!; + +1;