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 #ifndef VMG_PERL_PATCHLEVEL
17 # define VMG_PERL_PATCHLEVEL PERL_PATCHNUM
19 # define VMG_PERL_PATCHLEVEL 0
23 #define VMG_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
25 #define VMG_HAS_PERL_BRANCH(R, V, S) (PERL_REVISION == (R) && PERL_VERSION == (V) && PERL_SUBVERSION >= (S))
27 #define VMG_HAS_PERL_MAINT(R, V, S, P) (PERL_REVISION == (R) && PERL_VERSION == (V) && (VMG_PERL_PATCHLEVEL >= (P) || (!VMG_PERL_PATCHLEVEL && PERL_SUBVERSION >= (S))))
29 /* --- Threads and multiplicity -------------------------------------------- */
39 #ifndef VMG_MULTIPLICITY
40 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
41 # define VMG_MULTIPLICITY 1
43 # define VMG_MULTIPLICITY 0
46 #if VMG_MULTIPLICITY && !defined(tTHX)
47 # define tTHX PerlInterpreter*
50 #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))
51 # define VMG_THREADSAFE 1
53 # define MY_CXT_CLONE \
55 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
56 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
57 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
60 # define VMG_THREADSAFE 0
62 # define dMY_CXT dNOOP
64 # define MY_CXT vmg_globaldata
66 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
68 # define MY_CXT_INIT NOOP
70 # define MY_CXT_CLONE NOOP
75 STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
76 #define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O))
78 param.stashes = NULL; /* don't need it unless sv is a PVHV */
80 param.proto_perl = owner;
81 return sv_dup(sv, ¶m);
84 #endif /* VMG_THREADSAFE */
86 /* --- Compatibility ------------------------------------------------------- */
89 # define Newx(v, n, c) New(0, v, n, c)
93 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
97 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
101 # define SvPV_const SvPV
104 #ifndef PERL_MAGIC_ext
105 # define PERL_MAGIC_ext '~'
108 #ifndef PERL_MAGIC_tied
109 # define PERL_MAGIC_tied 'P'
124 #ifndef IN_PERL_COMPILETIME
125 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
128 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
129 # ifndef PL_error_count
130 # define PL_error_count PL_parser->error_count
133 # ifndef PL_error_count
134 # define PL_error_count PL_Ierror_count
138 /* uvar magic and Hash::Util::FieldHash were commited with 28419 */
139 #if VMG_HAS_PERL_MAINT(5, 9, 4, 28419) || VMG_HAS_PERL(5, 10, 0)
145 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160 */
146 #ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
147 # if VMG_HAS_PERL_MAINT(5, 8, 9, 28160) || VMG_HAS_PERL_MAINT(5, 9, 3, 25854) || VMG_HAS_PERL(5, 10, 0)
148 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
150 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
154 /* Applied to dev-5.11 as 34908 */
155 #if VMG_HAS_PERL_MAINT(5, 11, 0, 34908)
156 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
158 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
161 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
162 #if VMG_HAS_PERL_MAINT(5, 8, 9, 32542) || VMG_HAS_PERL_MAINT(5, 9, 5, 31473) || VMG_HAS_PERL(5, 10, 0)
163 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
165 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
168 #if VMG_HAS_PERL_MAINT(5, 11, 0, 32969)
169 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
171 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
176 /* Bug-free mg_magical - see http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html - but specialized to our needs. */
177 STATIC void vmg_sv_magicuvar(pTHX_ SV *sv, const char *uf, I32 len) {
178 #define vmg_sv_magicuvar(S, U, L) vmg_sv_magicuvar(aTHX_ (S), (U), (L))
180 sv_magic(sv, NULL, PERL_MAGIC_uvar, uf, len);
181 /* uvar magic has set and get magic, hence this has set SVs_GMG and SVs_SMG. */
182 if ((mg = SvMAGIC(sv))) {
185 const MGVTBL* const vtbl = mg->mg_virtual;
187 if (vtbl->svt_clear) {
192 } while ((mg = mg->mg_moremagic));
196 #endif /* VMG_UVAR */
198 /* --- Stolen chunk of B --------------------------------------------------- */
216 STATIC const char *const vmg_opclassnames[] = {
231 STATIC opclass vmg_opclass(const OP *o) {
238 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
240 if (o->op_type == OP_SASSIGN)
241 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
243 if (o->op_type == OP_AELEMFAST) {
244 if (o->op_flags & OPf_SPECIAL)
255 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
259 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
276 case OA_PVOP_OR_SVOP:
277 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
282 case OA_BASEOP_OR_UNOP:
283 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
285 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
287 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
289 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
292 if (o->op_flags & OPf_STACKED)
294 else if (o->op_flags & OPf_SPECIAL)
303 /* --- Context-safe global data -------------------------------------------- */
305 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
309 HV *b__op_stashes[OPc_MAX];
314 /* --- Error messages ------------------------------------------------------ */
316 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
317 STATIC const char vmg_invalid_sig[] = "Invalid numeric signature";
318 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
319 STATIC const char vmg_toomanysigs[] = "Too many magic signatures used";
320 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
321 STATIC const char vmg_globstorefail[] = "Couldn't store global wizard information";
323 /* --- Signatures ---------------------------------------------------------- */
325 #define SIG_MIN ((U16) 0u)
326 #define SIG_MAX ((U16) ((1u << 16) - 1))
327 #define SIG_NBR (SIG_MAX - SIG_MIN + 1)
329 #define SIG_WZO ((U16) (0x3891))
330 #define SIG_WIZ ((U16) (0x3892))
332 /* ... Generate signatures ................................................. */
334 STATIC U16 vmg_gensig(pTHX) {
335 #define vmg_gensig() vmg_gensig(aTHX)
340 if (HvKEYS(MY_CXT.wizards) >= SIG_NBR) croak(vmg_toomanysigs);
343 sig = SIG_NBR * Drand01() + SIG_MIN;
344 } while (hv_exists(MY_CXT.wizards, buf, sprintf(buf, "%u", sig)));
349 /* --- MGWIZ structure ----------------------------------------------------- */
359 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
362 #endif /* MGf_COPY */
368 #endif /* MGf_LOCAL */
370 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
371 #endif /* VMG_UVAR */
374 #endif /* VMG_MULTIPLICITY */
377 #define MGWIZ2SV(W) (newSVuv(PTR2UV(W)))
378 #define SV2MGWIZ(S) (INT2PTR(MGWIZ*, SvUVX((SV *) (S))))
380 /* ... Construct private data .............................................. */
382 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, AV *args) {
383 #define vmg_data_new(C, S, A) vmg_data_new(aTHX_ (C), (S), (A))
385 I32 i, alen = (args == NULL) ? 0 : av_len(args);
393 EXTEND(SP, alen + 1);
394 PUSHs(sv_2mortal(newRV_inc(sv)));
395 for (i = 0; i < alen; ++i)
396 PUSHs(*av_fetch(args, i, 0));
399 call_sv(ctor, G_SCALAR);
403 #if VMG_HAS_PERL(5, 8, 3)
404 SvREFCNT_inc(nsv); /* Or it will be destroyed in FREETMPS */
406 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
416 STATIC SV *vmg_data_get(SV *sv, U16 sig) {
417 MAGIC *mg, *moremagic;
419 if (SvTYPE(sv) >= SVt_PVMG) {
420 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
421 moremagic = mg->mg_moremagic;
422 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
423 MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
435 /* ... Magic cast/dispell .................................................. */
438 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
440 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
442 prevmagic->mg_moremagic = moremagic;
444 SvMAGIC_set(sv, moremagic);
446 mg->mg_moremagic = NULL;
447 Safefree(mg->mg_ptr);
450 #endif /* VMG_UVAR */
452 STATIC UV vmg_cast(pTHX_ SV *sv, SV *wiz, AV *args) {
453 #define vmg_cast(S, W, A) vmg_cast(aTHX_ (S), (W), (A))
454 MAGIC *mg = NULL, *moremagic = NULL;
457 U32 oldgmg = SvGMAGICAL(sv);
461 if (SvTYPE(sv) >= SVt_PVMG) {
462 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
463 moremagic = mg->mg_moremagic;
464 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
465 MGWIZ *z = SV2MGWIZ(mg->mg_ptr);
466 if (z->sig == w->sig)
474 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args) : NULL;
475 mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY);
476 mg->mg_private = SIG_WIZ;
479 mg->mg_flags |= MGf_COPY;
480 #endif /* MGf_COPY */
483 mg->mg_flags |= MGf_DUP;
487 mg->mg_flags |= MGf_LOCAL;
488 #endif /* MGf_LOCAL */
490 if (SvTYPE(sv) < SVt_PVHV)
493 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
494 * magic is actually never called for them. If the GMAGICAL flag was off before
495 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
496 * now on, then this wizard has get magic. Hence we can work around the
497 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
498 * has uvar callbacks, it will be turned back on later. */
499 if (!oldgmg && SvGMAGICAL(sv))
507 uf[0].uf_val = vmg_svt_val;
514 /* One uvar magic in the chain is enough. */
515 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
516 moremagic = mg->mg_moremagic;
517 if (mg->mg_type == PERL_MAGIC_uvar)
521 if (mg) { /* Found another uvar magic. */
522 struct ufuncs *olduf = (struct ufuncs *) mg->mg_ptr;
523 if (olduf->uf_val == vmg_svt_val) {
524 /* It's our uvar magic, nothing to do. oldgmg was true. */
527 /* It's another uvar magic, backup it and replace it by ours. */
529 vmg_uvar_del(sv, prevmagic, mg, moremagic);
533 vmg_sv_magicuvar(sv, (const char *) &uf, sizeof(uf));
534 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
535 * handled by our uvar callback. */
537 #endif /* VMG_UVAR */
543 STATIC UV vmg_dispell(pTHX_ SV *sv, U16 sig) {
544 #define vmg_dispell(S, Z) vmg_dispell(aTHX_ (S), (Z))
547 #endif /* VMG_UVAR */
548 MAGIC *mg, *prevmagic, *moremagic = NULL;
550 if (SvTYPE(sv) < SVt_PVMG)
553 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
554 moremagic = mg->mg_moremagic;
555 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
556 MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
559 /* If the current has no uvar, short-circuit uvar deletion. */
560 uvars = w->uvar ? (uvars + 1) : 0;
561 #endif /* VMG_UVAR */
564 } else if (w->uvar) {
566 /* We can't break here since we need to find the ext magic to delete. */
567 #endif /* VMG_UVAR */
575 prevmagic->mg_moremagic = moremagic;
577 SvMAGIC_set(sv, moremagic);
579 mg->mg_moremagic = NULL;
581 /* Destroy private data */
582 if (mg->mg_obj != sv)
583 SvREFCNT_dec(mg->mg_obj);
584 /* Unreference the wizard */
585 SvREFCNT_dec((SV *) mg->mg_ptr);
589 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
590 /* mg was the first ext magic in the chain that had uvar */
592 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
593 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
594 MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
604 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
605 moremagic = mg->mg_moremagic;
606 if (mg->mg_type == PERL_MAGIC_uvar)
610 uf = (struct ufuncs *) mg->mg_ptr;
611 /* assert(uf->uf_val == vmg_svt_val); */
612 if (uf[1].uf_val || uf[1].uf_set) {
613 /* Revert the original uvar magic. */
615 Renew(uf, 1, struct ufuncs);
616 mg->mg_ptr = (char *) uf;
617 mg->mg_len = sizeof(struct ufuncs);
619 /* Remove the uvar magic. */
620 vmg_uvar_del(sv, prevmagic, mg, moremagic);
624 #endif /* VMG_UVAR */
629 /* ... OP info ............................................................. */
631 #define VMG_OP_INFO_NAME 1
632 #define VMG_OP_INFO_OBJECT 2
635 STATIC perl_mutex vmg_op_name_init_mutex;
638 STATIC U32 vmg_op_name_init = 0;
639 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
641 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
642 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
644 case VMG_OP_INFO_NAME:
646 MUTEX_LOCK(&vmg_op_name_init_mutex);
648 if (!vmg_op_name_init) {
650 for (t = 0; t < OP_max; ++t)
651 vmg_op_name_len[t] = strlen(PL_op_name[t]);
652 vmg_op_name_init = 1;
655 MUTEX_UNLOCK(&vmg_op_name_init_mutex);
658 case VMG_OP_INFO_OBJECT: {
660 if (!MY_CXT.b__op_stashes[0]) {
663 for (c = 0; c < OPc_MAX; ++c)
664 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
673 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
674 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
679 case VMG_OP_INFO_NAME: {
680 OPCODE t = PL_op->op_type;
681 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
683 case VMG_OP_INFO_OBJECT: {
685 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
686 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
695 /* ... svt callbacks ....................................................... */
697 #define VMG_CB_CALL_SET_RET(D) \
702 ret = SvOK(svr) ? SvIV(svr) : (D); \
706 #define VMG_CB_CALL_ARGS_MASK 15
707 #define VMG_CB_CALL_ARGS_SHIFT 4
708 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT)
710 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
713 unsigned int i, args, opinfo;
717 args = flags & VMG_CB_CALL_ARGS_MASK;
718 flags >>= VMG_CB_CALL_ARGS_SHIFT;
719 opinfo = flags & VMG_CB_CALL_OPINFO;
725 EXTEND(SP, args + 1);
726 PUSHs(sv_2mortal(newRV_inc(sv)));
728 for (i = 0; i < args; ++i) {
729 SV *sva = va_arg(ap, SV *);
730 PUSHs(sva ? sva : &PL_sv_undef);
734 XPUSHs(vmg_op_info(opinfo));
737 call_sv(cb, G_SCALAR);
739 VMG_CB_CALL_SET_RET(0);
747 #define vmg_cb_call1(I, F, S, A1) \
748 vmg_cb_call(aTHX_ (I), (((F) << VMG_CB_CALL_ARGS_SHIFT) | 1), (S), (A1))
749 #define vmg_cb_call2(I, F, S, A1, A2) \
750 vmg_cb_call(aTHX_ (I), (((F) << VMG_CB_CALL_ARGS_SHIFT) | 2), (S), (A1), (A2))
751 #define vmg_cb_call3(I, F, S, A1, A2, A3) \
752 vmg_cb_call(aTHX_ (I), (((F) << VMG_CB_CALL_ARGS_SHIFT) | 3), (S), (A1), (A2), (A3))
754 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
755 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
756 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
759 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
760 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
761 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
764 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
765 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
766 unsigned int opinfo = w->opinfo;
768 svtype t = SvTYPE(sv);
777 PUSHs(sv_2mortal(newRV_inc(sv)));
778 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
781 U8 *s = (U8 *) SvPV_const(sv, l);
783 len = utf8_length(s, s + l);
787 } else if (t == SVt_PVAV) {
788 len = av_len((AV *) sv) + 1;
795 XPUSHs(vmg_op_info(opinfo));
798 call_sv(w->cb_len, G_SCALAR);
800 VMG_CB_CALL_SET_RET(len);
805 return t == SVt_PVAV ? ret - 1 : ret;
808 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
809 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
810 return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj);
813 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
815 #if VMG_HAS_PERL(5, 10, 0)
816 PERL_CONTEXT saved_cx;
819 unsigned int had_err, has_err, flags = G_SCALAR | G_EVAL;
824 /* Don't even bother if we are in global destruction - the wizard is prisoner
825 * of circular references and we are way beyond user realm */
829 w = SV2MGWIZ(mg->mg_ptr);
831 /* So that it survives the temp cleanup below */
834 #if !VMG_HAS_PERL_MAINT(5, 11, 0, 32686)
835 /* The previous magic tokens were freed but the magic chain wasn't updated, so
836 * if you access the sv from the callback the old deleted magics will trigger
837 * and cause memory misreads. Change 32686 solved it that way : */
846 PUSHs(sv_2mortal(newRV_inc(sv)));
847 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
849 XPUSHs(vmg_op_info(w->opinfo));
852 had_err = SvTRUE(ERRSV);
856 #if VMG_HAS_PERL(5, 10, 0)
857 /* This context should not be used anymore, but since we croak in places the
858 * core doesn't even dare to, some pointers to it may remain in the upper call
859 * stack. Make sure call_sv() doesn't clobber it. */
860 if (cxstack_ix < cxstack_max)
861 cxix = cxstack_ix + 1;
863 cxix = Perl_cxinc(aTHX);
864 saved_cx = cxstack[cxix];
867 call_sv(w->cb_free, flags);
869 #if VMG_HAS_PERL(5, 10, 0)
870 cxstack[cxix] = saved_cx;
873 has_err = SvTRUE(ERRSV);
874 if (IN_PERL_COMPILETIME && !had_err && has_err)
877 VMG_CB_CALL_SET_RET(0);
882 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
883 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
886 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
887 * mg->mg_ptr reference count */
892 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
893 # if VMG_HAS_PERL_MAINT(5, 11, 0, 33256)
900 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
903 if (keylen == HEf_SVKEY) {
906 keysv = newSVpvn(key, keylen);
909 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
911 if (keylen != HEf_SVKEY) {
917 #endif /* MGf_COPY */
920 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
926 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
927 const MGWIZ *w = SV2MGWIZ(mg->mg_ptr);
928 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
930 #endif /* MGf_LOCAL */
933 STATIC OP *vmg_pp_resetuvar(pTHX) {
934 SvRMAGICAL_on(cSVOP_sv);
938 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
941 SV *key = NULL, *newkey = NULL;
944 umg = mg_find(sv, PERL_MAGIC_uvar);
945 /* umg can't be NULL or we wouldn't be there. */
947 uf = (struct ufuncs *) umg->mg_ptr;
950 uf[1].uf_val(aTHX_ action, sv);
952 uf[1].uf_set(aTHX_ action, sv);
954 action &= HV_FETCH_ISSTORE | HV_FETCH_ISEXISTS | HV_FETCH_LVALUE | HV_DELETE;
955 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
957 switch (mg->mg_type) {
960 case PERL_MAGIC_tied:
966 if (mg->mg_private != SIG_WIZ) continue;
967 w = SV2MGWIZ(mg->mg_ptr);
973 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
978 vmg_cb_call2(w->cb_fetch, w->opinfo, sv, mg->mg_obj, key);
980 case HV_FETCH_ISSTORE:
981 case HV_FETCH_LVALUE:
982 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
984 vmg_cb_call2(w->cb_store, w->opinfo, sv, mg->mg_obj, key);
986 case HV_FETCH_ISEXISTS:
988 vmg_cb_call2(w->cb_exists, w->opinfo, sv, mg->mg_obj, key);
992 vmg_cb_call2(w->cb_delete, w->opinfo, sv, mg->mg_obj, key);
997 if (SvRMAGICAL(sv) && !tied) {
998 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
999 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1000 * the op_ppaddr of a new fake op injected between the current and the next
1003 if (!o->op_next || o->op_next->op_ppaddr != vmg_pp_resetuvar) {
1005 NewOp(1101, svop, 1, SVOP);
1006 svop->op_type = OP_STUB;
1007 svop->op_ppaddr = vmg_pp_resetuvar;
1008 svop->op_next = o->op_next;
1011 o->op_next = (OP *) svop;
1018 #endif /* VMG_UVAR */
1020 /* ... Wizard destructor ................................................... */
1022 STATIC int vmg_wizard_free(pTHX_ SV *wiz, MAGIC *mg) {
1026 if (PL_dirty) /* During global destruction, the context is already freed */
1030 #if VMG_MULTIPLICITY
1031 if (w->owner != aTHX)
1034 #endif /* VMG_MULTIPLICITY */
1038 if (hv_delete(MY_CXT.wizards, buf, sprintf(buf, "%u", w->sig), 0) != wiz)
1042 /* Unmortalize the wizard to avoid it being freed in weird places. */
1043 if (SvTEMP(wiz) && !SvREFCNT(wiz)) {
1044 const I32 myfloor = PL_tmps_floor;
1046 for (i = PL_tmps_ix; i > myfloor; --i) {
1047 if (PL_tmps_stack[i] == wiz)
1048 PL_tmps_stack[i] = NULL;
1052 if (w->cb_data) SvREFCNT_dec(SvRV(w->cb_data));
1053 if (w->cb_get) SvREFCNT_dec(SvRV(w->cb_get));
1054 if (w->cb_set) SvREFCNT_dec(SvRV(w->cb_set));
1055 if (w->cb_len) SvREFCNT_dec(SvRV(w->cb_len));
1056 if (w->cb_clear) SvREFCNT_dec(SvRV(w->cb_clear));
1057 if (w->cb_free) SvREFCNT_dec(SvRV(w->cb_free));
1059 if (w->cb_copy) SvREFCNT_dec(SvRV(w->cb_copy));
1060 #endif /* MGf_COPY */
1062 if (w->cb_dup) SvREFCNT_dec(SvRV(w->cb_dup));
1063 #endif /* MGf_DUP */
1065 if (w->cb_local) SvREFCNT_dec(SvRV(w->cb_local));
1066 #endif /* MGf_LOCAL */
1068 if (w->cb_fetch) SvREFCNT_dec(SvRV(w->cb_fetch));
1069 if (w->cb_store) SvREFCNT_dec(SvRV(w->cb_store));
1070 if (w->cb_exists) SvREFCNT_dec(SvRV(w->cb_exists));
1071 if (w->cb_delete) SvREFCNT_dec(SvRV(w->cb_delete));
1072 #endif /* VMG_UVAR */
1080 STATIC MGVTBL vmg_wizard_vtbl = {
1085 vmg_wizard_free, /* free */
1088 #endif /* MGf_COPY */
1091 #endif /* MGf_DUP */
1094 #endif /* MGf_LOCAL */
1097 STATIC U16 vmg_sv2sig(pTHX_ SV *sv) {
1098 #define vmg_sv2sig(S) vmg_sv2sig(aTHX_ (S))
1103 } else if (SvNOK(sv)) {
1105 } else if ((SvPOK(sv) && grok_number(SvPVX(sv), SvCUR(sv), NULL))) {
1108 croak(vmg_invalid_sig);
1111 if (sig < SIG_MIN || sig > SIG_MAX)
1112 croak(vmg_invalid_sig);
1117 STATIC U16 vmg_wizard_sig(pTHX_ SV *wiz) {
1118 #define vmg_wizard_sig(W) vmg_wizard_sig(aTHX_ (W))
1122 sig = SV2MGWIZ(SvRV(wiz))->sig;
1123 } else if (SvOK(wiz)) {
1124 sig = vmg_sv2sig(wiz);
1126 croak(vmg_invalid_wiz);
1132 SV **old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0);
1133 if (!(old && SV2MGWIZ(*old)))
1134 croak(vmg_invalid_wiz);
1140 STATIC SV *vmg_wizard_wiz(pTHX_ SV *wiz) {
1141 #define vmg_wizard_wiz(W) vmg_wizard_wiz(aTHX_ (W))
1146 #if VMG_MULTIPLICITY
1147 if (SV2MGWIZ(wiz)->owner == aTHX)
1149 #endif /* VMG_MULTIPLICITY */
1150 sig = SV2MGWIZ(wiz)->sig;
1151 } else if (SvOK(wiz)) {
1152 sig = vmg_sv2sig(wiz);
1154 croak(vmg_invalid_wiz);
1160 SV **old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0);
1161 if (!(old && SV2MGWIZ(*old)))
1162 croak(vmg_invalid_wiz);
1168 #define VMG_SET_CB(S, N) \
1170 w->cb_ ## N = (SvOK(cb) && SvROK(cb)) ? newRV_inc(SvRV(cb)) : NULL;
1172 #define VMG_SET_SVT_CB(S, N) \
1174 if (SvOK(cb) && SvROK(cb)) { \
1175 t->svt_ ## N = vmg_svt_ ## N; \
1176 w->cb_ ## N = newRV_inc(SvRV(cb)); \
1178 t->svt_ ## N = NULL; \
1179 w->cb_ ## N = NULL; \
1184 #define VMG_CLONE_CB(N) \
1185 z->cb_ ## N = (w->cb_ ## N) ? newRV_inc(vmg_clone(SvRV(w->cb_ ## N), \
1189 STATIC MGWIZ *vmg_wizard_clone(pTHX_ const MGWIZ *w) {
1190 #define vmg_wizard_clone(W) vmg_wizard_clone(aTHX_ (W))
1195 Copy(w->vtbl, t, 1, MGVTBL);
1202 VMG_CLONE_CB(clear);
1206 #endif /* MGf_COPY */
1209 #endif /* MGf_DUP */
1211 VMG_CLONE_CB(local);
1212 #endif /* MGf_LOCAL */
1214 VMG_CLONE_CB(fetch);
1215 VMG_CLONE_CB(store);
1216 VMG_CLONE_CB(exists);
1217 VMG_CLONE_CB(delete);
1218 #endif /* VMG_UVAR */
1223 z->opinfo = w->opinfo;
1228 #endif /* VMG_THREADSAFE */
1230 /* --- XS ------------------------------------------------------------------ */
1232 MODULE = Variable::Magic PACKAGE = Variable::Magic
1240 MY_CXT.wizards = newHV();
1241 hv_iterinit(MY_CXT.wizards); /* Allocate iterator */
1242 MY_CXT.b__op_stashes[0] = NULL;
1244 MUTEX_INIT(&vmg_op_name_init_mutex);
1247 stash = gv_stashpv(__PACKAGE__, 1);
1248 newCONSTSUB(stash, "SIG_MIN", newSVuv(SIG_MIN));
1249 newCONSTSUB(stash, "SIG_MAX", newSVuv(SIG_MAX));
1250 newCONSTSUB(stash, "SIG_NBR", newSVuv(SIG_NBR));
1251 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1252 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1253 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1254 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1255 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1256 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1257 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1258 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1259 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1260 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1261 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1262 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1263 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1264 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1265 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1266 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1276 U32 had_b__op_stash = 0;
1283 hv_iterinit(hv); /* Allocate iterator */
1284 hv_iterinit(MY_CXT.wizards);
1285 while ((key = hv_iternext(MY_CXT.wizards))) {
1287 char *sig = HePV(key, len);
1289 const MGWIZ *w = SV2MGWIZ(HeVAL(key));
1292 w = vmg_wizard_clone(w);
1294 mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
1295 mg->mg_private = SIG_WZO;
1297 sv = MGWIZ2SV(NULL);
1300 if (!hv_store(hv, sig, len, sv, HeHASH(key))) croak("%s during CLONE", vmg_globstorefail);
1302 for (c = 0; c < OPc_MAX; ++c) {
1303 if (MY_CXT.b__op_stashes[c])
1304 had_b__op_stash |= (((U32) 1) << c);
1309 MY_CXT.wizards = hv;
1310 for (c = 0; c < OPc_MAX; ++c) {
1311 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1312 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1316 #endif /* VMG_THREADSAFE */
1336 #endif /* MGf_COPY */
1339 #endif /* MGf_DUP */
1342 #endif /* MGf_LOCAL */
1345 #endif /* VMG_UVAR */
1346 ) { croak(vmg_wrongargnum); }
1351 sig = vmg_sv2sig(svsig);
1352 old = hv_fetch(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), 0);
1353 if (old && SV2MGWIZ(*old)) {
1354 ST(0) = sv_2mortal(newRV_inc(*old));
1364 VMG_SET_CB(ST(i++), data);
1366 w->opinfo = SvOK(cb) ? SvUV(cb) : 0;
1368 vmg_op_info_init(w->opinfo);
1369 VMG_SET_SVT_CB(ST(i++), get);
1370 VMG_SET_SVT_CB(ST(i++), set);
1371 VMG_SET_SVT_CB(ST(i++), len);
1372 VMG_SET_SVT_CB(ST(i++), clear);
1373 VMG_SET_SVT_CB(ST(i++), free);
1375 VMG_SET_SVT_CB(ST(i++), copy);
1376 #endif /* MGf_COPY */
1378 /* VMG_SET_SVT_CB(ST(i++), dup); */
1382 #endif /* MGf_DUP */
1384 VMG_SET_SVT_CB(ST(i++), local);
1385 #endif /* MGf_LOCAL */
1387 VMG_SET_CB(ST(i++), fetch);
1388 VMG_SET_CB(ST(i++), store);
1389 VMG_SET_CB(ST(i++), exists);
1390 VMG_SET_CB(ST(i++), delete);
1392 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1393 w->uvar = SvTRUE(cb) ? 2 : 1;
1396 #endif /* VMG_UVAR */
1397 #if VMG_MULTIPLICITY
1399 #endif /* VMG_MULTIPLICITY */
1404 mg = sv_magicext(sv, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
1405 mg->mg_private = SIG_WZO;
1408 if (!hv_store(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), sv, 0)) croak(vmg_globstorefail);
1410 RETVAL = newRV_noinc(sv);
1422 if (!hv_store(MY_CXT.wizards, buf, sprintf(buf, "%u", sig), MGWIZ2SV(NULL), 0)) croak(vmg_globstorefail);
1423 RETVAL = newSVuv(sig);
1432 sig = vmg_wizard_sig(wiz);
1433 RETVAL = newSVuv(sig);
1437 SV *cast(SV *sv, SV *wiz, ...)
1438 PROTOTYPE: \[$@%&*]$@
1443 wiz = vmg_wizard_wiz(wiz);
1447 av_fill(args, items - 2);
1448 for (i = 2; i < items; ++i) {
1451 if (av_store(args, i - 2, arg) == NULL) croak(vmg_argstorefailed);
1454 ret = newSVuv(vmg_cast(SvRV(sv), wiz, args));
1461 getdata(SV *sv, SV *wiz)
1462 PROTOTYPE: \[$@%&*]$
1467 sig = vmg_wizard_sig(wiz);
1468 data = vmg_data_get(SvRV(sv), sig);
1474 SV *dispell(SV *sv, SV *wiz)
1475 PROTOTYPE: \[$@%&*]$
1479 sig = vmg_wizard_sig(wiz);
1480 RETVAL = newSVuv(vmg_dispell(SvRV(sv), sig));