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 /* --- Safe version of call_sv() ------------------------------------------- */
281 STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, int (*cleanup)(pTHX_ void *), void *ud) {
282 #define vmg_call_sv(S, F, C, U) vmg_call_sv(aTHX_ (S), (F), (C), (U))
284 PERL_CONTEXT saved_cx;
292 cxix = (cxstack_ix < cxstack_max) ? (cxstack_ix + 1) : Perl_cxinc(aTHX);
293 /* The last popped context will be reused by call_sv(), but our callers may
294 * still need its previous value. Back it up so that it isn't clobbered. */
295 saved_cx = cxstack[cxix];
297 ret = call_sv(sv, flags | G_EVAL);
299 cxstack[cxix] = saved_cx;
303 sv_setsv(old_err, ERRSV);
307 if (IN_PERL_COMPILETIME) {
310 sv_catsv(PL_errors, ERRSV);
312 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
315 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
317 ++PL_parser->error_count;
318 #elif defined(PL_error_count)
324 if (!cleanup || cleanup(aTHX_ ud))
337 /* --- Stolen chunk of B --------------------------------------------------- */
352 #if VMG_HAS_PERL(5, 21, 5)
358 STATIC const char *const vmg_opclassnames[] = {
371 #if VMG_HAS_PERL(5, 21, 5)
377 STATIC opclass vmg_opclass(const OP *o) {
384 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
386 if (o->op_type == OP_SASSIGN)
387 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
389 if (o->op_type == OP_AELEMFAST) {
390 #if PERL_VERSION <= 14
391 if (o->op_flags & OPf_SPECIAL)
403 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
407 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
424 case OA_PVOP_OR_SVOP:
425 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
430 case OA_BASEOP_OR_UNOP:
431 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
433 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
435 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
437 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
440 if (o->op_flags & OPf_STACKED)
442 else if (o->op_flags & OPf_SPECIAL)
446 #if VMG_HAS_PERL(5, 21, 5)
455 /* --- Error messages ------------------------------------------------------ */
457 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
458 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
459 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
461 /* --- Context-safe global data -------------------------------------------- */
463 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
466 HV *b__op_stashes[OPc_MAX];
469 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
470 vmg_trampoline propagate_errsv;
472 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
473 vmg_trampoline reset_rmg;
479 /* --- <vmg_vtable> structure ---------------------------------------------- */
488 STATIC vmg_vtable *vmg_vtable_alloc(pTHX) {
489 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
492 t = VOID2(vmg_vtable *, PerlMemShared_malloc(sizeof *t));
494 t->vtbl = VOID2(MGVTBL *, PerlMemShared_malloc(sizeof *t->vtbl));
500 #define vmg_vtable_vtbl(T) (T)->vtbl
502 STATIC perl_mutex vmg_vtable_refcount_mutex;
504 STATIC vmg_vtable *vmg_vtable_dup(pTHX_ vmg_vtable *t) {
505 #define vmg_vtable_dup(T) vmg_vtable_dup(aTHX_ (T))
506 VMG_LOCK(&vmg_vtable_refcount_mutex);
508 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
513 STATIC void vmg_vtable_free(pTHX_ vmg_vtable *t) {
514 #define vmg_vtable_free(T) vmg_vtable_free(aTHX_ (T))
517 VMG_LOCK(&vmg_vtable_refcount_mutex);
518 refcount = --t->refcount;
519 VMG_UNLOCK(&vmg_vtable_refcount_mutex);
522 PerlMemShared_free(t->vtbl);
523 PerlMemShared_free(t);
527 #else /* VMG_THREADSAFE */
529 typedef MGVTBL vmg_vtable;
531 STATIC vmg_vtable *vmg_vtable_alloc(pTHX) {
532 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
535 Newx(t, 1, vmg_vtable);
540 #define vmg_vtable_vtbl(T) ((MGVTBL *) (T))
542 #define vmg_vtable_free(T) Safefree(T)
544 #endif /* !VMG_THREADSAFE */
546 /* --- <vmg_wizard> structure ---------------------------------------------- */
555 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
560 #endif /* MGf_LOCAL */
562 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
563 #endif /* VMG_UVAR */
566 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo);
568 STATIC vmg_wizard *vmg_wizard_alloc(pTHX_ UV opinfo) {
569 #define vmg_wizard_alloc(O) vmg_wizard_alloc(aTHX_ (O))
572 Newx(w, 1, vmg_wizard);
575 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
577 vmg_op_info_init(aTHX_ w->opinfo);
579 w->vtable = vmg_vtable_alloc();
584 STATIC void vmg_wizard_free(pTHX_ vmg_wizard *w) {
585 #define vmg_wizard_free(W) vmg_wizard_free(aTHX_ (W))
589 /* During global destruction, any of the callbacks may already have been
590 * freed, so we can't rely on still being able to access them. */
592 SvREFCNT_dec(w->cb_data);
593 SvREFCNT_dec(w->cb_get);
594 SvREFCNT_dec(w->cb_set);
595 SvREFCNT_dec(w->cb_len);
596 SvREFCNT_dec(w->cb_clear);
597 SvREFCNT_dec(w->cb_free);
598 SvREFCNT_dec(w->cb_copy);
600 SvREFCNT_dec(w->cb_dup);
603 SvREFCNT_dec(w->cb_local);
604 #endif /* MGf_LOCAL */
606 SvREFCNT_dec(w->cb_fetch);
607 SvREFCNT_dec(w->cb_store);
608 SvREFCNT_dec(w->cb_exists);
609 SvREFCNT_dec(w->cb_delete);
610 #endif /* VMG_UVAR */
613 /* PerlMemShared_free() and Safefree() are still fine during global
614 * destruction though. */
615 vmg_vtable_free(w->vtable);
623 #define VMG_CLONE_CB(N) \
624 z->cb_ ## N = (w->cb_ ## N) ? SvREFCNT_inc(sv_dup(w->cb_ ## N, params)) \
627 STATIC const vmg_wizard *vmg_wizard_dup(pTHX_ const vmg_wizard *w, CLONE_PARAMS *params) {
628 #define vmg_wizard_dup(W, P) vmg_wizard_dup(aTHX_ (W), (P))
634 Newx(z, 1, vmg_wizard);
636 z->vtable = vmg_vtable_dup(w->vtable);
638 z->opinfo = w->opinfo;
650 #endif /* MGf_LOCAL */
654 VMG_CLONE_CB(exists);
655 VMG_CLONE_CB(delete);
656 #endif /* VMG_UVAR */
661 #endif /* VMG_THREADSAFE */
663 #define vmg_wizard_id(W) PTR2IV(vmg_vtable_vtbl((W)->vtable))
665 /* --- Wizard SV objects --------------------------------------------------- */
667 STATIC int vmg_wizard_sv_free(pTHX_ SV *sv, MAGIC *mg) {
668 vmg_wizard_free((vmg_wizard *) mg->mg_ptr);
675 STATIC int vmg_wizard_sv_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
676 mg->mg_ptr = (char *) vmg_wizard_dup((const vmg_wizard *) mg->mg_ptr, params);
681 #endif /* VMG_THREADSAFE */
683 STATIC MGVTBL vmg_wizard_sv_vtbl = {
688 vmg_wizard_sv_free, /* free */
691 vmg_wizard_sv_dup, /* dup */
697 #endif /* MGf_LOCAL */
700 STATIC SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) {
701 #define vmg_wizard_sv_new(W) vmg_wizard_sv_new(aTHX_ (W))
707 wiz = newSViv(PTR2IV(w));
711 MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_sv_vtbl,
712 (const char *) w, 0);
715 mg->mg_flags |= MGf_DUP;
725 #define vmg_sv_has_wizard_type(S) (SvTYPE(S) >= SVt_PVMG)
727 STATIC const vmg_wizard *vmg_wizard_from_sv_nocheck(const SV *wiz) {
730 for (mg = SvMAGIC(wiz); mg; mg = mg->mg_moremagic) {
731 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vmg_wizard_sv_vtbl)
732 return (const vmg_wizard *) mg->mg_ptr;
738 #else /* VMG_THREADSAFE */
740 #define vmg_sv_has_wizard_type(S) SvIOK(S)
742 #define vmg_wizard_from_sv_nocheck(W) INT2PTR(const vmg_wizard *, SvIVX(W))
744 #endif /* !VMG_THREADSAFE */
746 #define vmg_wizard_from_sv(W) (vmg_sv_has_wizard_type(W) ? vmg_wizard_from_sv_nocheck(W) : NULL)
748 STATIC const vmg_wizard *vmg_wizard_from_mg(const MAGIC *mg) {
749 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_len == HEf_SVKEY) {
750 SV *sv = (SV *) mg->mg_ptr;
752 if (vmg_sv_has_wizard_type(sv))
753 return vmg_wizard_from_sv_nocheck(sv);
759 #define vmg_wizard_from_mg_nocheck(M) vmg_wizard_from_sv_nocheck((const SV *) (M)->mg_ptr)
761 /* --- User-level functions implementation --------------------------------- */
763 STATIC const MAGIC *vmg_find(const SV *sv, const vmg_wizard *w) {
767 if (SvTYPE(sv) < SVt_PVMG)
770 wid = vmg_wizard_id(w);
772 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
773 const vmg_wizard *z = vmg_wizard_from_mg(mg);
775 if (z && vmg_wizard_id(z) == wid)
782 /* ... Construct private data .............................................. */
784 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
785 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
795 EXTEND(SP, items + 1);
796 PUSHs(sv_2mortal(newRV_inc(sv)));
797 for (i = 0; i < items; ++i)
801 vmg_call_sv(ctor, G_SCALAR, 0, NULL);
805 #if VMG_HAS_PERL(5, 8, 3)
806 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
808 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
818 STATIC SV *vmg_data_get(pTHX_ SV *sv, const vmg_wizard *w) {
819 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
820 const MAGIC *mg = vmg_find(sv, w);
822 return mg ? mg->mg_obj : NULL;
825 /* ... Magic cast/dispell .................................................. */
829 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
832 struct ufuncs new_uf;
833 struct ufuncs old_uf;
836 #endif /* VMG_UVAR */
838 STATIC void vmg_mg_del(pTHX_ SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
839 #define vmg_mg_del(S, P, M, N) vmg_mg_del(aTHX_ (S), (P), (M), (N))
843 prevmagic->mg_moremagic = moremagic;
845 SvMAGIC_set(sv, moremagic);
847 /* Destroy private data */
849 if (mg->mg_type == PERL_MAGIC_uvar) {
850 Safefree(mg->mg_ptr);
852 #endif /* VMG_UVAR */
853 if (mg->mg_obj != sv) {
854 SvREFCNT_dec(mg->mg_obj);
857 /* Unreference the wizard */
858 SvREFCNT_dec((SV *) mg->mg_ptr);
862 #endif /* VMG_UVAR */
865 mg->mg_moremagic = MY_CXT.freed_tokens;
866 MY_CXT.freed_tokens = mg;
868 mg->mg_moremagic = NULL;
873 STATIC int vmg_magic_chain_free(pTHX_ MAGIC *mg, MAGIC *skip) {
874 #define vmg_magic_chain_free(M, S) vmg_magic_chain_free(aTHX_ (M), (S))
878 MAGIC *moremagic = mg->mg_moremagic;
891 STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, I32 items) {
892 #define vmg_cast(S, W, WIZ, A, I) vmg_cast(aTHX_ (S), (W), (WIZ), (A), (I))
901 oldgmg = SvGMAGICAL(sv);
903 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
905 t = vmg_vtable_vtbl(w->vtable);
906 mg = sv_magicext(sv, data, PERL_MAGIC_ext, t, (const char *) wiz, HEf_SVKEY);
909 /* sv_magicext() calls mg_magical and increments data's refcount */
913 mg->mg_flags |= MGf_COPY;
916 mg->mg_flags |= MGf_DUP;
920 mg->mg_flags |= MGf_LOCAL;
921 #endif /* MGf_LOCAL */
923 if (SvTYPE(sv) < SVt_PVHV)
926 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
927 * magic is actually never called for them. If the GMAGICAL flag was off before
928 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
929 * now on, then this wizard has get magic. Hence we can work around the
930 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
931 * has uvar callbacks, it will be turned back on later. */
932 if (!oldgmg && SvGMAGICAL(sv))
937 MAGIC *prevmagic, *moremagic = NULL;
940 ud.new_uf.uf_val = vmg_svt_val;
941 ud.new_uf.uf_set = NULL;
942 ud.new_uf.uf_index = 0;
943 ud.old_uf.uf_val = NULL;
944 ud.old_uf.uf_set = NULL;
945 ud.old_uf.uf_index = 0;
947 /* One uvar magic in the chain is enough. */
948 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
949 moremagic = mg->mg_moremagic;
950 if (mg->mg_type == PERL_MAGIC_uvar)
954 if (mg) { /* Found another uvar magic. */
955 struct ufuncs *uf = (struct ufuncs *) mg->mg_ptr;
956 if (uf->uf_val == vmg_svt_val) {
957 /* It's our uvar magic, nothing to do. oldgmg was true. */
960 /* It's another uvar magic, backup it and replace it by ours. */
962 vmg_mg_del(sv, prevmagic, mg, moremagic);
966 sv_magic(sv, NULL, PERL_MAGIC_uvar, (const char *) &ud, sizeof(ud));
968 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
969 * handled by our uvar callback. */
971 #endif /* VMG_UVAR */
977 STATIC UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) {
978 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
981 #endif /* VMG_UVAR */
982 MAGIC *mg, *prevmagic, *moremagic = NULL;
983 IV wid = vmg_wizard_id(w);
985 if (SvTYPE(sv) < SVt_PVMG)
988 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
991 moremagic = mg->mg_moremagic;
993 z = vmg_wizard_from_mg(mg);
995 IV zid = vmg_wizard_id(z);
999 /* If the current has no uvar, short-circuit uvar deletion. */
1000 uvars = z->uvar ? (uvars + 1) : 0;
1002 } else if (z->uvar) {
1004 /* We can't break here since we need to find the ext magic to delete. */
1006 #else /* VMG_UVAR */
1009 #endif /* !VMG_UVAR */
1015 vmg_mg_del(sv, prevmagic, mg, moremagic);
1018 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
1019 /* mg was the first ext magic in the chain that had uvar */
1021 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
1022 const vmg_wizard *z = vmg_wizard_from_mg(mg);
1033 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
1034 moremagic = mg->mg_moremagic;
1035 if (mg->mg_type == PERL_MAGIC_uvar)
1039 ud = (vmg_uvar_ud *) mg->mg_ptr;
1040 if (ud->old_uf.uf_val || ud->old_uf.uf_set) {
1041 /* Revert the original uvar magic. */
1043 Newx(uf, 1, struct ufuncs);
1046 mg->mg_ptr = (char *) uf;
1047 mg->mg_len = sizeof(*uf);
1049 /* Remove the uvar magic. */
1050 vmg_mg_del(sv, prevmagic, mg, moremagic);
1054 #endif /* VMG_UVAR */
1061 /* ... OP info ............................................................. */
1063 #define VMG_OP_INFO_NAME 1
1064 #define VMG_OP_INFO_OBJECT 2
1067 STATIC perl_mutex vmg_op_name_init_mutex;
1070 STATIC U32 vmg_op_name_init = 0;
1071 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
1073 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
1074 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
1076 case VMG_OP_INFO_NAME:
1077 VMG_LOCK(&vmg_op_name_init_mutex);
1078 if (!vmg_op_name_init) {
1080 for (t = 0; t < OP_max; ++t)
1081 vmg_op_name_len[t] = strlen(PL_op_name[t]);
1082 vmg_op_name_init = 1;
1084 VMG_UNLOCK(&vmg_op_name_init_mutex);
1086 case VMG_OP_INFO_OBJECT: {
1088 if (!MY_CXT.b__op_stashes[0]) {
1091 for (c = OPc_NULL; c < OPc_MAX; ++c)
1092 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
1101 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
1102 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
1104 return &PL_sv_undef;
1107 case VMG_OP_INFO_NAME: {
1108 OPCODE t = PL_op->op_type;
1109 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
1111 case VMG_OP_INFO_OBJECT: {
1113 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1114 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1120 return &PL_sv_undef;
1123 /* --- svt callbacks ------------------------------------------------------- */
1125 #define VMG_CB_CALL_ARGS_MASK 15
1126 #define VMG_CB_CALL_ARGS_SHIFT 4
1127 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT) /* 1|2 */
1128 #define VMG_CB_CALL_GUARD 4
1130 STATIC int vmg_dispell_guard_oncroak(pTHX_ void *ud) {
1135 /* If we're at the upmost magic call and we're about to die, we can just free
1136 * the tokens right now, since we will jump past the problematic part of our
1138 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
1139 vmg_magic_chain_free(MY_CXT.freed_tokens, NULL);
1140 MY_CXT.freed_tokens = NULL;
1146 STATIC int vmg_dispell_guard_free(pTHX_ SV *sv, MAGIC *mg) {
1147 vmg_magic_chain_free((MAGIC *) mg->mg_ptr, NULL);
1154 STATIC int vmg_dispell_guard_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
1155 /* The freed magic tokens aren't cloned by perl because it cannot reach them
1156 * (they have been detached from their parent SV when they were enqueued).
1157 * Hence there's nothing to purge in the new thread. */
1163 #endif /* VMG_THREADSAFE */
1165 STATIC MGVTBL vmg_dispell_guard_vtbl = {
1170 vmg_dispell_guard_free, /* free */
1173 vmg_dispell_guard_dup, /* dup */
1179 #endif /* MGf_LOCAL */
1182 STATIC SV *vmg_dispell_guard_new(pTHX_ MAGIC *root) {
1183 #define vmg_dispell_guard_new(R) vmg_dispell_guard_new(aTHX_ (R))
1186 guard = sv_newmortal();
1187 sv_magicext(guard, NULL, PERL_MAGIC_ext, &vmg_dispell_guard_vtbl,
1193 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1196 unsigned int i, args, opinfo;
1197 MAGIC **chain = NULL;
1202 args = flags & VMG_CB_CALL_ARGS_MASK;
1203 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1204 opinfo = flags & VMG_CB_CALL_OPINFO;
1210 EXTEND(SP, args + 1);
1211 PUSHs(sv_2mortal(newRV_inc(sv)));
1213 for (i = 0; i < args; ++i) {
1214 SV *sva = va_arg(ap, SV *);
1215 PUSHs(sva ? sva : &PL_sv_undef);
1219 XPUSHs(vmg_op_info(opinfo));
1222 if (flags & VMG_CB_CALL_GUARD) {
1225 vmg_call_sv(cb, G_SCALAR, vmg_dispell_guard_oncroak, NULL);
1227 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens)
1228 chain = &MY_CXT.freed_tokens;
1230 vmg_call_sv(cb, G_SCALAR, 0, NULL);
1236 ret = (int) SvIV(svr);
1243 vmg_dispell_guard_new(*chain);
1250 #define VMG_CB_FLAGS(OI, A) \
1251 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1253 #define vmg_cb_call1(I, OI, S, A1) \
1254 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1255 #define vmg_cb_call2(I, OI, S, A1, A2) \
1256 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1257 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1258 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1260 /* ... Default no-op magic callback ........................................ */
1262 STATIC int vmg_svt_default_noop(pTHX_ SV *sv, MAGIC *mg) {
1266 /* ... get magic ........................................................... */
1268 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1269 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1271 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1274 #define vmg_svt_get_noop vmg_svt_default_noop
1276 /* ... set magic ........................................................... */
1278 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1279 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1281 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1284 #define vmg_svt_set_noop vmg_svt_default_noop
1286 /* ... len magic ........................................................... */
1288 STATIC U32 vmg_sv_len(pTHX_ SV *sv) {
1289 #define vmg_sv_len(S) vmg_sv_len(aTHX_ (S))
1291 #if VMG_HAS_PERL(5, 9, 3)
1292 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, len)));
1294 U8 *s = SvPV(sv, len);
1297 return DO_UTF8(sv) ? utf8_length(s, s + len) : len;
1300 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1301 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1302 unsigned int opinfo = w->opinfo;
1305 svtype t = SvTYPE(sv);
1314 PUSHs(sv_2mortal(newRV_inc(sv)));
1315 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1317 len = vmg_sv_len(sv);
1319 } else if (t == SVt_PVAV) {
1320 len = av_len((AV *) sv) + 1;
1324 PUSHs(&PL_sv_undef);
1327 XPUSHs(vmg_op_info(opinfo));
1330 vmg_call_sv(w->cb_len, G_SCALAR, 0, NULL);
1334 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1345 STATIC U32 vmg_svt_len_noop(pTHX_ SV *sv, MAGIC *mg) {
1347 svtype t = SvTYPE(sv);
1350 len = vmg_sv_len(sv);
1351 } else if (t == SVt_PVAV) {
1352 len = (U32) av_len((AV *) sv);
1358 /* ... clear magic ......................................................... */
1360 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1361 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1362 unsigned int flags = w->opinfo;
1364 #if !VMG_HAS_PERL(5, 12, 0)
1365 flags |= VMG_CB_CALL_GUARD;
1368 return vmg_cb_call1(w->cb_clear, flags, sv, mg->mg_obj);
1371 #define vmg_svt_clear_noop vmg_svt_default_noop
1373 /* ... free magic .......................................................... */
1375 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1377 STATIC OP *vmg_pp_propagate_errsv(pTHX) {
1378 SVOP *o = cSVOPx(PL_op);
1381 SvREFCNT_dec(ERRSV);
1389 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1391 STATIC int vmg_propagate_errsv_free(pTHX_ SV *sv, MAGIC *mg) {
1395 mg->mg_flags &= ~MGf_REFCOUNTED;
1401 /* perl is already kind enough to handle the cloning of the mg_obj member,
1402 hence we don't need to define a dup magic callback. */
1404 STATIC MGVTBL vmg_propagate_errsv_vtbl = {
1409 vmg_propagate_errsv_free, /* free */
1414 #endif /* MGf_LOCAL */
1421 } vmg_svt_free_cleanup_ud;
1423 STATIC int vmg_svt_free_cleanup(pTHX_ void *ud_) {
1424 vmg_svt_free_cleanup_ud *ud = VOID2(vmg_svt_free_cleanup_ud *, ud_);
1427 U32 optype = PL_op ? PL_op->op_type : OP_NULL;
1429 if (optype == OP_LEAVETRY || optype == OP_LEAVEEVAL) {
1430 SV *errsv = newSVsv(ERRSV);
1433 LEAVE_SCOPE(ud->base);
1435 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1436 if (optype == OP_LEAVETRY) {
1438 PL_op = vmg_trampoline_bump(&MY_CXT.propagate_errsv, errsv, PL_op);
1439 } else if (optype == OP_LEAVEEVAL) {
1440 SV *guard = sv_newmortal();
1441 sv_magicext(guard, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl,
1444 #else /* !VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1445 # if !VMG_HAS_PERL(5, 8, 9)
1447 SV *guard = sv_newmortal();
1448 sv_magicext(guard, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl,
1452 sv_magicext(ERRSV, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl,
1454 SvREFCNT_dec(errsv);
1456 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1461 /* Don't propagate */
1467 /* We are about to croak() while sv is being destroyed. Try to clean up
1471 vmg_mg_del(sv, NULL, mg, mg->mg_moremagic);
1476 vmg_dispell_guard_oncroak(aTHX_ NULL);
1478 /* After that, propagate the error upwards. */
1483 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1484 vmg_svt_free_cleanup_ud ud;
1485 const vmg_wizard *w;
1491 /* During global destruction, we cannot be sure that the wizard and its free
1492 * callback are still alive. */
1496 w = vmg_wizard_from_mg_nocheck(mg);
1498 /* So that it survives the temp cleanup below */
1499 SvREFCNT_inc_simple_void(sv);
1501 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1502 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1503 * if you access the sv from the callback the old deleted magics will trigger
1504 * and cause memory misreads. Change 32686 solved it that way : */
1505 SvMAGIC_set(sv, mg);
1509 if (cxstack_ix < cxstack_max) {
1510 ud.in_eval = (CxTYPE(cxstack + cxstack_ix + 1) == CXt_EVAL);
1511 ud.base = ud.in_eval ? PL_scopestack[PL_scopestack_ix] : 0;
1522 PUSHs(sv_2mortal(newRV_inc(sv)));
1523 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1525 XPUSHs(vmg_op_info(w->opinfo));
1531 vmg_call_sv(w->cb_free, G_SCALAR, vmg_svt_free_cleanup, &ud);
1533 if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
1534 /* Free all the tokens in the chain but the current one (if it's present).
1535 * It will be taken care of by our caller, Perl_mg_free(). */
1536 vmg_magic_chain_free(MY_CXT.freed_tokens, mg);
1537 MY_CXT.freed_tokens = NULL;
1544 ret = (int) SvIV(svr);
1550 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1551 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1554 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1555 * mg->mg_ptr reference count */
1559 #define vmg_svt_free_noop vmg_svt_default_noop
1561 #if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1562 # define VMG_SVT_COPY_KEYLEN_TYPE I32
1564 # define VMG_SVT_COPY_KEYLEN_TYPE int
1567 /* ... copy magic .......................................................... */
1569 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1570 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1574 if (keylen == HEf_SVKEY) {
1577 keysv = newSVpvn(key, keylen);
1580 if (SvTYPE(sv) >= SVt_PVCV)
1581 nsv = sv_2mortal(newRV_inc(nsv));
1583 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1585 if (keylen != HEf_SVKEY) {
1586 SvREFCNT_dec(keysv);
1592 STATIC int vmg_svt_copy_noop(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key, VMG_SVT_COPY_KEYLEN_TYPE keylen) {
1596 /* ... dup magic ........................................................... */
1599 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1602 #define vmg_svt_dup_noop vmg_svt_dup
1605 /* ... local magic ......................................................... */
1609 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1610 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1612 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1615 #define vmg_svt_local_noop vmg_svt_default_noop
1617 #endif /* MGf_LOCAL */
1619 /* ... uvar magic .......................................................... */
1623 STATIC OP *vmg_pp_reset_rmg(pTHX) {
1624 SVOP *o = cSVOPx(PL_op);
1626 SvRMAGICAL_on(o->op_sv);
1632 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1634 MAGIC *mg, *umg, *moremagic;
1635 SV *key = NULL, *newkey = NULL;
1638 umg = mg_find(sv, PERL_MAGIC_uvar);
1639 /* umg can't be NULL or we wouldn't be there. */
1641 ud = (vmg_uvar_ud *) umg->mg_ptr;
1643 if (ud->old_uf.uf_val)
1644 ud->old_uf.uf_val(aTHX_ action, sv);
1645 if (ud->old_uf.uf_set)
1646 ud->old_uf.uf_set(aTHX_ action, sv);
1648 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
1649 const vmg_wizard *w;
1651 /* mg may be freed later by the uvar call, so we need to fetch the next
1652 * token before reaching that fateful point. */
1653 moremagic = mg->mg_moremagic;
1655 switch (mg->mg_type) {
1656 case PERL_MAGIC_ext:
1658 case PERL_MAGIC_tied:
1665 w = vmg_wizard_from_mg(mg);
1674 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1678 & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS|HV_FETCH_LVALUE|HV_DELETE)) {
1681 vmg_cb_call2(w->cb_fetch, w->opinfo | VMG_CB_CALL_GUARD, sv,
1684 case HV_FETCH_ISSTORE:
1685 case HV_FETCH_LVALUE:
1686 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1688 vmg_cb_call2(w->cb_store, w->opinfo | VMG_CB_CALL_GUARD, sv,
1691 case HV_FETCH_ISEXISTS:
1693 vmg_cb_call2(w->cb_exists, w->opinfo | VMG_CB_CALL_GUARD, sv,
1698 vmg_cb_call2(w->cb_delete, w->opinfo | VMG_CB_CALL_GUARD, sv,
1704 if (SvRMAGICAL(sv) && !tied && !(action & (HV_FETCH_ISSTORE|HV_DELETE))) {
1705 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1706 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1707 * the op_ppaddr of a new fake op injected between the current and the next
1710 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
1714 PL_op = vmg_trampoline_bump(&MY_CXT.reset_rmg, sv, PL_op);
1716 #else /* !VMG_RESET_RMG_NEEDS_TRAMPOLINE */
1718 OP *nop = PL_op->op_next;
1721 if (nop && nop->op_ppaddr == vmg_pp_reset_rmg) {
1722 svop = (SVOP *) nop;
1724 NewOp(1101, svop, 1, SVOP);
1725 svop->op_type = OP_STUB;
1726 svop->op_ppaddr = vmg_pp_reset_rmg;
1727 svop->op_next = nop;
1729 svop->op_private = 0;
1731 PL_op->op_next = (OP *) svop;
1736 #endif /* VMG_RESET_RMG_NEEDS_TRAMPOLINE */
1744 #endif /* VMG_UVAR */
1746 /* --- Macros for the XS section ------------------------------------------- */
1749 # define VMG_CVOK(C) \
1750 ((CvISXSUB(C) ? (void *) CvXSUB(C) : (void *) CvROOT(C)) ? 1 : 0)
1752 # define VMG_CVOK(C) (CvROOT(C) || CvXSUB(C))
1755 #define VMG_CBOK(S) ((SvTYPE(S) == SVt_PVCV) ? VMG_CVOK(S) : SvOK(S))
1757 #define VMG_SET_CB(S, N) { \
1759 if (SvOK(cb) && SvROK(cb)) { \
1762 SvREFCNT_inc_simple_void(cb); \
1771 #define VMG_SET_SVT_CB(S, N) { \
1773 if (SvOK(cb) && SvROK(cb)) { \
1775 if (VMG_CBOK(cb)) { \
1776 t->svt_ ## N = vmg_svt_ ## N; \
1777 SvREFCNT_inc_simple_void(cb); \
1779 t->svt_ ## N = vmg_svt_ ## N ## _noop; \
1783 t->svt_ ## N = NULL; \
1789 /* --- XS ------------------------------------------------------------------ */
1791 MODULE = Variable::Magic PACKAGE = Variable::Magic
1801 for (c = OPc_NULL; c < OPc_MAX; ++c)
1802 MY_CXT.b__op_stashes[c] = NULL;
1805 MY_CXT.freed_tokens = NULL;
1807 /* XS doesn't like a blank line here */
1808 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1809 vmg_trampoline_init(&MY_CXT.propagate_errsv, vmg_pp_propagate_errsv);
1811 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
1812 vmg_trampoline_init(&MY_CXT.reset_rmg, vmg_pp_reset_rmg);
1815 /* XS doesn't like a blank line here */
1817 MUTEX_INIT(&vmg_vtable_refcount_mutex);
1818 MUTEX_INIT(&vmg_op_name_init_mutex);
1821 stash = gv_stashpv(__PACKAGE__, 1);
1822 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1823 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1824 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1825 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1826 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1827 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1828 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_NOLEN",
1829 newSVuv(VMG_COMPAT_SCALAR_NOLEN));
1830 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1831 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1832 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1833 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1834 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1835 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1836 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1837 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1838 newCONSTSUB(stash, "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID",
1839 newSVuv(VMG_COMPAT_HASH_DELETE_NOUVAR_VOID));
1840 newCONSTSUB(stash, "VMG_COMPAT_CODE_COPY_CLONE",
1841 newSVuv(VMG_COMPAT_CODE_COPY_CLONE));
1842 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1843 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1844 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1845 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1846 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1847 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1856 U32 had_b__op_stash = 0;
1862 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1863 if (MY_CXT.b__op_stashes[c])
1864 had_b__op_stash |= (((U32) 1) << c);
1866 old_depth = MY_CXT.depth;
1870 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1871 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1872 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1874 MY_CXT.depth = old_depth;
1875 MY_CXT.freed_tokens = NULL;
1879 #endif /* VMG_THREADSAFE */
1886 SV *op_info, *copy_key;
1892 #endif /* MGf_LOCAL */
1895 #endif /* VMG_UVAR */
1896 ) { croak(vmg_wrongargnum); }
1899 w = vmg_wizard_alloc(SvOK(op_info) ? SvUV(op_info) : 0);
1900 t = vmg_vtable_vtbl(w->vtable);
1902 VMG_SET_CB(ST(i++), data);
1904 VMG_SET_SVT_CB(ST(i++), get);
1905 VMG_SET_SVT_CB(ST(i++), set);
1906 VMG_SET_SVT_CB(ST(i++), len);
1907 VMG_SET_SVT_CB(ST(i++), clear);
1908 VMG_SET_SVT_CB(ST(i++), free);
1909 VMG_SET_SVT_CB(ST(i++), copy);
1910 /* VMG_SET_SVT_CB(ST(i++), dup); */
1915 VMG_SET_SVT_CB(ST(i++), local);
1916 #endif /* MGf_LOCAL */
1918 VMG_SET_CB(ST(i++), fetch);
1919 VMG_SET_CB(ST(i++), store);
1920 VMG_SET_CB(ST(i++), exists);
1921 VMG_SET_CB(ST(i++), delete);
1924 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1925 w->uvar = SvTRUE(copy_key) ? 2 : 1;
1926 #endif /* VMG_UVAR */
1928 RETVAL = newRV_noinc(vmg_wizard_sv_new(w));
1932 SV *cast(SV *sv, SV *wiz, ...)
1933 PROTOTYPE: \[$@%&*]$@
1935 const vmg_wizard *w = NULL;
1944 wiz = SvRV_const(wiz);
1945 w = vmg_wizard_from_sv(wiz);
1948 croak(vmg_invalid_wiz);
1949 RETVAL = newSVuv(vmg_cast(SvRV(sv), w, wiz, args, i));
1954 getdata(SV *sv, SV *wiz)
1955 PROTOTYPE: \[$@%&*]$
1957 const vmg_wizard *w = NULL;
1961 w = vmg_wizard_from_sv(SvRV_const(wiz));
1963 croak(vmg_invalid_wiz);
1964 data = vmg_data_get(SvRV(sv), w);
1970 SV *dispell(SV *sv, SV *wiz)
1971 PROTOTYPE: \[$@%&*]$
1973 const vmg_wizard *w = NULL;
1976 w = vmg_wizard_from_sv(SvRV_const(wiz));
1978 croak(vmg_invalid_wiz);
1979 RETVAL = newSVuv(vmg_dispell(SvRV(sv), w));