# define PERL_MAGIC_ext '~'
#endif
+#ifndef MGf_COPY
+# define MGf_COPY 0
+#endif /* !MGf_COPY */
+
+#ifndef MGf_DUP
+# define MGf_DUP 0
+#endif /* !MGf_DUP */
+
+#ifndef MGf_LOCAL
+# define MGf_LOCAL 0
+#endif /* !MGf_LOCAL */
+
/* --- Our sv_magicext ----------------------------------------------------- */
#ifdef sv_magicext
typedef struct {
MGVTBL *vtbl;
U16 sig;
- SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free, *cb_data;
+ SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
+#if MGf_COPY
+ SV *cb_copy;
+#endif /* MGf_COPY */
+#if MGf_DUP
+ SV *cb_dup;
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ SV *cb_local;
+#endif /* MGf_LOCAL */
+ SV *cb_data;
} MGWIZ;
#define MGWIZ2SV(W) (newSVuv(PTR2UV(W)))
data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args) : NULL;
mg = vmg_sv_magicext(sv, data, w->vtbl, wiz, HEf_SVKEY);
mg->mg_private = w->sig;
+ mg->mg_flags = mg->mg_flags
+#if MGf_COPY
+ | MGf_COPY
+#endif /* MGf_COPY */
+#if MGf_DUP
+ | MGf_DUP
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ | MGf_LOCAL
+#endif /* MGf_LOCAL */
+ ;
return 1;
}
return vmg_cb_call(SV2MGWIZ(mg->mg_ptr)->cb_free, sv, mg->mg_obj);
}
+#if MGf_COPY
+STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *name, int namelen) {
+ int ret;
+
+ dSP;
+ int count;
+
+ ENTER;
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ XPUSHs(sv_2mortal(newRV_inc(sv)));
+ XPUSHs((mg->mg_obj) ? (mg->mg_obj) : &PL_sv_undef);
+ XPUSHs(sv_mortalcopy(nsv));
+ PUTBACK;
+
+ count = call_sv(SV2MGWIZ(mg->mg_ptr)->cb_copy, G_SCALAR);
+
+ SPAGAIN;
+
+ if (count != 1) { croak("Callback needs to return 1 scalar\n"); }
+ ret = POPi;
+
+ PUTBACK;
+
+ FREETMPS;
+ LEAVE;
+
+ return ret;
+}
+#endif /* MGf_COPY */
+
+#if MGf_DUP
+STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *p) {
+ return 0;
+}
+#endif /* MGf_DUP */
+
+#if MGf_LOCAL
+STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
+ return vmg_cb_call(SV2MGWIZ(mg->mg_ptr)->cb_local, nsv, mg->mg_obj);
+}
+#endif /* MGf_LOCAL */
+
/* ... Wizard destructor ................................................... */
STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) {
if (w->cb_len != NULL) { SvREFCNT_dec(SvRV(w->cb_len)); }
if (w->cb_clear != NULL) { SvREFCNT_dec(SvRV(w->cb_clear)); }
if (w->cb_free != NULL) { SvREFCNT_dec(SvRV(w->cb_free)); }
+#if MGf_COPY
+ if (w->cb_copy != NULL) { SvREFCNT_dec(SvRV(w->cb_copy)); }
+#endif /* MGf_COPY */
+#if MGf_DUP
+ if (w->cb_dup != NULL) { SvREFCNT_dec(SvRV(w->cb_dup)); }
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ if (w->cb_local != NULL) { SvREFCNT_dec(SvRV(w->cb_local)); }
+#endif /* MGf_LOCAL */
if (w->cb_data != NULL) { SvREFCNT_dec(SvRV(w->cb_data)); }
Safefree(w->vtbl);
Safefree(w);
NULL, /* len */
NULL, /* clear */
vmg_wizard_free, /* free */
-#ifdef MGf_COPY
+#if MGf_COPY
NULL, /* copy */
#endif /* MGf_COPY */
-#ifdef MGf_DUP
+#if MGf_DUP
NULL, /* dup */
#endif /* MGf_DUP */
+#if MGf_LOCAL
+ NULL, /* local */
+#endif /* MGf_LOCAL */
};
+/* --- Error messages and misc helpers ------------------------------------- */
+
+STATIC const char vmg__wizard_args[] = "_wizard() called with a wrong number of arguments - use wizard() instead";
STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
STATIC const char vmg_invalid_sv[] = "Invalid variable";
STATIC const char vmg_invalid_sig[] = "Invalid numeric signature";
return sig;
}
+#define VMG_SET_CB(T, V, M, CB) \
+ cb = (CB); \
+ if (SvROK(cb)) { \
+ (V)->svt_##T = vmg_svt_##T; (M)->cb_##T = newRV_inc(SvRV(cb)); \
+ } else { \
+ (V)->svt_##T = NULL; (M)->cb_##T = NULL; \
+ }
+
/* --- XS ------------------------------------------------------------------ */
MODULE = Variable::Magic PACKAGE = Variable::Magic
MY_CXT.wizz = newHV();
MY_CXT.count = 0;
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, "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));
}
-SV *_wizard(SV *svsig, SV *cb_get, SV *cb_set, SV *cb_len, SV *cb_clear, SV *cb_free, SV *cb_data)
-PROTOTYPE: $&&&&&&
+SV *_wizard(SV *svsig, ...)
+PROTOTYPE: $@
PREINIT:
U16 sig;
+ I32 i;
char buf[8];
MGWIZ *w;
MGVTBL *t;
MAGIC *mg;
- SV *sv;
+ SV *cb, *sv;
CODE:
dMY_CXT;
+
+ if (items != 7
+#if MGf_COPY
+ + 1
+#endif /* MGf_COPY */
+#if MGf_DUP
+ + 1
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ + 1
+#endif /* MGf_LOCAL */
+ ) { croak(vmg__wizard_args); }
+
if (SvOK(svsig)) {
SV **old;
sig = vmg_sv2sig(svsig);
if (MY_CXT.count >= SIG_NBR) { croak(vmg_toomanysigs); }
sig = vmg_gensig();
}
-
- Newx(t, 1, MGVTBL);
- t->svt_get = (SvOK(cb_get)) ? vmg_svt_get : NULL;
- t->svt_set = (SvOK(cb_set)) ? vmg_svt_set : NULL;
- t->svt_len = (SvOK(cb_len)) ? vmg_svt_len : NULL;
- t->svt_clear = (SvOK(cb_clear)) ? vmg_svt_clear : NULL;
- t->svt_free = (SvOK(cb_free)) ? vmg_svt_free : NULL;
-#ifdef MGf_COPY
- t->svt_copy = NULL;
-#endif /* MGf_COPY */
-#ifdef MGf_DUP
- t->svt_dup = NULL;
-#endif /* MGf_DUP */
+ Newx(t, 1, MGVTBL);
Newx(w, 1, MGWIZ);
w->vtbl = t;
w->sig = sig;
- w->cb_get = (SvROK(cb_get)) ? newRV_inc(SvRV(cb_get)) : NULL;
- w->cb_set = (SvROK(cb_set)) ? newRV_inc(SvRV(cb_set)) : NULL;
- w->cb_len = (SvROK(cb_len)) ? newRV_inc(SvRV(cb_len)) : NULL;
- w->cb_clear = (SvROK(cb_clear)) ? newRV_inc(SvRV(cb_clear)) : NULL;
- w->cb_free = (SvROK(cb_free)) ? newRV_inc(SvRV(cb_free)) : NULL;
- w->cb_data = (SvROK(cb_data)) ? newRV_inc(SvRV(cb_data)) : NULL;
+
+ cb = ST(1); w->cb_data = SvROK(cb) ? newRV_inc(SvRV(cb)) : NULL;
+ VMG_SET_CB(get, t, w, ST(2));
+ VMG_SET_CB(set, t, w, ST(3));
+ VMG_SET_CB(len, t, w, ST(4));
+ VMG_SET_CB(clear, t, w, ST(5));
+ VMG_SET_CB(free, t, w, ST(6));
+#if MGf_COPY
+ VMG_SET_CB(copy, t, w, ST(7));
+#endif /* MGf_COPY */
+#if MGf_DUP
+ VMG_SET_CB(dup, t, w, ST(8));
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ VMG_SET_CB(local, t, w, ST(9));
+#endif /* MGf_LOCAL */
sv = MGWIZ2SV(w);
mg = vmg_sv_magicext(sv, NULL, &vmg_wizard_vtbl, NULL, -1);
mg->mg_private = SIG_WIZ;
+#if MGf_COPY
+ if (t->svt_copy) { mg->mg_flags |= MGf_COPY; }
+#endif /* MGf_COPY */
+#if MGf_DUP
+ if (t->svt_dup) { mg->mg_flags |= MGf_DUP; }
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ if (t->svt_local) { mg->mg_flags |= MGf_LOCAL; }
+#endif /* MGf_LOCAL */
hv_store(MY_CXT.wizz, buf, sprintf(buf, "%u", sig), sv, 0);
++MY_CXT.count;
--- /dev/null
+#!perl -T
+
+use strict;
+use warnings;
+
+use Test::More;
+
+use Variable::Magic qw/wizard cast MGf_COPY/;
+
+if (!MGf_COPY) {
+ plan skip_all => "this perl doesn't handle copy magic";
+} else {
+ plan tests => 16;
+}
+
+my $c = 0;
+my $wiz = wizard copy => sub { ++$c };
+ok($c == 0, 'copy : create wizard');
+
+use Tie::Array;
+
+tie my @a, 'Tie::StdArray';
+cast @a, $wiz;
+ok($c == 0, 'copy (array) : cast');
+
+my $n = time;
+$a[0] = $n;
+ok($c == 1, 'copy (array) : store element');
+
+my $e = exists $a[0];
+ok($c == 2, 'copy (array) : exists element');
+ok($e, 'copy (array) : exists element, really');
+
+my $b = $a[0];
+ok($c == 3, 'copy (array) : fetch element');
+ok($b == $n, 'copy (array) : fetch element correctly');
+
+use Tie::Hash;
+
+$c = 0;
+
+tie my %h, 'Tie::StdHash';
+cast %h, $wiz;
+ok($c == 0, 'copy (hash) : cast');
+
+my ($k, $v) = (time, int rand time);
+$h{$k} = $v;
+ok($c == 1, 'copy (hash) : store element');
+
+$e = exists $h{$k};
+ok($c == 2, 'copy (hash) : exists element');
+ok($e, 'copy (hash) : exists element, really');
+
+my $w = $h{$k};
+ok($c == 3, 'copy (hash) : fetch element');
+ok($w == $v, 'copy (hash) : fetch element correctly');
+
+my ($K, $V) = each %h;
+ok($c == 4, 'copy (hash) : iterate');
+ok($k == $K && $v == $V, 'copy (hash) : iterate correctly');
+
+delete $h{$k};
+ok($c == 5, 'copy (hash) : delete');