1 /* This file is part of the Variable::Magic Perl module.
2 * See http://search.cpan.org/dist/Variable-Magic/ */
4 #include <stdarg.h> /* <va_list>, va_{start,arg,end}, ... */
6 #include <stdio.h> /* sprintf() */
8 #define PERL_NO_GET_CONTEXT
13 #define __PACKAGE__ "Variable::Magic"
15 #define PERL_VERSION_GE(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
17 #define PERL_VERSION_LE(R, V, S) (PERL_REVISION < (R) || (PERL_REVISION == (R) && (PERL_VERSION < (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION <= (S))))))
19 #ifndef VMG_PERL_PATCHLEVEL
21 # define VMG_PERL_PATCHLEVEL PERL_PATCHNUM
23 # define VMG_PERL_PATCHLEVEL 0
27 #define VMG_HAS_PERL_OR(P, R, V, S) ((VMG_PERL_PATCHLEVEL >= (P)) || (!VMG_PERL_PATCHLEVEL && PERL_VERSION_GE((R), (V), (S))))
29 #define VMG_HAS_PERL_AND(P, R, V, S) (PERL_VERSION_GE((R), (V), (S)) && (!VMG_PERL_PATCHLEVEL || (VMG_PERL_PATCHLEVEL >= (P))))
31 /* --- Threads and multiplicity -------------------------------------------- */
41 #ifndef VMG_MULTIPLICITY
42 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
43 # define VMG_MULTIPLICITY 1
45 # define VMG_MULTIPLICITY 0
48 #if VMG_MULTIPLICITY && !defined(tTHX)
49 # define tTHX PerlInterpreter*
52 #if VMG_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
53 # define VMG_THREADSAFE 1
55 # define MY_CXT_CLONE \
57 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
58 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
59 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
62 # define VMG_THREADSAFE 0
64 # define dMY_CXT dNOOP
66 # define MY_CXT vmg_globaldata
68 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
70 # define MY_CXT_INIT NOOP
72 # define MY_CXT_CLONE NOOP
77 STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
78 #define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O))
80 param.stashes = NULL; /* don't need it unless sv is a PVHV */
82 param.proto_perl = owner;
83 return sv_dup(sv, ¶m);
86 #endif /* VMG_THREADSAFE */
88 /* --- Compatibility ------------------------------------------------------- */
91 # define Newx(v, n, c) New(0, v, n, c)
95 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
99 # define mPUSHi(I) PUSHs(sv_2mortal(newSViv(I)))
102 #ifndef PERL_MAGIC_ext
103 # define PERL_MAGIC_ext '~'
118 /* uvar magic and Hash::Util::FieldHash were commited with p28419 */
119 #if VMG_HAS_PERL_AND(28419, 5, 9, 4)
125 #if !defined(VMG_COMPAT_ARRAY_PUSH_NOLEN) && VMG_HAS_PERL_OR(25854, 5, 9, 3)
126 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
128 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
131 /* since 5.9.5 - see #43357 */
132 #if VMG_HAS_PERL_OR(31473, 5, 9, 5)
133 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
135 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
138 #if VMG_HAS_PERL_OR(32969, 5, 11, 0)
139 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
141 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
146 /* Bug-free mg_magical - see http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html - but specialized to our needs. */
147 STATIC void vmg_sv_magicuvar(pTHX_ SV *sv, const char *uf, I32 len) {
148 #define vmg_sv_magicuvar(S, U, L) vmg_sv_magicuvar(aTHX_ (S), (U), (L))
150 sv_magic(sv, NULL, PERL_MAGIC_uvar, uf, len);
151 /* uvar magic has set and get magic, hence this has set SVs_GMG and SVs_SMG. */
153 if ((mg = SvMAGIC(sv))) {
156 const MGVTBL* const vtbl = mg->mg_virtual;
158 if (vtbl->svt_clear) {
163 } while ((mg = mg->mg_moremagic));
167 #endif /* VMG_UVAR */
169 /* --- Context-safe global data -------------------------------------------- */
171 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
173 typedef HV * my_cxt_t;
177 /* --- Signatures ---------------------------------------------------------- */
179 #define SIG_MIN ((U16) (1u << 8))
180 #define SIG_MAX ((U16) ((1u << 16) - 1))
181 #define SIG_NBR (SIG_MAX - SIG_MIN + 1)
182 #define SIG_WIZ ((U16) ((1u << 8) - 1))
184 /* ... Generate signatures ................................................. */
186 STATIC U16 vmg_gensig(pTHX) {
187 #define vmg_gensig() vmg_gensig(aTHX)
193 sig = SIG_NBR * Drand01() + SIG_MIN;
194 } while (hv_exists(MY_CXT, buf, sprintf(buf, "%u", sig)));
199 /* --- MGWIZ structure ----------------------------------------------------- */
206 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
209 #endif /* MGf_COPY */
215 #endif /* MGf_LOCAL */
217 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
218 #endif /* VMG_UVAR */
221 #endif /* VMG_MULTIPLICITY */
224 #define MGWIZ2SV(W) (newSVuv(PTR2UV(W)))
225 #define SV2MGWIZ(S) (INT2PTR(MGWIZ*, SvUVX((SV *) (S))))
227 /* ... Construct private data .............................................. */
229 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, AV *args) {
230 #define vmg_data_new(C, S, A) vmg_data_new(aTHX_ (C), (S), (A))
232 I32 i, alen = (args == NULL) ? 0 : av_len(args);
241 EXTEND(SP, alen + 1);
242 PUSHs(sv_2mortal(newRV_inc(sv)));
243 for (i = 0; i < alen; ++i)
244 PUSHs(*av_fetch(args, i, 0));
247 count = call_sv(ctor, G_SCALAR);
251 if (count != 1) { croak("Callback needs to return 1 scalar\n"); }
253 #if PERL_VERSION_LE(5, 8, 2)
254 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
256 SvREFCNT_inc(nsv); /* Or it will be destroyed in FREETMPS */
267 STATIC SV *vmg_data_get(SV *sv, U16 sig) {
268 MAGIC *mg, *moremagic;
270 if (SvTYPE(sv) >= SVt_PVMG) {
271 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
272 moremagic = mg->mg_moremagic;
273 if ((mg->mg_type == PERL_MAGIC_ext) && (mg->mg_private == sig)) { break; }
275 if (mg) { return mg->mg_obj; }
281 /* ... Magic cast/dispell .................................................. */
284 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
286 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
288 prevmagic->mg_moremagic = moremagic;
290 SvMAGIC_set(sv, moremagic);
292 mg->mg_moremagic = NULL;
293 Safefree(mg->mg_ptr);
296 #endif /* VMG_UVAR */
298 STATIC UV vmg_cast(pTHX_ SV *sv, SV *wiz, AV *args) {
299 #define vmg_cast(S, W, A) vmg_cast(aTHX_ (S), (W), (A))
300 MAGIC *mg = NULL, *moremagic = NULL;
306 if (SvTYPE(sv) >= SVt_PVMG) {
307 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
308 moremagic = mg->mg_moremagic;
309 if ((mg->mg_type == PERL_MAGIC_ext) && (mg->mg_private == w->sig)) { break; }
311 if (mg) { return 1; }
314 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args) : NULL;
315 mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY);
316 mg->mg_private = w->sig;
319 mg->mg_flags |= MGf_COPY;
320 #endif /* MGf_COPY */
323 mg->mg_flags |= MGf_DUP;
327 mg->mg_flags |= MGf_LOCAL;
328 #endif /* MGf_LOCAL */
331 if (w->uvar && SvTYPE(sv) >= SVt_PVHV) {
336 uf[0].uf_val = vmg_svt_val;
343 /* One uvar magic in the chain is enough. */
344 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
345 moremagic = mg->mg_moremagic;
346 if (mg->mg_type == PERL_MAGIC_uvar) { break; }
349 if (mg) { /* Found another uvar magic. */
350 struct ufuncs *olduf = (struct ufuncs *) mg->mg_ptr;
351 if (olduf->uf_val == vmg_svt_val) {
352 /* It's our uvar magic, nothing to do. */
355 /* It's another uvar magic, backup it and replace it by ours. */
357 vmg_uvar_del(sv, prevmagic, mg, moremagic);
362 vmg_sv_magicuvar(sv, (const char *) &uf, sizeof(uf));
366 #endif /* VMG_UVAR */
371 STATIC UV vmg_dispell(pTHX_ SV *sv, U16 sig) {
372 #define vmg_dispell(S, Z) vmg_dispell(aTHX_ (S), (Z))
375 #endif /* VMG_UVAR */
376 MAGIC *mg, *prevmagic, *moremagic = NULL;
378 if (SvTYPE(sv) < SVt_PVMG) { return 0; }
380 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
381 moremagic = mg->mg_moremagic;
382 if (mg->mg_type == PERL_MAGIC_ext) {
383 if (mg->mg_private == sig) {
385 /* If the current has no uvar, short-circuit uvar deletion. */
386 uvars = (SV2MGWIZ(mg->mg_ptr)->uvar) ? (uvars + 1) : 0;
387 #endif /* VMG_UVAR */
390 } else if ((mg->mg_private >= SIG_MIN) &&
391 (mg->mg_private <= SIG_MAX) &&
392 SV2MGWIZ(mg->mg_ptr)->uvar) {
394 /* We can't break here since we need to find the ext magic to delete. */
395 #endif /* VMG_UVAR */
399 if (!mg) { return 0; }
402 prevmagic->mg_moremagic = moremagic;
404 SvMAGIC_set(sv, moremagic);
406 mg->mg_moremagic = NULL;
408 if (mg->mg_obj != sv) { SvREFCNT_dec(mg->mg_obj); } /* Destroy private data */
409 SvREFCNT_dec((SV *) mg->mg_ptr); /* Unreference the wizard */
413 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
414 /* mg was the first ext magic in the chain that had uvar */
416 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
417 if ((mg->mg_type == PERL_MAGIC_ext) &&
418 (mg->mg_private >= SIG_MIN) &&
419 (mg->mg_private <= SIG_MAX) &&
420 SV2MGWIZ(mg->mg_ptr)->uvar) {
428 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
429 moremagic = mg->mg_moremagic;
430 if (mg->mg_type == PERL_MAGIC_uvar) { break; }
433 uf = (struct ufuncs *) mg->mg_ptr;
434 /* assert(uf->uf_val == vmg_svt_val); */
435 if (uf[1].uf_val || uf[1].uf_set) {
436 /* Revert the original uvar magic. */
438 Renew(uf, 1, struct ufuncs);
439 mg->mg_ptr = (char *) uf;
440 mg->mg_len = sizeof(struct ufuncs);
442 /* Remove the uvar magic. */
443 vmg_uvar_del(sv, prevmagic, mg, moremagic);
447 #endif /* VMG_UVAR */
452 /* ... svt callbacks ....................................................... */
454 STATIC int vmg_cb_call(pTHX_ SV *cb, SV *sv, SV *data, unsigned int args, ...) {
467 EXTEND(SP, args + 2);
468 PUSHs(sv_2mortal(newRV_inc(sv)));
469 PUSHs(data ? data : &PL_sv_undef);
471 for (i = 0; i < args; ++i) {
472 SV *sva = va_arg(ap, SV *);
473 PUSHs(sva ? sva : &PL_sv_undef);
478 count = call_sv(cb, G_SCALAR);
482 if (count != 1) { croak("Callback needs to return 1 scalar\n"); }
484 ret = SvOK(svr) ? SvIV(svr) : 0;
494 #define vmg_cb_call1(I, S, D) vmg_cb_call(aTHX_ (I), (S), (D), 0)
495 #define vmg_cb_call2(I, S, D, S2) vmg_cb_call(aTHX_ (I), (S), (D), 1, (S2))
496 #define vmg_cb_call3(I, S, D, S2, S3) vmg_cb_call(aTHX_ (I), (S), (D), 2, (S2), (S3))
498 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
499 return vmg_cb_call1(SV2MGWIZ(mg->mg_ptr)->cb_get, sv, mg->mg_obj);
502 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
503 return vmg_cb_call1(SV2MGWIZ(mg->mg_ptr)->cb_set, sv, mg->mg_obj);
506 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
519 PUSHs(sv_2mortal(newRV_inc(sv)));
520 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
521 if (SvTYPE(sv) == SVt_PVAV) {
522 len = av_len((AV *) sv) + 1;
530 count = call_sv(SV2MGWIZ(mg->mg_ptr)->cb_len, G_SCALAR);
534 if (count != 1) { croak("Callback needs to return 1 scalar\n"); }
536 ret = SvOK(svr) ? SvUV(svr) : len;
546 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
547 return vmg_cb_call1(SV2MGWIZ(mg->mg_ptr)->cb_clear, sv, mg->mg_obj);
550 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
551 /* So that it can survive tmp cleanup in vmg_cb_call */
553 #if !VMG_HAS_PERL_AND(32686, 5, 11, 0)
554 /* The previous magic tokens were freed but the magic chain wasn't updated, so
555 * if you access the sv from the callback the old deleted magics will trigger
556 * and cause memory misreads. Change 32686 solved it that way : */
559 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
560 * mg->mg_ptr reference count */
561 return vmg_cb_call1(SV2MGWIZ(mg->mg_ptr)->cb_free, sv, mg->mg_obj);
565 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
566 # if VMG_HAS_PERL_AND(33256, 5, 11, 0)
575 if (keylen == HEf_SVKEY) {
578 keysv = newSVpvn(key, keylen);
581 ret = vmg_cb_call3(SV2MGWIZ(mg->mg_ptr)->cb_copy, sv, mg->mg_obj, keysv, nsv);
583 if (keylen != HEf_SVKEY) {
589 #endif /* MGf_COPY */
592 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
598 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
599 return vmg_cb_call1(SV2MGWIZ(mg->mg_ptr)->cb_local, nsv, mg->mg_obj);
601 #endif /* MGf_LOCAL */
604 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
609 mg = mg_find(sv, PERL_MAGIC_uvar);
610 /* mg can't be NULL or we wouldn't be there. */
612 uf = (struct ufuncs *) mg->mg_ptr;
614 if (uf[1].uf_val != NULL) { uf[1].uf_val(aTHX_ action, sv); }
615 if (uf[1].uf_set != NULL) { uf[1].uf_set(aTHX_ action, sv); }
617 action &= HV_FETCH_ISSTORE | HV_FETCH_ISEXISTS | HV_FETCH_LVALUE | HV_DELETE;
618 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
620 if ((mg->mg_type != PERL_MAGIC_ext)
621 || (mg->mg_private < SIG_MIN)
622 || (mg->mg_private > SIG_MAX)) { continue; }
623 w = SV2MGWIZ(mg->mg_ptr);
624 if (!w->uvar) { continue; }
627 if (w->cb_fetch) { vmg_cb_call2(w->cb_fetch, sv, mg->mg_obj, key); }
629 case HV_FETCH_ISSTORE:
630 case HV_FETCH_LVALUE:
631 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
632 if (w->cb_store) { vmg_cb_call2(w->cb_store, sv, mg->mg_obj, key); }
634 case HV_FETCH_ISEXISTS:
635 if (w->cb_exists) { vmg_cb_call2(w->cb_exists, sv, mg->mg_obj, key); }
638 if (w->cb_delete) { vmg_cb_call2(w->cb_delete, sv, mg->mg_obj, key); }
645 #endif /* VMG_UVAR */
647 /* ... Wizard destructor ................................................... */
649 STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) {
653 if (PL_dirty) /* during global destruction, the context is already freed */
658 if (w->owner != aTHX)
661 #endif /* VMG_MULTIPLICITY */
665 if (hv_delete(MY_CXT, buf, sprintf(buf, "%u", w->sig), 0) != wiz)
668 SvFLAGS(wiz) |= SVf_BREAK;
671 if (w->cb_data != NULL) { SvREFCNT_dec(SvRV(w->cb_data)); }
672 if (w->cb_get != NULL) { SvREFCNT_dec(SvRV(w->cb_get)); }
673 if (w->cb_set != NULL) { SvREFCNT_dec(SvRV(w->cb_set)); }
674 if (w->cb_len != NULL) { SvREFCNT_dec(SvRV(w->cb_len)); }
675 if (w->cb_clear != NULL) { SvREFCNT_dec(SvRV(w->cb_clear)); }
676 if (w->cb_free != NULL) { SvREFCNT_dec(SvRV(w->cb_free)); }
678 if (w->cb_copy != NULL) { SvREFCNT_dec(SvRV(w->cb_copy)); }
679 #endif /* MGf_COPY */
681 if (w->cb_dup != NULL) { SvREFCNT_dec(SvRV(w->cb_dup)); }
684 if (w->cb_local != NULL) { SvREFCNT_dec(SvRV(w->cb_local)); }
685 #endif /* MGf_LOCAL */
687 if (w->cb_fetch != NULL) { SvREFCNT_dec(SvRV(w->cb_fetch)); }
688 if (w->cb_store != NULL) { SvREFCNT_dec(SvRV(w->cb_store)); }
689 if (w->cb_exists != NULL) { SvREFCNT_dec(SvRV(w->cb_exists)); }
690 if (w->cb_delete != NULL) { SvREFCNT_dec(SvRV(w->cb_delete)); }
691 #endif /* VMG_UVAR */
699 STATIC MGVTBL vmg_wizard_vtbl = {
704 vmg_wizard_free, /* free */
707 #endif /* MGf_COPY */
713 #endif /* MGf_LOCAL */
716 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
717 STATIC const char vmg_invalid_sv[] = "Invalid variable";
718 STATIC const char vmg_invalid_sig[] = "Invalid numeric signature";
719 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
720 STATIC const char vmg_toomanysigs[] = "Too many magic signatures used";
721 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
723 STATIC U16 vmg_sv2sig(pTHX_ SV *sv) {
724 #define vmg_sv2sig(S) vmg_sv2sig(aTHX_ (S))
729 } else if (SvNOK(sv)) {
731 } else if ((SvPOK(sv) && grok_number(SvPVX(sv), SvCUR(sv), NULL))) {
734 croak(vmg_invalid_sig);
736 if (sig < SIG_MIN) { sig += SIG_MIN; }
737 if (sig > SIG_MAX) { sig %= SIG_MAX + 1; }
742 STATIC U16 vmg_wizard_sig(pTHX_ SV *wiz) {
743 #define vmg_wizard_sig(W) vmg_wizard_sig(aTHX_ (W))
748 sig = SV2MGWIZ(SvRV(wiz))->sig;
749 } else if (SvOK(wiz)) {
750 sig = vmg_sv2sig(wiz);
752 croak(vmg_invalid_wiz);
757 if (!hv_fetch(MY_CXT, buf, sprintf(buf, "%u", sig), 0))
763 STATIC SV *vmg_wizard_wiz(pTHX_ SV *wiz) {
764 #define vmg_wizard_wiz(W) vmg_wizard_wiz(aTHX_ (W))
772 if (SV2MGWIZ(wiz)->owner == aTHX)
774 #endif /* VMG_MULTIPLICITY */
775 sig = SV2MGWIZ(wiz)->sig;
776 } else if (SvOK(wiz)) {
777 sig = vmg_sv2sig(wiz);
779 croak(vmg_invalid_wiz);
784 return (old = hv_fetch(MY_CXT, buf, sprintf(buf, "%u", sig), 0))
789 #define VMG_SET_CB(S, N) \
791 w->cb_ ## N = (SvOK(cb) && SvROK(cb)) ? newRV_inc(SvRV(cb)) : NULL;
793 #define VMG_SET_SVT_CB(S, N) \
795 if (SvOK(cb) && SvROK(cb)) { \
796 t->svt_ ## N = vmg_svt_ ## N; \
797 w->cb_ ## N = newRV_inc(SvRV(cb)); \
799 t->svt_ ## N = NULL; \
800 w->cb_ ## N = NULL; \
805 #define VMG_CLONE_CB(N) \
806 z->cb_ ## N = (w->cb_ ## N) ? newRV_noinc(vmg_clone(SvRV(w->cb_ ## N), \
810 STATIC MGWIZ *vmg_wizard_clone(pTHX_ const MGWIZ *w) {
811 #define vmg_wizard_clone(W) vmg_wizard_clone(aTHX_ (W))
816 Copy(w->vtbl, t, 1, MGVTBL);
827 #endif /* MGf_COPY */
833 #endif /* MGf_LOCAL */
837 VMG_CLONE_CB(exists);
838 VMG_CLONE_CB(delete);
839 #endif /* VMG_UVAR */
848 #endif /* VMG_THREADSAFE */
850 /* --- XS ------------------------------------------------------------------ */
852 MODULE = Variable::Magic PACKAGE = Variable::Magic
861 hv_iterinit(MY_CXT); /* Allocate iterator */
862 stash = gv_stashpv(__PACKAGE__, 1);
863 newCONSTSUB(stash, "SIG_MIN", newSVuv(SIG_MIN));
864 newCONSTSUB(stash, "SIG_MAX", newSVuv(SIG_MAX));
865 newCONSTSUB(stash, "SIG_NBR", newSVuv(SIG_NBR));
866 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
867 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
868 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
869 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
870 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
871 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
872 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
873 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
874 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
875 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
876 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
877 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
891 hv_iterinit(hv); /* Allocate iterator */
893 while ((key = hv_iternext(MY_CXT))) {
895 char *sig = HePV(key, len);
898 sv = MGWIZ2SV(vmg_wizard_clone(SV2MGWIZ(HeVAL(key))));
899 mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
900 mg->mg_private = SIG_WIZ;
902 hv_store(hv, sig, len, sv, HeHASH(key));
909 #endif /* VMG_THREADSAFE */
929 #endif /* MGf_COPY */
935 #endif /* MGf_LOCAL */
938 #endif /* VMG_UVAR */
939 ) { croak(vmg_wrongargnum); }
944 sig = vmg_sv2sig(svsig);
945 if ((old = hv_fetch(MY_CXT, buf, sprintf(buf, "%u", sig), 0))) {
946 ST(0) = sv_2mortal(newRV_inc(*old));
950 if (HvKEYS(MY_CXT) >= SIG_NBR) { croak(vmg_toomanysigs); }
957 VMG_SET_CB(ST(i++), data);
958 VMG_SET_SVT_CB(ST(i++), get);
959 VMG_SET_SVT_CB(ST(i++), set);
960 VMG_SET_SVT_CB(ST(i++), len);
961 VMG_SET_SVT_CB(ST(i++), clear);
962 VMG_SET_SVT_CB(ST(i++), free);
964 VMG_SET_SVT_CB(ST(i++), copy);
965 #endif /* MGf_COPY */
967 /* VMG_SET_SVT_CB(ST(i++), dup); */
973 VMG_SET_SVT_CB(ST(i++), local);
974 #endif /* MGf_LOCAL */
976 VMG_SET_CB(ST(i++), fetch);
977 VMG_SET_CB(ST(i++), store);
978 VMG_SET_CB(ST(i++), exists);
979 VMG_SET_CB(ST(i++), delete);
980 #endif /* VMG_UVAR */
983 #endif /* VMG_MULTIPLICITY */
988 w->uvar = (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete);
989 #endif /* VMG_UVAR */
992 mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
993 mg->mg_private = SIG_WIZ;
996 hv_store(MY_CXT, buf, sprintf(buf, "%u", sig), sv, 0);
998 RETVAL = newRV_noinc(sv);
1006 if (HvKEYS(MY_CXT) >= SIG_NBR) { croak(vmg_toomanysigs); }
1007 RETVAL = newSVuv(vmg_gensig());
1014 if (!SvROK(wiz)) { croak(vmg_invalid_wiz); }
1015 RETVAL = newSVuv(SV2MGWIZ(SvRV(wiz))->sig);
1019 SV *cast(SV *sv, SV *wiz, ...)
1020 PROTOTYPE: \[$@%&*]$@
1025 wiz = vmg_wizard_wiz(wiz);
1031 av_fill(args, items - 2);
1032 for (i = 2; i < items; ++i) {
1035 if (av_store(args, i - 2, arg) == NULL) { croak(vmg_argstorefailed); }
1038 ret = newSVuv(vmg_cast(SvRV(sv), wiz, args));
1044 SV *getdata(SV *sv, SV *wiz)
1045 PROTOTYPE: \[$@%&*]$
1050 sig = vmg_wizard_sig(wiz);
1053 data = vmg_data_get(SvRV(sv), sig);
1054 if (!data) { XSRETURN_UNDEF; }
1058 SV *dispell(SV *sv, SV *wiz)
1059 PROTOTYPE: \[$@%&*]$
1063 sig = vmg_wizard_sig(wiz);
1066 RETVAL = newSVuv(vmg_dispell(SvRV(sv), sig));