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"
17 # define VOID2(T, P) static_cast<T>(P)
19 # define VOID2(T, P) (P)
22 #ifndef VMG_PERL_PATCHLEVEL
24 # define VMG_PERL_PATCHLEVEL PERL_PATCHNUM
26 # define VMG_PERL_PATCHLEVEL 0
30 #define VMG_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
32 #define VMG_HAS_PERL_BRANCH(R, V, S) (PERL_REVISION == (R) && PERL_VERSION == (V) && PERL_SUBVERSION >= (S))
34 #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))))
36 /* --- Threads and multiplicity -------------------------------------------- */
46 /* Safe unless stated otherwise in Makefile.PL */
48 # define VMG_FORKSAFE 1
51 #ifndef VMG_MULTIPLICITY
52 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
53 # define VMG_MULTIPLICITY 1
55 # define VMG_MULTIPLICITY 0
59 #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))
60 # define VMG_THREADSAFE 1
62 # define MY_CXT_CLONE \
64 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
65 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
66 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
69 # define VMG_THREADSAFE 0
71 # define dMY_CXT dNOOP
73 # define MY_CXT vmg_globaldata
75 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
77 # define MY_CXT_INIT NOOP
79 # define MY_CXT_CLONE NOOP
83 # define VMG_LOCK(M) MUTEX_LOCK(M)
84 # define VMG_UNLOCK(M) MUTEX_UNLOCK(M)
87 # define VMG_UNLOCK(M)
90 /* --- Compatibility ------------------------------------------------------- */
93 # define Newx(v, n, c) New(0, v, n, c)
97 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
101 # define SvRV_const(sv) SvRV((SV *) sv)
104 #ifndef SvREFCNT_inc_simple_void
105 # define SvREFCNT_inc_simple_void(sv) ((void) SvREFCNT_inc(sv))
109 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
112 #ifndef PERL_MAGIC_ext
113 # define PERL_MAGIC_ext '~'
116 #ifndef PERL_MAGIC_tied
117 # define PERL_MAGIC_tied 'P'
124 #ifndef IN_PERL_COMPILETIME
125 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
128 /* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
129 * enable them on 5.10 */
130 #if VMG_HAS_PERL(5, 10, 0)
136 #if VMG_HAS_PERL_MAINT(5, 11, 0, 32969) || VMG_HAS_PERL(5, 12, 0)
137 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
139 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
142 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160, partially
143 * reverted to dev-5.11 as 9cdcb38b */
144 #if VMG_HAS_PERL_MAINT(5, 8, 9, 28160) || VMG_HAS_PERL_MAINT(5, 9, 3, 25854) || VMG_HAS_PERL(5, 10, 0)
145 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
146 # if VMG_HAS_PERL(5, 11, 0)
147 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
149 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
152 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
153 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 1
156 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
157 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
159 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
160 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 0
164 /* Applied to dev-5.11 as 34908 */
165 #if VMG_HAS_PERL_MAINT(5, 11, 0, 34908) || VMG_HAS_PERL(5, 12, 0)
166 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
168 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
171 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
172 #if VMG_HAS_PERL_MAINT(5, 8, 9, 32542) || VMG_HAS_PERL_MAINT(5, 9, 5, 31473) || VMG_HAS_PERL(5, 10, 0)
173 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
175 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
178 #if VMG_HAS_PERL(5, 11, 0)
179 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 1
181 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 0
184 #if VMG_HAS_PERL(5, 13, 2)
185 # define VMG_COMPAT_GLOB_GET 1
187 # define VMG_COMPAT_GLOB_GET 0
190 /* ... Bug-free mg_magical ................................................. */
192 /* See the discussion at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html */
194 #if VMG_HAS_PERL(5, 11, 3)
196 #define vmg_mg_magical(S) mg_magical(S)
200 STATIC void vmg_mg_magical(SV *sv) {
204 if ((mg = SvMAGIC(sv))) {
206 const MGVTBL* const vtbl = mg->mg_virtual;
208 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
215 } while ((mg = mg->mg_moremagic));
216 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
223 /* ... Safe version of call_sv() ........................................... */
225 STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, SV *dsv) {
226 #define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
227 I32 ret, cxix, in_eval = 0;
228 PERL_CONTEXT saved_cx;
236 if (cxstack_ix < cxstack_max) {
237 cxix = cxstack_ix + 1;
238 in_eval = CxTYPE(cxstack + cxix) == CXt_EVAL;
240 cxix = Perl_cxinc(aTHX);
242 /* The last popped context will be reused by call_sv(), but our callers may
243 * still need its previous value. Back it up so that it isn't clobbered. */
244 saved_cx = cxstack[cxix];
246 ret = call_sv(sv, flags | G_EVAL);
248 cxstack[cxix] = saved_cx;
252 sv_setsv(old_err, ERRSV);
256 if (IN_PERL_COMPILETIME) {
259 sv_catsv(PL_errors, ERRSV);
261 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
264 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
266 ++PL_parser->error_count;
267 #elif defined(PL_error_count)
272 } else if (!in_eval) {
274 /* We are about to croak() while dsv is being destroyed. Try to clean up
276 MAGIC *mg = SvMAGIC(dsv);
277 SvREFCNT_dec((SV *) mg->mg_ptr);
278 /* mg->mg_obj may not be refcounted if the data constructor returned the
279 * variable itself. */
280 if (mg->mg_flags & MGf_REFCOUNTED)
281 SvREFCNT_dec(mg->mg_obj);
282 SvMAGIC_set(dsv, mg->mg_moremagic);
299 /* --- Stolen chunk of B --------------------------------------------------- */
317 STATIC const char *const vmg_opclassnames[] = {
332 STATIC opclass vmg_opclass(const OP *o) {
339 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
341 if (o->op_type == OP_SASSIGN)
342 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
344 if (o->op_type == OP_AELEMFAST) {
345 #if PERL_VERSION <= 14
346 if (o->op_flags & OPf_SPECIAL)
358 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
362 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
379 case OA_PVOP_OR_SVOP:
380 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
385 case OA_BASEOP_OR_UNOP:
386 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
388 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
390 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
392 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
395 if (o->op_flags & OPf_STACKED)
397 else if (o->op_flags & OPf_SPECIAL)
406 /* --- Error messages ------------------------------------------------------ */
408 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
409 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
410 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
412 /* --- Context-safe global data -------------------------------------------- */
414 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
417 HV *b__op_stashes[OPc_MAX];
422 /* --- <vmg_vtable> structure ---------------------------------------------- */
431 STATIC vmg_vtable *vmg_vtable_alloc(pTHX) {
432 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
435 t = VOID2(vmg_vtable *, PerlMemShared_malloc(sizeof *t));
437 t->vtbl = VOID2(MGVTBL *, PerlMemShared_malloc(sizeof *t->vtbl));
443 #define vmg_vtable_vtbl(T) (T)->vtbl
445 STATIC perl_mutex vmg_vtable_refcount_mutex;
447 STATIC vmg_vtable *vmg_vtable_dup(pTHX_ vmg_vtable *t) {
448 #define vmg_vtable_dup(T) vmg_vtable_dup(aTHX_ (T))
449 VMG_LOCK(&vmg_vtable_refcount_mutex);
451 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
456 STATIC void vmg_vtable_free(pTHX_ vmg_vtable *t) {
457 #define vmg_vtable_free(T) vmg_vtable_free(aTHX_ (T))
460 VMG_LOCK(&vmg_vtable_refcount_mutex);
461 refcount = --t->refcount;
462 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
465 PerlMemShared_free(t->vtbl);
466 PerlMemShared_free(t);
470 #else /* VMG_THREADSAFE */
472 typedef MGVTBL vmg_vtable;
474 STATIC vmg_vtable *vmg_vtable_alloc(pTHX) {
475 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
478 Newx(t, 1, vmg_vtable);
483 #define vmg_vtable_vtbl(T) ((MGVTBL *) (T))
485 #define vmg_vtable_free(T) Safefree(T)
487 #endif /* !VMG_THREADSAFE */
489 /* --- <vmg_wizard> structure ---------------------------------------------- */
498 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
503 #endif /* MGf_LOCAL */
505 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
506 #endif /* VMG_UVAR */
509 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo);
511 STATIC vmg_wizard *vmg_wizard_alloc(pTHX_ UV opinfo) {
512 #define vmg_wizard_alloc(O) vmg_wizard_alloc(aTHX_ (O))
515 Newx(w, 1, vmg_wizard);
518 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
520 vmg_op_info_init(aTHX_ w->opinfo);
522 w->vtable = vmg_vtable_alloc();
527 STATIC void vmg_wizard_free(pTHX_ vmg_wizard *w) {
528 #define vmg_wizard_free(W) vmg_wizard_free(aTHX_ (W))
532 /* During global destruction, any of the callbacks may already have been
533 * freed, so we can't rely on still being able to access them. */
535 SvREFCNT_dec(w->cb_data);
536 SvREFCNT_dec(w->cb_get);
537 SvREFCNT_dec(w->cb_set);
538 SvREFCNT_dec(w->cb_len);
539 SvREFCNT_dec(w->cb_clear);
540 SvREFCNT_dec(w->cb_free);
541 SvREFCNT_dec(w->cb_copy);
543 SvREFCNT_dec(w->cb_dup);
546 SvREFCNT_dec(w->cb_local);
547 #endif /* MGf_LOCAL */
549 SvREFCNT_dec(w->cb_fetch);
550 SvREFCNT_dec(w->cb_store);
551 SvREFCNT_dec(w->cb_exists);
552 SvREFCNT_dec(w->cb_delete);
553 #endif /* VMG_UVAR */
556 /* PerlMemShared_free() and Safefree() are still fine during global
557 * destruction though. */
558 vmg_vtable_free(w->vtable);
566 #define VMG_CLONE_CB(N) \
567 z->cb_ ## N = (w->cb_ ## N) ? SvREFCNT_inc(sv_dup(w->cb_ ## N, params)) \
570 STATIC const vmg_wizard *vmg_wizard_dup(pTHX_ const vmg_wizard *w, CLONE_PARAMS *params) {
571 #define vmg_wizard_dup(W, P) vmg_wizard_dup(aTHX_ (W), (P))
577 Newx(z, 1, vmg_wizard);
579 z->vtable = vmg_vtable_dup(w->vtable);
581 z->opinfo = w->opinfo;
593 #endif /* MGf_LOCAL */
597 VMG_CLONE_CB(exists);
598 VMG_CLONE_CB(delete);
599 #endif /* VMG_UVAR */
604 #endif /* VMG_THREADSAFE */
606 #define vmg_wizard_id(W) PTR2IV(vmg_vtable_vtbl((W)->vtable))
608 /* --- Wizard SV objects --------------------------------------------------- */
610 STATIC int vmg_wizard_sv_free(pTHX_ SV *sv, MAGIC *mg) {
611 vmg_wizard_free((vmg_wizard *) mg->mg_ptr);
618 STATIC int vmg_wizard_sv_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
619 mg->mg_ptr = (char *) vmg_wizard_dup((const vmg_wizard *) mg->mg_ptr, params);
624 #endif /* VMG_THREADSAFE */
626 STATIC MGVTBL vmg_wizard_sv_vtbl = {
631 vmg_wizard_sv_free, /* free */
634 vmg_wizard_sv_dup, /* dup */
640 #endif /* MGf_LOCAL */
643 STATIC SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) {
644 #define vmg_wizard_sv_new(W) vmg_wizard_sv_new(aTHX_ (W))
650 wiz = newSViv(PTR2IV(w));
654 MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_sv_vtbl,
655 (const char *) w, 0);
658 mg->mg_flags |= MGf_DUP;
668 #define vmg_sv_has_wizard_type(S) (SvTYPE(S) >= SVt_PVMG)
670 STATIC const vmg_wizard *vmg_wizard_from_sv_nocheck(const SV *wiz) {
673 for (mg = SvMAGIC(wiz); mg; mg = mg->mg_moremagic) {
674 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vmg_wizard_sv_vtbl)
675 return (const vmg_wizard *) mg->mg_ptr;
681 #else /* VMG_THREADSAFE */
683 #define vmg_sv_has_wizard_type(S) SvIOK(S)
685 #define vmg_wizard_from_sv_nocheck(W) INT2PTR(const vmg_wizard *, SvIVX(W))
687 #endif /* !VMG_THREADSAFE */
689 #define vmg_wizard_from_sv(W) (vmg_sv_has_wizard_type(W) ? vmg_wizard_from_sv_nocheck(W) : NULL)
691 STATIC const vmg_wizard *vmg_wizard_from_mg(const MAGIC *mg) {
692 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_len == HEf_SVKEY) {
693 SV *sv = (SV *) mg->mg_ptr;
695 if (vmg_sv_has_wizard_type(sv))
696 return vmg_wizard_from_sv_nocheck(sv);
702 #define vmg_wizard_from_mg_nocheck(M) vmg_wizard_from_sv_nocheck((const SV *) (M)->mg_ptr)
704 /* --- User-level functions implementation --------------------------------- */
706 STATIC const MAGIC *vmg_find(const SV *sv, const vmg_wizard *w) {
710 if (SvTYPE(sv) < SVt_PVMG)
713 wid = vmg_wizard_id(w);
715 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
716 const vmg_wizard *z = vmg_wizard_from_mg(mg);
718 if (z && vmg_wizard_id(z) == wid)
725 /* ... Construct private data .............................................. */
727 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
728 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
738 EXTEND(SP, items + 1);
739 PUSHs(sv_2mortal(newRV_inc(sv)));
740 for (i = 0; i < items; ++i)
744 vmg_call_sv(ctor, G_SCALAR, NULL);
748 #if VMG_HAS_PERL(5, 8, 3)
749 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
751 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
761 STATIC SV *vmg_data_get(pTHX_ SV *sv, const vmg_wizard *w) {
762 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
763 const MAGIC *mg = vmg_find(sv, w);
765 return mg ? mg->mg_obj : NULL;
768 /* ... Magic cast/dispell .................................................. */
771 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
774 struct ufuncs new_uf;
775 struct ufuncs old_uf;
778 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
780 prevmagic->mg_moremagic = moremagic;
782 SvMAGIC_set(sv, moremagic);
784 mg->mg_moremagic = NULL;
785 Safefree(mg->mg_ptr);
788 #endif /* VMG_UVAR */
790 STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, I32 items) {
791 #define vmg_cast(S, W, WIZ, A, I) vmg_cast(aTHX_ (S), (W), (WIZ), (A), (I))
800 oldgmg = SvGMAGICAL(sv);
802 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
804 t = vmg_vtable_vtbl(w->vtable);
805 mg = sv_magicext(sv, data, PERL_MAGIC_ext, t, (const char *) wiz, HEf_SVKEY);
808 /* sv_magicext() calls mg_magical and increments data's refcount */
812 mg->mg_flags |= MGf_COPY;
815 mg->mg_flags |= MGf_DUP;
819 mg->mg_flags |= MGf_LOCAL;
820 #endif /* MGf_LOCAL */
822 if (SvTYPE(sv) < SVt_PVHV)
825 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
826 * magic is actually never called for them. If the GMAGICAL flag was off before
827 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
828 * now on, then this wizard has get magic. Hence we can work around the
829 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
830 * has uvar callbacks, it will be turned back on later. */
831 if (!oldgmg && SvGMAGICAL(sv))
836 MAGIC *prevmagic, *moremagic = NULL;
839 ud.new_uf.uf_val = vmg_svt_val;
840 ud.new_uf.uf_set = NULL;
841 ud.new_uf.uf_index = 0;
842 ud.old_uf.uf_val = NULL;
843 ud.old_uf.uf_set = NULL;
844 ud.old_uf.uf_index = 0;
846 /* One uvar magic in the chain is enough. */
847 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
848 moremagic = mg->mg_moremagic;
849 if (mg->mg_type == PERL_MAGIC_uvar)
853 if (mg) { /* Found another uvar magic. */
854 struct ufuncs *uf = (struct ufuncs *) mg->mg_ptr;
855 if (uf->uf_val == vmg_svt_val) {
856 /* It's our uvar magic, nothing to do. oldgmg was true. */
859 /* It's another uvar magic, backup it and replace it by ours. */
861 vmg_uvar_del(sv, prevmagic, mg, moremagic);
865 sv_magic(sv, NULL, PERL_MAGIC_uvar, (const char *) &ud, sizeof(ud));
867 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
868 * handled by our uvar callback. */
870 #endif /* VMG_UVAR */
876 STATIC UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) {
877 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
880 #endif /* VMG_UVAR */
881 MAGIC *mg, *prevmagic, *moremagic = NULL;
882 IV wid = vmg_wizard_id(w);
884 if (SvTYPE(sv) < SVt_PVMG)
887 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
890 moremagic = mg->mg_moremagic;
892 z = vmg_wizard_from_mg(mg);
894 IV zid = vmg_wizard_id(z);
898 /* If the current has no uvar, short-circuit uvar deletion. */
899 uvars = z->uvar ? (uvars + 1) : 0;
901 } else if (z->uvar) {
903 /* We can't break here since we need to find the ext magic to delete. */
908 #endif /* !VMG_UVAR */
915 prevmagic->mg_moremagic = moremagic;
917 SvMAGIC_set(sv, moremagic);
919 mg->mg_moremagic = NULL;
921 /* Destroy private data */
922 if (mg->mg_obj != sv)
923 SvREFCNT_dec(mg->mg_obj);
924 /* Unreference the wizard */
925 SvREFCNT_dec((SV *) mg->mg_ptr);
929 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
930 /* mg was the first ext magic in the chain that had uvar */
932 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
933 const vmg_wizard *z = vmg_wizard_from_mg(mg);
944 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
945 moremagic = mg->mg_moremagic;
946 if (mg->mg_type == PERL_MAGIC_uvar)
950 ud = (vmg_uvar_ud *) mg->mg_ptr;
951 if (ud->old_uf.uf_val || ud->old_uf.uf_set) {
952 /* Revert the original uvar magic. */
954 Newx(uf, 1, struct ufuncs);
957 mg->mg_ptr = (char *) uf;
958 mg->mg_len = sizeof(*uf);
960 /* Remove the uvar magic. */
961 vmg_uvar_del(sv, prevmagic, mg, moremagic);
965 #endif /* VMG_UVAR */
972 /* ... OP info ............................................................. */
974 #define VMG_OP_INFO_NAME 1
975 #define VMG_OP_INFO_OBJECT 2
978 STATIC perl_mutex vmg_op_name_init_mutex;
981 STATIC U32 vmg_op_name_init = 0;
982 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
984 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
985 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
987 case VMG_OP_INFO_NAME:
988 VMG_LOCK(&vmg_op_name_init_mutex);
989 if (!vmg_op_name_init) {
991 for (t = 0; t < OP_max; ++t)
992 vmg_op_name_len[t] = strlen(PL_op_name[t]);
993 vmg_op_name_init = 1;
995 VMG_UNLOCK(&vmg_op_name_init_mutex);
997 case VMG_OP_INFO_OBJECT: {
999 if (!MY_CXT.b__op_stashes[0]) {
1002 for (c = OPc_NULL; c < OPc_MAX; ++c)
1003 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
1012 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
1013 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
1015 return &PL_sv_undef;
1018 case VMG_OP_INFO_NAME: {
1019 OPCODE t = PL_op->op_type;
1020 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
1022 case VMG_OP_INFO_OBJECT: {
1024 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1025 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1031 return &PL_sv_undef;
1034 /* --- svt callbacks ------------------------------------------------------- */
1036 #define VMG_CB_CALL_ARGS_MASK 15
1037 #define VMG_CB_CALL_ARGS_SHIFT 4
1038 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT)
1040 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1043 unsigned int i, args, opinfo;
1048 args = flags & VMG_CB_CALL_ARGS_MASK;
1049 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1050 opinfo = flags & VMG_CB_CALL_OPINFO;
1056 EXTEND(SP, args + 1);
1057 PUSHs(sv_2mortal(newRV_inc(sv)));
1059 for (i = 0; i < args; ++i) {
1060 SV *sva = va_arg(ap, SV *);
1061 PUSHs(sva ? sva : &PL_sv_undef);
1065 XPUSHs(vmg_op_info(opinfo));
1068 vmg_call_sv(cb, G_SCALAR, NULL);
1073 ret = (int) SvIV(svr);
1082 #define VMG_CB_FLAGS(OI, A) \
1083 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1085 #define vmg_cb_call1(I, OI, S, A1) \
1086 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1087 #define vmg_cb_call2(I, OI, S, A1, A2) \
1088 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1089 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1090 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1092 STATIC int vmg_svt_default_noop(pTHX_ SV *sv, MAGIC *mg) {
1096 /* ... get magic ........................................................... */
1098 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1099 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1101 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1104 #define vmg_svt_get_noop vmg_svt_default_noop
1106 /* ... set magic ........................................................... */
1108 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1109 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1111 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1114 #define vmg_svt_set_noop vmg_svt_default_noop
1116 /* ... len magic ........................................................... */
1118 STATIC U32 vmg_sv_len(pTHX_ SV *sv) {
1119 #define vmg_sv_len(S) vmg_sv_len(aTHX_ (S))
1121 #if VMG_HAS_PERL(5, 9, 3)
1122 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, len)));
1124 U8 *s = SvPV(sv, len);
1127 return DO_UTF8(sv) ? utf8_length(s, s + len) : len;
1130 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1131 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1132 unsigned int opinfo = w->opinfo;
1135 svtype t = SvTYPE(sv);
1144 PUSHs(sv_2mortal(newRV_inc(sv)));
1145 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1147 len = vmg_sv_len(sv);
1149 } else if (t == SVt_PVAV) {
1150 len = av_len((AV *) sv) + 1;
1154 PUSHs(&PL_sv_undef);
1157 XPUSHs(vmg_op_info(opinfo));
1160 vmg_call_sv(w->cb_len, G_SCALAR, NULL);
1164 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1175 STATIC U32 vmg_svt_len_noop(pTHX_ SV *sv, MAGIC *mg) {
1177 svtype t = SvTYPE(sv);
1180 len = vmg_sv_len(sv);
1181 } else if (t == SVt_PVAV) {
1182 len = (U32) av_len((AV *) sv);
1188 /* ... clear magic ......................................................... */
1190 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1191 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1193 return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj);
1196 #define vmg_svt_clear_noop vmg_svt_default_noop
1198 /* ... free magic .......................................................... */
1200 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1201 const vmg_wizard *w;
1207 /* During global destruction, we cannot be sure that the wizard and its free
1208 * callback are still alive. */
1212 w = vmg_wizard_from_mg_nocheck(mg);
1214 /* So that it survives the temp cleanup below */
1215 SvREFCNT_inc_simple_void(sv);
1217 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1218 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1219 * if you access the sv from the callback the old deleted magics will trigger
1220 * and cause memory misreads. Change 32686 solved it that way : */
1221 SvMAGIC_set(sv, mg);
1229 PUSHs(sv_2mortal(newRV_inc(sv)));
1230 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1232 XPUSHs(vmg_op_info(w->opinfo));
1235 vmg_call_sv(w->cb_free, G_SCALAR, sv);
1240 ret = (int) SvIV(svr);
1246 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1247 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1250 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1251 * mg->mg_ptr reference count */
1255 #define vmg_svt_free_noop vmg_svt_default_noop
1257 #if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1258 # define VMG_SVT_COPY_KEYLEN_TYPE I32
1260 # define VMG_SVT_COPY_KEYLEN_TYPE int
1263 /* ... copy magic .......................................................... */
1265 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1266 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1270 if (keylen == HEf_SVKEY) {
1273 keysv = newSVpvn(key, keylen);
1276 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1278 if (keylen != HEf_SVKEY) {
1279 SvREFCNT_dec(keysv);
1285 STATIC int vmg_svt_copy_noop(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1289 /* ... dup magic ........................................................... */
1292 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1295 #define vmg_svt_dup_noop vmg_svt_dup
1298 /* ... local magic ......................................................... */
1302 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1303 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1305 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1308 #define vmg_svt_local_noop vmg_svt_default_noop
1310 #endif /* MGf_LOCAL */
1312 /* ... uvar magic .......................................................... */
1315 STATIC OP *vmg_pp_resetuvar(pTHX) {
1316 SvRMAGICAL_on(cSVOP_sv);
1320 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1323 SV *key = NULL, *newkey = NULL;
1326 umg = mg_find(sv, PERL_MAGIC_uvar);
1327 /* umg can't be NULL or we wouldn't be there. */
1329 ud = (vmg_uvar_ud *) umg->mg_ptr;
1331 if (ud->old_uf.uf_val)
1332 ud->old_uf.uf_val(aTHX_ action, sv);
1333 if (ud->old_uf.uf_set)
1334 ud->old_uf.uf_set(aTHX_ action, sv);
1336 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
1337 const vmg_wizard *w;
1339 switch (mg->mg_type) {
1340 case PERL_MAGIC_ext:
1342 case PERL_MAGIC_tied:
1349 w = vmg_wizard_from_mg(mg);
1358 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1362 & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS|HV_FETCH_LVALUE|HV_DELETE)) {
1365 vmg_cb_call2(w->cb_fetch, w->opinfo, sv, mg->mg_obj, key);
1367 case HV_FETCH_ISSTORE:
1368 case HV_FETCH_LVALUE:
1369 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1371 vmg_cb_call2(w->cb_store, w->opinfo, sv, mg->mg_obj, key);
1373 case HV_FETCH_ISEXISTS:
1375 vmg_cb_call2(w->cb_exists, w->opinfo, sv, mg->mg_obj, key);
1379 vmg_cb_call2(w->cb_delete, w->opinfo, sv, mg->mg_obj, key);
1384 if (SvRMAGICAL(sv) && !tied && !(action & (HV_FETCH_ISSTORE|HV_DELETE))) {
1385 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1386 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1387 * the op_ppaddr of a new fake op injected between the current and the next
1389 OP *nop = PL_op->op_next;
1390 if (!nop || nop->op_ppaddr != vmg_pp_resetuvar) {
1392 NewOp(1101, svop, 1, SVOP);
1393 svop->op_type = OP_STUB;
1394 svop->op_ppaddr = vmg_pp_resetuvar;
1395 svop->op_next = nop;
1398 PL_op->op_next = (OP *) svop;
1405 #endif /* VMG_UVAR */
1407 /* --- Macros for the XS section ------------------------------------------- */
1410 # define VMG_CVOK(C) \
1411 ((CvISXSUB(C) ? (void *) CvXSUB(C) : (void *) CvROOT(C)) ? 1 : 0)
1413 # define VMG_CVOK(C) (CvROOT(C) || CvXSUB(C))
1416 #define VMG_CBOK(S) ((SvTYPE(S) == SVt_PVCV) ? VMG_CVOK(S) : SvOK(S))
1418 #define VMG_SET_CB(S, N) { \
1420 if (SvOK(cb) && SvROK(cb)) { \
1423 SvREFCNT_inc_simple_void(cb); \
1432 #define VMG_SET_SVT_CB(S, N) { \
1434 if (SvOK(cb) && SvROK(cb)) { \
1436 if (VMG_CBOK(cb)) { \
1437 t->svt_ ## N = vmg_svt_ ## N; \
1438 SvREFCNT_inc_simple_void(cb); \
1440 t->svt_ ## N = vmg_svt_ ## N ## _noop; \
1444 t->svt_ ## N = NULL; \
1450 /* --- XS ------------------------------------------------------------------ */
1452 MODULE = Variable::Magic PACKAGE = Variable::Magic
1461 MY_CXT.b__op_stashes[0] = NULL;
1463 MUTEX_INIT(&vmg_vtable_refcount_mutex);
1464 MUTEX_INIT(&vmg_op_name_init_mutex);
1467 stash = gv_stashpv(__PACKAGE__, 1);
1468 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1469 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1470 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1471 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1472 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1473 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1474 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1475 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1476 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1477 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1478 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1479 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1480 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1481 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1482 newCONSTSUB(stash, "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID",
1483 newSVuv(VMG_COMPAT_HASH_DELETE_NOUVAR_VOID));
1484 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1485 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1486 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1487 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1488 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1489 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1498 U32 had_b__op_stash = 0;
1503 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1504 if (MY_CXT.b__op_stashes[c])
1505 had_b__op_stash |= (((U32) 1) << c);
1510 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1511 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1512 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1517 #endif /* VMG_THREADSAFE */
1524 SV *op_info, *copy_key;
1530 #endif /* MGf_LOCAL */
1533 #endif /* VMG_UVAR */
1534 ) { croak(vmg_wrongargnum); }
1537 w = vmg_wizard_alloc(SvOK(op_info) ? SvUV(op_info) : 0);
1538 t = vmg_vtable_vtbl(w->vtable);
1540 VMG_SET_CB(ST(i++), data);
1542 VMG_SET_SVT_CB(ST(i++), get);
1543 VMG_SET_SVT_CB(ST(i++), set);
1544 VMG_SET_SVT_CB(ST(i++), len);
1545 VMG_SET_SVT_CB(ST(i++), clear);
1546 VMG_SET_SVT_CB(ST(i++), free);
1547 VMG_SET_SVT_CB(ST(i++), copy);
1548 /* VMG_SET_SVT_CB(ST(i++), dup); */
1553 VMG_SET_SVT_CB(ST(i++), local);
1554 #endif /* MGf_LOCAL */
1556 VMG_SET_CB(ST(i++), fetch);
1557 VMG_SET_CB(ST(i++), store);
1558 VMG_SET_CB(ST(i++), exists);
1559 VMG_SET_CB(ST(i++), delete);
1562 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1563 w->uvar = SvTRUE(copy_key) ? 2 : 1;
1564 #endif /* VMG_UVAR */
1566 RETVAL = newRV_noinc(vmg_wizard_sv_new(w));
1570 SV *cast(SV *sv, SV *wiz, ...)
1571 PROTOTYPE: \[$@%&*]$@
1573 const vmg_wizard *w = NULL;
1583 wiz = SvRV_const(wiz);
1584 w = vmg_wizard_from_sv(wiz);
1587 croak(vmg_invalid_wiz);
1588 RETVAL = newSVuv(vmg_cast(SvRV(sv), w, wiz, args, i));
1593 getdata(SV *sv, SV *wiz)
1594 PROTOTYPE: \[$@%&*]$
1596 const vmg_wizard *w = NULL;
1600 w = vmg_wizard_from_sv(SvRV_const(wiz));
1602 croak(vmg_invalid_wiz);
1603 data = vmg_data_get(SvRV(sv), w);
1609 SV *dispell(SV *sv, SV *wiz)
1610 PROTOTYPE: \[$@%&*]$
1612 const vmg_wizard *w = NULL;
1615 w = vmg_wizard_from_sv(SvRV_const(wiz));
1617 croak(vmg_invalid_wiz);
1618 RETVAL = newSVuv(vmg_dispell(SvRV(sv), w));