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 #if VMG_HAS_PERL(5, 17, 4)
143 # define VMG_COMPAT_SCALAR_NOLEN 1
145 # define VMG_COMPAT_SCALAR_NOLEN 0
148 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160, partially
149 * reverted to dev-5.11 as 9cdcb38b */
150 #if VMG_HAS_PERL_MAINT(5, 8, 9, 28160) || VMG_HAS_PERL_MAINT(5, 9, 3, 25854) || VMG_HAS_PERL(5, 10, 0)
151 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
152 # if VMG_HAS_PERL(5, 11, 0)
153 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
155 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
158 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
159 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 1
162 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
163 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
165 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
166 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 0
170 /* Applied to dev-5.11 as 34908 */
171 #if VMG_HAS_PERL_MAINT(5, 11, 0, 34908) || VMG_HAS_PERL(5, 12, 0)
172 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
174 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
177 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
178 #if VMG_HAS_PERL_MAINT(5, 8, 9, 32542) || VMG_HAS_PERL_MAINT(5, 9, 5, 31473) || VMG_HAS_PERL(5, 10, 0)
179 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
181 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
184 #if VMG_HAS_PERL(5, 11, 0)
185 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 1
187 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 0
190 #if VMG_HAS_PERL(5, 17, 0)
191 # define VMG_COMPAT_CODE_COPY_CLONE 1
193 # define VMG_COMPAT_CODE_COPY_CLONE 0
196 #if VMG_HAS_PERL(5, 13, 2)
197 # define VMG_COMPAT_GLOB_GET 1
199 # define VMG_COMPAT_GLOB_GET 0
202 #define VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE (VMG_HAS_PERL(5, 10, 0) && !VMG_HAS_PERL(5, 10, 1))
204 /* NewOp() isn't public in perl 5.8.0. */
205 #define VMG_RESET_RMG_NEEDS_TRAMPOLINE (VMG_UVAR && (VMG_THREADSAFE || !VMG_HAS_PERL(5, 8, 1)))
207 /* ... Bug-free mg_magical ................................................. */
209 /* See the discussion at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html */
211 #if VMG_HAS_PERL(5, 11, 3)
213 #define vmg_mg_magical(S) mg_magical(S)
217 static void vmg_mg_magical(SV *sv) {
221 if ((mg = SvMAGIC(sv))) {
223 const MGVTBL* const vtbl = mg->mg_virtual;
225 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
232 } while ((mg = mg->mg_moremagic));
233 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
240 /* --- Trampoline ops ------------------------------------------------------ */
242 #define VMG_NEEDS_TRAMPOLINE VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE || VMG_RESET_RMG_NEEDS_TRAMPOLINE
244 #if VMG_NEEDS_TRAMPOLINE
251 static void vmg_trampoline_init(vmg_trampoline *t, OP *(*cb)(pTHX)) {
252 t->temp.op_type = OP_STUB;
253 t->temp.op_ppaddr = 0;
254 t->temp.op_next = (OP *) &t->target;
255 t->temp.op_flags = 0;
256 t->temp.op_private = 0;
258 t->target.op_type = OP_STUB;
259 t->target.op_ppaddr = cb;
260 t->target.op_next = NULL;
261 t->target.op_flags = 0;
262 t->target.op_private = 0;
263 t->target.op_sv = NULL;
266 static OP *vmg_trampoline_bump(pTHX_ vmg_trampoline *t, SV *sv, OP *o) {
267 #define vmg_trampoline_bump(T, S, O) vmg_trampoline_bump(aTHX_ (T), (S), (O))
269 t->temp.op_next = (OP *) &t->target;
271 t->target.op_sv = sv;
272 t->target.op_next = o->op_next;
277 #endif /* VMG_NEEDS_TRAMPOLINE */
279 /* --- Cleaner version of sv_magicext() ------------------------------------ */
281 static MAGIC *vmg_sv_magicext(pTHX_ SV *sv, SV *obj, const MGVTBL *vtbl, const void *ptr, I32 len) {
282 #define vmg_sv_magicext(S, O, V, P, L) vmg_sv_magicext(aTHX_ (S), (O), (V), (P), (L))
285 mg = sv_magicext(sv, obj, PERL_MAGIC_ext, vtbl, ptr, len);
292 mg->mg_flags |= MGf_COPY;
295 mg->mg_flags |= MGf_DUP;
299 mg->mg_flags |= MGf_LOCAL;
300 #endif /* MGf_LOCAL */
302 if (mg->mg_flags & MGf_REFCOUNTED)
308 /* --- Safe version of call_sv() ------------------------------------------- */
310 static I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, int (*cleanup)(pTHX_ void *), void *ud) {
311 #define vmg_call_sv(S, F, C, U) vmg_call_sv(aTHX_ (S), (F), (C), (U))
313 PERL_CONTEXT saved_cx;
317 old_err = newSVsv(ERRSV);
318 sv_setsv(ERRSV, &PL_sv_undef);
321 cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
322 /* The last popped context will be reused by call_sv(), but our callers may
323 * still need its previous value. Back it up so that it isn't clobbered. */
324 saved_cx = cxstack[cxix];
326 ret = call_sv(sv, flags | G_EVAL);
328 cxstack[cxix] = saved_cx;
331 SvREFCNT_dec(old_err);
333 if (IN_PERL_COMPILETIME) {
336 sv_catsv(PL_errors, ERRSV);
338 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
341 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
343 ++PL_parser->error_count;
344 #elif defined(PL_error_count)
350 if (!cleanup || cleanup(aTHX_ ud))
355 sv_setsv(ERRSV, old_err);
356 SvREFCNT_dec(old_err);
363 /* --- Stolen chunk of B --------------------------------------------------- */
378 #if VMG_HAS_PERL(5, 21, 5)
384 static const char *const vmg_opclassnames[] = {
397 #if VMG_HAS_PERL(5, 21, 5)
403 static opclass vmg_opclass(const OP *o) {
410 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
412 if (o->op_type == OP_SASSIGN)
413 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
415 if (o->op_type == OP_AELEMFAST) {
416 #if PERL_VERSION <= 14
417 if (o->op_flags & OPf_SPECIAL)
429 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
433 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
450 case OA_PVOP_OR_SVOP:
451 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
456 case OA_BASEOP_OR_UNOP:
457 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
459 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
461 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
463 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
466 if (o->op_flags & OPf_STACKED)
468 else if (o->op_flags & OPf_SPECIAL)
472 #if VMG_HAS_PERL(5, 21, 5)
481 /* --- Error messages ------------------------------------------------------ */
483 static const char vmg_invalid_wiz[] = "Invalid wizard object";
484 static const char vmg_wrongargnum[] = "Wrong number of arguments";
485 static const char vmg_argstorefailed[] = "Error while storing arguments";
487 /* --- Context-safe global data -------------------------------------------- */
489 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
492 HV *b__op_stashes[OPc_MAX];
495 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
496 vmg_trampoline propagate_errsv;
498 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
499 vmg_trampoline reset_rmg;
505 /* --- <vmg_vtable> structure ---------------------------------------------- */
514 static vmg_vtable *vmg_vtable_alloc(pTHX) {
515 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
518 t = VOID2(vmg_vtable *, PerlMemShared_malloc(sizeof *t));
520 t->vtbl = VOID2(MGVTBL *, PerlMemShared_malloc(sizeof *t->vtbl));
526 #define vmg_vtable_vtbl(T) (T)->vtbl
528 static perl_mutex vmg_vtable_refcount_mutex;
530 static vmg_vtable *vmg_vtable_dup(pTHX_ vmg_vtable *t) {
531 #define vmg_vtable_dup(T) vmg_vtable_dup(aTHX_ (T))
532 VMG_LOCK(&vmg_vtable_refcount_mutex);
534 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
539 static void vmg_vtable_free(pTHX_ vmg_vtable *t) {
540 #define vmg_vtable_free(T) vmg_vtable_free(aTHX_ (T))
543 VMG_LOCK(&vmg_vtable_refcount_mutex);
544 refcount = --t->refcount;
545 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
548 PerlMemShared_free(t->vtbl);
549 PerlMemShared_free(t);
553 #else /* VMG_THREADSAFE */
555 typedef MGVTBL vmg_vtable;
557 static vmg_vtable *vmg_vtable_alloc(pTHX) {
558 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
561 Newx(t, 1, vmg_vtable);
566 #define vmg_vtable_vtbl(T) ((MGVTBL *) (T))
568 #define vmg_vtable_free(T) Safefree(T)
570 #endif /* !VMG_THREADSAFE */
572 /* --- <vmg_wizard> structure ---------------------------------------------- */
581 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
586 #endif /* MGf_LOCAL */
588 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
589 #endif /* VMG_UVAR */
592 static void vmg_op_info_init(pTHX_ unsigned int opinfo);
594 static vmg_wizard *vmg_wizard_alloc(pTHX_ UV opinfo) {
595 #define vmg_wizard_alloc(O) vmg_wizard_alloc(aTHX_ (O))
598 Newx(w, 1, vmg_wizard);
601 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
603 vmg_op_info_init(aTHX_ w->opinfo);
605 w->vtable = vmg_vtable_alloc();
610 static void vmg_wizard_free(pTHX_ vmg_wizard *w) {
611 #define vmg_wizard_free(W) vmg_wizard_free(aTHX_ (W))
615 /* During global destruction, any of the callbacks may already have been
616 * freed, so we can't rely on still being able to access them. */
618 SvREFCNT_dec(w->cb_data);
619 SvREFCNT_dec(w->cb_get);
620 SvREFCNT_dec(w->cb_set);
621 SvREFCNT_dec(w->cb_len);
622 SvREFCNT_dec(w->cb_clear);
623 SvREFCNT_dec(w->cb_free);
624 SvREFCNT_dec(w->cb_copy);
626 SvREFCNT_dec(w->cb_dup);
629 SvREFCNT_dec(w->cb_local);
630 #endif /* MGf_LOCAL */
632 SvREFCNT_dec(w->cb_fetch);
633 SvREFCNT_dec(w->cb_store);
634 SvREFCNT_dec(w->cb_exists);
635 SvREFCNT_dec(w->cb_delete);
636 #endif /* VMG_UVAR */
639 /* PerlMemShared_free() and Safefree() are still fine during global
640 * destruction though. */
641 vmg_vtable_free(w->vtable);
649 #define VMG_CLONE_CB(N) \
650 z->cb_ ## N = (w->cb_ ## N) ? SvREFCNT_inc(sv_dup(w->cb_ ## N, params)) \
653 static const vmg_wizard *vmg_wizard_dup(pTHX_ const vmg_wizard *w, CLONE_PARAMS *params) {
654 #define vmg_wizard_dup(W, P) vmg_wizard_dup(aTHX_ (W), (P))
660 Newx(z, 1, vmg_wizard);
662 z->vtable = vmg_vtable_dup(w->vtable);
664 z->opinfo = w->opinfo;
676 #endif /* MGf_LOCAL */
680 VMG_CLONE_CB(exists);
681 VMG_CLONE_CB(delete);
682 #endif /* VMG_UVAR */
687 #endif /* VMG_THREADSAFE */
689 #define vmg_wizard_id(W) PTR2IV(vmg_vtable_vtbl((W)->vtable))
691 /* --- Wizard SV objects --------------------------------------------------- */
693 static int vmg_wizard_sv_free(pTHX_ SV *sv, MAGIC *mg) {
694 vmg_wizard_free((vmg_wizard *) mg->mg_ptr);
701 static int vmg_wizard_sv_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
702 mg->mg_ptr = (char *) vmg_wizard_dup((const vmg_wizard *) mg->mg_ptr, params);
707 #endif /* VMG_THREADSAFE */
709 static MGVTBL vmg_wizard_sv_vtbl = {
714 vmg_wizard_sv_free, /* free */
717 vmg_wizard_sv_dup, /* dup */
723 #endif /* MGf_LOCAL */
726 static SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) {
727 #define vmg_wizard_sv_new(W) vmg_wizard_sv_new(aTHX_ (W))
733 wiz = newSViv(PTR2IV(w));
736 vmg_sv_magicext(wiz, NULL, &vmg_wizard_sv_vtbl, w, 0);
745 #define vmg_sv_has_wizard_type(S) (SvTYPE(S) >= SVt_PVMG)
747 static const vmg_wizard *vmg_wizard_from_sv_nocheck(const SV *wiz) {
750 for (mg = SvMAGIC(wiz); mg; mg = mg->mg_moremagic) {
751 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vmg_wizard_sv_vtbl)
752 return (const vmg_wizard *) mg->mg_ptr;
758 #else /* VMG_THREADSAFE */
760 #define vmg_sv_has_wizard_type(S) SvIOK(S)
762 #define vmg_wizard_from_sv_nocheck(W) INT2PTR(const vmg_wizard *, SvIVX(W))
764 #endif /* !VMG_THREADSAFE */
766 #define vmg_wizard_from_sv(W) (vmg_sv_has_wizard_type(W) ? vmg_wizard_from_sv_nocheck(W) : NULL)
768 static const vmg_wizard *vmg_wizard_from_mg(const MAGIC *mg) {
769 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_len == HEf_SVKEY) {
770 SV *sv = (SV *) mg->mg_ptr;
772 if (vmg_sv_has_wizard_type(sv))
773 return vmg_wizard_from_sv_nocheck(sv);
779 #define vmg_wizard_from_mg_nocheck(M) vmg_wizard_from_sv_nocheck((const SV *) (M)->mg_ptr)
781 /* --- User-level functions implementation --------------------------------- */
783 static const MAGIC *vmg_find(const SV *sv, const vmg_wizard *w) {
787 if (SvTYPE(sv) < SVt_PVMG)
790 wid = vmg_wizard_id(w);
792 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
793 const vmg_wizard *z = vmg_wizard_from_mg(mg);
795 if (z && vmg_wizard_id(z) == wid)
802 /* ... Construct private data .............................................. */
804 static SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
805 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
815 EXTEND(SP, items + 1);
816 PUSHs(sv_2mortal(newRV_inc(sv)));
817 for (i = 0; i < items; ++i)
821 vmg_call_sv(ctor, G_SCALAR, 0, NULL);
825 #if VMG_HAS_PERL(5, 8, 3)
826 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
828 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
838 static SV *vmg_data_get(pTHX_ SV *sv, const vmg_wizard *w) {
839 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
840 const MAGIC *mg = vmg_find(sv, w);
842 return mg ? mg->mg_obj : NULL;
845 /* ... Magic cast/dispell .................................................. */
849 static I32 vmg_svt_val(pTHX_ IV, SV *);
852 struct ufuncs new_uf;
853 struct ufuncs old_uf;
856 #endif /* VMG_UVAR */
858 static void vmg_mg_del(pTHX_ SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
859 #define vmg_mg_del(S, P, M, N) vmg_mg_del(aTHX_ (S), (P), (M), (N))
863 prevmagic->mg_moremagic = moremagic;
865 SvMAGIC_set(sv, moremagic);
867 /* Destroy private data */
869 if (mg->mg_type == PERL_MAGIC_uvar) {
870 Safefree(mg->mg_ptr);
872 #endif /* VMG_UVAR */
873 if (mg->mg_obj != sv) {
874 SvREFCNT_dec(mg->mg_obj);
877 /* Unreference the wizard */
878 SvREFCNT_dec((SV *) mg->mg_ptr);
882 #endif /* VMG_UVAR */
885 mg->mg_moremagic = MY_CXT.freed_tokens;
886 MY_CXT.freed_tokens = mg;
888 mg->mg_moremagic = NULL;
893 static int vmg_magic_chain_free(pTHX_ MAGIC *mg, MAGIC *skip) {
894 #define vmg_magic_chain_free(M, S) vmg_magic_chain_free(aTHX_ (M), (S))
898 MAGIC *moremagic = mg->mg_moremagic;
911 static UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, I32 items) {
912 #define vmg_cast(S, W, WIZ, A, I) vmg_cast(aTHX_ (S), (W), (WIZ), (A), (I))
921 oldgmg = SvGMAGICAL(sv);
923 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
925 t = vmg_vtable_vtbl(w->vtable);
926 mg = vmg_sv_magicext(sv, data, t, wiz, HEf_SVKEY);
928 if (SvTYPE(sv) < SVt_PVHV)
931 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
932 * magic is actually never called for them. If the GMAGICAL flag was off before
933 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
934 * now on, then this wizard has get magic. Hence we can work around the
935 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
936 * has uvar callbacks, it will be turned back on later. */
937 if (!oldgmg && SvGMAGICAL(sv))
942 MAGIC *prevmagic, *moremagic = NULL;
945 ud.new_uf.uf_val = vmg_svt_val;
946 ud.new_uf.uf_set = NULL;
947 ud.new_uf.uf_index = 0;
948 ud.old_uf.uf_val = NULL;
949 ud.old_uf.uf_set = NULL;
950 ud.old_uf.uf_index = 0;
952 /* One uvar magic in the chain is enough. */
953 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
954 moremagic = mg->mg_moremagic;
955 if (mg->mg_type == PERL_MAGIC_uvar)
959 if (mg) { /* Found another uvar magic. */
960 struct ufuncs *uf = (struct ufuncs *) mg->mg_ptr;
961 if (uf->uf_val == vmg_svt_val) {
962 /* It's our uvar magic, nothing to do. oldgmg was true. */
965 /* It's another uvar magic, backup it and replace it by ours. */
967 vmg_mg_del(sv, prevmagic, mg, moremagic);
971 sv_magic(sv, NULL, PERL_MAGIC_uvar, (const char *) &ud, sizeof(ud));
973 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
974 * handled by our uvar callback. */
976 #endif /* VMG_UVAR */
982 static UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) {
983 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
986 #endif /* VMG_UVAR */
987 MAGIC *mg, *prevmagic, *moremagic = NULL;
988 IV wid = vmg_wizard_id(w);
990 if (SvTYPE(sv) < SVt_PVMG)
993 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
996 moremagic = mg->mg_moremagic;
998 z = vmg_wizard_from_mg(mg);
1000 IV zid = vmg_wizard_id(z);
1004 /* If the current has no uvar, short-circuit uvar deletion. */
1005 uvars = z->uvar ? (uvars + 1) : 0;
1007 } else if (z->uvar) {
1009 /* We can't break here since we need to find the ext magic to delete. */
1011 #else /* VMG_UVAR */
1014 #endif /* !VMG_UVAR */
1020 vmg_mg_del(sv, prevmagic, mg, moremagic);
1023 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
1024 /* mg was the first ext magic in the chain that had uvar */
1026 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
1027 const vmg_wizard *z = vmg_wizard_from_mg(mg);
1038 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
1039 moremagic = mg->mg_moremagic;
1040 if (mg->mg_type == PERL_MAGIC_uvar)
1044 ud = (vmg_uvar_ud *) mg->mg_ptr;
1045 if (ud->old_uf.uf_val || ud->old_uf.uf_set) {
1046 /* Revert the original uvar magic. */
1048 Newx(uf, 1, struct ufuncs);
1051 mg->mg_ptr = (char *) uf;
1052 mg->mg_len = sizeof(*uf);
1054 /* Remove the uvar magic. */
1055 vmg_mg_del(sv, prevmagic, mg, moremagic);
1059 #endif /* VMG_UVAR */
1066 /* ... OP info ............................................................. */
1068 #define VMG_OP_INFO_NAME 1
1069 #define VMG_OP_INFO_OBJECT 2
1072 static perl_mutex vmg_op_name_init_mutex;
1075 static U32 vmg_op_name_init = 0;
1076 static unsigned char vmg_op_name_len[MAXO] = { 0 };
1078 static void vmg_op_info_init(pTHX_ unsigned int opinfo) {
1079 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
1081 case VMG_OP_INFO_NAME:
1082 VMG_LOCK(&vmg_op_name_init_mutex);
1083 if (!vmg_op_name_init) {
1085 for (t = 0; t < OP_max; ++t)
1086 vmg_op_name_len[t] = strlen(PL_op_name[t]);
1087 vmg_op_name_init = 1;
1089 VMG_UNLOCK(&vmg_op_name_init_mutex);
1091 case VMG_OP_INFO_OBJECT: {
1093 if (!MY_CXT.b__op_stashes[0]) {
1096 for (c = OPc_NULL; c < OPc_MAX; ++c)
1097 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
1106 static SV *vmg_op_info(pTHX_ unsigned int opinfo) {
1107 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
1109 return &PL_sv_undef;
1112 case VMG_OP_INFO_NAME: {
1113 OPCODE t = PL_op->op_type;
1114 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
1116 case VMG_OP_INFO_OBJECT: {
1118 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1119 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1125 return &PL_sv_undef;
1128 /* --- svt callbacks ------------------------------------------------------- */
1130 #define VMG_CB_CALL_ARGS_MASK 15
1131 #define VMG_CB_CALL_ARGS_SHIFT 4
1132 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT) /* 1|2 */
1133 #define VMG_CB_CALL_GUARD 4
1135 static int vmg_dispell_guard_oncroak(pTHX_ void *ud) {
1140 /* If we're at the upmost magic call and we're about to die, we can just free
1141 * the tokens right now, since we will jump past the problematic part of our
1143 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
1144 vmg_magic_chain_free(MY_CXT.freed_tokens, NULL);
1145 MY_CXT.freed_tokens = NULL;
1151 static int vmg_dispell_guard_free(pTHX_ SV *sv, MAGIC *mg) {
1152 vmg_magic_chain_free((MAGIC *) mg->mg_ptr, NULL);
1159 static int vmg_dispell_guard_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
1160 /* The freed magic tokens aren't cloned by perl because it cannot reach them
1161 * (they have been detached from their parent SV when they were enqueued).
1162 * Hence there's nothing to purge in the new thread. */
1168 #endif /* VMG_THREADSAFE */
1170 static MGVTBL vmg_dispell_guard_vtbl = {
1175 vmg_dispell_guard_free, /* free */
1178 vmg_dispell_guard_dup, /* dup */
1184 #endif /* MGf_LOCAL */
1187 static SV *vmg_dispell_guard_new(pTHX_ MAGIC *root) {
1188 #define vmg_dispell_guard_new(R) vmg_dispell_guard_new(aTHX_ (R))
1191 guard = sv_newmortal();
1192 vmg_sv_magicext(guard, NULL, &vmg_dispell_guard_vtbl, root, 0);
1197 static int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1200 unsigned int i, args, opinfo;
1201 MAGIC **chain = NULL;
1206 args = flags & VMG_CB_CALL_ARGS_MASK;
1207 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1208 opinfo = flags & VMG_CB_CALL_OPINFO;
1214 EXTEND(SP, args + 1);
1215 PUSHs(sv_2mortal(newRV_inc(sv)));
1217 for (i = 0; i < args; ++i) {
1218 SV *sva = va_arg(ap, SV *);
1219 PUSHs(sva ? sva : &PL_sv_undef);
1223 XPUSHs(vmg_op_info(opinfo));
1226 if (flags & VMG_CB_CALL_GUARD) {
1229 vmg_call_sv(cb, G_SCALAR, vmg_dispell_guard_oncroak, NULL);
1231 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens)
1232 chain = &MY_CXT.freed_tokens;
1234 vmg_call_sv(cb, G_SCALAR, 0, NULL);
1240 ret = (int) SvIV(svr);
1247 vmg_dispell_guard_new(*chain);
1254 #define VMG_CB_FLAGS(OI, A) \
1255 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1257 #define vmg_cb_call1(I, OI, S, A1) \
1258 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1259 #define vmg_cb_call2(I, OI, S, A1, A2) \
1260 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1261 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1262 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1264 /* ... Default no-op magic callback ........................................ */
1266 static int vmg_svt_default_noop(pTHX_ SV *sv, MAGIC *mg) {
1270 /* ... get magic ........................................................... */
1272 static int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1273 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1275 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1278 #define vmg_svt_get_noop vmg_svt_default_noop
1280 /* ... set magic ........................................................... */
1282 static int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1283 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1285 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1288 #define vmg_svt_set_noop vmg_svt_default_noop
1290 /* ... len magic ........................................................... */
1292 static U32 vmg_sv_len(pTHX_ SV *sv) {
1293 #define vmg_sv_len(S) vmg_sv_len(aTHX_ (S))
1295 #if VMG_HAS_PERL(5, 9, 3)
1296 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, len)));
1298 U8 *s = SvPV(sv, len);
1301 return DO_UTF8(sv) ? utf8_length(s, s + len) : len;
1304 static U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1305 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1306 unsigned int opinfo = w->opinfo;
1309 svtype t = SvTYPE(sv);
1318 PUSHs(sv_2mortal(newRV_inc(sv)));
1319 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1321 len = vmg_sv_len(sv);
1323 } else if (t == SVt_PVAV) {
1324 len = av_len((AV *) sv) + 1;
1328 PUSHs(&PL_sv_undef);
1331 XPUSHs(vmg_op_info(opinfo));
1334 vmg_call_sv(w->cb_len, G_SCALAR, 0, NULL);
1338 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1349 static U32 vmg_svt_len_noop(pTHX_ SV *sv, MAGIC *mg) {
1351 svtype t = SvTYPE(sv);
1354 len = vmg_sv_len(sv);
1355 } else if (t == SVt_PVAV) {
1356 len = (U32) av_len((AV *) sv);
1362 /* ... clear magic ......................................................... */
1364 static int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1365 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1366 unsigned int flags = w->opinfo;
1368 #if !VMG_HAS_PERL(5, 12, 0)
1369 flags |= VMG_CB_CALL_GUARD;
1372 return vmg_cb_call1(w->cb_clear, flags, sv, mg->mg_obj);
1375 #define vmg_svt_clear_noop vmg_svt_default_noop
1377 /* ... free magic .......................................................... */
1379 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1381 static OP *vmg_pp_propagate_errsv(pTHX) {
1382 SVOP *o = cSVOPx(PL_op);
1385 sv_setsv(ERRSV, o->op_sv);
1386 SvREFCNT_dec(o->op_sv);
1393 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1395 static int vmg_propagate_errsv_free(pTHX_ SV *sv, MAGIC *mg) {
1397 sv_setsv(ERRSV, mg->mg_obj);
1402 /* perl is already kind enough to handle the cloning of the mg_obj member,
1403 hence we don't need to define a dup magic callback. */
1405 static MGVTBL vmg_propagate_errsv_vtbl = {
1410 vmg_propagate_errsv_free, /* free */
1415 #endif /* MGf_LOCAL */
1422 } vmg_svt_free_cleanup_ud;
1424 static int vmg_svt_free_cleanup(pTHX_ void *ud_) {
1425 vmg_svt_free_cleanup_ud *ud = VOID2(vmg_svt_free_cleanup_ud *, ud_);
1428 U32 optype = PL_op ? PL_op->op_type : OP_NULL;
1430 if (optype == OP_LEAVETRY || optype == OP_LEAVEEVAL) {
1431 SV *errsv = newSVsv(ERRSV);
1434 LEAVE_SCOPE(ud->base);
1436 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1437 if (optype == OP_LEAVETRY) {
1439 PL_op = vmg_trampoline_bump(&MY_CXT.propagate_errsv, errsv, PL_op);
1440 } else if (optype == OP_LEAVEEVAL) {
1441 SV *guard = sv_newmortal();
1442 vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1444 #else /* !VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1445 # if !VMG_HAS_PERL(5, 8, 9)
1447 SV *guard = sv_newmortal();
1448 vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1451 vmg_sv_magicext(ERRSV, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1453 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1458 /* Don't propagate */
1464 /* We are about to croak() while sv is being destroyed. Try to clean up
1468 vmg_mg_del(sv, NULL, mg, mg->mg_moremagic);
1473 vmg_dispell_guard_oncroak(aTHX_ NULL);
1475 /* After that, propagate the error upwards. */
1480 static int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1481 vmg_svt_free_cleanup_ud ud;
1482 const vmg_wizard *w;
1488 /* During global destruction, we cannot be sure that the wizard and its free
1489 * callback are still alive. */
1493 w = vmg_wizard_from_mg_nocheck(mg);
1495 /* So that it survives the temp cleanup below */
1496 SvREFCNT_inc_simple_void(sv);
1498 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1499 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1500 * if you access the sv from the callback the old deleted magics will trigger
1501 * and cause memory misreads. Change 32686 solved it that way : */
1502 SvMAGIC_set(sv, mg);
1506 if (cxstack_ix < cxstack_max) {
1507 ud.in_eval = (CxTYPE(cxstack + cxstack_ix + 1) == CXt_EVAL);
1508 ud.base = ud.in_eval ? PL_scopestack[PL_scopestack_ix] : 0;
1519 PUSHs(sv_2mortal(newRV_inc(sv)));
1520 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1522 XPUSHs(vmg_op_info(w->opinfo));
1528 vmg_call_sv(w->cb_free, G_SCALAR, vmg_svt_free_cleanup, &ud);
1530 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
1531 /* Free all the tokens in the chain but the current one (if it's present).
1532 * It will be taken care of by our caller, Perl_mg_free(). */
1533 vmg_magic_chain_free(MY_CXT.freed_tokens, mg);
1534 MY_CXT.freed_tokens = NULL;
1541 ret = (int) SvIV(svr);
1547 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1548 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1551 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1552 * mg->mg_ptr reference count */
1556 #define vmg_svt_free_noop vmg_svt_default_noop
1558 #if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1559 # define VMG_SVT_COPY_KEYLEN_TYPE I32
1561 # define VMG_SVT_COPY_KEYLEN_TYPE int
1564 /* ... copy magic .......................................................... */
1566 static int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1567 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1571 if (keylen == HEf_SVKEY) {
1574 keysv = newSVpvn(key, keylen);
1577 if (SvTYPE(sv) >= SVt_PVCV)
1578 nsv = sv_2mortal(newRV_inc(nsv));
1580 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1582 if (keylen != HEf_SVKEY) {
1583 SvREFCNT_dec(keysv);
1589 static int vmg_svt_copy_noop(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1593 /* ... dup magic ........................................................... */
1596 static int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1599 #define vmg_svt_dup_noop vmg_svt_dup
1602 /* ... local magic ......................................................... */
1606 static int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1607 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1609 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1612 #define vmg_svt_local_noop vmg_svt_default_noop
1614 #endif /* MGf_LOCAL */
1616 /* ... uvar magic .......................................................... */
1620 static OP *vmg_pp_reset_rmg(pTHX) {
1621 SVOP *o = cSVOPx(PL_op);
1623 SvRMAGICAL_on(o->op_sv);
1629 static I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1631 MAGIC *mg, *umg, *moremagic;
1632 SV *key = NULL, *newkey = NULL;
1635 umg = mg_find(sv, PERL_MAGIC_uvar);
1636 /* umg can't be NULL or we wouldn't be there. */
1638 ud = (vmg_uvar_ud *) umg->mg_ptr;
1640 if (ud->old_uf.uf_val)
1641 ud->old_uf.uf_val(aTHX_ action, sv);
1642 if (ud->old_uf.uf_set)
1643 ud->old_uf.uf_set(aTHX_ action, sv);
1645 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
1646 const vmg_wizard *w;
1648 /* mg may be freed later by the uvar call, so we need to fetch the next
1649 * token before reaching that fateful point. */
1650 moremagic = mg->mg_moremagic;
1652 switch (mg->mg_type) {
1653 case PERL_MAGIC_ext:
1655 case PERL_MAGIC_tied:
1662 w = vmg_wizard_from_mg(mg);
1671 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1675 & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS|HV_FETCH_LVALUE|HV_DELETE)) {
1678 vmg_cb_call2(w->cb_fetch, w->opinfo | VMG_CB_CALL_GUARD, sv,
1681 case HV_FETCH_ISSTORE:
1682 case HV_FETCH_LVALUE:
1683 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1685 vmg_cb_call2(w->cb_store, w->opinfo | VMG_CB_CALL_GUARD, sv,
1688 case HV_FETCH_ISEXISTS:
1690 vmg_cb_call2(w->cb_exists, w->opinfo | VMG_CB_CALL_GUARD, sv,
1695 vmg_cb_call2(w->cb_delete, w->opinfo | VMG_CB_CALL_GUARD, sv,
1701 if (SvRMAGICAL(sv) && !tied && !(action & (HV_FETCH_ISSTORE|HV_DELETE))) {
1702 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1703 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1704 * the op_ppaddr of a new fake op injected between the current and the next
1707 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
1711 PL_op = vmg_trampoline_bump(&MY_CXT.reset_rmg, sv, PL_op);
1713 #else /* !VMG_RESET_RMG_NEEDS_TRAMPOLINE */
1715 OP *nop = PL_op->op_next;
1718 if (nop && nop->op_ppaddr == vmg_pp_reset_rmg) {
1719 svop = (SVOP *) nop;
1721 NewOp(1101, svop, 1, SVOP);
1722 svop->op_type = OP_STUB;
1723 svop->op_ppaddr = vmg_pp_reset_rmg;
1724 svop->op_next = nop;
1726 svop->op_private = 0;
1728 PL_op->op_next = (OP *) svop;
1733 #endif /* VMG_RESET_RMG_NEEDS_TRAMPOLINE */
1741 #endif /* VMG_UVAR */
1743 /* --- Global setup/teardown ----------------------------------------------- */
1745 static U32 vmg_initialized = 0;
1747 static void vmg_global_teardown_late(pTHX) {
1748 #define vmg_global_teardown_late() vmg_global_teardown_late(aTHX)
1750 MUTEX_DESTROY(&vmg_op_name_init_mutex);
1751 MUTEX_DESTROY(&vmg_vtable_refcount_mutex);
1754 vmg_initialized = 0;
1759 static int vmg_global_teardown_free(pTHX_ SV *sv, MAGIC *mg) {
1760 vmg_global_teardown_late();
1765 static MGVTBL vmg_global_teardown_vtbl = {
1770 vmg_global_teardown_free
1782 static signed char vmg_destruct_level(pTHX) {
1783 #define vmg_destruct_level() vmg_destruct_level(aTHX)
1786 lvl = PL_perl_destruct_level;
1790 const char *s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL");
1793 #if VMG_HAS_PERL(5, 21, 3)
1794 if (strEQ(s, "-1")) {
1797 # if VMG_HAS_PERL(5, 21, 10)
1799 if (Perl_grok_atoUV(s, &uv, NULL) && uv <= INT_MAX)
1803 # else /* VMG_HAS_PERL(5, 21, 3) && !VMG_HAS_PERL(5, 21, 10) */
1804 i = Perl_grok_atou(s, NULL);
1807 #else /* !VMG_HAS_PERL(5, 21, 3) */
1819 static void vmg_global_teardown(pTHX_ void *root) {
1820 if (!vmg_initialized)
1823 #if VMG_MULTIPLICITY
1828 if (vmg_destruct_level() == 0) {
1829 vmg_global_teardown_late();
1832 PL_strtab = newHV();
1833 vmg_sv_magicext((SV *) PL_strtab, NULL, &vmg_global_teardown_vtbl, NULL, 0);
1839 static void vmg_global_setup(pTHX) {
1840 #define vmg_global_setup() vmg_global_setup(aTHX)
1841 if (vmg_initialized)
1845 MUTEX_INIT(&vmg_vtable_refcount_mutex);
1846 MUTEX_INIT(&vmg_op_name_init_mutex);
1849 #if VMG_MULTIPLICITY
1850 call_atexit(vmg_global_teardown, aTHX);
1852 call_atexit(vmg_global_teardown, NULL);
1855 vmg_initialized = 1;
1860 /* --- Interpreter setup/teardown ------------------------------------------ */
1862 static void vmg_local_teardown(pTHX_ void *param) {
1865 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
1866 vmg_magic_chain_free(MY_CXT.freed_tokens, NULL);
1867 MY_CXT.freed_tokens = NULL;
1873 static void vmg_local_setup(pTHX) {
1874 #define vmg_local_setup() vmg_local_setup(aTHX)
1879 for (c = OPc_NULL; c < OPc_MAX; ++c)
1880 MY_CXT.b__op_stashes[c] = NULL;
1883 MY_CXT.freed_tokens = NULL;
1885 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1886 vmg_trampoline_init(&MY_CXT.propagate_errsv, vmg_pp_propagate_errsv);
1888 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
1889 vmg_trampoline_init(&MY_CXT.reset_rmg, vmg_pp_reset_rmg);
1892 stash = gv_stashpv(__PACKAGE__, 1);
1893 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1894 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1895 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1896 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1897 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1898 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1899 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_NOLEN",
1900 newSVuv(VMG_COMPAT_SCALAR_NOLEN));
1901 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1902 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1903 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1904 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1905 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1906 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1907 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1908 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1909 newCONSTSUB(stash, "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID",
1910 newSVuv(VMG_COMPAT_HASH_DELETE_NOUVAR_VOID));
1911 newCONSTSUB(stash, "VMG_COMPAT_CODE_COPY_CLONE",
1912 newSVuv(VMG_COMPAT_CODE_COPY_CLONE));
1913 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1914 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1915 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1916 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1917 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1918 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1920 call_atexit(vmg_local_teardown, NULL);
1925 /* --- Macros for the XS section ------------------------------------------- */
1928 # define VMG_CVOK(C) \
1929 ((CvISXSUB(C) ? (void *) CvXSUB(C) : (void *) CvROOT(C)) ? 1 : 0)
1931 # define VMG_CVOK(C) (CvROOT(C) || CvXSUB(C))
1934 #define VMG_CBOK(S) ((SvTYPE(S) == SVt_PVCV) ? VMG_CVOK(S) : SvOK(S))
1936 #define VMG_SET_CB(S, N) { \
1938 if (SvOK(cb) && SvROK(cb)) { \
1941 SvREFCNT_inc_simple_void(cb); \
1950 #define VMG_SET_SVT_CB(S, N) { \
1952 if (SvOK(cb) && SvROK(cb)) { \
1954 if (VMG_CBOK(cb)) { \
1955 t->svt_ ## N = vmg_svt_ ## N; \
1956 SvREFCNT_inc_simple_void(cb); \
1958 t->svt_ ## N = vmg_svt_ ## N ## _noop; \
1962 t->svt_ ## N = NULL; \
1968 /* --- XS ------------------------------------------------------------------ */
1970 MODULE = Variable::Magic PACKAGE = Variable::Magic
1986 U32 had_b__op_stash = 0;
1992 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1993 if (MY_CXT.b__op_stashes[c])
1994 had_b__op_stash |= (((U32) 1) << c);
1996 old_depth = MY_CXT.depth;
2000 for (c = OPc_NULL; c < OPc_MAX; ++c) {
2001 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
2002 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
2004 MY_CXT.depth = old_depth;
2005 MY_CXT.freed_tokens = NULL;
2009 #endif /* VMG_THREADSAFE */
2016 SV *op_info, *copy_key;
2022 #endif /* MGf_LOCAL */
2025 #endif /* VMG_UVAR */
2026 ) { croak(vmg_wrongargnum); }
2029 w = vmg_wizard_alloc(SvOK(op_info) ? SvUV(op_info) : 0);
2030 t = vmg_vtable_vtbl(w->vtable);
2032 VMG_SET_CB(ST(i++), data);
2034 VMG_SET_SVT_CB(ST(i++), get);
2035 VMG_SET_SVT_CB(ST(i++), set);
2036 VMG_SET_SVT_CB(ST(i++), len);
2037 VMG_SET_SVT_CB(ST(i++), clear);
2038 VMG_SET_SVT_CB(ST(i++), free);
2039 VMG_SET_SVT_CB(ST(i++), copy);
2040 /* VMG_SET_SVT_CB(ST(i++), dup); */
2045 VMG_SET_SVT_CB(ST(i++), local);
2046 #endif /* MGf_LOCAL */
2048 VMG_SET_CB(ST(i++), fetch);
2049 VMG_SET_CB(ST(i++), store);
2050 VMG_SET_CB(ST(i++), exists);
2051 VMG_SET_CB(ST(i++), delete);
2054 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
2055 w->uvar = SvTRUE(copy_key) ? 2 : 1;
2056 #endif /* VMG_UVAR */
2058 RETVAL = newRV_noinc(vmg_wizard_sv_new(w));
2062 SV *cast(SV *sv, SV *wiz, ...)
2063 PROTOTYPE: \[$@%&*]$@
2065 const vmg_wizard *w = NULL;
2074 wiz = SvRV_const(wiz);
2075 w = vmg_wizard_from_sv(wiz);
2078 croak(vmg_invalid_wiz);
2079 RETVAL = newSVuv(vmg_cast(SvRV(sv), w, wiz, args, i));
2084 getdata(SV *sv, SV *wiz)
2085 PROTOTYPE: \[$@%&*]$
2087 const vmg_wizard *w = NULL;
2091 w = vmg_wizard_from_sv(SvRV_const(wiz));
2093 croak(vmg_invalid_wiz);
2094 data = vmg_data_get(SvRV(sv), w);
2100 SV *dispell(SV *sv, SV *wiz)
2101 PROTOTYPE: \[$@%&*]$
2103 const vmg_wizard *w = NULL;
2106 w = vmg_wizard_from_sv(SvRV_const(wiz));
2108 croak(vmg_invalid_wiz);
2109 RETVAL = newSVuv(vmg_dispell(SvRV(sv), w));