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
58 #if VMG_MULTIPLICITY && !defined(tTHX)
59 # define tTHX PerlInterpreter*
62 #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))
63 # define VMG_THREADSAFE 1
65 # define MY_CXT_CLONE \
67 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
68 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
69 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
72 # define VMG_THREADSAFE 0
74 # define dMY_CXT dNOOP
76 # define MY_CXT vmg_globaldata
78 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
80 # define MY_CXT_INIT NOOP
82 # define MY_CXT_CLONE NOOP
87 STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
88 #define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O))
91 #if VMG_HAS_PERL(5, 13, 2)
92 CLONE_PARAMS *param = Perl_clone_params_new(owner, aTHX);
94 dupsv = sv_dup(sv, param);
96 Perl_clone_params_del(param);
100 param.stashes = NULL; /* don't need it unless sv is a PVHV */
102 param.proto_perl = owner;
104 dupsv = sv_dup(sv, ¶m);
107 return SvREFCNT_inc(dupsv);
110 #endif /* VMG_THREADSAFE */
112 /* --- Compatibility ------------------------------------------------------- */
115 # define Newx(v, n, c) New(0, v, n, c)
119 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
123 # define SvRV_const(sv) SvRV((SV *) sv)
126 #ifndef SvREFCNT_inc_simple_void
127 # define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
131 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
134 #ifndef PERL_MAGIC_ext
135 # define PERL_MAGIC_ext '~'
138 #ifndef PERL_MAGIC_tied
139 # define PERL_MAGIC_tied 'P'
154 #ifndef IN_PERL_COMPILETIME
155 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
158 /* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
159 * enable them on 5.10 */
160 #if VMG_HAS_PERL(5, 10, 0)
166 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160, partially
167 * reverted to dev-5.11 as 9cdcb38b */
168 #if VMG_HAS_PERL_MAINT(5, 8, 9, 28160) || VMG_HAS_PERL_MAINT(5, 9, 3, 25854) || VMG_HAS_PERL(5, 10, 0)
169 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
170 # if VMG_HAS_PERL(5, 11, 0)
171 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
173 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
176 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
177 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 1
180 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
181 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
183 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
184 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 0
188 /* Applied to dev-5.11 as 34908 */
189 #if VMG_HAS_PERL_MAINT(5, 11, 0, 34908) || VMG_HAS_PERL(5, 12, 0)
190 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
192 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
195 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
196 #if VMG_HAS_PERL_MAINT(5, 8, 9, 32542) || VMG_HAS_PERL_MAINT(5, 9, 5, 31473) || VMG_HAS_PERL(5, 10, 0)
197 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
199 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
202 #if VMG_HAS_PERL_MAINT(5, 11, 0, 32969) || VMG_HAS_PERL(5, 12, 0)
203 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
205 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
208 #if VMG_HAS_PERL(5, 13, 2)
209 # define VMG_COMPAT_GLOB_GET 1
211 # define VMG_COMPAT_GLOB_GET 0
214 /* ... Bug-free mg_magical ................................................. */
216 /* See the discussion at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html. This version is specialized to our needs. */
220 STATIC void vmg_sv_magicuvar(pTHX_ SV *sv, const char *uf, I32 len) {
221 #define vmg_sv_magicuvar(S, U, L) vmg_sv_magicuvar(aTHX_ (S), (U), (L))
223 sv_magic(sv, NULL, PERL_MAGIC_uvar, uf, len);
224 /* uvar magic has set and get magic, hence this has set SVs_GMG and SVs_SMG. */
225 if ((mg = SvMAGIC(sv))) {
228 const MGVTBL* const vtbl = mg->mg_virtual;
230 if (vtbl->svt_clear) {
235 } while ((mg = mg->mg_moremagic));
239 #endif /* VMG_UVAR */
241 /* ... Safe version of call_sv() ........................................... */
243 #define VMG_SAVE_LAST_CX (!VMG_HAS_PERL(5, 8, 4) || VMG_HAS_PERL(5, 9, 5))
245 STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, I32 destructor) {
246 #define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
247 I32 ret, cxix = 0, in_eval = 0;
249 PERL_CONTEXT saved_cx;
258 if (cxstack_ix < cxstack_max) {
259 cxix = cxstack_ix + 1;
260 if (destructor && CxTYPE(cxstack + cxix) == CXt_EVAL)
265 /* The last popped context will be reused by call_sv(), but our callers may
266 * still need its previous value. Back it up so that it isn't clobbered. */
267 saved_cx = cxstack[cxix];
270 ret = call_sv(sv, flags | G_EVAL);
273 cxstack[cxix] = saved_cx;
278 sv_setsv(old_err, ERRSV);
282 if (IN_PERL_COMPILETIME) {
285 sv_catsv(PL_errors, ERRSV);
287 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
290 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
292 ++PL_parser->error_count;
293 #elif defined(PL_error_count)
310 /* --- Stolen chunk of B --------------------------------------------------- */
328 STATIC const char *const vmg_opclassnames[] = {
343 STATIC opclass vmg_opclass(const OP *o) {
350 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
352 if (o->op_type == OP_SASSIGN)
353 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
355 if (o->op_type == OP_AELEMFAST) {
356 if (o->op_flags & OPf_SPECIAL)
367 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
371 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
388 case OA_PVOP_OR_SVOP:
389 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
394 case OA_BASEOP_OR_UNOP:
395 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
397 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
399 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
401 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
404 if (o->op_flags & OPf_STACKED)
406 else if (o->op_flags & OPf_SPECIAL)
415 /* --- Error messages ------------------------------------------------------ */
417 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
418 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
419 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
421 /* --- Signatures ---------------------------------------------------------- */
423 #define SIG_WZO ((U16) (0x3891))
424 #define SIG_WIZ ((U16) (0x3892))
426 /* --- MGWIZ structure ----------------------------------------------------- */
435 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
438 #endif /* MGf_COPY */
444 #endif /* MGf_LOCAL */
446 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
447 #endif /* VMG_UVAR */
451 #endif /* VMG_MULTIPLICITY */
454 STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *w) {
455 #define vmg_mgwiz_free(W) vmg_mgwiz_free(aTHX_ (W))
459 /* We reach this point in dirty state when ptable_free() is called from the
460 * atexit cleanup callback, and that the global table still holds a live
461 * wizard. This happens before all the SV bodies are freed, so all the wizard
462 * callbacks are still alive (as they are referenced by the undead wizard).
463 * Hence it is safe to decrement their refcount. Later on, the wizard free
464 * callback itself will trigger when the wizard body is reaped, but it will
465 * be skipped as it guards against dirty state - which is good since nothing
466 * has to be done anymore at that point. */
468 SvREFCNT_dec(w->cb_data);
469 SvREFCNT_dec(w->cb_get);
470 SvREFCNT_dec(w->cb_set);
471 SvREFCNT_dec(w->cb_len);
472 SvREFCNT_dec(w->cb_clear);
473 SvREFCNT_dec(w->cb_free);
475 SvREFCNT_dec(w->cb_copy);
476 #endif /* MGf_COPY */
478 SvREFCNT_dec(w->cb_dup);
481 SvREFCNT_dec(w->cb_local);
482 #endif /* MGf_LOCAL */
484 SvREFCNT_dec(w->cb_fetch);
485 SvREFCNT_dec(w->cb_store);
486 SvREFCNT_dec(w->cb_exists);
487 SvREFCNT_dec(w->cb_delete);
488 #endif /* VMG_UVAR */
498 #define VMG_CLONE_CB(N) \
499 z->cb_ ## N = (w->cb_ ## N) ? vmg_clone(w->cb_ ## N, w->owner) \
502 STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) {
503 #define vmg_mgwiz_clone(W) vmg_mgwiz_clone(aTHX_ (W))
511 Copy(w->vtbl, t, 1, MGVTBL);
517 z->opinfo = w->opinfo;
527 #endif /* MGf_COPY */
533 #endif /* MGf_LOCAL */
537 VMG_CLONE_CB(exists);
538 VMG_CLONE_CB(delete);
539 #endif /* VMG_UVAR */
546 #endif /* VMG_THREADSAFE */
548 /* --- Context-safe global data -------------------------------------------- */
552 #define PTABLE_NAME ptable
553 #define PTABLE_VAL_FREE(V) vmg_mgwiz_free(VOID2(MGWIZ *, (V)))
562 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
563 #define ptable_clear(T) ptable_clear(aTHX_ (T))
564 #define ptable_free(T) ptable_free(aTHX_ (T))
566 #endif /* VMG_THREADSAFE */
568 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
575 HV *b__op_stashes[OPc_MAX];
582 STATIC void vmg_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
583 my_cxt_t *ud = VOID2(my_cxt_t *, ud_);
586 if (ud->owner == aTHX)
589 w = vmg_mgwiz_clone(VOID2(MGWIZ *, ent->val));
591 ptable_store(ud->wizards, ent->key, w);
594 #endif /* VMG_THREADSAFE */
596 /* --- Wizard objects ------------------------------------------------------ */
598 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg);
600 STATIC MGVTBL vmg_wizard_vtbl = {
605 vmg_wizard_free, /* free */
608 #endif /* MGf_COPY */
614 #endif /* MGf_LOCAL */
617 /* ... Wizard constructor .................................................. */
619 STATIC SV *vmg_wizard_new(pTHX_ const MGWIZ *w) {
620 #define vmg_wizard_new(W) vmg_wizard_new(aTHX_ (W))
621 SV *wiz = newSVuv(PTR2IV(w));
624 MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
625 mg->mg_private = SIG_WZO;
632 STATIC const SV *vmg_wizard_validate(pTHX_ const SV *wiz) {
633 #define vmg_wizard_validate(W) vmg_wizard_validate(aTHX_ (W))
635 wiz = SvRV_const(wiz);
640 croak(vmg_invalid_wiz);
645 #define vmg_wizard_id(W) SvIVX((const SV *) (W))
646 #define vmg_wizard_main_mgwiz(W) INT2PTR(const MGWIZ *, vmg_wizard_id(W))
648 /* ... Wizard destructor ................................................... */
650 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg) {
653 if (PL_dirty) /* During global destruction, the context is already freed */
656 w = (MGWIZ *) vmg_wizard_main_mgwiz(sv);
661 ptable_store(MY_CXT.wizards, w, NULL);
663 #else /* VMG_THREADSAFE */
665 #endif /* !VMG_THREADSAFE */
672 STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ const SV *wiz) {
673 #define vmg_wizard_mgwiz(W) vmg_wizard_mgwiz(aTHX_ ((const SV *) (W)))
676 w = vmg_wizard_main_mgwiz(wiz);
677 if (w->owner == aTHX)
682 return VOID2(const MGWIZ *, ptable_fetch(MY_CXT.wizards, w));
686 #else /* VMG_THREADSAFE */
688 #define vmg_wizard_mgwiz(W) vmg_wizard_main_mgwiz(W)
690 #endif /* !VMG_THREADSAFE */
692 /* --- User-level functions implementation --------------------------------- */
694 STATIC const MAGIC *vmg_find(const SV *sv, const SV *wiz) {
695 const MAGIC *mg, *moremagic;
698 if (SvTYPE(sv) < SVt_PVMG)
701 wid = vmg_wizard_id(wiz);
702 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
703 moremagic = mg->mg_moremagic;
704 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
705 IV zid = vmg_wizard_id(mg->mg_ptr);
714 /* ... Construct private data .............................................. */
716 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
717 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
727 EXTEND(SP, items + 1);
728 PUSHs(sv_2mortal(newRV_inc(sv)));
729 for (i = 0; i < items; ++i)
733 vmg_call_sv(ctor, G_SCALAR, 0);
737 #if VMG_HAS_PERL(5, 8, 3)
738 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
740 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
750 STATIC SV *vmg_data_get(pTHX_ SV *sv, const SV *wiz) {
751 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
752 const MAGIC *mg = vmg_find(sv, wiz);
753 return mg ? mg->mg_obj : NULL;
756 /* ... Magic cast/dispell .................................................. */
759 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
761 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
763 prevmagic->mg_moremagic = moremagic;
765 SvMAGIC_set(sv, moremagic);
767 mg->mg_moremagic = NULL;
768 Safefree(mg->mg_ptr);
771 #endif /* VMG_UVAR */
773 STATIC UV vmg_cast(pTHX_ SV *sv, const SV *wiz, SV **args, I32 items) {
774 #define vmg_cast(S, W, A, I) vmg_cast(aTHX_ (S), (W), (A), (I))
775 MAGIC *mg, *moremagic = NULL;
780 if (vmg_find(sv, wiz))
783 w = vmg_wizard_mgwiz(wiz);
784 oldgmg = SvGMAGICAL(sv);
786 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
787 mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY);
789 mg->mg_private = SIG_WIZ;
792 mg->mg_flags |= MGf_COPY;
793 #endif /* MGf_COPY */
796 mg->mg_flags |= MGf_DUP;
800 mg->mg_flags |= MGf_LOCAL;
801 #endif /* MGf_LOCAL */
803 if (SvTYPE(sv) < SVt_PVHV)
806 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
807 * magic is actually never called for them. If the GMAGICAL flag was off before
808 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
809 * now on, then this wizard has get magic. Hence we can work around the
810 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
811 * has uvar callbacks, it will be turned back on later. */
812 if (!oldgmg && SvGMAGICAL(sv))
820 uf[0].uf_val = vmg_svt_val;
827 /* One uvar magic in the chain is enough. */
828 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
829 moremagic = mg->mg_moremagic;
830 if (mg->mg_type == PERL_MAGIC_uvar)
834 if (mg) { /* Found another uvar magic. */
835 struct ufuncs *olduf = (struct ufuncs *) mg->mg_ptr;
836 if (olduf->uf_val == vmg_svt_val) {
837 /* It's our uvar magic, nothing to do. oldgmg was true. */
840 /* It's another uvar magic, backup it and replace it by ours. */
842 vmg_uvar_del(sv, prevmagic, mg, moremagic);
846 vmg_sv_magicuvar(sv, (const char *) &uf, sizeof(uf));
847 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
848 * handled by our uvar callback. */
850 #endif /* VMG_UVAR */
856 STATIC UV vmg_dispell(pTHX_ SV *sv, const SV *wiz) {
857 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
860 #endif /* VMG_UVAR */
861 MAGIC *mg, *prevmagic, *moremagic = NULL;
862 IV wid = vmg_wizard_id(wiz);
864 if (SvTYPE(sv) < SVt_PVMG)
867 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
868 moremagic = mg->mg_moremagic;
869 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
870 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
871 IV zid = vmg_wizard_id(mg->mg_ptr);
874 /* If the current has no uvar, short-circuit uvar deletion. */
875 uvars = z->uvar ? (uvars + 1) : 0;
876 #endif /* VMG_UVAR */
879 } else if (z->uvar) {
881 /* We can't break here since we need to find the ext magic to delete. */
882 #endif /* VMG_UVAR */
890 prevmagic->mg_moremagic = moremagic;
892 SvMAGIC_set(sv, moremagic);
894 mg->mg_moremagic = NULL;
896 /* Destroy private data */
897 if (mg->mg_obj != sv)
898 SvREFCNT_dec(mg->mg_obj);
899 /* Unreference the wizard */
900 SvREFCNT_dec((SV *) mg->mg_ptr);
904 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
905 /* mg was the first ext magic in the chain that had uvar */
907 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
908 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
909 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
919 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
920 moremagic = mg->mg_moremagic;
921 if (mg->mg_type == PERL_MAGIC_uvar)
925 uf = (struct ufuncs *) mg->mg_ptr;
926 /* assert(uf->uf_val == vmg_svt_val); */
927 if (uf[1].uf_val || uf[1].uf_set) {
928 /* Revert the original uvar magic. */
930 Renew(uf, 1, struct ufuncs);
931 mg->mg_ptr = (char *) uf;
932 mg->mg_len = sizeof(struct ufuncs);
934 /* Remove the uvar magic. */
935 vmg_uvar_del(sv, prevmagic, mg, moremagic);
939 #endif /* VMG_UVAR */
944 /* ... OP info ............................................................. */
946 #define VMG_OP_INFO_NAME 1
947 #define VMG_OP_INFO_OBJECT 2
950 STATIC perl_mutex vmg_op_name_init_mutex;
953 STATIC U32 vmg_op_name_init = 0;
954 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
956 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
957 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
959 case VMG_OP_INFO_NAME:
961 MUTEX_LOCK(&vmg_op_name_init_mutex);
963 if (!vmg_op_name_init) {
965 for (t = 0; t < OP_max; ++t)
966 vmg_op_name_len[t] = strlen(PL_op_name[t]);
967 vmg_op_name_init = 1;
970 MUTEX_UNLOCK(&vmg_op_name_init_mutex);
973 case VMG_OP_INFO_OBJECT: {
975 if (!MY_CXT.b__op_stashes[0]) {
978 for (c = OPc_NULL; c < OPc_MAX; ++c)
979 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
988 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
989 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
994 case VMG_OP_INFO_NAME: {
995 OPCODE t = PL_op->op_type;
996 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
998 case VMG_OP_INFO_OBJECT: {
1000 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1001 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1007 return &PL_sv_undef;
1010 /* ... svt callbacks ....................................................... */
1012 #define VMG_CB_CALL_ARGS_MASK 15
1013 #define VMG_CB_CALL_ARGS_SHIFT 4
1014 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT)
1016 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1019 unsigned int i, args, opinfo;
1024 args = flags & VMG_CB_CALL_ARGS_MASK;
1025 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1026 opinfo = flags & VMG_CB_CALL_OPINFO;
1032 EXTEND(SP, args + 1);
1033 PUSHs(sv_2mortal(newRV_inc(sv)));
1035 for (i = 0; i < args; ++i) {
1036 SV *sva = va_arg(ap, SV *);
1037 PUSHs(sva ? sva : &PL_sv_undef);
1041 XPUSHs(vmg_op_info(opinfo));
1044 vmg_call_sv(cb, G_SCALAR, 0);
1049 ret = (int) SvIV(svr);
1058 #define VMG_CB_FLAGS(OI, A) \
1059 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1061 #define vmg_cb_call1(I, OI, S, A1) \
1062 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1063 #define vmg_cb_call2(I, OI, S, A1, A2) \
1064 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1065 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1066 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1068 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1069 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1070 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1073 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1074 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1075 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1078 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1079 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1080 unsigned int opinfo = w->opinfo;
1083 svtype t = SvTYPE(sv);
1092 PUSHs(sv_2mortal(newRV_inc(sv)));
1093 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1096 #if VMG_HAS_PERL(5, 9, 3)
1097 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, l)));
1099 U8 *s = SvPV(sv, l);
1102 len = utf8_length(s, s + l);
1106 } else if (t == SVt_PVAV) {
1107 len = av_len((AV *) sv) + 1;
1111 PUSHs(&PL_sv_undef);
1114 XPUSHs(vmg_op_info(opinfo));
1117 vmg_call_sv(w->cb_len, G_SCALAR, 0);
1121 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1132 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1133 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1134 return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj);
1137 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1144 /* Don't even bother if we are in global destruction - the wizard is prisoner
1145 * of circular references and we are way beyond user realm */
1149 w = vmg_wizard_mgwiz(mg->mg_ptr);
1151 /* So that it survives the temp cleanup below */
1152 SvREFCNT_inc_simple_void(sv);
1154 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1155 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1156 * if you access the sv from the callback the old deleted magics will trigger
1157 * and cause memory misreads. Change 32686 solved it that way : */
1158 SvMAGIC_set(sv, mg);
1166 PUSHs(sv_2mortal(newRV_inc(sv)));
1167 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1169 XPUSHs(vmg_op_info(w->opinfo));
1172 vmg_call_sv(w->cb_free, G_SCALAR, 1);
1177 ret = (int) SvIV(svr);
1183 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1184 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1187 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1188 * mg->mg_ptr reference count */
1193 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
1194 # if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1201 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1204 if (keylen == HEf_SVKEY) {
1207 keysv = newSVpvn(key, keylen);
1210 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1212 if (keylen != HEf_SVKEY) {
1213 SvREFCNT_dec(keysv);
1218 #endif /* MGf_COPY */
1221 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1224 #endif /* MGf_DUP */
1227 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1228 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1229 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1231 #endif /* MGf_LOCAL */
1234 STATIC OP *vmg_pp_resetuvar(pTHX) {
1235 SvRMAGICAL_on(cSVOP_sv);
1239 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1242 SV *key = NULL, *newkey = NULL;
1245 umg = mg_find(sv, PERL_MAGIC_uvar);
1246 /* umg can't be NULL or we wouldn't be there. */
1248 uf = (struct ufuncs *) umg->mg_ptr;
1251 uf[1].uf_val(aTHX_ action, sv);
1253 uf[1].uf_set(aTHX_ action, sv);
1255 action &= HV_FETCH_ISSTORE | HV_FETCH_ISEXISTS | HV_FETCH_LVALUE | HV_DELETE;
1256 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
1258 switch (mg->mg_type) {
1259 case PERL_MAGIC_ext:
1261 case PERL_MAGIC_tied:
1267 if (mg->mg_private != SIG_WIZ) continue;
1268 w = vmg_wizard_mgwiz(mg->mg_ptr);
1274 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1279 vmg_cb_call2(w->cb_fetch, w->opinfo, sv, mg->mg_obj, key);
1281 case HV_FETCH_ISSTORE:
1282 case HV_FETCH_LVALUE:
1283 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1285 vmg_cb_call2(w->cb_store, w->opinfo, sv, mg->mg_obj, key);
1287 case HV_FETCH_ISEXISTS:
1289 vmg_cb_call2(w->cb_exists, w->opinfo, sv, mg->mg_obj, key);
1293 vmg_cb_call2(w->cb_delete, w->opinfo, sv, mg->mg_obj, key);
1298 if (SvRMAGICAL(sv) && !tied) {
1299 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1300 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1301 * the op_ppaddr of a new fake op injected between the current and the next
1304 if (!o->op_next || o->op_next->op_ppaddr != vmg_pp_resetuvar) {
1306 NewOp(1101, svop, 1, SVOP);
1307 svop->op_type = OP_STUB;
1308 svop->op_ppaddr = vmg_pp_resetuvar;
1309 svop->op_next = o->op_next;
1312 o->op_next = (OP *) svop;
1319 #endif /* VMG_UVAR */
1321 /* --- Macros for the XS section ------------------------------------------- */
1323 #define VMG_SET_CB(S, N) \
1325 w->cb_ ## N = (SvOK(cb) && SvROK(cb)) ? SvREFCNT_inc(SvRV(cb)) : NULL;
1327 #define VMG_SET_SVT_CB(S, N) \
1329 if (SvOK(cb) && SvROK(cb)) { \
1330 t->svt_ ## N = vmg_svt_ ## N; \
1331 w->cb_ ## N = SvREFCNT_inc(SvRV(cb)); \
1333 t->svt_ ## N = NULL; \
1334 w->cb_ ## N = NULL; \
1339 STATIC void vmg_cleanup(pTHX_ void *ud) {
1342 ptable_free(MY_CXT.wizards);
1343 MY_CXT.wizards = NULL;
1346 #endif /* VMG_THREADSAFE */
1348 /* --- XS ------------------------------------------------------------------ */
1350 MODULE = Variable::Magic PACKAGE = Variable::Magic
1360 MY_CXT.wizards = ptable_new();
1361 MY_CXT.owner = aTHX;
1363 MY_CXT.b__op_stashes[0] = NULL;
1365 MUTEX_INIT(&vmg_op_name_init_mutex);
1366 call_atexit(vmg_cleanup, NULL);
1369 stash = gv_stashpv(__PACKAGE__, 1);
1370 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1371 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1372 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1373 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1374 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1375 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1376 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1377 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1378 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1379 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1380 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1381 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1382 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1383 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1384 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1385 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1386 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1387 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1388 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1389 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1399 U32 had_b__op_stash = 0;
1406 ud.wizards = t = ptable_new();
1407 ud.owner = MY_CXT.owner;
1408 ptable_walk(MY_CXT.wizards, vmg_ptable_clone, &ud);
1410 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1411 if (MY_CXT.b__op_stashes[c])
1412 had_b__op_stash |= (((U32) 1) << c);
1418 MY_CXT.owner = aTHX;
1419 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1420 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1421 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1426 #endif /* VMG_THREADSAFE */
1442 #endif /* MGf_COPY */
1445 #endif /* MGf_DUP */
1448 #endif /* MGf_LOCAL */
1451 #endif /* VMG_UVAR */
1452 ) { croak(vmg_wrongargnum); }
1457 VMG_SET_CB(ST(i++), data);
1460 opinfo = SvOK(cb) ? SvUV(cb) : 0;
1461 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
1463 vmg_op_info_init(w->opinfo);
1465 VMG_SET_SVT_CB(ST(i++), get);
1466 VMG_SET_SVT_CB(ST(i++), set);
1467 VMG_SET_SVT_CB(ST(i++), len);
1468 VMG_SET_SVT_CB(ST(i++), clear);
1469 VMG_SET_SVT_CB(ST(i++), free);
1471 VMG_SET_SVT_CB(ST(i++), copy);
1472 #endif /* MGf_COPY */
1474 /* VMG_SET_SVT_CB(ST(i++), dup); */
1478 #endif /* MGf_DUP */
1480 VMG_SET_SVT_CB(ST(i++), local);
1481 #endif /* MGf_LOCAL */
1483 VMG_SET_CB(ST(i++), fetch);
1484 VMG_SET_CB(ST(i++), store);
1485 VMG_SET_CB(ST(i++), exists);
1486 VMG_SET_CB(ST(i++), delete);
1488 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1489 w->uvar = SvTRUE(cb) ? 2 : 1;
1492 #endif /* VMG_UVAR */
1493 #if VMG_MULTIPLICITY
1495 #endif /* VMG_MULTIPLICITY */
1498 ptable_store(MY_CXT.wizards, w, w);
1499 #endif /* VMG_THREADSAFE */
1501 RETVAL = newRV_noinc(vmg_wizard_new(w));
1505 SV *cast(SV *sv, SV *wiz, ...)
1506 PROTOTYPE: \[$@%&*]$@
1515 RETVAL = newSVuv(vmg_cast(SvRV(sv), vmg_wizard_validate(wiz), args, i));
1520 getdata(SV *sv, SV *wiz)
1521 PROTOTYPE: \[$@%&*]$
1525 data = vmg_data_get(SvRV(sv), vmg_wizard_validate(wiz));
1531 SV *dispell(SV *sv, SV *wiz)
1532 PROTOTYPE: \[$@%&*]$
1534 RETVAL = newSVuv(vmg_dispell(SvRV(sv), vmg_wizard_validate(wiz)));