X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Upper.xs;h=24e3f0a2e09d5dd9029ab23e6d6022121a345b2b;hb=fe6605581cbe68b5935e72c98a8a685379d1f320;hp=42f457e4302a5f46294c457612e79477aafb1634;hpb=939954e5645f79398208b529b5e3ed189224a07a;p=perl%2Fmodules%2FScope-Upper.git diff --git a/Upper.xs b/Upper.xs index 42f457e..24e3f0a 100644 --- a/Upper.xs +++ b/Upper.xs @@ -69,6 +69,7 @@ #endif #define SU_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S)))))) +#define SU_HAS_PERL_EXACT(R, V, S) ((PERL_REVISION == (R)) && (PERL_VERSION == (V)) && (PERL_SUBVERSION == (S))) /* --- Threads and multiplicity -------------------------------------------- */ @@ -114,8 +115,61 @@ # define MY_CXT_CLONE NOOP #endif +/* --- Global data --------------------------------------------------------- */ + +#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION + +typedef struct { + char *stack_placeholder; + I32 cxix; + I32 items; + SV **savesp; + OP fakeop; +} my_cxt_t; + +START_MY_CXT + /* --- Stack manipulations ------------------------------------------------- */ +#define SU_SAVE_PLACEHOLDER() save_pptr(&MY_CXT.stack_placeholder) + +#define SU_SAVE_DESTRUCTOR_SIZE 3 +#define SU_SAVE_PLACEHOLDER_SIZE 3 + +#define SU_SAVE_SCALAR_SIZE 3 + +#define SU_SAVE_ARY_SIZE 3 +#define SU_SAVE_AELEM_SIZE 4 +#ifdef SAVEADELETE +# define SU_SAVE_ADELETE_SIZE 3 +#else +# define SU_SAVE_ADELETE_SIZE SU_SAVE_DESTRUCTOR_SIZE +#endif +#if SU_SAVE_AELEM_SIZE < SU_SAVE_ADELETE_SIZE +# define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_ADELETE_SIZE +#else +# define SU_SAVE_AELEM_OR_ADELETE_SIZE SU_SAVE_AELEM_SIZE +#endif + +#define SU_SAVE_HASH_SIZE 3 +#define SU_SAVE_HELEM_SIZE 4 +#define SU_SAVE_HDELETE_SIZE 4 +#if SU_SAVE_HELEM_SIZE < SU_SAVE_HDELETE_SIZE +# define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HDELETE_SIZE +#else +# define SU_SAVE_HELEM_OR_HDELETE_SIZE SU_SAVE_HELEM_SIZE +#endif + +#define SU_SAVE_SPTR_SIZE 3 + +#if !SU_HAS_PERL(5, 8, 9) +# define SU_SAVE_GP_SIZE 6 +#elif !SU_HAS_PERL(5, 13, 0) || (SU_RELEASE && SU_HAS_PERL_EXACT(5, 13, 0)) +# define SU_SAVE_GP_SIZE 3 +#else +# define SU_SAVE_GP_SIZE 4 +#endif + #ifndef SvCANEXISTDELETE # define SvCANEXISTDELETE(sv) \ (!SvRMAGICAL(sv) \ @@ -259,11 +313,13 @@ STATIC void su_save_helem(pTHX_ HV *hv, SV *keysv, SV *val) { typedef struct { I32 depth; + I32 pad; I32 *origin; void (*handler)(pTHX_ void *); } su_ud_common; #define SU_UD_DEPTH(U) (((su_ud_common *) (U))->depth) +#define SU_UD_PAD(U) (((su_ud_common *) (U))->pad) #define SU_UD_ORIGIN(U) (((su_ud_common *) (U))->origin) #define SU_UD_HANDLER(U) (((su_ud_common *) (U))->handler) @@ -283,7 +339,6 @@ STATIC void su_call(pTHX_ void *ud_) { su_ud_reap *ud = (su_ud_reap *) ud_; #if SU_HAS_PERL(5, 9, 5) PERL_CONTEXT saved_cx; - I32 dieing = PL_op->op_type == OP_DIE; I32 cxix; #endif @@ -305,20 +360,17 @@ STATIC void su_call(pTHX_ void *ud_) { * the sub scope from call_sv, although it's still needed in our caller. */ #if SU_HAS_PERL(5, 9, 5) - if (dieing) { - if (cxstack_ix < cxstack_max) - cxix = cxstack_ix + 1; - else - cxix = Perl_cxinc(aTHX); - saved_cx = cxstack[cxix]; - } + if (cxstack_ix < cxstack_max) + cxix = cxstack_ix + 1; + else + cxix = Perl_cxinc(aTHX); + saved_cx = cxstack[cxix]; #endif call_sv(ud->cb, G_VOID); #if SU_HAS_PERL(5, 9, 5) - if (dieing) - cxstack[cxix] = saved_cx; + cxstack[cxix] = saved_cx; #endif PUTBACK; @@ -351,10 +403,18 @@ typedef struct { svtype type; } su_ud_localize; -STATIC void su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) { +#define SU_UD_LOCALIZE_FREE(U) STMT_START { \ + SvREFCNT_dec((U)->elem); \ + SvREFCNT_dec((U)->val); \ + SvREFCNT_dec((U)->sv); \ + SU_UD_FREE(U); \ +} STMT_END + +STATIC I32 su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *elem) { #define su_ud_localize_init(UD, S, V, E) su_ud_localize_init(aTHX_ (UD), (S), (V), (E)) UV deref = 0; svtype t = SVt_NULL; + I32 size; SvREFCNT_inc_simple_void(sv); @@ -365,6 +425,9 @@ STATIC void su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *e t = SvTYPE(SvRV(val)); deref = 1; } + } else if (SvROK(sv)) { + croak("Invalid %s reference as the localization target", + sv_reftype(SvRV(sv), 0)); } else { STRLEN len, l; const char *p = SvPV_const(sv, len), *s; @@ -397,11 +460,25 @@ STATIC void su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *e switch (t) { case SVt_PVAV: + size = elem ? SU_SAVE_AELEM_OR_ADELETE_SIZE + : SU_SAVE_ARY_SIZE; + deref = 0; + break; case SVt_PVHV: - case SVt_PVCV: + size = elem ? SU_SAVE_HELEM_OR_HDELETE_SIZE + : SU_SAVE_HASH_SIZE; + deref = 0; + break; case SVt_PVGV: + size = SU_SAVE_GP_SIZE; deref = 0; + break; + case SVt_PVCV: + size = SU_SAVE_SPTR_SIZE; + deref = 0; + break; default: + size = SU_SAVE_SCALAR_SIZE; break; } /* When deref is set, val isn't NULL */ @@ -410,6 +487,8 @@ STATIC void su_ud_localize_init(pTHX_ su_ud_localize *ud, SV *sv, SV *val, SV *e ud->val = val ? newSVsv(deref ? SvRV(val) : val) : NULL; ud->elem = SvREFCNT_inc(elem); ud->type = t; + + return size; } STATIC void su_localize(pTHX_ void *ud_) { @@ -425,11 +504,11 @@ STATIC void su_localize(pTHX_ void *ud_) { gv = (GV *) sv; } else { #ifdef gv_fetchsv - gv = gv_fetchsv(sv, GV_ADDMULTI, SVt_PVGV); + gv = gv_fetchsv(sv, GV_ADDMULTI, t); #else STRLEN len; const char *name = SvPV_const(sv, len); - gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, SVt_PVGV); + gv = gv_fetchpvn_flags(name, len, GV_ADDMULTI, t); #endif } @@ -475,10 +554,7 @@ STATIC void su_localize(pTHX_ void *ud_) { SvSetMagicSV((SV *) gv, val); done: - SvREFCNT_dec(ud->elem); - SvREFCNT_dec(ud->val); - SvREFCNT_dec(ud->sv); - SU_UD_FREE(ud); + SU_UD_LOCALIZE_FREE(ud); } /* --- Pop a context back -------------------------------------------------- */ @@ -522,30 +598,22 @@ STATIC void su_pop(pTHX_ void *ud) { SU_UD_DEPTH(ud) = --depth; if (depth > 0) { - I32 i = 1; - - SAVEDESTRUCTOR_X(su_pop, ud); - - /* Skip depths corresponding to scopes for which leave_scope() might not be - * called. */ - while (depth > 1 && PL_scopestack_ix >= i) { - I32 j = PL_scopestack[PL_scopestack_ix - i]; - - if (j < PL_savestack_ix) - break; - - SU_D(PerlIO_printf(Perl_debug_log, - "%p: skip scope%*cat depth=%2d scope_ix=%2d new_top=%2d >= cur_base=%2d\n", - ud, 6, ' ', depth, PL_scopestack_ix - i, j, PL_savestack_ix)); - - SU_UD_DEPTH(ud) = --depth; - - ++i; + I32 pad; + + if ((pad = SU_UD_PAD(ud))) { + dMY_CXT; + do { + SU_D(PerlIO_printf(Perl_debug_log, + "%p: push a pad slot at depth=%2d scope_ix=%2d save_ix=%2d\n", + ud, depth, PL_scopestack_ix, PL_savestack_ix)); + SU_SAVE_PLACEHOLDER(); + } while (--pad); } SU_D(PerlIO_printf(Perl_debug_log, - "%p: set destructor at depth=%2d scope_ix=%2d save_ix=%2d\n", - ud, depth, PL_scopestack_ix, PL_savestack_ix)); + "%p: push destructor at depth=%2d scope_ix=%2d save_ix=%2d\n", + ud, depth, PL_scopestack_ix, PL_savestack_ix)); + SAVEDESTRUCTOR_X(su_pop, ud); } else { SU_UD_HANDLER(ud)(aTHX_ ud); } @@ -555,28 +623,27 @@ STATIC void su_pop(pTHX_ void *ud) { ud, PL_savestack_ix, PL_scopestack[PL_scopestack_ix])); } -/* --- Global data --------------------------------------------------------- */ - -#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION - -typedef struct { - int stack_placeholder; - I32 cxix; - I32 items; - SV **savesp; - OP fakeop; -} my_cxt_t; - -START_MY_CXT - /* --- Initialize the stack and the action userdata ------------------------ */ -STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) { -#define su_init(L, U, S) su_init(aTHX_ (L), (U), (S)) - I32 i, depth = 1, *origin; +STATIC I32 su_init(pTHX_ void *ud, I32 cxix, I32 size) { +#define su_init(U, C, S) su_init(aTHX_ (U), (C), (S)) + I32 i, depth = 1, pad, offset, *origin; SU_D(PerlIO_printf(Perl_debug_log, "%p: ### init for cx %d\n", ud, cxix)); + if (size <= SU_SAVE_DESTRUCTOR_SIZE) + pad = 0; + else { + I32 extra = size - SU_SAVE_DESTRUCTOR_SIZE; + pad = extra / SU_SAVE_PLACEHOLDER_SIZE; + if (extra % SU_SAVE_PLACEHOLDER_SIZE) + ++pad; + } + offset = SU_SAVE_DESTRUCTOR_SIZE + SU_SAVE_PLACEHOLDER_SIZE * pad; + + SU_D(PerlIO_printf(Perl_debug_log, "%p: size=%d pad=%d offset=%d\n", + ud, size, pad, offset)); + for (i = cxstack_ix; i > cxix; --i) { PERL_CONTEXT *cx = cxstack + i; switch (CxTYPE(cx)) { @@ -610,7 +677,7 @@ STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) { break; } } - SU_D(PerlIO_printf(Perl_debug_log, "%p: depth is %d\n", ud, depth)); + SU_D(PerlIO_printf(Perl_debug_log, "%p: going down to depth %d\n", ud, depth)); Newx(origin, depth + 1, I32); origin[0] = PL_scopestack[PL_scopestack_ix - depth]; @@ -618,26 +685,30 @@ STATIC I32 su_init(pTHX_ I32 cxix, void *ud, I32 size) { for (i = depth - 1; i >= 1; --i) { I32 j = PL_scopestack_ix - i; origin[depth - i] = PL_scopestack[j]; - PL_scopestack[j] += 3; + PL_scopestack[j] += offset; } origin[depth] = PL_savestack_ix; SU_UD_ORIGIN(ud) = origin; SU_UD_DEPTH(ud) = depth; - - SU_D(PerlIO_printf(Perl_debug_log, - "%p: set original destructor at depth=%2d scope_ix=%2d save_ix=%2d\n", - ud, depth, PL_scopestack_ix - 1, PL_savestack_ix)); + SU_UD_PAD(ud) = pad; /* Make sure the first destructor fires by pushing enough fake slots on the * stack. */ - if (PL_savestack_ix + 3 <= PL_scopestack[PL_scopestack_ix - 1]) { + if (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE + <= PL_scopestack[PL_scopestack_ix - 1]) { dMY_CXT; do { - save_int(&MY_CXT.stack_placeholder); - } while (PL_savestack_ix + 3 <= PL_scopestack[PL_scopestack_ix - 1]); + SU_D(PerlIO_printf(Perl_debug_log, + "%p: push a fake slot at scope_ix=%2d save_ix=%2d\n", + ud, PL_scopestack_ix, PL_savestack_ix)); + SU_SAVE_PLACEHOLDER(); + } while (PL_savestack_ix + SU_SAVE_DESTRUCTOR_SIZE + <= PL_scopestack[PL_scopestack_ix - 1]); } - + SU_D(PerlIO_printf(Perl_debug_log, + "%p: push first destructor at scope_ix=%2d save_ix=%2d\n", + ud, PL_scopestack_ix, PL_savestack_ix)); SAVEDESTRUCTOR_X(su_pop, ud); SU_D({ @@ -807,7 +878,7 @@ BOOT: HV *stash; MY_CXT_INIT; - MY_CXT.stack_placeholder = 0; + MY_CXT.stack_placeholder = NULL; stash = gv_stashpv(__PACKAGE__, 1); newCONSTSUB(stash, "TOP", newSViv(0)); @@ -821,11 +892,11 @@ BOOT: void CLONE(...) PROTOTYPE: DISABLE -CODE: - PERL_UNUSED_VAR(items); +PPCODE: { MY_CXT_CLONE; } + XSRETURN(0); #endif /* SU_THREADSAFE */ @@ -981,56 +1052,55 @@ CODE: SU_UD_ORIGIN(ud) = NULL; SU_UD_HANDLER(ud) = su_reap; ud->cb = newSVsv(hook); - su_init(cxix, ud, 3); + su_init(ud, cxix, SU_SAVE_DESTRUCTOR_SIZE); void localize(SV *sv, SV *val, ...) PROTOTYPE: $$;$ PREINIT: I32 cxix; - I32 size = 3; + I32 size; su_ud_localize *ud; CODE: SU_GET_CONTEXT(2, 2); Newx(ud, 1, su_ud_localize); SU_UD_ORIGIN(ud) = NULL; SU_UD_HANDLER(ud) = su_localize; - su_ud_localize_init(ud, sv, val, NULL); -#if !SU_HAS_PERL(5, 8, 9) - if (ud->type >= SVt_PVGV) - size = 6; -#endif - su_init(cxix, ud, size); + size = su_ud_localize_init(ud, sv, val, NULL); + su_init(ud, cxix, size); void localize_elem(SV *sv, SV *elem, SV *val, ...) PROTOTYPE: $$$;$ PREINIT: I32 cxix; + I32 size; su_ud_localize *ud; CODE: + if (SvTYPE(sv) >= SVt_PVGV) + croak("Can't infer the element localization type from a glob and the value"); SU_GET_CONTEXT(3, 3); Newx(ud, 1, su_ud_localize); SU_UD_ORIGIN(ud) = NULL; SU_UD_HANDLER(ud) = su_localize; - su_ud_localize_init(ud, sv, val, elem); - su_init(cxix, ud, 4); + size = su_ud_localize_init(ud, sv, val, elem); + if (ud->type != SVt_PVAV && ud->type != SVt_PVHV) { + SU_UD_LOCALIZE_FREE(ud); + croak("Can't localize an element of something that isn't an array or a hash"); + } + su_init(ud, cxix, size); void localize_delete(SV *sv, SV *elem, ...) PROTOTYPE: $$;$ PREINIT: I32 cxix; - I32 size = 4; + I32 size; su_ud_localize *ud; CODE: SU_GET_CONTEXT(2, 2); Newx(ud, 1, su_ud_localize); SU_UD_ORIGIN(ud) = NULL; SU_UD_HANDLER(ud) = su_localize; - su_ud_localize_init(ud, sv, NULL, elem); -#if !SU_HAS_PERL(5, 8, 9) - if (ud->type >= SVt_PVGV) - size = 6; -#endif - su_init(cxix, ud, size); + size = su_ud_localize_init(ud, sv, NULL, elem); + su_init(ud, cxix, size);