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"
15 #ifndef VMG_PERL_PATCHLEVEL
17 # define VMG_PERL_PATCHLEVEL PERL_PATCHNUM
19 # define VMG_PERL_PATCHLEVEL 0
23 #define VMG_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
25 #define VMG_HAS_PERL_BRANCH(R, V, S) (PERL_REVISION == (R) && PERL_VERSION == (V) && PERL_SUBVERSION >= (S))
27 #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))))
29 /* --- Threads and multiplicity -------------------------------------------- */
39 /* Safe unless stated otherwise in Makefile.PL */
41 # define VMG_FORKSAFE 1
44 #ifndef VMG_MULTIPLICITY
45 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
46 # define VMG_MULTIPLICITY 1
48 # define VMG_MULTIPLICITY 0
51 #if VMG_MULTIPLICITY && !defined(tTHX)
52 # define tTHX PerlInterpreter*
55 #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))
56 # define VMG_THREADSAFE 1
58 # define MY_CXT_CLONE \
60 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
61 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
62 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
65 # define VMG_THREADSAFE 0
67 # define dMY_CXT dNOOP
69 # define MY_CXT vmg_globaldata
71 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
73 # define MY_CXT_INIT NOOP
75 # define MY_CXT_CLONE NOOP
80 STATIC SV *vmg_clone(pTHX_ SV *sv, tTHX owner) {
81 #define vmg_clone(P, O) vmg_clone(aTHX_ (P), (O))
84 #if VMG_HAS_PERL(5, 13, 2)
85 CLONE_PARAMS *param = Perl_clone_params_new(owner, aTHX);
87 dupsv = sv_dup(sv, param);
89 Perl_clone_params_del(param);
93 param.stashes = NULL; /* don't need it unless sv is a PVHV */
95 param.proto_perl = owner;
97 dupsv = sv_dup(sv, ¶m);
100 return SvREFCNT_inc(dupsv);
103 #endif /* VMG_THREADSAFE */
105 /* --- Compatibility ------------------------------------------------------- */
108 # define Newx(v, n, c) New(0, v, n, c)
112 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
116 # define SvRV_const(sv) SvRV((SV *) sv)
119 #ifndef SvREFCNT_inc_simple_void
120 # define SvREFCNT_inc_simple_void(sv) SvREFCNT_inc(sv)
124 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
127 #ifndef PERL_MAGIC_ext
128 # define PERL_MAGIC_ext '~'
131 #ifndef PERL_MAGIC_tied
132 # define PERL_MAGIC_tied 'P'
147 #ifndef IN_PERL_COMPILETIME
148 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
151 /* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
152 * enable them on 5.10 */
153 #if VMG_HAS_PERL(5, 10, 0)
159 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160, partially
160 * reverted to dev-5.11 as 9cdcb38b */
161 #if VMG_HAS_PERL_MAINT(5, 8, 9, 28160) || VMG_HAS_PERL_MAINT(5, 9, 3, 25854) || VMG_HAS_PERL(5, 10, 0)
162 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
163 # if VMG_HAS_PERL(5, 11, 0)
164 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
166 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
169 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
170 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 1
173 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
174 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
176 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
177 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 0
181 /* Applied to dev-5.11 as 34908 */
182 #if VMG_HAS_PERL_MAINT(5, 11, 0, 34908) || VMG_HAS_PERL(5, 12, 0)
183 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
185 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
188 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
189 #if VMG_HAS_PERL_MAINT(5, 8, 9, 32542) || VMG_HAS_PERL_MAINT(5, 9, 5, 31473) || VMG_HAS_PERL(5, 10, 0)
190 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
192 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
195 #if VMG_HAS_PERL_MAINT(5, 11, 0, 32969) || VMG_HAS_PERL(5, 12, 0)
196 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
198 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
201 #if VMG_HAS_PERL(5, 13, 2)
202 # define VMG_COMPAT_GLOB_GET 1
204 # define VMG_COMPAT_GLOB_GET 0
207 /* ... Bug-free mg_magical ................................................. */
209 /* 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. */
213 STATIC void vmg_sv_magicuvar(pTHX_ SV *sv, const char *uf, I32 len) {
214 #define vmg_sv_magicuvar(S, U, L) vmg_sv_magicuvar(aTHX_ (S), (U), (L))
216 sv_magic(sv, NULL, PERL_MAGIC_uvar, uf, len);
217 /* uvar magic has set and get magic, hence this has set SVs_GMG and SVs_SMG. */
218 if ((mg = SvMAGIC(sv))) {
221 const MGVTBL* const vtbl = mg->mg_virtual;
223 if (vtbl->svt_clear) {
228 } while ((mg = mg->mg_moremagic));
232 #endif /* VMG_UVAR */
234 /* ... Safe version of call_sv() ........................................... */
236 #define VMG_SAVE_LAST_CX (!VMG_HAS_PERL(5, 8, 4) || VMG_HAS_PERL(5, 9, 5))
238 STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, I32 destructor) {
239 #define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
240 I32 ret, cxix = 0, in_eval = 0;
242 PERL_CONTEXT saved_cx;
251 if (cxstack_ix < cxstack_max) {
252 cxix = cxstack_ix + 1;
253 if (destructor && CxTYPE(cxstack + cxix) == CXt_EVAL)
258 /* The last popped context will be reused by call_sv(), but our callers may
259 * still need its previous value. Back it up so that it isn't clobbered. */
260 saved_cx = cxstack[cxix];
263 ret = call_sv(sv, flags | G_EVAL);
266 cxstack[cxix] = saved_cx;
271 sv_setsv(old_err, ERRSV);
275 if (IN_PERL_COMPILETIME) {
278 sv_catsv(PL_errors, ERRSV);
280 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
283 #if VMG_HAS_PERL(5, 10, 0) || defined(PL_parser)
285 ++PL_parser->error_count;
286 #elif defined(PL_error_count)
303 /* --- Stolen chunk of B --------------------------------------------------- */
321 STATIC const char *const vmg_opclassnames[] = {
336 STATIC opclass vmg_opclass(const OP *o) {
343 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
345 if (o->op_type == OP_SASSIGN)
346 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
348 if (o->op_type == OP_AELEMFAST) {
349 if (o->op_flags & OPf_SPECIAL)
360 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
364 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
381 case OA_PVOP_OR_SVOP:
382 return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
387 case OA_BASEOP_OR_UNOP:
388 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
390 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
392 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
394 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
397 if (o->op_flags & OPf_STACKED)
399 else if (o->op_flags & OPf_SPECIAL)
408 /* --- Error messages ------------------------------------------------------ */
410 STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
411 STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
412 STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
414 /* --- Signatures ---------------------------------------------------------- */
416 #define SIG_WZO ((U16) (0x3891))
417 #define SIG_WIZ ((U16) (0x3892))
419 /* --- MGWIZ structure ----------------------------------------------------- */
428 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
431 #endif /* MGf_COPY */
437 #endif /* MGf_LOCAL */
439 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
440 #endif /* VMG_UVAR */
444 #endif /* VMG_MULTIPLICITY */
447 STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *w) {
448 #define vmg_mgwiz_free(W) vmg_mgwiz_free(aTHX_ (W))
452 SvREFCNT_dec(w->cb_data);
453 SvREFCNT_dec(w->cb_get);
454 SvREFCNT_dec(w->cb_set);
455 SvREFCNT_dec(w->cb_len);
456 SvREFCNT_dec(w->cb_clear);
457 SvREFCNT_dec(w->cb_free);
459 SvREFCNT_dec(w->cb_copy);
460 #endif /* MGf_COPY */
462 SvREFCNT_dec(w->cb_dup);
465 SvREFCNT_dec(w->cb_local);
466 #endif /* MGf_LOCAL */
468 SvREFCNT_dec(w->cb_fetch);
469 SvREFCNT_dec(w->cb_store);
470 SvREFCNT_dec(w->cb_exists);
471 SvREFCNT_dec(w->cb_delete);
472 #endif /* VMG_UVAR */
482 #define VMG_CLONE_CB(N) \
483 z->cb_ ## N = (w->cb_ ## N) ? vmg_clone(w->cb_ ## N, w->owner) \
486 STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) {
487 #define vmg_mgwiz_clone(W) vmg_mgwiz_clone(aTHX_ (W))
495 Copy(w->vtbl, t, 1, MGVTBL);
501 z->opinfo = w->opinfo;
511 #endif /* MGf_COPY */
517 #endif /* MGf_LOCAL */
521 VMG_CLONE_CB(exists);
522 VMG_CLONE_CB(delete);
523 #endif /* VMG_UVAR */
530 #endif /* VMG_THREADSAFE */
532 /* --- Context-safe global data -------------------------------------------- */
536 #define PTABLE_NAME ptable
537 #define PTABLE_VAL_FREE(V) vmg_mgwiz_free(V)
546 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
547 #define ptable_clear(T) ptable_clear(aTHX_ (T))
548 #define ptable_free(T) ptable_free(aTHX_ (T))
550 #endif /* VMG_THREADSAFE */
552 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
559 HV *b__op_stashes[OPc_MAX];
566 STATIC void vmg_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
570 if (ud->owner == aTHX)
573 w = vmg_mgwiz_clone(ent->val);
575 ptable_store(ud->wizards, ent->key, w);
578 #endif /* VMG_THREADSAFE */
580 /* --- Wizard objects ------------------------------------------------------ */
582 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg);
584 STATIC MGVTBL vmg_wizard_vtbl = {
589 vmg_wizard_free, /* free */
592 #endif /* MGf_COPY */
598 #endif /* MGf_LOCAL */
601 /* ... Wizard constructor .................................................. */
603 STATIC SV *vmg_wizard_new(pTHX_ const MGWIZ *w) {
604 #define vmg_wizard_new(W) vmg_wizard_new(aTHX_ (W))
605 SV *wiz = newSVuv(PTR2IV(w));
608 MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_vtbl, NULL, 0);
609 mg->mg_private = SIG_WZO;
616 STATIC const SV *vmg_wizard_validate(pTHX_ const SV *wiz) {
617 #define vmg_wizard_validate(W) vmg_wizard_validate(aTHX_ (W))
619 wiz = SvRV_const(wiz);
624 croak(vmg_invalid_wiz);
629 #define vmg_wizard_id(W) SvIVX((const SV *) (W))
630 #define vmg_wizard_main_mgwiz(W) INT2PTR(const MGWIZ *, vmg_wizard_id(W))
632 /* ... Wizard destructor ................................................... */
634 STATIC int vmg_wizard_free(pTHX_ SV *sv, MAGIC *mg) {
637 if (PL_dirty) /* During global destruction, the context is already freed */
640 w = (MGWIZ *) vmg_wizard_main_mgwiz(sv);
645 ptable_store(MY_CXT.wizards, w, NULL);
647 #else /* VMG_THREADSAFE */
649 #endif /* !VMG_THREADSAFE */
656 STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ const SV *wiz) {
657 #define vmg_wizard_mgwiz(W) vmg_wizard_mgwiz(aTHX_ ((const SV *) (W)))
660 w = vmg_wizard_main_mgwiz(wiz);
661 if (w->owner == aTHX)
666 return ptable_fetch(MY_CXT.wizards, w);
670 #else /* VMG_THREADSAFE */
672 #define vmg_wizard_mgwiz(W) vmg_wizard_main_mgwiz(W)
674 #endif /* !VMG_THREADSAFE */
676 /* --- User-level functions implementation --------------------------------- */
678 STATIC const MAGIC *vmg_find(const SV *sv, const SV *wiz) {
679 const MAGIC *mg, *moremagic;
682 if (SvTYPE(sv) < SVt_PVMG)
685 wid = vmg_wizard_id(wiz);
686 for (mg = SvMAGIC(sv); mg; mg = moremagic) {
687 moremagic = mg->mg_moremagic;
688 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
689 IV zid = vmg_wizard_id(mg->mg_ptr);
698 /* ... Construct private data .............................................. */
700 STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
701 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
711 EXTEND(SP, items + 1);
712 PUSHs(sv_2mortal(newRV_inc(sv)));
713 for (i = 0; i < items; ++i)
717 vmg_call_sv(ctor, G_SCALAR, 0);
721 #if VMG_HAS_PERL(5, 8, 3)
722 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
724 nsv = sv_newref(nsv); /* Workaround some bug in SvREFCNT_inc() */
734 STATIC SV *vmg_data_get(pTHX_ SV *sv, const SV *wiz) {
735 #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W))
736 const MAGIC *mg = vmg_find(sv, wiz);
737 return mg ? mg->mg_obj : NULL;
740 /* ... Magic cast/dispell .................................................. */
743 STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
745 STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) {
747 prevmagic->mg_moremagic = moremagic;
749 SvMAGIC_set(sv, moremagic);
751 mg->mg_moremagic = NULL;
752 Safefree(mg->mg_ptr);
755 #endif /* VMG_UVAR */
757 STATIC UV vmg_cast(pTHX_ SV *sv, const SV *wiz, SV **args, I32 items) {
758 #define vmg_cast(S, W, A, I) vmg_cast(aTHX_ (S), (W), (A), (I))
759 MAGIC *mg, *moremagic = NULL;
764 if (vmg_find(sv, wiz))
767 w = vmg_wizard_mgwiz(wiz);
768 oldgmg = SvGMAGICAL(sv);
770 data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL;
771 mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY);
773 mg->mg_private = SIG_WIZ;
776 mg->mg_flags |= MGf_COPY;
777 #endif /* MGf_COPY */
780 mg->mg_flags |= MGf_DUP;
784 mg->mg_flags |= MGf_LOCAL;
785 #endif /* MGf_LOCAL */
787 if (SvTYPE(sv) < SVt_PVHV)
790 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
791 * magic is actually never called for them. If the GMAGICAL flag was off before
792 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
793 * now on, then this wizard has get magic. Hence we can work around the
794 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
795 * has uvar callbacks, it will be turned back on later. */
796 if (!oldgmg && SvGMAGICAL(sv))
804 uf[0].uf_val = vmg_svt_val;
811 /* One uvar magic in the chain is enough. */
812 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
813 moremagic = mg->mg_moremagic;
814 if (mg->mg_type == PERL_MAGIC_uvar)
818 if (mg) { /* Found another uvar magic. */
819 struct ufuncs *olduf = (struct ufuncs *) mg->mg_ptr;
820 if (olduf->uf_val == vmg_svt_val) {
821 /* It's our uvar magic, nothing to do. oldgmg was true. */
824 /* It's another uvar magic, backup it and replace it by ours. */
826 vmg_uvar_del(sv, prevmagic, mg, moremagic);
830 vmg_sv_magicuvar(sv, (const char *) &uf, sizeof(uf));
831 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
832 * handled by our uvar callback. */
834 #endif /* VMG_UVAR */
840 STATIC UV vmg_dispell(pTHX_ SV *sv, const SV *wiz) {
841 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
844 #endif /* VMG_UVAR */
845 MAGIC *mg, *prevmagic, *moremagic = NULL;
846 IV wid = vmg_wizard_id(wiz);
848 if (SvTYPE(sv) < SVt_PVMG)
851 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
852 moremagic = mg->mg_moremagic;
853 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
854 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
855 IV zid = vmg_wizard_id(mg->mg_ptr);
858 /* If the current has no uvar, short-circuit uvar deletion. */
859 uvars = z->uvar ? (uvars + 1) : 0;
860 #endif /* VMG_UVAR */
863 } else if (z->uvar) {
865 /* We can't break here since we need to find the ext magic to delete. */
866 #endif /* VMG_UVAR */
874 prevmagic->mg_moremagic = moremagic;
876 SvMAGIC_set(sv, moremagic);
878 mg->mg_moremagic = NULL;
880 /* Destroy private data */
881 if (mg->mg_obj != sv)
882 SvREFCNT_dec(mg->mg_obj);
883 /* Unreference the wizard */
884 SvREFCNT_dec((SV *) mg->mg_ptr);
888 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
889 /* mg was the first ext magic in the chain that had uvar */
891 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
892 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) {
893 const MGWIZ *z = vmg_wizard_mgwiz(mg->mg_ptr);
903 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
904 moremagic = mg->mg_moremagic;
905 if (mg->mg_type == PERL_MAGIC_uvar)
909 uf = (struct ufuncs *) mg->mg_ptr;
910 /* assert(uf->uf_val == vmg_svt_val); */
911 if (uf[1].uf_val || uf[1].uf_set) {
912 /* Revert the original uvar magic. */
914 Renew(uf, 1, struct ufuncs);
915 mg->mg_ptr = (char *) uf;
916 mg->mg_len = sizeof(struct ufuncs);
918 /* Remove the uvar magic. */
919 vmg_uvar_del(sv, prevmagic, mg, moremagic);
923 #endif /* VMG_UVAR */
928 /* ... OP info ............................................................. */
930 #define VMG_OP_INFO_NAME 1
931 #define VMG_OP_INFO_OBJECT 2
934 STATIC perl_mutex vmg_op_name_init_mutex;
937 STATIC U32 vmg_op_name_init = 0;
938 STATIC unsigned char vmg_op_name_len[MAXO] = { 0 };
940 STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
941 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
943 case VMG_OP_INFO_NAME:
945 MUTEX_LOCK(&vmg_op_name_init_mutex);
947 if (!vmg_op_name_init) {
949 for (t = 0; t < OP_max; ++t)
950 vmg_op_name_len[t] = strlen(PL_op_name[t]);
951 vmg_op_name_init = 1;
954 MUTEX_UNLOCK(&vmg_op_name_init_mutex);
957 case VMG_OP_INFO_OBJECT: {
959 if (!MY_CXT.b__op_stashes[0]) {
962 for (c = OPc_NULL; c < OPc_MAX; ++c)
963 MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
972 STATIC SV *vmg_op_info(pTHX_ unsigned int opinfo) {
973 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
978 case VMG_OP_INFO_NAME: {
979 OPCODE t = PL_op->op_type;
980 return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
982 case VMG_OP_INFO_OBJECT: {
984 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
985 MY_CXT.b__op_stashes[vmg_opclass(PL_op)]);
994 /* ... svt callbacks ....................................................... */
996 #define VMG_CB_CALL_ARGS_MASK 15
997 #define VMG_CB_CALL_ARGS_SHIFT 4
998 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT)
1000 STATIC int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1003 unsigned int i, args, opinfo;
1008 args = flags & VMG_CB_CALL_ARGS_MASK;
1009 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1010 opinfo = flags & VMG_CB_CALL_OPINFO;
1016 EXTEND(SP, args + 1);
1017 PUSHs(sv_2mortal(newRV_inc(sv)));
1019 for (i = 0; i < args; ++i) {
1020 SV *sva = va_arg(ap, SV *);
1021 PUSHs(sva ? sva : &PL_sv_undef);
1025 XPUSHs(vmg_op_info(opinfo));
1028 vmg_call_sv(cb, G_SCALAR, 0);
1033 ret = (int) SvIV(svr);
1042 #define VMG_CB_FLAGS(OI, A) \
1043 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1045 #define vmg_cb_call1(I, OI, S, A1) \
1046 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1047 #define vmg_cb_call2(I, OI, S, A1, A2) \
1048 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1049 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1050 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1052 STATIC int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1053 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1054 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1057 STATIC int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1058 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1059 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1062 STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1063 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1064 unsigned int opinfo = w->opinfo;
1067 svtype t = SvTYPE(sv);
1076 PUSHs(sv_2mortal(newRV_inc(sv)));
1077 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1080 #if VMG_HAS_PERL(5, 9, 3)
1081 const U8 *s = SvPV_const(sv, l);
1083 U8 *s = SvPV(sv, l);
1086 len = utf8_length(s, s + l);
1090 } else if (t == SVt_PVAV) {
1091 len = av_len((AV *) sv) + 1;
1095 PUSHs(&PL_sv_undef);
1098 XPUSHs(vmg_op_info(opinfo));
1101 vmg_call_sv(w->cb_len, G_SCALAR, 0);
1105 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1116 STATIC int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1117 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1118 return vmg_cb_call1(w->cb_clear, w->opinfo, sv, mg->mg_obj);
1121 STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1128 /* Don't even bother if we are in global destruction - the wizard is prisoner
1129 * of circular references and we are way beyond user realm */
1133 w = vmg_wizard_mgwiz(mg->mg_ptr);
1135 /* So that it survives the temp cleanup below */
1136 SvREFCNT_inc_simple_void(sv);
1138 #if !(VMG_HAS_PERL_MAINT(5, 11, 0, 32686) || VMG_HAS_PERL(5, 12, 0))
1139 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1140 * if you access the sv from the callback the old deleted magics will trigger
1141 * and cause memory misreads. Change 32686 solved it that way : */
1142 SvMAGIC_set(sv, mg);
1150 PUSHs(sv_2mortal(newRV_inc(sv)));
1151 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1153 XPUSHs(vmg_op_info(w->opinfo));
1156 vmg_call_sv(w->cb_free, G_SCALAR, 1);
1161 ret = (int) SvIV(svr);
1167 /* Calling SvREFCNT_dec() will trigger destructors in an infinite loop, so
1168 * we have to rely on SvREFCNT() being a lvalue. Heck, even the core does it */
1171 /* Perl_mg_free will get rid of the magic and decrement mg->mg_obj and
1172 * mg->mg_ptr reference count */
1177 STATIC int vmg_svt_copy(pTHX_ SV *sv, MAGIC *mg, SV *nsv, const char *key,
1178 # if VMG_HAS_PERL_MAINT(5, 11, 0, 33256) || VMG_HAS_PERL(5, 12, 0)
1185 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1188 if (keylen == HEf_SVKEY) {
1191 keysv = newSVpvn(key, keylen);
1194 ret = vmg_cb_call3(w->cb_copy, w->opinfo, sv, mg->mg_obj, keysv, nsv);
1196 if (keylen != HEf_SVKEY) {
1197 SvREFCNT_dec(keysv);
1202 #endif /* MGf_COPY */
1205 STATIC int vmg_svt_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param) {
1208 #endif /* MGf_DUP */
1211 STATIC int vmg_svt_local(pTHX_ SV *nsv, MAGIC *mg) {
1212 const MGWIZ *w = vmg_wizard_mgwiz(mg->mg_ptr);
1213 return vmg_cb_call1(w->cb_local, w->opinfo, nsv, mg->mg_obj);
1215 #endif /* MGf_LOCAL */
1218 STATIC OP *vmg_pp_resetuvar(pTHX) {
1219 SvRMAGICAL_on(cSVOP_sv);
1223 STATIC I32 vmg_svt_val(pTHX_ IV action, SV *sv) {
1226 SV *key = NULL, *newkey = NULL;
1229 umg = mg_find(sv, PERL_MAGIC_uvar);
1230 /* umg can't be NULL or we wouldn't be there. */
1232 uf = (struct ufuncs *) umg->mg_ptr;
1235 uf[1].uf_val(aTHX_ action, sv);
1237 uf[1].uf_set(aTHX_ action, sv);
1239 action &= HV_FETCH_ISSTORE | HV_FETCH_ISEXISTS | HV_FETCH_LVALUE | HV_DELETE;
1240 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
1242 switch (mg->mg_type) {
1243 case PERL_MAGIC_ext:
1245 case PERL_MAGIC_tied:
1251 if (mg->mg_private != SIG_WIZ) continue;
1252 w = vmg_wizard_mgwiz(mg->mg_ptr);
1258 newkey = key = umg->mg_obj = sv_mortalcopy(umg->mg_obj);
1263 vmg_cb_call2(w->cb_fetch, w->opinfo, sv, mg->mg_obj, key);
1265 case HV_FETCH_ISSTORE:
1266 case HV_FETCH_LVALUE:
1267 case (HV_FETCH_ISSTORE|HV_FETCH_LVALUE):
1269 vmg_cb_call2(w->cb_store, w->opinfo, sv, mg->mg_obj, key);
1271 case HV_FETCH_ISEXISTS:
1273 vmg_cb_call2(w->cb_exists, w->opinfo, sv, mg->mg_obj, key);
1277 vmg_cb_call2(w->cb_delete, w->opinfo, sv, mg->mg_obj, key);
1282 if (SvRMAGICAL(sv) && !tied) {
1283 /* Temporarily hide the RMAGICAL flag of the hash so it isn't wrongly
1284 * mistaken for a tied hash by the rest of hv_common. It will be reset by
1285 * the op_ppaddr of a new fake op injected between the current and the next
1288 if (!o->op_next || o->op_next->op_ppaddr != vmg_pp_resetuvar) {
1290 NewOp(1101, svop, 1, SVOP);
1291 svop->op_type = OP_STUB;
1292 svop->op_ppaddr = vmg_pp_resetuvar;
1293 svop->op_next = o->op_next;
1296 o->op_next = (OP *) svop;
1303 #endif /* VMG_UVAR */
1305 /* --- Macros for the XS section ------------------------------------------- */
1307 #define VMG_SET_CB(S, N) \
1309 w->cb_ ## N = (SvOK(cb) && SvROK(cb)) ? SvREFCNT_inc(SvRV(cb)) : NULL;
1311 #define VMG_SET_SVT_CB(S, N) \
1313 if (SvOK(cb) && SvROK(cb)) { \
1314 t->svt_ ## N = vmg_svt_ ## N; \
1315 w->cb_ ## N = SvREFCNT_inc(SvRV(cb)); \
1317 t->svt_ ## N = NULL; \
1318 w->cb_ ## N = NULL; \
1323 STATIC void vmg_cleanup(pTHX_ void *ud) {
1326 ptable_free(MY_CXT.wizards);
1327 MY_CXT.wizards = NULL;
1330 #endif /* VMG_THREADSAFE */
1332 /* --- XS ------------------------------------------------------------------ */
1334 MODULE = Variable::Magic PACKAGE = Variable::Magic
1344 MY_CXT.wizards = ptable_new();
1345 MY_CXT.owner = aTHX;
1347 MY_CXT.b__op_stashes[0] = NULL;
1349 MUTEX_INIT(&vmg_op_name_init_mutex);
1350 call_atexit(vmg_cleanup, NULL);
1353 stash = gv_stashpv(__PACKAGE__, 1);
1354 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1355 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1356 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1357 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1358 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1359 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1360 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1361 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1362 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1363 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1364 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1365 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1366 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1367 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1368 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1369 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(VMG_PERL_PATCHLEVEL));
1370 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(VMG_THREADSAFE));
1371 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(VMG_FORKSAFE));
1372 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1373 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1383 U32 had_b__op_stash = 0;
1390 ud.wizards = t = ptable_new();
1391 ud.owner = MY_CXT.owner;
1392 ptable_walk(MY_CXT.wizards, vmg_ptable_clone, &ud);
1394 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1395 if (MY_CXT.b__op_stashes[c])
1396 had_b__op_stash |= (((U32) 1) << c);
1402 MY_CXT.owner = aTHX;
1403 for (c = OPc_NULL; c < OPc_MAX; ++c) {
1404 MY_CXT.b__op_stashes[c] = (had_b__op_stash & (((U32) 1) << c))
1405 ? gv_stashpv(vmg_opclassnames[c], 1) : NULL;
1410 #endif /* VMG_THREADSAFE */
1426 #endif /* MGf_COPY */
1429 #endif /* MGf_DUP */
1432 #endif /* MGf_LOCAL */
1435 #endif /* VMG_UVAR */
1436 ) { croak(vmg_wrongargnum); }
1441 VMG_SET_CB(ST(i++), data);
1444 opinfo = SvOK(cb) ? SvUV(cb) : 0;
1445 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
1447 vmg_op_info_init(w->opinfo);
1449 VMG_SET_SVT_CB(ST(i++), get);
1450 VMG_SET_SVT_CB(ST(i++), set);
1451 VMG_SET_SVT_CB(ST(i++), len);
1452 VMG_SET_SVT_CB(ST(i++), clear);
1453 VMG_SET_SVT_CB(ST(i++), free);
1455 VMG_SET_SVT_CB(ST(i++), copy);
1456 #endif /* MGf_COPY */
1458 /* VMG_SET_SVT_CB(ST(i++), dup); */
1462 #endif /* MGf_DUP */
1464 VMG_SET_SVT_CB(ST(i++), local);
1465 #endif /* MGf_LOCAL */
1467 VMG_SET_CB(ST(i++), fetch);
1468 VMG_SET_CB(ST(i++), store);
1469 VMG_SET_CB(ST(i++), exists);
1470 VMG_SET_CB(ST(i++), delete);
1472 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1473 w->uvar = SvTRUE(cb) ? 2 : 1;
1476 #endif /* VMG_UVAR */
1477 #if VMG_MULTIPLICITY
1479 #endif /* VMG_MULTIPLICITY */
1482 ptable_store(MY_CXT.wizards, w, w);
1483 #endif /* VMG_THREADSAFE */
1485 RETVAL = newRV_noinc(vmg_wizard_new(w));
1489 SV *cast(SV *sv, SV *wiz, ...)
1490 PROTOTYPE: \[$@%&*]$@
1499 RETVAL = newSVuv(vmg_cast(SvRV(sv), vmg_wizard_validate(wiz), args, i));
1504 getdata(SV *sv, SV *wiz)
1505 PROTOTYPE: \[$@%&*]$
1509 data = vmg_data_get(SvRV(sv), vmg_wizard_validate(wiz));
1515 SV *dispell(SV *sv, SV *wiz)
1516 PROTOTYPE: \[$@%&*]$
1518 RETVAL = newSVuv(vmg_dispell(SvRV(sv), vmg_wizard_validate(wiz)));