Makefile.PL
README
lib/Variable/Magic.pm
+ptable.h
samples/copy.pl
samples/magic.pl
samples/synopsis.pl
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
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 */
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;
#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 .............................................. */
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;
}
}
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;
}
}
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;
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 */
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;
}
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);
}
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);
}
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);
# 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) {
#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 */
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;
}
#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); \
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
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));
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);
}
{
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;
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 */
#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);
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
AV *args = NULL;
SV *ret;
CODE:
- wiz = vmg_wizard_wiz(wiz);
+ wiz = vmg_wizard_validate(wiz);
if (items > 2) {
I32 i;
args = newAV();
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;
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
=item *
-C<sig>
-
-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<deprecated> and will be removed in december 2009.
-
-=item *
-
C<data>
A code reference to a private data constructor.
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;
return $ret;
}
-=head2 C<gensig>
-
-With this tool, you can manually generate random magic signature between SIG_MIN and SIG_MAX inclusive.
-That's the way L</wizard> creates them when no signature is supplied.
-
- # Generate a signature
- my $sig = gensig;
-
-This function is B<deprecated> and will be removed in december 2009.
-
-=head2 C<getsig>
-
- getsig $wiz
-
-This accessor returns the magic signature of this wizard.
-
- # Get $wiz signature
- my $sig = getsig $wiz;
-
-This function is B<deprecated> and will be removed in december 2009.
-
=head2 C<cast>
cast [$@%&*]var, $wiz, ...
=head1 CONSTANTS
-=head2 C<SIG_MIN>
-
-The minimum integer used as a signature for user-defined magic.
-
-This constant is B<deprecated> and will be removed in december 2009.
-
-=head2 C<SIG_MAX>
-
-The maximum integer used as a signature for user-defined magic.
-
-This constant is B<deprecated> and will be removed in december 2009.
-
-=head2 C<SIG_NBR>
-
- SIG_NBR = SIG_MAX - SIG_MIN + 1
-
-This constant is B<deprecated> and will be removed in december 2009.
-
=head2 C<MGf_COPY>
Evaluates to true iff the 'copy' magic is available.
=head1 EXPORT
-The functions L</wizard>, L</gensig>, L</getsig>, L</cast>, L</getdata> and L</dispell> are only exported on request.
+The functions L</wizard>, L</cast>, L</getdata> and L</dispell> 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'>.
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/,
--- /dev/null
+/* 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
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" },
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;
my $b = $a; # "got 3!"
$a = 3; # "now set to 4!"
$b = 3; # (nothing)
-dispell $a, $sig;
-$a = 4; # (nothing)
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" },
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
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;
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');
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');
+++ /dev/null
-#!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');
-
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 }
};
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');
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');
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');
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;
$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');
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;
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];
}
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';
}
}
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;
}
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 };
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;
$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;
{