From: Vincent Pit Date: Thu, 26 Nov 2009 00:40:07 +0000 (+0100) Subject: Remove all signature-related features X-Git-Tag: v0.39~17 X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=commitdiff_plain;h=8edd65482a48cda016b4677014dcb80b2b923cb1 Remove all signature-related features From now on, the wizards are only stored in a global pointer table with threaded perls. --- diff --git a/MANIFEST b/MANIFEST index 592ee8b..d4d24e8 100644 --- a/MANIFEST +++ b/MANIFEST @@ -5,6 +5,7 @@ Magic.xs Makefile.PL README lib/Variable/Magic.pm +ptable.h samples/copy.pl samples/magic.pl samples/synopsis.pl @@ -14,7 +15,6 @@ t/00-load.t t/01-import.t t/10-simple.t t/11-multiple.t -t/12-sig.t t/13-data.t t/14-callbacks.t t/15-self.t diff --git a/Magic.xs b/Magic.xs index 628bf56..6ffe42d 100644 --- a/Magic.xs +++ b/Magic.xs @@ -80,10 +80,12 @@ STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) { #define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O)) CLONE_PARAMS param; + param.stashes = NULL; /* don't need it unless sv is a PVHV */ param.flags = 0; param.proto_perl = owner; - return sv_dup(sv, ¶m); + + return SvREFCNT_inc(sv_dup(sv, ¶m)); } #endif /* VMG_THREADSAFE */ @@ -317,58 +319,22 @@ STATIC opclass vmg_opclass(const OP *o) { return OPc_BASEOP; } -/* --- Context-safe global data -------------------------------------------- */ - -#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION - -typedef struct { - HV *wizards; - HV *b__op_stashes[OPc_MAX]; -} my_cxt_t; - -START_MY_CXT - /* --- Error messages ------------------------------------------------------ */ STATIC const char vmg_invalid_wiz[] = "Invalid wizard object"; -STATIC const char vmg_invalid_sig[] = "Invalid numeric signature"; STATIC const char vmg_wrongargnum[] = "Wrong number of arguments"; -STATIC const char vmg_toomanysigs[] = "Too many magic signatures used"; STATIC const char vmg_argstorefailed[] = "Error while storing arguments"; -STATIC const char vmg_globstorefail[] = "Couldn't store global wizard information"; /* --- Signatures ---------------------------------------------------------- */ -#define SIG_MIN ((U16) 0u) -#define SIG_MAX ((U16) ((1u << 16) - 1)) -#define SIG_NBR (SIG_MAX - SIG_MIN + 1) - #define SIG_WZO ((U16) (0x3891)) #define SIG_WIZ ((U16) (0x3892)) -/* ... Generate signatures ................................................. */ - -STATIC U16 vmg_gensig(pTHX) { -#define vmg_gensig() vmg_gensig(aTHX) - U16 sig; - char buf[8]; - dMY_CXT; - - if (HvKEYS(MY_CXT.wizards) >= SIG_NBR) croak(vmg_toomanysigs); - - do { - sig = SIG_NBR * Drand01() + SIG_MIN; - } while (hv_exists(MY_CXT.wizards, buf, sprintf(buf, "%u", sig))); - - return sig; -} - /* --- MGWIZ structure ----------------------------------------------------- */ typedef struct { MGVTBL *vtbl; - U16 sig; U8 uvar; U8 opinfo; @@ -386,13 +352,241 @@ typedef struct { #if VMG_UVAR SV *cb_fetch, *cb_store, *cb_exists, *cb_delete; #endif /* VMG_UVAR */ + #if VMG_MULTIPLICITY tTHX owner; #endif /* VMG_MULTIPLICITY */ } MGWIZ; -#define MGWIZ2SV(W) (newSVuv(PTR2UV(W))) -#define SV2MGWIZ(S) (INT2PTR(MGWIZ*, SvUVX((SV *) (S)))) +STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *w) { +#define vmg_mgwiz_free(W) vmg_mgwiz_free(aTHX_ (W)) + if (!w) + return; + + if (w->cb_data) SvREFCNT_dec(SvRV(w->cb_data)); + if (w->cb_get) SvREFCNT_dec(SvRV(w->cb_get)); + if (w->cb_set) SvREFCNT_dec(SvRV(w->cb_set)); + if (w->cb_len) SvREFCNT_dec(SvRV(w->cb_len)); + if (w->cb_clear) SvREFCNT_dec(SvRV(w->cb_clear)); + if (w->cb_free) SvREFCNT_dec(SvRV(w->cb_free)); +#if MGf_COPY + if (w->cb_copy) SvREFCNT_dec(SvRV(w->cb_copy)); +#endif /* MGf_COPY */ +#if 0 /* MGf_DUP */ + if (w->cb_dup) SvREFCNT_dec(SvRV(w->cb_dup)); +#endif /* MGf_DUP */ +#if MGf_LOCAL + if (w->cb_local) SvREFCNT_dec(SvRV(w->cb_local)); +#endif /* MGf_LOCAL */ +#if VMG_UVAR + if (w->cb_fetch) SvREFCNT_dec(SvRV(w->cb_fetch)); + if (w->cb_store) SvREFCNT_dec(SvRV(w->cb_store)); + if (w->cb_exists) SvREFCNT_dec(SvRV(w->cb_exists)); + if (w->cb_delete) SvREFCNT_dec(SvRV(w->cb_delete)); +#endif /* VMG_UVAR */ + + Safefree(w->vtbl); + Safefree(w); + + return; +} + +#if VMG_THREADSAFE + +#define VMG_CLONE_CB(N) \ + z->cb_ ## N = (w->cb_ ## N) ? newRV_noinc(vmg_clone(SvRV(w->cb_ ## N), \ + w->owner)) \ + : NULL; + +STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) { +#define vmg_mgwiz_clone(W) vmg_mgwiz_clone(aTHX_ (W)) + MGVTBL *t; + MGWIZ *z; + + if (!w) + return NULL; + + Newx(t, 1, MGVTBL); + Copy(w->vtbl, t, 1, MGVTBL); + + Newx(z, 1, MGWIZ); + + z->vtbl = t; + z->uvar = w->uvar; + z->opinfo = w->opinfo; + + VMG_CLONE_CB(data); + VMG_CLONE_CB(get); + VMG_CLONE_CB(set); + VMG_CLONE_CB(len); + VMG_CLONE_CB(clear); + VMG_CLONE_CB(free); +#if MGf_COPY + VMG_CLONE_CB(copy); +#endif /* MGf_COPY */ +#if MGf_DUP + VMG_CLONE_CB(dup); +#endif /* MGf_DUP */ +#if MGf_LOCAL + VMG_CLONE_CB(local); +#endif /* MGf_LOCAL */ +#if VMG_UVAR + VMG_CLONE_CB(fetch); + VMG_CLONE_CB(store); + VMG_CLONE_CB(exists); + VMG_CLONE_CB(delete); +#endif /* VMG_UVAR */ + + z->owner = aTHX; + + return z; +} + +#endif /* VMG_THREADSAFE */ + +/* --- Context-safe global data -------------------------------------------- */ + +#if VMG_THREADSAFE + +#define PTABLE_NAME ptable +#define PTABLE_VAL_FREE(V) vmg_mgwiz_free(V) + +#define pPTBL pTHX +#define pPTBL_ pTHX_ +#define aPTBL aTHX +#define aPTBL_ aTHX_ + +#include "ptable.h" + +#define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V)) +#define ptable_clear(T) ptable_clear(aTHX_ (T)) +#define ptable_free(T) ptable_free(aTHX_ (T)) + +#endif /* VMG_THREADSAFE */ + +#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION + +typedef struct { +#if VMG_THREADSAFE + ptable *wizards; + tTHX owner; +#endif + HV *b__op_stashes[OPc_MAX]; +} my_cxt_t; + +START_MY_CXT + +#if VMG_THREADSAFE + +STATIC void vmg_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) { + my_cxt_t *ud = ud_; + MGWIZ *w; + + if (ud->owner == aTHX) + return; + + w = vmg_mgwiz_clone(ent->val); + if (w) + ptable_store(ud->wizards, ent->key, w); +} + +#endif /* VMG_THREADSAFE */ + +/* --- Wizard objects ------------------------------------------------------ */ + +STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg); + +STATIC MGVTBL vmg_wizard_vtbl = { + NULL, /* get */ + NULL, /* set */ + NULL, /* len */ + NULL, /* clear */ + vmg_wizard_free, /* free */ +#if MGf_COPY + NULL, /* copy */ +#endif /* MGf_COPY */ +#if MGf_DUP + NULL, /* dup */ +#endif /* MGf_DUP */ +#if MGf_LOCAL + NULL, /* local */ +#endif /* MGf_LOCAL */ +}; + +/* ... Wizard constructor .................................................. */ + +STATIC SV *vmg_wizard_new(pTHX_ const MGWIZ *w) { +#define vmg_wizard_new(W) vmg_wizard_new(aTHX_ (W)) + SV *wiz = newSVuv(PTR2UV(w)); + + if (w) { + MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0); + mg->mg_private = SIG_WZO; + } + SvREADONLY_on(wiz); + + return wiz; +} + +STATIC SV *vmg_wizard_validate(pTHX_ SV *wiz) { +#define vmg_wizard_validate(W) vmg_wizard_validate(aTHX_ (W)) + if (SvROK(wiz)) { + wiz = SvRV(wiz); + if (SvIOK(wiz)) + return wiz; + } + + croak(vmg_invalid_wiz); +} + +#define vmg_wizard_id(W) SvUV((SV *) (W)) +#define vmg_wizard_main_mgwiz(W) INT2PTR(const MGWIZ *, vmg_wizard_id(W)) + +/* ... Wizard destructor ................................................... */ + +STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg) { + MGWIZ *w; + + if (PL_dirty) /* During global destruction, the context is already freed */ + return 0; + + w = (MGWIZ *) vmg_wizard_main_mgwiz(sv); + +#if VMG_THREADSAFE + { + dMY_CXT; + ptable_store(MY_CXT.wizards, w, NULL); + } +#else /* VMG_THREADSAFE */ + vmg_mgwiz_free(w); +#endif /* !VMG_THREADSAFE */ + + return 0; +} + +#if VMG_THREADSAFE + +STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ SV *wiz) { +#define vmg_wizard_mgwiz(W) vmg_wizard_mgwiz(aTHX_ ((SV *) (W))) + const MGWIZ *w; + + w = vmg_wizard_main_mgwiz(wiz); + if (w->owner == aTHX) + return w; + + { + dMY_CXT; + return ptable_fetch(MY_CXT.wizards, w); + } +} + +#else /* VMG_THREADSAFE */ + +#define vmg_wizard_mgwiz(W) vmg_wizard_main_mgwiz(W) + +#endif /* !VMG_THREADSAFE */ + +/* --- User-level functions implementation --------------------------------- */ /* ... Construct private data .............................................. */ @@ -430,15 +624,17 @@ STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, AV *args) { return nsv; } -STATIC SV *vmg_data_get(SV *sv, U16 sig) { +STATIC SV *vmg_data_get(pTHX_ SV *sv, SV *wiz) { +#define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W)) MAGIC *mg, *moremagic; if (SvTYPE(sv) >= SVt_PVMG) { + UV wid = vmg_wizard_id(wiz); for (mg = SvMAGIC(sv); mg; mg = moremagic) { moremagic = mg->mg_moremagic; if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - MGWIZ *w = SV2MGWIZ(mg->mg_ptr); - if (w->sig == sig) + UV zid = vmg_wizard_id(mg->mg_ptr); + if (zid == wid) break; } } @@ -469,18 +665,17 @@ STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) STATIC UV vmg_cast(pTHX_ SV *sv, SV *wiz, AV *args) { #define vmg_cast(S, W, A) vmg_cast(aTHX_ (S), (W), (A)) MAGIC *mg = NULL, *moremagic = NULL; - MGWIZ *w; SV *data; - U32 oldgmg = SvGMAGICAL(sv); - - w = SV2MGWIZ(wiz); + const MGWIZ *w = vmg_wizard_mgwiz(wiz); + U32 oldgmg = SvGMAGICAL(sv); if (SvTYPE(sv) >= SVt_PVMG) { + UV wid = vmg_wizard_id(wiz); for (mg = SvMAGIC(sv); mg; mg = moremagic) { moremagic = mg->mg_moremagic; if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - MGWIZ *z = SV2MGWIZ(mg->mg_ptr); - if (z->sig == w->sig) + UV zid = vmg_wizard_id(mg->mg_ptr); + if (zid == wid) break; } } @@ -557,12 +752,13 @@ done: return 1; } -STATIC UV vmg_dispell(pTHX_ SV *sv, U16 sig) { -#define vmg_dispell(S, Z) vmg_dispell(aTHX_ (S), (Z)) +STATIC UV vmg_dispell(pTHX_ SV *sv, SV *wiz) { +#define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W)) #if VMG_UVAR U32 uvars = 0; #endif /* VMG_UVAR */ MAGIC *mg, *prevmagic, *moremagic = NULL; + UV wid = vmg_wizard_id(wiz); if (SvTYPE(sv) < SVt_PVMG) return 0; @@ -570,15 +766,16 @@ STATIC UV vmg_dispell(pTHX_ SV *sv, U16 sig) { for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) { moremagic = mg->mg_moremagic; if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - MGWIZ *w = SV2MGWIZ(mg->mg_ptr); - if (w->sig == sig) { + const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr); + UV zid = vmg_wizard_id(mg->mg_ptr); + if (zid == wid) { #if VMG_UVAR /* If the current has no uvar, short-circuit uvar deletion. */ - uvars = w->uvar ? (uvars + 1) : 0; + uvars = z->uvar ? (uvars + 1) : 0; #endif /* VMG_UVAR */ break; #if VMG_UVAR - } else if (w->uvar) { + } else if (z->uvar) { ++uvars; /* We can't break here since we need to find the ext magic to delete. */ #endif /* VMG_UVAR */ @@ -608,8 +805,8 @@ STATIC UV vmg_dispell(pTHX_ SV *sv, U16 sig) { for (mg = moremagic; mg; mg = mg->mg_moremagic) { if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - MGWIZ *w = SV2MGWIZ(mg->mg_ptr); - if (w->uvar) { + const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr); + if (z->uvar) { ++uvars; break; } @@ -769,17 +966,17 @@ STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) { vmg_cb_call(aTHX_ (I), (((F) << VMG_CB_CALL_ARGS_SHIFT) | 3), (S), (A1), (A2), (A3)) STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) { - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj); } STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) { - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj); } STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); unsigned int opinfo = w->opinfo; U32 len, ret; svtype t = SvTYPE(sv); @@ -823,7 +1020,7 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) { } STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) { - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj); } @@ -843,7 +1040,7 @@ STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) { if (PL_dirty) return 0; - w = SV2MGWIZ(mg->mg_ptr); + w = vmg_wizard_mgwiz(mg->mg_ptr); /* So that it survives the temp cleanup below */ SvREFCNT_inc(sv); @@ -914,7 +1111,7 @@ STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, # endif ) { SV *keysv; - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); int ret; if (keylen == HEf_SVKEY) { @@ -941,7 +1138,7 @@ STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) { #if MGf_LOCAL STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) { - const MGWIZ *w = SV2MGWIZ(mg->mg_ptr); + const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr); return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj); } #endif /* MGf_LOCAL */ @@ -981,7 +1178,7 @@ STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) { continue; } if (mg->mg_private != SIG_WIZ) continue; - w = SV2MGWIZ(mg->mg_ptr); + w = vmg_wizard_mgwiz(mg->mg_ptr); switch (w->uvar) { case 0: continue; @@ -1034,153 +1231,7 @@ STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) { } #endif /* VMG_UVAR */ -/* ... Wizard destructor ................................................... */ - -STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) { - char buf[8]; - MGWIZ *w; - - if (PL_dirty) /* During global destruction, the context is already freed */ - return 0; - - w = SV2MGWIZ(wiz); -#if VMG_MULTIPLICITY - if (w->owner != aTHX) - return 0; - w->owner = NULL; -#endif /* VMG_MULTIPLICITY */ - - { - dMY_CXT; - if (hv_delete(MY_CXT.wizards, buf, sprintf(buf, "%u", w->sig), 0) != wiz) - return 0; - } - - /* Unmortalize the wizard to avoid it being freed in weird places. */ - if (SvTEMP(wiz) && !SvREFCNT(wiz)) { - const I32 myfloor = PL_tmps_floor; - I32 i; - for (i = PL_tmps_ix; i > myfloor; --i) { - if (PL_tmps_stack[i] == wiz) - PL_tmps_stack[i] = NULL; - } - } - - if (w->cb_data) SvREFCNT_dec(SvRV(w->cb_data)); - if (w->cb_get) SvREFCNT_dec(SvRV(w->cb_get)); - if (w->cb_set) SvREFCNT_dec(SvRV(w->cb_set)); - if (w->cb_len) SvREFCNT_dec(SvRV(w->cb_len)); - if (w->cb_clear) SvREFCNT_dec(SvRV(w->cb_clear)); - if (w->cb_free) SvREFCNT_dec(SvRV(w->cb_free)); -#if MGf_COPY - if (w->cb_copy) SvREFCNT_dec(SvRV(w->cb_copy)); -#endif /* MGf_COPY */ -#if 0 /* MGf_DUP */ - if (w->cb_dup) SvREFCNT_dec(SvRV(w->cb_dup)); -#endif /* MGf_DUP */ -#if MGf_LOCAL - if (w->cb_local) SvREFCNT_dec(SvRV(w->cb_local)); -#endif /* MGf_LOCAL */ -#if VMG_UVAR - if (w->cb_fetch) SvREFCNT_dec(SvRV(w->cb_fetch)); - if (w->cb_store) SvREFCNT_dec(SvRV(w->cb_store)); - if (w->cb_exists) SvREFCNT_dec(SvRV(w->cb_exists)); - if (w->cb_delete) SvREFCNT_dec(SvRV(w->cb_delete)); -#endif /* VMG_UVAR */ - - Safefree(w->vtbl); - Safefree(w); - - return 0; -} - -STATIC MGVTBL vmg_wizard_vtbl = { - NULL, /* get */ - NULL, /* set */ - NULL, /* len */ - NULL, /* clear */ - vmg_wizard_free, /* free */ -#if MGf_COPY - NULL, /* copy */ -#endif /* MGf_COPY */ -#if MGf_DUP - NULL, /* dup */ -#endif /* MGf_DUP */ -#if MGf_LOCAL - NULL, /* local */ -#endif /* MGf_LOCAL */ -}; - -STATIC U16 vmg_sv2sig(pTHX_ SV *sv) { -#define vmg_sv2sig(S) vmg_sv2sig(aTHX_ (S)) - IV sig; - - if (SvIOK(sv)) { - sig = SvIVX(sv); - } else if (SvNOK(sv)) { - sig = SvNVX(sv); - } else if ((SvPOK(sv) && grok_number(SvPVX(sv), SvCUR(sv), NULL))) { - sig = SvIV(sv); - } else { - croak(vmg_invalid_sig); - } - - if (sig < SIG_MIN || sig > SIG_MAX) - croak(vmg_invalid_sig); - - return sig; -} - -STATIC U16 vmg_wizard_sig(pTHX_ SV *wiz) { -#define vmg_wizard_sig(W) vmg_wizard_sig(aTHX_ (W)) - U16 sig; - - if (SvROK(wiz)) { - sig = SV2MGWIZ(SvRV(wiz))->sig; - } else if (SvOK(wiz)) { - sig = vmg_sv2sig(wiz); - } else { - croak(vmg_invalid_wiz); - } - - { - dMY_CXT; - char buf[8]; - SV **old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0); - if (!(old && SV2MGWIZ(*old))) - croak(vmg_invalid_wiz); - } - - return sig; -} - -STATIC SV *vmg_wizard_wiz(pTHX_ SV *wiz) { -#define vmg_wizard_wiz(W) vmg_wizard_wiz(aTHX_ (W)) - U16 sig; - - if (SvROK(wiz)) { - wiz = SvRV(wiz); -#if VMG_MULTIPLICITY - if (SV2MGWIZ(wiz)->owner == aTHX) - return wiz; -#endif /* VMG_MULTIPLICITY */ - sig = SV2MGWIZ(wiz)->sig; - } else if (SvOK(wiz)) { - sig = vmg_sv2sig(wiz); - } else { - croak(vmg_invalid_wiz); - } - - { - dMY_CXT; - char buf[8]; - SV **old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0); - if (!(old && SV2MGWIZ(*old))) - croak(vmg_invalid_wiz); - - return *old; - } -} +/* --- Macros for the XS section ------------------------------------------- */ #define VMG_SET_CB(S, N) \ cb = (S); \ @@ -1196,54 +1247,6 @@ STATIC SV *vmg_wizard_wiz(pTHX_ SV *wiz) { w->cb_ ## N = NULL; \ } -#if VMG_THREADSAFE - -#define VMG_CLONE_CB(N) \ - z->cb_ ## N = (w->cb_ ## N) ? newRV_inc(vmg_clone(SvRV(w->cb_ ## N), \ - w->owner)) \ - : NULL; - -STATIC MGWIZ *vmg_wizard_clone(pTHX_ const MGWIZ *w) { -#define vmg_wizard_clone(W) vmg_wizard_clone(aTHX_ (W)) - MGVTBL *t; - MGWIZ *z; - - Newx(t, 1, MGVTBL); - Copy(w->vtbl, t, 1, MGVTBL); - - Newx(z, 1, MGWIZ); - VMG_CLONE_CB(data); - VMG_CLONE_CB(get); - VMG_CLONE_CB(set); - VMG_CLONE_CB(len); - VMG_CLONE_CB(clear); - VMG_CLONE_CB(free); -#if MGf_COPY - VMG_CLONE_CB(copy); -#endif /* MGf_COPY */ -#if MGf_DUP - VMG_CLONE_CB(dup); -#endif /* MGf_DUP */ -#if MGf_LOCAL - VMG_CLONE_CB(local); -#endif /* MGf_LOCAL */ -#if VMG_UVAR - VMG_CLONE_CB(fetch); - VMG_CLONE_CB(store); - VMG_CLONE_CB(exists); - VMG_CLONE_CB(delete); -#endif /* VMG_UVAR */ - z->owner = aTHX; - z->vtbl = t; - z->sig = w->sig; - z->uvar = w->uvar; - z->opinfo = w->opinfo; - - return z; -} - -#endif /* VMG_THREADSAFE */ - /* --- XS ------------------------------------------------------------------ */ MODULE = Variable::Magic PACKAGE = Variable::Magic @@ -1253,18 +1256,18 @@ PROTOTYPES: ENABLE BOOT: { HV *stash; + MY_CXT_INIT; - MY_CXT.wizards = newHV(); - hv_iterinit(MY_CXT.wizards); /* Allocate iterator */ +#if VMG_THREADSAFE + MY_CXT.wizards = ptable_new(); + MY_CXT.owner = aTHX; +#endif MY_CXT.b__op_stashes[0] = NULL; #if VMG_THREADSAFE MUTEX_INIT(&vmg_op_name_init_mutex); #endif stash = gv_stashpv(__PACKAGE__, 1); - newCONSTSUB(stash, "SIG_MIN", newSVuv(SIG_MIN)); - newCONSTSUB(stash, "SIG_MAX", newSVuv(SIG_MAX)); - newCONSTSUB(stash, "SIG_NBR", newSVuv(SIG_NBR)); newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY)); newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP)); newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL)); @@ -1292,33 +1295,19 @@ void CLONE(...) PROTOTYPE: DISABLE PREINIT: - HV *hv; - U32 had_b__op_stash = 0; + ptable *t; + int *level; + U32 had_b__op_stash = 0; opclass c; CODE: { - HE *key; + my_cxt_t ud; dMY_CXT; - hv = newHV(); - hv_iterinit(hv); /* Allocate iterator */ - hv_iterinit(MY_CXT.wizards); - while ((key = hv_iternext(MY_CXT.wizards))) { - STRLEN len; - char *sig = HePV(key, len); - SV *sv; - const MGWIZ *w = SV2MGWIZ(HeVAL(key)); - if (w) { - MAGIC *mg; - w = vmg_wizard_clone(w); - sv = MGWIZ2SV(w); - mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0); - mg->mg_private = SIG_WZO; - } else { - sv = MGWIZ2SV(NULL); - } - SvREADONLY_on(sv); - if (!hv_store(hv, sig, len, sv, HeHASH(key))) croak("%s during CLONE", vmg_globstorefail); - } + + ud.wizards = t = ptable_new(); + ud.owner = MY_CXT.owner; + ptable_walk(MY_CXT.wizards, vmg_ptable_clone, &ud); + for (c = 0; c < OPc_MAX; ++c) { if (MY_CXT.b__op_stashes[c]) had_b__op_stash |= (((U32) 1) << c); @@ -1326,7 +1315,8 @@ CODE: } { MY_CXT_CLONE; - MY_CXT.wizards = hv; + MY_CXT.wizards = t; + MY_CXT.owner = aTHX; for (c = 0; c < OPc_MAX; ++c) { MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c)) ? gv_stashpv(vmg_opclassnames[c], 1) : NULL; @@ -1339,18 +1329,16 @@ SV *_wizard(...) PROTOTYPE: DISABLE PREINIT: I32 i = 0; - U16 sig; char buf[8]; MGWIZ *w; MGVTBL *t; MAGIC *mg; - SV *sv; - SV *svsig; + SV *wiz; SV *cb; CODE: dMY_CXT; - if (items != 8 + if (items != 7 #if MGf_COPY + 1 #endif /* MGf_COPY */ @@ -1365,19 +1353,6 @@ CODE: #endif /* VMG_UVAR */ ) { croak(vmg_wrongargnum); } - svsig = ST(i++); - if (SvOK(svsig)) { - SV **old; - sig = vmg_sv2sig(svsig); - old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0); - if (old && SV2MGWIZ(*old)) { - ST(0) = sv_2mortal(newRV_inc(*old)); - XSRETURN(1); - } - } else { - sig = vmg_gensig(); - } - Newx(t, 1, MGVTBL); Newx(w, 1, MGWIZ); @@ -1418,39 +1393,11 @@ CODE: w->owner = aTHX; #endif /* VMG_MULTIPLICITY */ w->vtbl = t; - w->sig = sig; - - sv = MGWIZ2SV(w); - mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0); - mg->mg_private = SIG_WZO; - SvREADONLY_on(sv); - - if (!hv_store(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), sv, 0)) croak(vmg_globstorefail); - - RETVAL = newRV_noinc(sv); -OUTPUT: - RETVAL - -SV *gensig() -PROTOTYPE: -PREINIT: - U16 sig; - char buf[8]; -CODE: - dMY_CXT; - sig = vmg_gensig(); - if (!hv_store(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), MGWIZ2SV(NULL), 0)) croak(vmg_globstorefail); - RETVAL = newSVuv(sig); -OUTPUT: - RETVAL +#if VMG_THREADSAFE + ptable_store(MY_CXT.wizards, w, w); +#endif /* VMG_THREADSAFE */ -SV *getsig(SV *wiz) -PROTOTYPE: $ -PREINIT: - U16 sig; -CODE: - sig = vmg_wizard_sig(wiz); - RETVAL = newSVuv(sig); + RETVAL = newRV_noinc(vmg_wizard_new(w)); OUTPUT: RETVAL @@ -1460,7 +1407,7 @@ PREINIT: AV *args = NULL; SV *ret; CODE: - wiz = vmg_wizard_wiz(wiz); + wiz = vmg_wizard_validate(wiz); if (items > 2) { I32 i; args = newAV(); @@ -1482,10 +1429,9 @@ getdata(SV *sv, SV *wiz) PROTOTYPE: \[$@%&*]$ PREINIT: SV *data; - U16 sig; PPCODE: - sig = vmg_wizard_sig(wiz); - data = vmg_data_get(SvRV(sv), sig); + wiz = vmg_wizard_validate(wiz); + data = vmg_data_get(SvRV(sv), wiz); if (!data) XSRETURN_EMPTY; ST(0) = data; @@ -1496,7 +1442,7 @@ PROTOTYPE: \[$@%&*]$ PREINIT: U16 sig; CODE: - sig = vmg_wizard_sig(wiz); - RETVAL = newSVuv(vmg_dispell(SvRV(sv), sig)); + wiz = vmg_wizard_validate(wiz); + RETVAL = newSVuv(vmg_dispell(SvRV(sv), wiz)); OUTPUT: RETVAL diff --git a/lib/Variable/Magic.pm b/lib/Variable/Magic.pm index 772c8bd..c600a15 100644 --- a/lib/Variable/Magic.pm +++ b/lib/Variable/Magic.pm @@ -230,16 +230,6 @@ It takes a list of keys / values as argument, whose keys can be : =item * -C - -The numerical signature. -If not specified or undefined, a random signature is generated. -If the signature matches an already defined magic, then the existant magic object is returned. - -This option is B and will be removed in december 2009. - -=item * - C A code reference to a private data constructor. @@ -304,7 +294,7 @@ Note that C callbacks are I called during global destruction, as th sub wizard { croak 'Wrong number of arguments for wizard()' if @_ % 2; my %opts = @_; - my @keys = qw/sig data op_info get set len clear free/; + my @keys = qw/data op_info get set len clear free/; push @keys, 'copy' if MGf_COPY; push @keys, 'dup' if MGf_DUP; push @keys, 'local' if MGf_LOCAL; @@ -317,27 +307,6 @@ sub wizard { return $ret; } -=head2 C - -With this tool, you can manually generate random magic signature between SIG_MIN and SIG_MAX inclusive. -That's the way L creates them when no signature is supplied. - - # Generate a signature - my $sig = gensig; - -This function is B and will be removed in december 2009. - -=head2 C - - getsig $wiz - -This accessor returns the magic signature of this wizard. - - # Get $wiz signature - my $sig = getsig $wiz; - -This function is B and will be removed in december 2009. - =head2 C cast [$@%&*]var, $wiz, ... @@ -382,24 +351,6 @@ This function returns true on success, C<0> when no magic represented by C<$wiz> =head1 CONSTANTS -=head2 C - -The minimum integer used as a signature for user-defined magic. - -This constant is B and will be removed in december 2009. - -=head2 C - -The maximum integer used as a signature for user-defined magic. - -This constant is B and will be removed in december 2009. - -=head2 C - - SIG_NBR = SIG_MAX - SIG_MIN + 1 - -This constant is B and will be removed in december 2009. - =head2 C Evaluates to true iff the 'copy' magic is available. @@ -595,7 +546,7 @@ I : 'len' magic is called again when pushing into a magical array in =head1 EXPORT -The functions L, L, L, L, L and L are only exported on request. +The functions L, L, L and L are only exported on request. All of them are exported by the tags C<':funcs'> and C<':all'>. All the constants are also only exported on request, either individually or by the tags C<':consts'> and C<':all'>. @@ -606,9 +557,9 @@ use base qw/Exporter/; our @EXPORT = (); our %EXPORT_TAGS = ( - 'funcs' => [ qw/wizard gensig getsig cast getdata dispell/ ], + 'funcs' => [ qw/wizard cast getdata dispell/ ], 'consts' => [ - qw/SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/, + qw/MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/, qw/VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID/, qw/VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID/, qw/VMG_COMPAT_ARRAY_UNDEF_CLEAR/, diff --git a/ptable.h b/ptable.h new file mode 100644 index 0000000..73fa135 --- /dev/null +++ b/ptable.h @@ -0,0 +1,221 @@ +/* This file is part of the Variable::Magic Perl module. + * See http://search.cpan.org/dist/Variable-Magic/ */ + +/* This is a pointer table implementation essentially copied from the ptr_table + * implementation in perl's sv.c, except that it has been modified to use memory + * shared across threads. + * Copyright goes to the original authors, bug reports to me. */ + +/* This header is designed to be included several times with different + * definitions for PTABLE_NAME and PTABLE_VAL_FREE(). */ + +#undef pPTBLMS +#undef pPTBLMS_ +#undef aPTBLMS +#undef aPTBLMS_ + +/* Context for PerlMemShared_* functions */ + +#ifdef PERL_IMPLICIT_SYS +# define pPTBLMS pTHX +# define pPTBLMS_ pTHX_ +# define aPTBLMS aTHX +# define aPTBLMS_ aTHX_ +#else +# define pPTBLMS +# define pPTBLMS_ +# define aPTBLMS +# define aPTBLMS_ +#endif + +#ifndef pPTBL +# define pPTBL pPTBLMS +#endif +#ifndef pPTBL_ +# define pPTBL_ pPTBLMS_ +#endif +#ifndef aPTBL +# define aPTBL aPTBLMS +#endif +#ifndef aPTBL_ +# define aPTBL_ aPTBLMS_ +#endif + +#ifndef PTABLE_NAME +# define PTABLE_NAME ptable +#endif + +#ifndef PTABLE_VAL_FREE +# define PTABLE_VAL_FREE(V) +#endif + +#ifndef PTABLE_JOIN +# define PTABLE_PASTE(A, B) A ## B +# define PTABLE_JOIN(A, B) PTABLE_PASTE(A, B) +#endif + +#ifndef PTABLE_PREFIX +# define PTABLE_PREFIX(X) PTABLE_JOIN(PTABLE_NAME, X) +#endif + +#ifndef ptable_ent +typedef struct ptable_ent { + struct ptable_ent *next; + const void * key; + void * val; +} ptable_ent; +#define ptable_ent ptable_ent +#endif /* !ptable_ent */ + +#ifndef ptable +typedef struct ptable { + ptable_ent **ary; + UV max; + UV items; +} ptable; +#define ptable ptable +#endif /* !ptable */ + +#ifndef ptable_new +STATIC ptable *ptable_new(pPTBLMS) { +#define ptable_new() ptable_new(aPTBLMS) + ptable *t = PerlMemShared_malloc(sizeof *t); + t->max = 15; + t->items = 0; + t->ary = PerlMemShared_calloc(t->max + 1, sizeof *t->ary); + return t; +} +#endif /* !ptable_new */ + +#ifndef PTABLE_HASH +# define PTABLE_HASH(ptr) \ + ((PTR2UV(ptr) >> 3) ^ (PTR2UV(ptr) >> (3 + 7)) ^ (PTR2UV(ptr) >> (3 + 17))) +#endif + +#ifndef ptable_find +STATIC ptable_ent *ptable_find(const ptable * const t, const void * const key) { +#define ptable_find ptable_find + ptable_ent *ent; + const UV hash = PTABLE_HASH(key); + + ent = t->ary[hash & t->max]; + for (; ent; ent = ent->next) { + if (ent->key == key) + return ent; + } + + return NULL; +} +#endif /* !ptable_find */ + +#ifndef ptable_fetch +STATIC void *ptable_fetch(const ptable * const t, const void * const key) { +#define ptable_fetch ptable_fetch + const ptable_ent *const ent = ptable_find(t, key); + + return ent ? ent->val : NULL; +} +#endif /* !ptable_fetch */ + +#ifndef ptable_split +STATIC void ptable_split(pPTBLMS_ ptable * const t) { +#define ptable_split(T) ptable_split(aPTBLMS_ (T)) + ptable_ent **ary = t->ary; + const UV oldsize = t->max + 1; + UV newsize = oldsize * 2; + UV i; + + ary = PerlMemShared_realloc(ary, newsize * sizeof(*ary)); + Zero(&ary[oldsize], newsize - oldsize, sizeof(*ary)); + t->max = --newsize; + t->ary = ary; + + for (i = 0; i < oldsize; i++, ary++) { + ptable_ent **curentp, **entp, *ent; + if (!*ary) + continue; + curentp = ary + oldsize; + for (entp = ary, ent = *ary; ent; ent = *entp) { + if ((newsize & PTABLE_HASH(ent->key)) != i) { + *entp = ent->next; + ent->next = *curentp; + *curentp = ent; + continue; + } else + entp = &ent->next; + } + } +} +#endif /* !ptable_split */ + +STATIC void PTABLE_PREFIX(_store)(pPTBL_ ptable * const t, const void * const key, void * const val) { + ptable_ent *ent = ptable_find(t, key); + + if (ent) { + void *oldval = ent->val; + PTABLE_VAL_FREE(oldval); + ent->val = val; + } else if (val) { + const UV i = PTABLE_HASH(key) & t->max; + ent = PerlMemShared_malloc(sizeof *ent); + ent->key = key; + ent->val = val; + ent->next = t->ary[i]; + t->ary[i] = ent; + t->items++; + if (ent->next && t->items > t->max) + ptable_split(t); + } +} + +#ifndef ptable_walk +STATIC void ptable_walk(pTHX_ ptable * const t, void (*cb)(pTHX_ ptable_ent *ent, void *userdata), void *userdata) { +#define ptable_walk(T, CB, UD) ptable_walk(aTHX_ (T), (CB), (UD)) + if (t && t->items) { + register ptable_ent ** const array = t->ary; + UV i = t->max; + do { + ptable_ent *entry; + for (entry = array[i]; entry; entry = entry->next) + cb(aTHX_ entry, userdata); + } while (i--); + } +} +#endif /* !ptable_walk */ + +STATIC void PTABLE_PREFIX(_clear)(pPTBL_ ptable * const t) { + if (t && t->items) { + register ptable_ent ** const array = t->ary; + UV i = t->max; + + do { + ptable_ent *entry = array[i]; + while (entry) { + ptable_ent * const oentry = entry; + void *val = oentry->val; + entry = entry->next; + PTABLE_VAL_FREE(val); + PerlMemShared_free(oentry); + } + array[i] = NULL; + } while (i--); + + t->items = 0; + } +} + +STATIC void PTABLE_PREFIX(_free)(pPTBL_ ptable * const t) { + if (!t) + return; + PTABLE_PREFIX(_clear)(aPTBL_ t); + PerlMemShared_free(t->ary); + PerlMemShared_free(t); +} + +#undef pPTBL +#undef pPTBL_ +#undef aPTBL +#undef aPTBL_ + +#undef PTABLE_NAME +#undef PTABLE_VAL_FREE diff --git a/samples/copy.pl b/samples/copy.pl index f716906..6a705f6 100755 --- a/samples/copy.pl +++ b/samples/copy.pl @@ -4,7 +4,7 @@ use strict; use warnings; use lib qw{blib/arch blib/lib}; -use Variable::Magic qw/wizard getsig cast/; +use Variable::Magic qw/wizard cast/; use Tie::Hash; my $wiz = wizard copy => sub { print STDERR "COPY $_[2] => $_[3]\n" }, diff --git a/samples/magic.pl b/samples/magic.pl index 3c14d49..5ce53ae 100755 --- a/samples/magic.pl +++ b/samples/magic.pl @@ -4,19 +4,16 @@ use strict; use warnings; use lib qw{blib/arch blib/lib}; -use Variable::Magic qw/wizard getsig cast dispell/; +use Variable::Magic qw/wizard cast dispell/; sub foo { print STDERR "got ${$_[0]}!\n" } my $bar = sub { ++${$_[0]}; print STDERR "now set to ${$_[0]}!\n"; }; my $a = 1; -my $sig; { my $wiz = wizard get => \&foo, set => $bar, free => sub { print STDERR "deleted!\n"; }; - $sig = getsig $wiz; - print "my sig is $sig\n"; cast $a, $wiz, qw/a b c/; ++$a; # "got 1!", "now set to 3!" dispell $a, $wiz; @@ -27,5 +24,3 @@ my $sig; my $b = $a; # "got 3!" $a = 3; # "now set to 4!" $b = 3; # (nothing) -dispell $a, $sig; -$a = 4; # (nothing) diff --git a/samples/uvar.pl b/samples/uvar.pl index 738a5db..700692f 100755 --- a/samples/uvar.pl +++ b/samples/uvar.pl @@ -4,7 +4,7 @@ use strict; use warnings; use lib qw{blib/arch blib/lib}; -use Variable::Magic qw/wizard getsig cast dispell/; +use Variable::Magic qw/wizard cast dispell/; my $wiz = wizard fetch => sub { print STDERR "$_[0] FETCH KEY $_[2]\n" }, diff --git a/t/01-import.t b/t/01-import.t index 5dc418d..19d165f 100644 --- a/t/01-import.t +++ b/t/01-import.t @@ -3,19 +3,16 @@ use strict; use warnings; -use Test::More tests => 2 * 23; +use Test::More tests => 2 * 18; require Variable::Magic; my %syms = ( wizard => undef, - gensig => '', - getsig => '$', cast => '\[$@%&*]$@', getdata => '\[$@%&*]$', dispell => '\[$@%&*]$', map { $_ => '' } qw/ - SIG_MIN SIG_MAX SIG_NBR MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR VMG_COMPAT_ARRAY_PUSH_NOLEN VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID diff --git a/t/10-simple.t b/t/10-simple.t index 1617fdc..69c3a40 100644 --- a/t/10-simple.t +++ b/t/10-simple.t @@ -3,13 +3,13 @@ use strict; use warnings; -use Test::More tests => 48; +use Test::More tests => 43; -use Variable::Magic qw/wizard gensig getsig cast dispell MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/; +use Variable::Magic qw/wizard cast dispell MGf_COPY MGf_DUP MGf_LOCAL VMG_UVAR/; my $inv_wiz_obj = qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/; -my $args = 8; +my $args = 7; ++$args if MGf_COPY; ++$args if MGf_DUP; ++$args if MGf_LOCAL; @@ -25,20 +25,12 @@ for (0 .. 3) { like($@, qr/Wrong\s+number\s+of\s+arguments\s+for\s+&?wizard\(\)\s+at\s+\Q$0\E/, 'wizard called with an odd number of arguments croaks'); } -my $sig = gensig; - -my $a = 1; -my $res = eval { cast $a, $sig }; -like($@, $inv_wiz_obj, 'cast from wrong sig croaks'); -is($res, undef, 'cast from wrong sig doesn\'t return anything'); - -my $wiz = eval { wizard sig => $sig }; +my $wiz = eval { wizard }; is($@, '', 'wizard doesn\'t croak'); ok(defined $wiz, 'wizard is defined'); is(ref $wiz, 'SCALAR', 'wizard is a scalar ref'); -is($sig, getsig $wiz, 'wizard signature is correct'); -$res = eval { cast $a, $wiz }; +my $res = eval { cast $a, $wiz }; is($@, '', 'cast doesn\'t croak'); ok($res, 'cast is valid'); @@ -50,33 +42,23 @@ $res = eval { cast $a, $wiz }; is($@, '', 're-cast doesn\'t croak'); ok($res, 're-cast is valid'); -$res = eval { dispell $a, gensig }; -like($@, $inv_wiz_obj, 're-dispell from wrong sig croaks'); -is($res, undef, 're-dispell from wrong sig doesn\'t return anything'); +$res = eval { dispell $a, \"blargh" }; +like($@, $inv_wiz_obj, 're-dispell from wrong wizard croaks'); +is($res, undef, 're-dispell from wrong wizard doesn\'t return anything'); $res = eval { dispell $a, undef }; like($@, $inv_wiz_obj, 're-dispell from undef croaks'); is($res, undef, 're-dispell from undef doesn\'t return anything'); -$res = eval { dispell $a, $sig }; -is($@, '', 're-dispell from good sig doesn\'t croak'); -ok($res, 're-dispell from good sig is valid'); +$res = eval { dispell $a, $wiz }; +is($@, '', 're-dispell from good wizard doesn\'t croak'); +ok($res, 're-dispell from good wizard is valid'); $res = eval { dispell my $b, $wiz }; is($@, '', 'dispell non-magic object doesn\'t croak'); is($res, 0, 'dispell non-magic object returns 0'); -$sig = gensig; -{ - my $wiz = wizard sig => $sig; - my $b = 2; - my $res = cast $b, $wiz; -} my $c = 3; -$res = eval { cast $c, $sig }; -like($@, $inv_wiz_obj, 'cast from obsolete signature croaks'); -is($res, undef, 'cast from obsolete signature returns undef'); - $res = eval { cast $c, undef }; like($@, $inv_wiz_obj, 'cast from undef croaks'); is($res, undef, 'cast from undef doesn\'t return anything'); diff --git a/t/12-sig.t b/t/12-sig.t deleted file mode 100644 index 0575584..0000000 --- a/t/12-sig.t +++ /dev/null @@ -1,74 +0,0 @@ -#!perl -T - -use strict; -use warnings; - -use Test::More tests => 30; - -use Variable::Magic qw/wizard getsig cast dispell SIG_MIN SIG_MAX/; - -my $sig = 300; - -my ($a, $b, $c, $d) = 1 .. 4; - -my $inv_num_sig = qr/Invalid\s+numeric\s+signature\s+at\s+\Q$0\E/; - -{ - my $wiz = eval { wizard sig => $sig }; - is($@, '', 'wizard creation doesn\'t croak'); - ok(defined $wiz, 'wizard is defined'); - is(ref $wiz, 'SCALAR', 'wizard is a scalar ref'); - is($sig, getsig $wiz, 'wizard signature is correct'); - - my $wiz2 = eval { wizard sig => $sig }; - is($@, '', 'wizard retreive doesn\'t croak'); - ok(defined $wiz2, 'retrieved wizard is defined'); - is(ref $wiz2, 'SCALAR', 'retrieved wizard is a scalar ref'); - is($sig, getsig $wiz2, 'retrieved wizard signature is correct'); - - my $wiz3 = eval { wizard sig => [ ] }; - like($@, $inv_num_sig, 'non numeric signature croaks'); - is($wiz3, undef, 'non numeric signature doesn\'t return anything'); - - $wiz3 = eval { wizard sig => SIG_MIN - 1 }; - like($@, $inv_num_sig, 'numeric signature too small croaks'); - is($wiz3, undef, 'numeric signature too small doesn\'t return anything'); - - $wiz3 = eval { wizard sig => SIG_MAX + 1 }; - like($@, $inv_num_sig, 'numeric signature too big croaks'); - is($wiz3, undef, 'numeric signature too big doesn\'t return anything'); - - my $a = 1; - my $res = eval { cast $a, $wiz }; - is($@, '', 'cast from wizard doesn\'t croak'); - ok($res, 'cast from wizard invalid'); - - $res = eval { dispell $a, $wiz2 }; - is($@, '', 'dispell from retrieved wizard doesn\'t croak'); - ok($res, 'dispell from retrieved wizard invalid'); - - $res = eval { cast $b, $sig }; - is($@, '', 'cast from integer doesn\'t croak'); - ok($res, 'cast from integer invalid'); -} - -my $res = eval { cast $c, $sig + 0.1 }; -is($@, '', 'cast from float doesn\'t croak'); -ok($res, 'cast from float invalid'); - -$res = eval { cast $d, sprintf "%u", $sig }; -is($@, '', 'cast from string doesn\'t croak'); -ok($res, 'cast from string invalid'); - -$res = eval { dispell $b, $sig }; -is($@, '', 'dispell from integer doesn\'t croak'); -ok($res, 'dispell from integer invalid'); - -$res = eval { dispell $c, $sig + 0.1 }; -is($@, '', 'dispell from float doesn\'t croak'); -ok($res, 'dispell from float invalid'); - -$res = eval { dispell $d, sprintf "%u", $sig }; -is($@, '', 'dispell from string doesn\'t croak'); -ok($res, 'dispell from string invalid'); - diff --git a/t/13-data.t b/t/13-data.t index 41e0b3a..b693f92 100644 --- a/t/13-data.t +++ b/t/13-data.t @@ -3,16 +3,14 @@ use strict; use warnings; -use Test::More tests => 38; +use Test::More tests => 35; -use Variable::Magic qw/wizard getdata cast dispell SIG_MIN/; +use Variable::Magic qw/wizard getdata cast dispell/; my $c = 1; -my $sig = SIG_MIN; my $wiz = eval { - wizard sig => $sig, - data => sub { return { foo => $_[1] || 12, bar => $_[3] || 27 } }, + wizard data => sub { return { foo => $_[1] || 12, bar => $_[3] || 27 } }, get => sub { $c += $_[1]->{foo}; $_[1]->{foo} = $c }, set => sub { $c += $_[1]->{bar}; $_[1]->{bar} = $c } }; @@ -25,21 +23,15 @@ my $res = eval { cast $a, $wiz }; is($@, '', 'cast doesn\'t croak'); ok($res, 'cast returns true'); -my $data = eval { getdata $a, $wiz }; -is($@, '', 'getdata from wizard doesn\'t croak'); -ok($res, 'getdata from wizard returns true'); -is_deeply($data, { foo => 12, bar => 27 }, - 'getdata from wizard return value is ok'); - -$data = eval { getdata my $b, $wiz }; +my $data = eval { getdata my $b, $wiz }; is($@, '', 'getdata from non-magical scalar doesn\'t croak'); is($data, undef, 'getdata from non-magical scalar returns undef'); -$data = eval { getdata $a, $sig }; -is($@, '', 'getdata from sig doesn\'t croak'); -ok($res, 'getdata from sig returns true'); +$data = eval { getdata $a, $wiz }; +is($@, '', 'getdata from wizard doesn\'t croak'); +ok($res, 'getdata from wizard returns true'); is_deeply($data, { foo => 12, bar => 27 }, - 'getdata from sig return value is ok'); + 'getdata from wizard return value is ok'); my $b = $a; is($c, 13, 'get magic : pass data'); @@ -49,9 +41,9 @@ $a = 57; is($c, 40, 'set magic : pass data'); is($data->{bar}, 40, 'set magic : pass data'); -$data = eval { getdata $a, ($sig + 1) }; -like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 'getdata from invalid sig croaks'); -is($data, undef, 'getdata from invalid sig returns undef'); +$data = eval { getdata $a, \"blargh" }; +like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 'getdata from invalid wizard croaks'); +is($data, undef, 'getdata from invalid wizard returns undef'); $data = eval { getdata $a, undef }; like($@, qr/Invalid\s+wizard\s+object\s+at\s+\Q$0\E/, 'getdata from undef croaks'); @@ -71,8 +63,9 @@ ok($res, 'getdata from wizard with arguments returns true'); is_deeply($data, { foo => 'z', bar => 't' }, 'getdata from wizard with arguments return value is ok'); +dispell $a, $wiz; + $wiz = wizard get => sub { }; -dispell $a, $sig; $a = 63; $res = eval { cast $a, $wiz }; is($@, '', 'cast non-data wizard doesn\'t croak'); diff --git a/t/15-self.t b/t/15-self.t index 504c4b2..cfca708 100644 --- a/t/15-self.t +++ b/t/15-self.t @@ -5,7 +5,7 @@ use warnings; use Test::More tests => 17; -use Variable::Magic qw/wizard cast dispell getdata getsig/; +use Variable::Magic qw/wizard cast dispell getdata/; my $c = 0; @@ -39,7 +39,6 @@ my $c = 0; $w = getdata $wiz, $wiz; is($c, 1, 'getdata on magical self doesn\'t trigger callbacks'); - # is(getsig($w), getsig($wiz), 'getdata returns the correct wizard'); $res = eval { dispell $wiz, $wiz }; is($@, '', 're-dispell on self doesn\'t croak'); diff --git a/t/40-threads.t b/t/40-threads.t index bd64663..d10146d 100644 --- a/t/40-threads.t +++ b/t/40-threads.t @@ -28,7 +28,7 @@ use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NA BEGIN { skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE; - plan tests => 2 * (4 * 18 + 1) + 2 * (4 * 13 + 1); + plan tests => (4 * 18 + 1) + (4 * 13 + 1); my $v = $threads::VERSION; diag "Using threads $v" if defined $v; $v = $threads::shared::VERSION; @@ -38,12 +38,11 @@ BEGIN { my $destroyed : shared = 0; sub try { - my ($dispell, $sig, $op_info) = @_; + my ($dispell, $op_info) = @_; my $tid = threads->tid(); my $c = 0; my $wiz = eval { wizard data => sub { $_[1] + $tid }, - sig => $sig, get => sub { ++$c; 0 }, set => sub { my $op = $_[-1]; @@ -91,19 +90,17 @@ sub try { } for my $dispell (1, 0) { - for my $sig (undef, Variable::Magic::gensig()) { - { - lock $destroyed; - $destroyed = 0; - } + { + lock $destroyed; + $destroyed = 0; + } - my @t = map { threads->create(\&try, $dispell, $sig, $_) } - (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2; - $_->join for @t; + my @t = map { threads->create(\&try, $dispell, $_) } + (VMG_OP_INFO_NAME) x 2, (VMG_OP_INFO_OBJECT) x 2; + $_->join for @t; - { - lock $destroyed; - is $destroyed, (1 - $dispell) * 4, 'destructors'; - } + { + lock $destroyed; + is $destroyed, (1 - $dispell) * 4, 'destructors'; } } diff --git a/t/41-clone.t b/t/41-clone.t index 0e9c4d1..b6bd2a8 100644 --- a/t/41-clone.t +++ b/t/41-clone.t @@ -24,11 +24,11 @@ BEGIN { use Test::More; # after threads -use Variable::Magic qw/wizard cast dispell getdata getsig VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/; +use Variable::Magic qw/wizard cast dispell getdata VMG_THREADSAFE VMG_OP_INFO_NAME VMG_OP_INFO_OBJECT/; BEGIN { skipall 'This Variable::Magic isn\'t thread safe' unless VMG_THREADSAFE; - plan tests => 2 * 3 + 4 * (2 * 10 + 2) + 4 * (2 * 7 + 2); + plan tests => 2 * 3 + 2 * (2 * 10 + 2) + 2 * (2 * 7 + 2); my $v = $threads::VERSION; diag "Using threads $v" if defined $v; $v = $threads::shared::VERSION; @@ -67,22 +67,22 @@ sub spawn_wiz { } sub try { - my ($dispell, $sig) = @_; + my ($dispell, $wiz) = @_; my $tid = threads->tid(); my $a = 3; - my $res = eval { cast $a, $sig, sub { 5 }->() }; + my $res = eval { cast $a, $wiz, sub { 5 }->() }; is($@, '', "cast in thread $tid doesn't croak"); my $b; eval { $b = $a }; is($@, '', "get in thread $tid doesn't croak"); is($b, 3, "get in thread $tid returns the right thing"); - my $d = eval { getdata $a, $sig }; + my $d = eval { getdata $a, $wiz }; is($@, '', "getdata in thread $tid doesn't croak"); is($d, 5 + $tid, "getdata in thread $tid returns the right thing"); eval { $a = 9 }; is($@, '', "set in thread $tid (check opname) doesn't croak"); if ($dispell) { - $res = eval { dispell $a, $sig }; + $res = eval { dispell $a, $wiz }; is($@, '', "dispell in thread $tid doesn't croak"); undef $b; eval { $b = $a }; @@ -96,7 +96,7 @@ my $wiz_name = spawn_wiz VMG_OP_INFO_NAME; my $wiz_obj = spawn_wiz VMG_OP_INFO_OBJECT; for my $dispell (1, 0) { - for my $sig ($wiz_name, getsig($wiz_name), $wiz_obj, getsig($wiz_obj)) { + for my $wiz ($wiz_name, $wiz_obj) { { lock $c; $c = 0; @@ -106,7 +106,7 @@ for my $dispell (1, 0) { $destroyed = 0; } - my @t = map { threads->create(\&try, $dispell, $sig) } 1 .. 2; + my @t = map { threads->create(\&try, $dispell, $wiz) } 1 .. 2; $_->join for @t; {