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 */
218 #if VMG_HAS_PERL(5, 11, 3)
220 #define vmg_mg_magical(S) mg_magical(S)
224 STATIC void vmg_mg_magical(SV *sv) {
228 if ((mg = SvMAGIC(sv))) {
230 const MGVTBL* const vtbl = mg->mg_virtual;
232 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
239 } while ((mg = mg->mg_moremagic));
240 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
247 /* ... Safe version of call_sv() ........................................... */
249 #define VMG_SAVE_LAST_CX (!VMG_HAS_PERL(5, 8, 4) || VMG_HAS_PERL(5, 9, 5))
251 STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, I32 destructor) {
252 #define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
253 I32 ret, cxix = 0, in_eval = 0;
255 PERL_CONTEXT saved_cx;
264 if (cxstack_ix < cxstack_max) {
265 cxix = cxstack_ix + 1;
266 if (destructor && CxTYPE(cxstack + cxix) == CXt_EVAL)
271 /* The last popped context will be reused by call_sv(), but our callers may
272 * still need its previous value. Back it up so that it isn't clobbered. */
273 saved_cx = cxstack[cxix];
276 ret = call_sv(sv, flags | G_EVAL);
279 cxstack[cxix] = saved_cx;
284 sv_setsv(old_err, ERRSV);
288 if (IN_PERL_COMPILETIME) {
291 sv_catsv(PL_errors, ERRSV);
293 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
296 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
298 ++PL_parser->error_count;
299 #elif defined(PL_error_count)
316 /* --- Stolen chunk of B --------------------------------------------------- */
334 STATIC const char *const vmg_opclassnames[] = {
349 STATIC opclass vmg_opclass(const OP *o) {
356 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
358 if (o->op_type == OP_SASSIGN)
359 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
361 if (o->op_type == OP_AELEMFAST) {
362 #if PERL_VERSION <= 14
363 if (o->op_flags & OPf_SPECIAL)
375 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
379 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
396 case OA_PVOP_OR_SVOP:
397 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
402 case OA_BASEOP_OR_UNOP:
403 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
405 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
407 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
409 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
412 if (o->op_flags & OPf_STACKED)
414 else if (o->op_flags & OPf_SPECIAL)
423 /* --- Error messages ------------------------------------------------------ */
425 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
426 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
427 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
429 /* --- Signatures ---------------------------------------------------------- */
431 #define SIG_WZO ((U16) (0x3891))
432 #define SIG_WIZ ((U16) (0x3892))
434 /* --- MGWIZ structure ----------------------------------------------------- */
443 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
446 #endif /* MGf_COPY */
452 #endif /* MGf_LOCAL */
454 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
455 #endif /* VMG_UVAR */
459 #endif /* VMG_MULTIPLICITY */
462 STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *w) {
463 #define vmg_mgwiz_free(W) vmg_mgwiz_free(aTHX_ (W))
467 /* We reach this point in dirty state when ptable_free() is called from the
468 * atexit cleanup callback, and that the global table still holds a live
469 * wizard. This happens before all the SV bodies are freed, so all the wizard
470 * callbacks are still alive (as they are referenced by the undead wizard).
471 * Hence it is safe to decrement their refcount. Later on, the wizard free
472 * callback itself will trigger when the wizard body is reaped, but it will
473 * be skipped as it guards against dirty state - which is good since nothing
474 * has to be done anymore at that point. */
476 SvREFCNT_dec(w->cb_data);
477 SvREFCNT_dec(w->cb_get);
478 SvREFCNT_dec(w->cb_set);
479 SvREFCNT_dec(w->cb_len);
480 SvREFCNT_dec(w->cb_clear);
481 SvREFCNT_dec(w->cb_free);
483 SvREFCNT_dec(w->cb_copy);
484 #endif /* MGf_COPY */
486 SvREFCNT_dec(w->cb_dup);
489 SvREFCNT_dec(w->cb_local);
490 #endif /* MGf_LOCAL */
492 SvREFCNT_dec(w->cb_fetch);
493 SvREFCNT_dec(w->cb_store);
494 SvREFCNT_dec(w->cb_exists);
495 SvREFCNT_dec(w->cb_delete);
496 #endif /* VMG_UVAR */
506 #define VMG_CLONE_CB(N) \
507 z->cb_ ## N = (w->cb_ ## N) ? vmg_clone(w->cb_ ## N, w->owner) \
510 STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) {
511 #define vmg_mgwiz_clone(W) vmg_mgwiz_clone(aTHX_ (W))
519 Copy(w->vtbl, t, 1, MGVTBL);
525 z->opinfo = w->opinfo;
535 #endif /* MGf_COPY */
541 #endif /* MGf_LOCAL */
545 VMG_CLONE_CB(exists);
546 VMG_CLONE_CB(delete);
547 #endif /* VMG_UVAR */
554 #endif /* VMG_THREADSAFE */
556 /* --- Context-safe global data -------------------------------------------- */
560 #define PTABLE_NAME ptable
561 #define PTABLE_VAL_FREE(V) vmg_mgwiz_free(VOID2(MGWIZ *, (V)))
570 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
571 #define ptable_clear(T) ptable_clear(aTHX_ (T))
572 #define ptable_free(T) ptable_free(aTHX_ (T))
574 #endif /* VMG_THREADSAFE */
576 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
583 HV *b__op_stashes[OPc_MAX];
590 STATIC void vmg_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
591 my_cxt_t *ud = VOID2(my_cxt_t *, ud_);
594 if (ud->owner == aTHX)
597 w = vmg_mgwiz_clone(VOID2(MGWIZ *, ent->val));
599 ptable_store(ud->wizards, ent->key, w);
602 #endif /* VMG_THREADSAFE */
604 /* --- Wizard objects ------------------------------------------------------ */
606 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg);
608 STATIC MGVTBL vmg_wizard_vtbl = {
613 vmg_wizard_free, /* free */
616 #endif /* MGf_COPY */
622 #endif /* MGf_LOCAL */
625 /* ... Wizard constructor .................................................. */
627 STATIC SV *vmg_wizard_new(pTHX_ const MGWIZ *w) {
628 #define vmg_wizard_new(W) vmg_wizard_new(aTHX_ (W))
629 SV *wiz = newSVuv(PTR2IV(w));
632 MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
633 mg->mg_private = SIG_WZO;
640 STATIC const SV *vmg_wizard_validate(pTHX_ const SV *wiz) {
641 #define vmg_wizard_validate(W) vmg_wizard_validate(aTHX_ (W))
643 wiz = SvRV_const(wiz);
648 croak(vmg_invalid_wiz);
653 #define vmg_wizard_id(W) SvIVX((const SV *) (W))
654 #define vmg_wizard_main_mgwiz(W) INT2PTR(const MGWIZ *, vmg_wizard_id(W))
656 /* ... Wizard destructor ................................................... */
658 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg) {
661 if (PL_dirty) /* During global destruction, the context is already freed */
664 w = (MGWIZ *) vmg_wizard_main_mgwiz(sv);
669 ptable_store(MY_CXT.wizards, w, NULL);
671 #else /* VMG_THREADSAFE */
673 #endif /* !VMG_THREADSAFE */
680 STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ const SV *wiz) {
681 #define vmg_wizard_mgwiz(W) vmg_wizard_mgwiz(aTHX_ ((const SV *) (W)))
684 w = vmg_wizard_main_mgwiz(wiz);
685 if (w->owner == aTHX)
690 return VOID2(const MGWIZ *, ptable_fetch(MY_CXT.wizards, w));
694 #else /* VMG_THREADSAFE */
696 #define vmg_wizard_mgwiz(W) vmg_wizard_main_mgwiz(W)
698 #endif /* !VMG_THREADSAFE */
700 /* --- User-level functions implementation --------------------------------- */
702 STATIC const MAGIC *vmg_find(const SV *sv, const SV *wiz) {
703 const MAGIC *mg, *moremagic;
706 if (SvTYPE(sv) < SVt_PVMG)
709 wid = vmg_wizard_id(wiz);
710 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
711 moremagic = mg->mg_moremagic;
712 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
713 IV zid = vmg_wizard_id(mg->mg_ptr);
722 /* ... Construct private data .............................................. */
724 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
725 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
735 EXTEND(SP, items + 1);
736 PUSHs(sv_2mortal(newRV_inc(sv)));
737 for (i = 0; i < items; ++i)
741 vmg_call_sv(ctor, G_SCALAR, 0);
745 #if VMG_HAS_PERL(5, 8, 3)
746 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
748 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
758 STATIC SV *vmg_data_get(pTHX_ SV *sv, const SV *wiz) {
759 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
760 const MAGIC *mg = vmg_find(sv, wiz);
761 return mg ? mg->mg_obj : NULL;
764 /* ... Magic cast/dispell .................................................. */
767 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
769 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
771 prevmagic->mg_moremagic = moremagic;
773 SvMAGIC_set(sv, moremagic);
775 mg->mg_moremagic = NULL;
776 Safefree(mg->mg_ptr);
779 #endif /* VMG_UVAR */
781 STATIC UV vmg_cast(pTHX_ SV *sv, const SV *wiz, SV **args, I32 items) {
782 #define vmg_cast(S, W, A, I) vmg_cast(aTHX_ (S), (W), (A), (I))
788 if (vmg_find(sv, wiz))
791 w = vmg_wizard_mgwiz(wiz);
792 oldgmg = SvGMAGICAL(sv);
794 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
795 /* sv_magicext() calls mg_magical and increments data's refcount */
796 mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl,
797 (const char *) wiz, HEf_SVKEY);
799 mg->mg_private = SIG_WIZ;
802 mg->mg_flags |= MGf_COPY;
803 #endif /* MGf_COPY */
806 mg->mg_flags |= MGf_DUP;
810 mg->mg_flags |= MGf_LOCAL;
811 #endif /* MGf_LOCAL */
813 if (SvTYPE(sv) < SVt_PVHV)
816 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
817 * magic is actually never called for them. If the GMAGICAL flag was off before
818 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
819 * now on, then this wizard has get magic. Hence we can work around the
820 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
821 * has uvar callbacks, it will be turned back on later. */
822 if (!oldgmg && SvGMAGICAL(sv))
827 MAGIC *prevmagic, *moremagic = NULL;
830 uf[0].uf_val = vmg_svt_val;
837 /* One uvar magic in the chain is enough. */
838 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
839 moremagic = mg->mg_moremagic;
840 if (mg->mg_type == PERL_MAGIC_uvar)
844 if (mg) { /* Found another uvar magic. */
845 struct ufuncs *olduf = (struct ufuncs *) mg->mg_ptr;
846 if (olduf->uf_val == vmg_svt_val) {
847 /* It's our uvar magic, nothing to do. oldgmg was true. */
850 /* It's another uvar magic, backup it and replace it by ours. */
852 vmg_uvar_del(sv, prevmagic, mg, moremagic);
856 sv_magic(sv, NULL, PERL_MAGIC_uvar, (const char *) &uf, sizeof(uf));
858 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
859 * handled by our uvar callback. */
861 #endif /* VMG_UVAR */
867 STATIC UV vmg_dispell(pTHX_ SV *sv, const SV *wiz) {
868 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
871 #endif /* VMG_UVAR */
872 MAGIC *mg, *prevmagic, *moremagic = NULL;
873 IV wid = vmg_wizard_id(wiz);
875 if (SvTYPE(sv) < SVt_PVMG)
878 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
879 moremagic = mg->mg_moremagic;
880 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
882 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
883 #endif /* VMG_UVAR */
884 IV zid = vmg_wizard_id(mg->mg_ptr);
887 /* If the current has no uvar, short-circuit uvar deletion. */
888 uvars = z->uvar ? (uvars + 1) : 0;
889 #endif /* VMG_UVAR */
892 } else if (z->uvar) {
894 /* We can't break here since we need to find the ext magic to delete. */
895 #endif /* VMG_UVAR */
903 prevmagic->mg_moremagic = moremagic;
905 SvMAGIC_set(sv, moremagic);
907 mg->mg_moremagic = NULL;
909 /* Destroy private data */
910 if (mg->mg_obj != sv)
911 SvREFCNT_dec(mg->mg_obj);
912 /* Unreference the wizard */
913 SvREFCNT_dec((SV *) mg->mg_ptr);
917 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
918 /* mg was the first ext magic in the chain that had uvar */
920 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
921 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
922 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
932 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
933 moremagic = mg->mg_moremagic;
934 if (mg->mg_type == PERL_MAGIC_uvar)
938 uf = (struct ufuncs *) mg->mg_ptr;
939 /* assert(uf->uf_val == vmg_svt_val); */
940 if (uf[1].uf_val || uf[1].uf_set) {
941 /* Revert the original uvar magic. */
943 Renew(uf, 1, struct ufuncs);
944 mg->mg_ptr = (char *) uf;
945 mg->mg_len = sizeof(struct ufuncs);
947 /* Remove the uvar magic. */
948 vmg_uvar_del(sv, prevmagic, mg, moremagic);
952 #endif /* VMG_UVAR */
959 /* ... OP info ............................................................. */
961 #define VMG_OP_INFO_NAME 1
962 #define VMG_OP_INFO_OBJECT 2
965 STATIC perl_mutex vmg_op_name_init_mutex;
968 STATIC U32 vmg_op_name_init = 0;
969 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
971 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
972 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
974 case VMG_OP_INFO_NAME:
976 MUTEX_LOCK(&vmg_op_name_init_mutex);
978 if (!vmg_op_name_init) {
980 for (t = 0; t < OP_max; ++t)
981 vmg_op_name_len[t] = strlen(PL_op_name[t]);
982 vmg_op_name_init = 1;
985 MUTEX_UNLOCK(&vmg_op_name_init_mutex);
988 case VMG_OP_INFO_OBJECT: {
990 if (!MY_CXT.b__op_stashes[0]) {
993 for (c = OPc_NULL; c < OPc_MAX; ++c)
994 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
1003 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
1004 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
1006 return &PL_sv_undef;
1009 case VMG_OP_INFO_NAME: {
1010 OPCODE t = PL_op->op_type;
1011 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
1013 case VMG_OP_INFO_OBJECT: {
1015 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1016 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1022 return &PL_sv_undef;
1025 /* ... svt callbacks ....................................................... */
1027 #define VMG_CB_CALL_ARGS_MASK 15
1028 #define VMG_CB_CALL_ARGS_SHIFT 4
1029 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT)
1031 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1034 unsigned int i, args, opinfo;
1039 args = flags & VMG_CB_CALL_ARGS_MASK;
1040 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1041 opinfo = flags & VMG_CB_CALL_OPINFO;
1047 EXTEND(SP, args + 1);
1048 PUSHs(sv_2mortal(newRV_inc(sv)));
1050 for (i = 0; i < args; ++i) {
1051 SV *sva = va_arg(ap, SV *);
1052 PUSHs(sva ? sva : &PL_sv_undef);
1056 XPUSHs(vmg_op_info(opinfo));
1059 vmg_call_sv(cb, G_SCALAR, 0);
1064 ret = (int) SvIV(svr);
1073 #define VMG_CB_FLAGS(OI, A) \
1074 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1076 #define vmg_cb_call1(I, OI, S, A1) \
1077 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1078 #define vmg_cb_call2(I, OI, S, A1, A2) \
1079 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1080 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1081 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1083 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1084 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1085 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1088 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1089 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1090 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1093 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1094 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1095 unsigned int opinfo = w->opinfo;
1098 svtype t = SvTYPE(sv);
1107 PUSHs(sv_2mortal(newRV_inc(sv)));
1108 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1111 #if VMG_HAS_PERL(5, 9, 3)
1112 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, l)));
1114 U8 *s = SvPV(sv, l);
1117 len = utf8_length(s, s + l);
1121 } else if (t == SVt_PVAV) {
1122 len = av_len((AV *) sv) + 1;
1126 PUSHs(&PL_sv_undef);
1129 XPUSHs(vmg_op_info(opinfo));
1132 vmg_call_sv(w->cb_len, G_SCALAR, 0);
1136 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1147 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1148 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1149 return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj);
1152 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1159 /* Don't even bother if we are in global destruction - the wizard is prisoner
1160 * of circular references and we are way beyond user realm */
1164 w = vmg_wizard_mgwiz(mg->mg_ptr);
1166 /* So that it survives the temp cleanup below */
1167 SvREFCNT_inc_simple_void(sv);
1169 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1170 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1171 * if you access the sv from the callback the old deleted magics will trigger
1172 * and cause memory misreads. Change 32686 solved it that way : */
1173 SvMAGIC_set(sv, mg);
1181 PUSHs(sv_2mortal(newRV_inc(sv)));
1182 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1184 XPUSHs(vmg_op_info(w->opinfo));
1187 vmg_call_sv(w->cb_free, G_SCALAR, 1);
1192 ret = (int) SvIV(svr);
1198 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1199 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1202 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1203 * mg->mg_ptr reference count */
1208 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
1209 # if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1216 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1219 if (keylen == HEf_SVKEY) {
1222 keysv = newSVpvn(key, keylen);
1225 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1227 if (keylen != HEf_SVKEY) {
1228 SvREFCNT_dec(keysv);
1233 #endif /* MGf_COPY */
1236 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1239 #endif /* MGf_DUP */
1242 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1243 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1244 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1246 #endif /* MGf_LOCAL */
1249 STATIC OP *vmg_pp_resetuvar(pTHX) {
1250 SvRMAGICAL_on(cSVOP_sv);
1254 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1257 SV *key = NULL, *newkey = NULL;
1260 umg = mg_find(sv, PERL_MAGIC_uvar);
1261 /* umg can't be NULL or we wouldn't be there. */
1263 uf = (struct ufuncs *) umg->mg_ptr;
1266 uf[1].uf_val(aTHX_ action, sv);
1268 uf[1].uf_set(aTHX_ action, sv);
1270 action &= HV_FETCH_ISSTORE | HV_FETCH_ISEXISTS | HV_FETCH_LVALUE | HV_DELETE;
1271 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
1273 switch (mg->mg_type) {
1274 case PERL_MAGIC_ext:
1276 case PERL_MAGIC_tied:
1282 if (mg->mg_private != SIG_WIZ) continue;
1283 w = vmg_wizard_mgwiz(mg->mg_ptr);
1289 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1294 vmg_cb_call2(w->cb_fetch, w->opinfo, sv, mg->mg_obj, key);
1296 case HV_FETCH_ISSTORE:
1297 case HV_FETCH_LVALUE:
1298 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1300 vmg_cb_call2(w->cb_store, w->opinfo, sv, mg->mg_obj, key);
1302 case HV_FETCH_ISEXISTS:
1304 vmg_cb_call2(w->cb_exists, w->opinfo, sv, mg->mg_obj, key);
1308 vmg_cb_call2(w->cb_delete, w->opinfo, sv, mg->mg_obj, key);
1313 if (SvRMAGICAL(sv) && !tied) {
1314 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1315 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1316 * the op_ppaddr of a new fake op injected between the current and the next
1319 if (!o->op_next || o->op_next->op_ppaddr != vmg_pp_resetuvar) {
1321 NewOp(1101, svop, 1, SVOP);
1322 svop->op_type = OP_STUB;
1323 svop->op_ppaddr = vmg_pp_resetuvar;
1324 svop->op_next = o->op_next;
1327 o->op_next = (OP *) svop;
1334 #endif /* VMG_UVAR */
1336 /* --- Macros for the XS section ------------------------------------------- */
1338 #define VMG_SET_CB(S, N) \
1340 w->cb_ ## N = (SvOK(cb) && SvROK(cb)) ? SvREFCNT_inc(SvRV(cb)) : NULL;
1342 #define VMG_SET_SVT_CB(S, N) \
1344 if (SvOK(cb) && SvROK(cb)) { \
1345 t->svt_ ## N = vmg_svt_ ## N; \
1346 w->cb_ ## N = SvREFCNT_inc(SvRV(cb)); \
1348 t->svt_ ## N = NULL; \
1349 w->cb_ ## N = NULL; \
1354 STATIC void vmg_cleanup(pTHX_ void *ud) {
1357 ptable_free(MY_CXT.wizards);
1358 MY_CXT.wizards = NULL;
1361 #endif /* VMG_THREADSAFE */
1363 /* --- XS ------------------------------------------------------------------ */
1365 MODULE = Variable::Magic PACKAGE = Variable::Magic
1375 MY_CXT.wizards = ptable_new();
1376 MY_CXT.owner = aTHX;
1378 MY_CXT.b__op_stashes[0] = NULL;
1380 MUTEX_INIT(&vmg_op_name_init_mutex);
1381 call_atexit(vmg_cleanup, NULL);
1384 stash = gv_stashpv(__PACKAGE__, 1);
1385 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1386 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1387 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1388 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1389 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1390 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1391 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1392 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1393 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1394 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1395 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1396 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1397 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1398 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1399 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1400 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1401 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1402 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1403 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1404 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1414 U32 had_b__op_stash = 0;
1421 ud.wizards = t = ptable_new();
1422 ud.owner = MY_CXT.owner;
1423 ptable_walk(MY_CXT.wizards, vmg_ptable_clone, &ud);
1425 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1426 if (MY_CXT.b__op_stashes[c])
1427 had_b__op_stash |= (((U32) 1) << c);
1433 MY_CXT.owner = aTHX;
1434 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1435 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1436 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1441 #endif /* VMG_THREADSAFE */
1457 #endif /* MGf_COPY */
1460 #endif /* MGf_DUP */
1463 #endif /* MGf_LOCAL */
1466 #endif /* VMG_UVAR */
1467 ) { croak(vmg_wrongargnum); }
1472 VMG_SET_CB(ST(i++), data);
1475 opinfo = SvOK(cb) ? SvUV(cb) : 0;
1476 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
1478 vmg_op_info_init(w->opinfo);
1480 VMG_SET_SVT_CB(ST(i++), get);
1481 VMG_SET_SVT_CB(ST(i++), set);
1482 VMG_SET_SVT_CB(ST(i++), len);
1483 VMG_SET_SVT_CB(ST(i++), clear);
1484 VMG_SET_SVT_CB(ST(i++), free);
1486 VMG_SET_SVT_CB(ST(i++), copy);
1487 #endif /* MGf_COPY */
1489 /* VMG_SET_SVT_CB(ST(i++), dup); */
1493 #endif /* MGf_DUP */
1495 VMG_SET_SVT_CB(ST(i++), local);
1496 #endif /* MGf_LOCAL */
1498 VMG_SET_CB(ST(i++), fetch);
1499 VMG_SET_CB(ST(i++), store);
1500 VMG_SET_CB(ST(i++), exists);
1501 VMG_SET_CB(ST(i++), delete);
1503 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1504 w->uvar = SvTRUE(cb) ? 2 : 1;
1507 #endif /* VMG_UVAR */
1508 #if VMG_MULTIPLICITY
1510 #endif /* VMG_MULTIPLICITY */
1513 ptable_store(MY_CXT.wizards, w, w);
1514 #endif /* VMG_THREADSAFE */
1516 RETVAL = newRV_noinc(vmg_wizard_new(w));
1520 SV *cast(SV *sv, SV *wiz, ...)
1521 PROTOTYPE: \[$@%&*]$@
1530 RETVAL = newSVuv(vmg_cast(SvRV(sv), vmg_wizard_validate(wiz), args, i));
1535 getdata(SV *sv, SV *wiz)
1536 PROTOTYPE: \[$@%&*]$
1540 data = vmg_data_get(SvRV(sv), vmg_wizard_validate(wiz));
1546 SV *dispell(SV *sv, SV *wiz)
1547 PROTOTYPE: \[$@%&*]$
1549 RETVAL = newSVuv(vmg_dispell(SvRV(sv), vmg_wizard_validate(wiz)));