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 /* --- XS helpers ---------------------------------------------------------- */
15 #define XSH_PACKAGE "Variable::Magic"
20 /* ... Features ............................................................ */
22 /* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
23 * enable them on 5.10 */
24 #if XSH_HAS_PERL(5, 10, 0)
30 #if XSH_HAS_PERL_MAINT(5, 11, 0, 32969) || XSH_HAS_PERL(5, 12, 0)
31 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 1
33 # define VMG_COMPAT_SCALAR_LENGTH_NOLEN 0
36 #if XSH_HAS_PERL(5, 17, 4)
37 # define VMG_COMPAT_SCALAR_NOLEN 1
39 # define VMG_COMPAT_SCALAR_NOLEN 0
42 /* Applied to dev-5.9 as 25854, integrated to maint-5.8 as 28160, partially
43 * reverted to dev-5.11 as 9cdcb38b */
44 #if XSH_HAS_PERL_MAINT(5, 8, 9, 28160) || XSH_HAS_PERL_MAINT(5, 9, 3, 25854) || XSH_HAS_PERL(5, 10, 0)
45 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
46 # if XSH_HAS_PERL(5, 11, 0)
47 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
49 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 1
52 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
53 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 1
56 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN
57 # define VMG_COMPAT_ARRAY_PUSH_NOLEN 0
59 # ifndef VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID
60 # define VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID 0
64 /* Applied to dev-5.11 as 34908 */
65 #if XSH_HAS_PERL_MAINT(5, 11, 0, 34908) || XSH_HAS_PERL(5, 12, 0)
66 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 1
68 # define VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID 0
71 /* Applied to dev-5.9 as 31473 (see #43357), integrated to maint-5.8 as 32542 */
72 #if XSH_HAS_PERL_MAINT(5, 8, 9, 32542) || XSH_HAS_PERL_MAINT(5, 9, 5, 31473) || XSH_HAS_PERL(5, 10, 0)
73 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 1
75 # define VMG_COMPAT_ARRAY_UNDEF_CLEAR 0
78 #if XSH_HAS_PERL(5, 11, 0)
79 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 1
81 # define VMG_COMPAT_HASH_DELETE_NOUVAR_VOID 0
84 #if XSH_HAS_PERL(5, 17, 0)
85 # define VMG_COMPAT_CODE_COPY_CLONE 1
87 # define VMG_COMPAT_CODE_COPY_CLONE 0
90 #if XSH_HAS_PERL(5, 13, 2)
91 # define VMG_COMPAT_GLOB_GET 1
93 # define VMG_COMPAT_GLOB_GET 0
96 /* ... Trampoline ops ...................................................... */
98 #define VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE (XSH_HAS_PERL(5, 10, 0) && !XSH_HAS_PERL(5, 10, 1))
100 /* NewOp() isn't public in perl 5.8.0. */
101 #define VMG_RESET_RMG_NEEDS_TRAMPOLINE (VMG_UVAR && (XSH_THREADSAFE || !XSH_HAS_PERL(5, 8, 1)))
103 #define VMG_NEEDS_TRAMPOLINE VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE || VMG_RESET_RMG_NEEDS_TRAMPOLINE
105 #if VMG_NEEDS_TRAMPOLINE
112 static void vmg_trampoline_init(vmg_trampoline *t, OP *(*cb)(pTHX)) {
113 t->temp.op_type = OP_STUB;
114 t->temp.op_ppaddr = 0;
115 t->temp.op_next = (OP *) &t->target;
116 t->temp.op_flags = 0;
117 t->temp.op_private = 0;
119 t->target.op_type = OP_STUB;
120 t->target.op_ppaddr = cb;
121 t->target.op_next = NULL;
122 t->target.op_flags = 0;
123 t->target.op_private = 0;
124 t->target.op_sv = NULL;
127 static OP *vmg_trampoline_bump(pTHX_ vmg_trampoline *t, SV *sv, OP *o) {
128 #define vmg_trampoline_bump(T, S, O) vmg_trampoline_bump(aTHX_ (T), (S), (O))
130 t->temp.op_next = (OP *) &t->target;
132 t->target.op_sv = sv;
133 t->target.op_next = o->op_next;
138 #endif /* VMG_NEEDS_TRAMPOLINE */
140 /* --- Compatibility ------------------------------------------------------- */
143 # define Newx(v, n, c) New(0, v, n, c)
147 # define SvMAGIC_set(sv, val) (SvMAGIC(sv) = (val))
151 # define SvRV_const(sv) SvRV((SV *) sv)
154 #ifndef SvREFCNT_inc_simple_void
155 # define SvREFCNT_inc_simple_void(sv) ((void) SvREFCNT_inc(sv))
159 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
162 #ifndef PERL_MAGIC_ext
163 # define PERL_MAGIC_ext '~'
166 #ifndef PERL_MAGIC_tied
167 # define PERL_MAGIC_tied 'P'
174 #ifndef IN_PERL_COMPILETIME
175 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
179 # define OP_NAME(O) (PL_op_name[(O)->op_type])
183 # define OP_CLASS(O) (PL_opargs[(O)->op_type] & OA_CLASS_MASK)
186 /* ... Bug-free mg_magical ................................................. */
188 /* See the discussion at http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/2008-01/msg00036.html */
190 #if XSH_HAS_PERL(5, 11, 3)
192 #define vmg_mg_magical(S) mg_magical(S)
196 static void vmg_mg_magical(SV *sv) {
200 if ((mg = SvMAGIC(sv))) {
202 const MGVTBL* const vtbl = mg->mg_virtual;
204 if (vtbl->svt_get && !(mg->mg_flags & MGf_GSKIP))
211 } while ((mg = mg->mg_moremagic));
212 if (!(SvFLAGS(sv) & (SVs_GMG|SVs_SMG)))
219 /* ... Cleaner version of sv_magicext() .................................... */
221 static MAGIC *vmg_sv_magicext(pTHX_ SV *sv, SV *obj, const MGVTBL *vtbl, const void *ptr, I32 len) {
222 #define vmg_sv_magicext(S, O, V, P, L) vmg_sv_magicext(aTHX_ (S), (O), (V), (P), (L))
225 mg = sv_magicext(sv, obj, PERL_MAGIC_ext, vtbl, ptr, len);
232 mg->mg_flags |= MGf_COPY;
235 mg->mg_flags |= MGf_DUP;
239 mg->mg_flags |= MGf_LOCAL;
240 #endif /* MGf_LOCAL */
242 if (mg->mg_flags & MGf_REFCOUNTED)
248 /* ... Safe version of call_sv() ........................................... */
250 static I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, int (*cleanup)(pTHX_ void *), void *ud) {
251 #define vmg_call_sv(S, F, C, U) vmg_call_sv(aTHX_ (S), (F), (C), (U))
256 old_err = newSVsv(ERRSV);
257 sv_setsv(ERRSV, &PL_sv_undef);
260 ret = call_sv(sv, flags | G_EVAL);
263 SvREFCNT_dec(old_err);
265 if (IN_PERL_COMPILETIME) {
268 sv_catsv(PL_errors, ERRSV);
270 Perl_warn(aTHX_ "%s", SvPV_nolen(ERRSV));
273 #if XSH_HAS_PERL(5, 10, 0) || defined(PL_parser)
275 ++PL_parser->error_count;
276 #elif defined(PL_error_count)
282 if (!cleanup || cleanup(aTHX_ ud))
287 sv_setsv(ERRSV, old_err);
288 SvREFCNT_dec(old_err);
295 /* --- Stolen chunk of B --------------------------------------------------- */
310 #if XSH_HAS_PERL(5, 21, 5)
313 #if XSH_HAS_PERL(5, 21, 7)
319 static const char *const vmg_opclassnames[] = {
332 #if XSH_HAS_PERL(5, 21, 5)
335 #if XSH_HAS_PERL(5, 21, 7)
341 static opclass vmg_opclass(pTHX_ const OP *o) {
342 #define vmg_opclass(O) vmg_opclass(aTHX_ (O))
348 if (o->op_type == 0) {
349 #if XSH_HAS_PERL(5, 21, 7)
350 if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
353 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
356 if (o->op_type == OP_SASSIGN)
357 return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
359 if (o->op_type == OP_AELEMFAST) {
360 #if PERL_VERSION <= 14
361 if (o->op_flags & OPf_SPECIAL)
373 if (o->op_type == OP_GV || o->op_type == OP_GVSV || o->op_type == OP_RCATLINE)
377 switch (OP_CLASS(o)) {
394 case OA_PVOP_OR_SVOP:
396 #if XSH_HAS_PERL(5, 13, 7)
397 (o->op_type != OP_CUSTOM) &&
399 (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)))
400 #if defined(USE_ITHREADS) && XSH_HAS_PERL(5, 8, 9)
401 ? OPc_PADOP : OPc_PVOP;
403 ? OPc_SVOP : OPc_PVOP;
409 case OA_BASEOP_OR_UNOP:
410 return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
412 return ((o->op_flags & OPf_KIDS) ? OPc_UNOP :
414 (o->op_flags & OPf_REF) ? OPc_PADOP : OPc_BASEOP);
416 (o->op_flags & OPf_REF) ? OPc_SVOP : OPc_BASEOP);
419 if (o->op_flags & OPf_STACKED)
421 else if (o->op_flags & OPf_SPECIAL)
425 #if XSH_HAS_PERL(5, 21, 5)
429 #if XSH_HAS_PERL(5, 21, 7)
438 /* --- Error messages ------------------------------------------------------ */
440 static const char vmg_invalid_wiz[] = "Invalid wizard object";
441 static const char vmg_wrongargnum[] = "Wrong number of arguments";
443 /* --- Thread-local storage ------------------------------------------------ */
446 HV *b__op_stashes[OPc_MAX];
449 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
450 vmg_trampoline propagate_errsv;
452 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
453 vmg_trampoline reset_rmg;
459 static void xsh_user_clone(pTHX_ const xsh_user_cxt_t *old_cxt, xsh_user_cxt_t *new_cxt) {
462 for (c = OPc_NULL; c < OPc_MAX; ++c) {
463 new_cxt->b__op_stashes[c] = old_cxt->b__op_stashes[c]
464 ? gv_stashpv(vmg_opclassnames[c], 1)
468 new_cxt->depth = old_cxt->depth;
469 new_cxt->freed_tokens = NULL;
475 #endif /* XSH_THREADSAFE */
477 #define XSH_THREADS_NEED_TEARDOWN_LATE 1
479 #include "xsh/threads.h"
481 /* --- <vmg_vtable> structure ---------------------------------------------- */
490 static vmg_vtable *vmg_vtable_alloc(pTHX) {
491 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
494 t = VOID2(vmg_vtable *, PerlMemShared_malloc(sizeof *t));
496 t->vtbl = VOID2(MGVTBL *, PerlMemShared_malloc(sizeof *t->vtbl));
502 #define vmg_vtable_vtbl(T) (T)->vtbl
504 static perl_mutex vmg_vtable_refcount_mutex;
506 static vmg_vtable *vmg_vtable_dup(pTHX_ vmg_vtable *t) {
507 #define vmg_vtable_dup(T) vmg_vtable_dup(aTHX_ (T))
508 XSH_LOCK(&vmg_vtable_refcount_mutex);
510 XSH_UNLOCK(&vmg_vtable_refcount_mutex);
515 static void vmg_vtable_free(pTHX_ vmg_vtable *t) {
516 #define vmg_vtable_free(T) vmg_vtable_free(aTHX_ (T))
519 XSH_LOCK(&vmg_vtable_refcount_mutex);
520 refcount = --t->refcount;
521 XSH_UNLOCK(&vmg_vtable_refcount_mutex);
524 PerlMemShared_free(t->vtbl);
525 PerlMemShared_free(t);
529 #else /* XSH_THREADSAFE */
531 typedef MGVTBL vmg_vtable;
533 static vmg_vtable *vmg_vtable_alloc(pTHX) {
534 #define vmg_vtable_alloc() vmg_vtable_alloc(aTHX)
537 Newx(t, 1, vmg_vtable);
542 #define vmg_vtable_vtbl(T) ((MGVTBL *) (T))
544 #define vmg_vtable_free(T) Safefree(T)
546 #endif /* !XSH_THREADSAFE */
548 /* --- <vmg_wizard> structure ---------------------------------------------- */
557 SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
562 #endif /* MGf_LOCAL */
564 SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
565 #endif /* VMG_UVAR */
568 static void vmg_op_info_init(pTHX_ unsigned int opinfo);
570 static vmg_wizard *vmg_wizard_alloc(pTHX_ UV opinfo) {
571 #define vmg_wizard_alloc(O) vmg_wizard_alloc(aTHX_ (O))
574 Newx(w, 1, vmg_wizard);
577 w->opinfo = (U8) ((opinfo < 255) ? opinfo : 255);
579 vmg_op_info_init(aTHX_ w->opinfo);
581 w->vtable = vmg_vtable_alloc();
586 static void vmg_wizard_free(pTHX_ vmg_wizard *w) {
587 #define vmg_wizard_free(W) vmg_wizard_free(aTHX_ (W))
591 /* During global destruction, any of the callbacks may already have been
592 * freed, so we can't rely on still being able to access them. */
594 SvREFCNT_dec(w->cb_data);
595 SvREFCNT_dec(w->cb_get);
596 SvREFCNT_dec(w->cb_set);
597 SvREFCNT_dec(w->cb_len);
598 SvREFCNT_dec(w->cb_clear);
599 SvREFCNT_dec(w->cb_free);
600 SvREFCNT_dec(w->cb_copy);
602 SvREFCNT_dec(w->cb_dup);
605 SvREFCNT_dec(w->cb_local);
606 #endif /* MGf_LOCAL */
608 SvREFCNT_dec(w->cb_fetch);
609 SvREFCNT_dec(w->cb_store);
610 SvREFCNT_dec(w->cb_exists);
611 SvREFCNT_dec(w->cb_delete);
612 #endif /* VMG_UVAR */
615 /* PerlMemShared_free() and Safefree() are still fine during global
616 * destruction though. */
617 vmg_vtable_free(w->vtable);
625 #define VMG_CLONE_CB(N) \
626 z->cb_ ## N = (w->cb_ ## N) ? SvREFCNT_inc(sv_dup(w->cb_ ## N, params)) \
629 static const vmg_wizard *vmg_wizard_dup(pTHX_ const vmg_wizard *w, CLONE_PARAMS *params) {
630 #define vmg_wizard_dup(W, P) vmg_wizard_dup(aTHX_ (W), (P))
636 Newx(z, 1, vmg_wizard);
638 z->vtable = vmg_vtable_dup(w->vtable);
640 z->opinfo = w->opinfo;
652 #endif /* MGf_LOCAL */
656 VMG_CLONE_CB(exists);
657 VMG_CLONE_CB(delete);
658 #endif /* VMG_UVAR */
663 #endif /* XSH_THREADSAFE */
665 #define vmg_wizard_id(W) PTR2IV(vmg_vtable_vtbl((W)->vtable))
667 /* --- Wizard SV objects --------------------------------------------------- */
669 static int vmg_wizard_sv_free(pTHX_ SV *sv, MAGIC *mg) {
670 vmg_wizard_free((vmg_wizard *) mg->mg_ptr);
677 static int vmg_wizard_sv_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
678 mg->mg_ptr = (char *) vmg_wizard_dup((const vmg_wizard *) mg->mg_ptr, params);
683 #endif /* XSH_THREADSAFE */
685 static MGVTBL vmg_wizard_sv_vtbl = {
690 vmg_wizard_sv_free, /* free */
693 vmg_wizard_sv_dup, /* dup */
699 #endif /* MGf_LOCAL */
702 static SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) {
703 #define vmg_wizard_sv_new(W) vmg_wizard_sv_new(aTHX_ (W))
709 wiz = newSViv(PTR2IV(w));
712 vmg_sv_magicext(wiz, NULL, &vmg_wizard_sv_vtbl, w, 0);
721 #define vmg_sv_has_wizard_type(S) (SvTYPE(S) >= SVt_PVMG)
723 static const vmg_wizard *vmg_wizard_from_sv_nocheck(const SV *wiz) {
726 for (mg = SvMAGIC(wiz); mg; mg = mg->mg_moremagic) {
727 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_virtual == &vmg_wizard_sv_vtbl)
728 return (const vmg_wizard *) mg->mg_ptr;
734 #else /* XSH_THREADSAFE */
736 #define vmg_sv_has_wizard_type(S) SvIOK(S)
738 #define vmg_wizard_from_sv_nocheck(W) INT2PTR(const vmg_wizard *, SvIVX(W))
740 #endif /* !XSH_THREADSAFE */
742 #define vmg_wizard_from_sv(W) (vmg_sv_has_wizard_type(W) ? vmg_wizard_from_sv_nocheck(W) : NULL)
744 static const vmg_wizard *vmg_wizard_from_mg(const MAGIC *mg) {
745 if (mg->mg_type == PERL_MAGIC_ext && mg->mg_len == HEf_SVKEY) {
746 SV *sv = (SV *) mg->mg_ptr;
748 if (vmg_sv_has_wizard_type(sv))
749 return vmg_wizard_from_sv_nocheck(sv);
755 #define vmg_wizard_from_mg_nocheck(M) vmg_wizard_from_sv_nocheck((const SV *) (M)->mg_ptr)
757 /* --- User-level functions implementation --------------------------------- */
759 static const MAGIC *vmg_find(const SV *sv, const vmg_wizard *w) {
763 if (SvTYPE(sv) < SVt_PVMG)
766 wid = vmg_wizard_id(w);
768 for (mg = SvMAGIC(sv); mg; mg = mg->mg_moremagic) {
769 const vmg_wizard *z = vmg_wizard_from_mg(mg);
771 if (z && vmg_wizard_id(z) == wid)
778 /* ... Construct private data .............................................. */
780 static SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, SV **args, I32 items) {
781 #define vmg_data_new(C, S, A, I) vmg_data_new(aTHX_ (C), (S), (A), (I))
790 PUSHSTACKi(PERLSI_MAGIC);
793 EXTEND(SP, items + 1);
794 PUSHs(sv_2mortal(newRV_inc(sv)));
795 for (i = 0; i < items; ++i)
799 vmg_call_sv(ctor, G_SCALAR, 0, NULL);
803 #if XSH_HAS_PERL(5, 8, 3)
804 SvREFCNT_inc_simple_void(nsv); /* Or it will be destroyed in FREETMPS */
806 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 = XSH_CXT.freed_tokens;
866 XSH_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 = vmg_sv_magicext(sv, data, t, wiz, HEf_SVKEY);
908 if (SvTYPE(sv) < SVt_PVHV)
911 /* The GMAGICAL flag only says that a hash is tied or has uvar magic - get
912 * magic is actually never called for them. If the GMAGICAL flag was off before
913 * calling sv_magicext(), the hash isn't tied and has no uvar magic. If it's
914 * now on, then this wizard has get magic. Hence we can work around the
915 * get/clear shortcoming by turning the GMAGICAL flag off. If the current magic
916 * has uvar callbacks, it will be turned back on later. */
917 if (!oldgmg && SvGMAGICAL(sv))
922 MAGIC *prevmagic, *moremagic = NULL;
925 ud.new_uf.uf_val = vmg_svt_val;
926 ud.new_uf.uf_set = NULL;
927 ud.new_uf.uf_index = 0;
928 ud.old_uf.uf_val = NULL;
929 ud.old_uf.uf_set = NULL;
930 ud.old_uf.uf_index = 0;
932 /* One uvar magic in the chain is enough. */
933 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
934 moremagic = mg->mg_moremagic;
935 if (mg->mg_type == PERL_MAGIC_uvar)
939 if (mg) { /* Found another uvar magic. */
940 struct ufuncs *uf = (struct ufuncs *) mg->mg_ptr;
941 if (uf->uf_val == vmg_svt_val) {
942 /* It's our uvar magic, nothing to do. oldgmg was true. */
945 /* It's another uvar magic, backup it and replace it by ours. */
947 vmg_mg_del(sv, prevmagic, mg, moremagic);
951 sv_magic(sv, NULL, PERL_MAGIC_uvar, (const char *) &ud, sizeof(ud));
953 /* Our hash now carries uvar magic. The uvar/clear shortcoming has to be
954 * handled by our uvar callback. */
956 #endif /* VMG_UVAR */
962 static UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) {
963 #define vmg_dispell(S, W) vmg_dispell(aTHX_ (S), (W))
966 #endif /* VMG_UVAR */
967 MAGIC *mg, *prevmagic, *moremagic = NULL;
968 IV wid = vmg_wizard_id(w);
970 if (SvTYPE(sv) < SVt_PVMG)
973 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic) {
976 moremagic = mg->mg_moremagic;
978 z = vmg_wizard_from_mg(mg);
980 IV zid = vmg_wizard_id(z);
984 /* If the current has no uvar, short-circuit uvar deletion. */
985 uvars = z->uvar ? (uvars + 1) : 0;
987 } else if (z->uvar) {
989 /* We can't break here since we need to find the ext magic to delete. */
994 #endif /* !VMG_UVAR */
1000 vmg_mg_del(sv, prevmagic, mg, moremagic);
1003 if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) {
1004 /* mg was the first ext magic in the chain that had uvar */
1006 for (mg = moremagic; mg; mg = mg->mg_moremagic) {
1007 const vmg_wizard *z = vmg_wizard_from_mg(mg);
1018 for (prevmagic = NULL, mg = SvMAGIC(sv); mg; prevmagic = mg, mg = moremagic){
1019 moremagic = mg->mg_moremagic;
1020 if (mg->mg_type == PERL_MAGIC_uvar)
1024 ud = (vmg_uvar_ud *) mg->mg_ptr;
1025 if (ud->old_uf.uf_val || ud->old_uf.uf_set) {
1026 /* Revert the original uvar magic. */
1028 Newx(uf, 1, struct ufuncs);
1031 mg->mg_ptr = (char *) uf;
1032 mg->mg_len = sizeof(*uf);
1034 /* Remove the uvar magic. */
1035 vmg_mg_del(sv, prevmagic, mg, moremagic);
1039 #endif /* VMG_UVAR */
1046 /* ... OP info ............................................................. */
1048 #define VMG_OP_INFO_NAME 1
1049 #define VMG_OP_INFO_OBJECT 2
1052 static perl_mutex vmg_op_name_init_mutex;
1055 static U32 vmg_op_name_init = 0;
1056 static unsigned char vmg_op_name_len[MAXO] = { 0 };
1058 static void vmg_op_info_init(pTHX_ unsigned int opinfo) {
1059 #define vmg_op_info_init(W) vmg_op_info_init(aTHX_ (W))
1061 case VMG_OP_INFO_NAME:
1062 XSH_LOCK(&vmg_op_name_init_mutex);
1063 if (!vmg_op_name_init) {
1065 for (t = 0; t < OP_max; ++t)
1066 vmg_op_name_len[t] = strlen(PL_op_name[t]);
1067 vmg_op_name_init = 1;
1069 XSH_UNLOCK(&vmg_op_name_init_mutex);
1071 case VMG_OP_INFO_OBJECT: {
1073 if (!XSH_CXT.b__op_stashes[0]) {
1076 for (c = OPc_NULL; c < OPc_MAX; ++c)
1077 XSH_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
1086 static SV *vmg_op_info(pTHX_ unsigned int opinfo) {
1087 #define vmg_op_info(W) vmg_op_info(aTHX_ (W))
1089 return &PL_sv_undef;
1092 case VMG_OP_INFO_NAME: {
1095 OPCODE t = PL_op->op_type;
1096 name = OP_NAME(PL_op);
1097 name_len = (t == OP_CUSTOM) ? strlen(name) : vmg_op_name_len[t];
1098 return sv_2mortal(newSVpvn(name, name_len));
1100 case VMG_OP_INFO_OBJECT: {
1102 return sv_bless(sv_2mortal(newRV_noinc(newSViv(PTR2IV(PL_op)))),
1103 XSH_CXT.b__op_stashes[vmg_opclass(PL_op)]);
1109 return &PL_sv_undef;
1112 /* --- svt callbacks ------------------------------------------------------- */
1114 #define VMG_CB_CALL_ARGS_MASK 15
1115 #define VMG_CB_CALL_ARGS_SHIFT 4
1116 #define VMG_CB_CALL_OPINFO (VMG_OP_INFO_NAME|VMG_OP_INFO_OBJECT) /* 1|2 */
1117 #define VMG_CB_CALL_GUARD 4
1119 static int vmg_dispell_guard_oncroak(pTHX_ void *ud) {
1124 /* If we're at the upmost magic call and we're about to die, we can just free
1125 * the tokens right now, since we will jump past the problematic part of our
1127 if (XSH_CXT.depth == 0 && XSH_CXT.freed_tokens) {
1128 vmg_magic_chain_free(XSH_CXT.freed_tokens, NULL);
1129 XSH_CXT.freed_tokens = NULL;
1135 static int vmg_dispell_guard_free(pTHX_ SV *sv, MAGIC *mg) {
1136 vmg_magic_chain_free((MAGIC *) mg->mg_ptr, NULL);
1143 static int vmg_dispell_guard_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *params) {
1144 /* The freed magic tokens aren't cloned by perl because it cannot reach them
1145 * (they have been detached from their parent SV when they were enqueued).
1146 * Hence there's nothing to purge in the new thread. */
1152 #endif /* XSH_THREADSAFE */
1154 static MGVTBL vmg_dispell_guard_vtbl = {
1159 vmg_dispell_guard_free, /* free */
1162 vmg_dispell_guard_dup, /* dup */
1168 #endif /* MGf_LOCAL */
1171 static SV *vmg_dispell_guard_new(pTHX_ MAGIC *root) {
1172 #define vmg_dispell_guard_new(R) vmg_dispell_guard_new(aTHX_ (R))
1175 guard = sv_newmortal();
1176 vmg_sv_magicext(guard, NULL, &vmg_dispell_guard_vtbl, root, 0);
1181 static int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
1184 unsigned int i, args, opinfo;
1185 MAGIC **chain = NULL;
1190 args = flags & VMG_CB_CALL_ARGS_MASK;
1191 flags >>= VMG_CB_CALL_ARGS_SHIFT;
1192 opinfo = flags & VMG_CB_CALL_OPINFO;
1197 PUSHSTACKi(PERLSI_MAGIC);
1200 EXTEND(SP, args + 1);
1201 PUSHs(sv_2mortal(newRV_inc(sv)));
1203 for (i = 0; i < args; ++i) {
1204 SV *sva = va_arg(ap, SV *);
1205 PUSHs(sva ? sva : &PL_sv_undef);
1209 XPUSHs(vmg_op_info(opinfo));
1212 if (flags & VMG_CB_CALL_GUARD) {
1215 vmg_call_sv(cb, G_SCALAR, vmg_dispell_guard_oncroak, NULL);
1217 if (XSH_CXT.depth == 0 && XSH_CXT.freed_tokens)
1218 chain = &XSH_CXT.freed_tokens;
1220 vmg_call_sv(cb, G_SCALAR, 0, NULL);
1226 ret = (int) SvIV(svr);
1238 if (svr && !SvTEMP(svr))
1242 vmg_dispell_guard_new(*chain);
1249 #define VMG_CB_FLAGS(OI, A) \
1250 ((((unsigned int) (OI)) << VMG_CB_CALL_ARGS_SHIFT) | (A))
1252 #define vmg_cb_call1(I, OI, S, A1) \
1253 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 1), (S), (A1))
1254 #define vmg_cb_call2(I, OI, S, A1, A2) \
1255 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 2), (S), (A1), (A2))
1256 #define vmg_cb_call3(I, OI, S, A1, A2, A3) \
1257 vmg_cb_call(aTHX_ (I), VMG_CB_FLAGS((OI), 3), (S), (A1), (A2), (A3))
1259 /* ... Default no-op magic callback ........................................ */
1261 static int vmg_svt_default_noop(pTHX_ SV *sv, MAGIC *mg) {
1265 /* ... get magic ........................................................... */
1267 static int vmg_svt_get(pTHX_ SV *sv, MAGIC *mg) {
1268 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1270 return vmg_cb_call1(w->cb_get, w->opinfo, sv, mg->mg_obj);
1273 #define vmg_svt_get_noop vmg_svt_default_noop
1275 /* ... set magic ........................................................... */
1277 static int vmg_svt_set(pTHX_ SV *sv, MAGIC *mg) {
1278 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1280 return vmg_cb_call1(w->cb_set, w->opinfo, sv, mg->mg_obj);
1283 #define vmg_svt_set_noop vmg_svt_default_noop
1285 /* ... len magic ........................................................... */
1287 static U32 vmg_sv_len(pTHX_ SV *sv) {
1288 #define vmg_sv_len(S) vmg_sv_len(aTHX_ (S))
1290 #if XSH_HAS_PERL(5, 9, 3)
1291 const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, len)));
1293 U8 *s = SvPV(sv, len);
1296 return DO_UTF8(sv) ? utf8_length(s, s + len) : len;
1299 static U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
1300 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1301 unsigned int opinfo = w->opinfo;
1304 svtype t = SvTYPE(sv);
1311 PUSHSTACKi(PERLSI_MAGIC);
1315 PUSHs(sv_2mortal(newRV_inc(sv)));
1316 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1318 len = vmg_sv_len(sv);
1320 } else if (t == SVt_PVAV) {
1321 len = av_len((AV *) sv) + 1;
1325 PUSHs(&PL_sv_undef);
1328 XPUSHs(vmg_op_info(opinfo));
1331 vmg_call_sv(w->cb_len, G_SCALAR, 0, NULL);
1335 ret = SvOK(svr) ? (U32) SvUV(svr) : len;
1348 static U32 vmg_svt_len_noop(pTHX_ SV *sv, MAGIC *mg) {
1350 svtype t = SvTYPE(sv);
1353 len = vmg_sv_len(sv);
1354 } else if (t == SVt_PVAV) {
1355 len = (U32) av_len((AV *) sv);
1361 /* ... clear magic ......................................................... */
1363 static int vmg_svt_clear(pTHX_ SV *sv, MAGIC *mg) {
1364 const vmg_wizard *w = vmg_wizard_from_mg_nocheck(mg);
1365 unsigned int flags = w->opinfo;
1367 #if !XSH_HAS_PERL(5, 12, 0)
1368 flags |= VMG_CB_CALL_GUARD;
1371 return vmg_cb_call1(w->cb_clear, flags, sv, mg->mg_obj);
1374 #define vmg_svt_clear_noop vmg_svt_default_noop
1376 /* ... free magic .......................................................... */
1378 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1380 static OP *vmg_pp_propagate_errsv(pTHX) {
1381 SVOP *o = cSVOPx(PL_op);
1384 sv_setsv(ERRSV, o->op_sv);
1385 SvREFCNT_dec(o->op_sv);
1392 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1394 static int vmg_propagate_errsv_free(pTHX_ SV *sv, MAGIC *mg) {
1396 sv_setsv(ERRSV, mg->mg_obj);
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(&XSH_CXT.propagate_errsv, errsv, PL_op);
1439 } else if (optype == OP_LEAVEEVAL) {
1440 SV *guard = sv_newmortal();
1441 vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1443 #else /* !VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1444 # if !XSH_HAS_PERL(5, 8, 9)
1446 SV *guard = sv_newmortal();
1447 vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1450 vmg_sv_magicext(ERRSV, errsv, &vmg_propagate_errsv_vtbl, NULL, 0);
1452 #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */
1457 /* Don't propagate */
1463 /* We are about to croak() while sv is being destroyed. Try to clean up
1467 vmg_mg_del(sv, NULL, mg, mg->mg_moremagic);
1472 vmg_dispell_guard_oncroak(aTHX_ NULL);
1474 /* After that, propagate the error upwards. */
1479 static int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
1480 vmg_svt_free_cleanup_ud ud;
1481 const vmg_wizard *w;
1487 /* During global destruction, we cannot be sure that the wizard and its free
1488 * callback are still alive. */
1492 w = vmg_wizard_from_mg_nocheck(mg);
1494 /* So that it survives the temp cleanup below */
1495 SvREFCNT_inc_simple_void(sv);
1497 #if !(XSH_HAS_PERL_MAINT(5, 11, 0, 32686) || XSH_HAS_PERL(5, 12, 0))
1498 /* The previous magic tokens were freed but the magic chain wasn't updated, so
1499 * if you access the sv from the callback the old deleted magics will trigger
1500 * and cause memory misreads. Change 32686 solved it that way : */
1501 SvMAGIC_set(sv, mg);
1505 if (cxstack_ix < cxstack_max) {
1506 ud.in_eval = (CxTYPE(cxstack + cxstack_ix + 1) == CXt_EVAL);
1507 ud.base = ud.in_eval ? PL_scopestack[PL_scopestack_ix] : 0;
1516 PUSHSTACKi(PERLSI_MAGIC);
1520 PUSHs(sv_2mortal(newRV_inc(sv)));
1521 PUSHs(mg->mg_obj ? mg->mg_obj : &PL_sv_undef);
1523 XPUSHs(vmg_op_info(w->opinfo));
1529 vmg_call_sv(w->cb_free, G_SCALAR, vmg_svt_free_cleanup, &ud);
1531 if (XSH_CXT.depth == 0 && XSH_CXT.freed_tokens) {
1532 /* Free all the tokens in the chain but the current one (if it's present).
1533 * It will be taken care of by our caller, Perl_mg_free(). */
1534 vmg_magic_chain_free(XSH_CXT.freed_tokens, mg);
1535 XSH_CXT.freed_tokens = NULL;
1542 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 XSH_HAS_PERL_MAINT(5, 11, 0, 33256) || XSH_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(&XSH_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 /* --- Module setup/teardown ----------------------------------------------- */
1750 static void vmg_global_teardown_late_locked(pTHX_ void *ud) {
1751 #define vmg_global_teardown_late_locked(UD) vmg_global_teardown_late_locked(aTHX_ (UD))
1752 MUTEX_DESTROY(&vmg_op_name_init_mutex);
1753 MUTEX_DESTROY(&vmg_vtable_refcount_mutex);
1758 static signed char vmg_destruct_level(pTHX) {
1759 #define vmg_destruct_level() vmg_destruct_level(aTHX)
1762 lvl = PL_perl_destruct_level;
1766 const char *s = PerlEnv_getenv("PERL_DESTRUCT_LEVEL");
1769 #if XSH_HAS_PERL(5, 21, 3)
1770 if (strEQ(s, "-1")) {
1773 # if XSH_HAS_PERL(5, 21, 10)
1775 if (Perl_grok_atoUV(s, &uv, NULL) && uv <= INT_MAX)
1779 # else /* XSH_HAS_PERL(5, 21, 3) && !XSH_HAS_PERL(5, 21, 10) */
1780 i = Perl_grok_atou(s, NULL);
1783 #else /* !XSH_HAS_PERL(5, 21, 3) */
1795 #endif /* XSH_THREADSAFE */
1797 static void xsh_user_global_setup(pTHX) {
1799 MUTEX_INIT(&vmg_vtable_refcount_mutex);
1800 MUTEX_INIT(&vmg_op_name_init_mutex);
1806 static void xsh_user_local_setup(pTHX_ xsh_user_cxt_t *cxt) {
1810 for (c = OPc_NULL; c < OPc_MAX; ++c)
1811 cxt->b__op_stashes[c] = NULL;
1814 cxt->freed_tokens = NULL;
1816 #if VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE
1817 vmg_trampoline_init(&cxt->propagate_errsv, vmg_pp_propagate_errsv);
1819 #if VMG_RESET_RMG_NEEDS_TRAMPOLINE
1820 vmg_trampoline_init(&cxt->reset_rmg, vmg_pp_reset_rmg);
1823 stash = gv_stashpv(XSH_PACKAGE, 1);
1824 newCONSTSUB(stash, "MGf_COPY", newSVuv(MGf_COPY));
1825 newCONSTSUB(stash, "MGf_DUP", newSVuv(MGf_DUP));
1826 newCONSTSUB(stash, "MGf_LOCAL", newSVuv(MGf_LOCAL));
1827 newCONSTSUB(stash, "VMG_UVAR", newSVuv(VMG_UVAR));
1828 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_LENGTH_NOLEN",
1829 newSVuv(VMG_COMPAT_SCALAR_LENGTH_NOLEN));
1830 newCONSTSUB(stash, "VMG_COMPAT_SCALAR_NOLEN",
1831 newSVuv(VMG_COMPAT_SCALAR_NOLEN));
1832 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN",
1833 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN));
1834 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID",
1835 newSVuv(VMG_COMPAT_ARRAY_PUSH_NOLEN_VOID));
1836 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID",
1837 newSVuv(VMG_COMPAT_ARRAY_UNSHIFT_NOLEN_VOID));
1838 newCONSTSUB(stash, "VMG_COMPAT_ARRAY_UNDEF_CLEAR",
1839 newSVuv(VMG_COMPAT_ARRAY_UNDEF_CLEAR));
1840 newCONSTSUB(stash, "VMG_COMPAT_HASH_DELETE_NOUVAR_VOID",
1841 newSVuv(VMG_COMPAT_HASH_DELETE_NOUVAR_VOID));
1842 newCONSTSUB(stash, "VMG_COMPAT_CODE_COPY_CLONE",
1843 newSVuv(VMG_COMPAT_CODE_COPY_CLONE));
1844 newCONSTSUB(stash, "VMG_COMPAT_GLOB_GET", newSVuv(VMG_COMPAT_GLOB_GET));
1845 newCONSTSUB(stash, "VMG_PERL_PATCHLEVEL", newSVuv(XSH_PERL_PATCHLEVEL));
1846 newCONSTSUB(stash, "VMG_THREADSAFE", newSVuv(XSH_THREADSAFE));
1847 newCONSTSUB(stash, "VMG_FORKSAFE", newSVuv(XSH_FORKSAFE));
1848 newCONSTSUB(stash, "VMG_OP_INFO_NAME", newSVuv(VMG_OP_INFO_NAME));
1849 newCONSTSUB(stash, "VMG_OP_INFO_OBJECT", newSVuv(VMG_OP_INFO_OBJECT));
1854 static void xsh_user_local_teardown(pTHX_ xsh_user_cxt_t *cxt) {
1855 if (cxt->depth == 0 && cxt->freed_tokens) {
1856 vmg_magic_chain_free(cxt->freed_tokens, NULL);
1857 cxt->freed_tokens = NULL;
1863 static void xsh_user_global_teardown(pTHX) {
1865 if (vmg_destruct_level() == 0)
1866 vmg_global_teardown_late_locked(NULL);
1868 xsh_teardown_late_register(vmg_global_teardown_late_locked, NULL);
1874 /* --- Macros for the XS section ------------------------------------------- */
1877 # define VMG_CVOK(C) \
1878 ((CvISXSUB(C) ? (void *) CvXSUB(C) : (void *) CvROOT(C)) ? 1 : 0)
1880 # define VMG_CVOK(C) (CvROOT(C) || CvXSUB(C))
1883 #define VMG_CBOK(S) ((SvTYPE(S) == SVt_PVCV) ? VMG_CVOK(S) : SvOK(S))
1885 #define VMG_SET_CB(S, N) { \
1887 if (SvOK(cb) && SvROK(cb)) { \
1890 SvREFCNT_inc_simple_void(cb); \
1899 #define VMG_SET_SVT_CB(S, N) { \
1901 if (SvOK(cb) && SvROK(cb)) { \
1903 if (VMG_CBOK(cb)) { \
1904 t->svt_ ## N = vmg_svt_ ## N; \
1905 SvREFCNT_inc_simple_void(cb); \
1907 t->svt_ ## N = vmg_svt_ ## N ## _noop; \
1911 t->svt_ ## N = NULL; \
1917 /* --- XS ------------------------------------------------------------------ */
1919 MODULE = Variable::Magic PACKAGE = Variable::Magic
1937 #endif /* XSH_THREADSAFE */
1944 SV *op_info, *copy_key;
1950 #endif /* MGf_LOCAL */
1953 #endif /* VMG_UVAR */
1954 ) { croak(vmg_wrongargnum); }
1957 w = vmg_wizard_alloc(SvOK(op_info) ? SvUV(op_info) : 0);
1958 t = vmg_vtable_vtbl(w->vtable);
1960 VMG_SET_CB(ST(i++), data);
1962 VMG_SET_SVT_CB(ST(i++), get);
1963 VMG_SET_SVT_CB(ST(i++), set);
1964 VMG_SET_SVT_CB(ST(i++), len);
1965 VMG_SET_SVT_CB(ST(i++), clear);
1966 VMG_SET_SVT_CB(ST(i++), free);
1967 VMG_SET_SVT_CB(ST(i++), copy);
1968 /* VMG_SET_SVT_CB(ST(i++), dup); */
1973 VMG_SET_SVT_CB(ST(i++), local);
1974 #endif /* MGf_LOCAL */
1976 VMG_SET_CB(ST(i++), fetch);
1977 VMG_SET_CB(ST(i++), store);
1978 VMG_SET_CB(ST(i++), exists);
1979 VMG_SET_CB(ST(i++), delete);
1982 if (w->cb_fetch || w->cb_store || w->cb_exists || w->cb_delete)
1983 w->uvar = SvTRUE(copy_key) ? 2 : 1;
1984 #endif /* VMG_UVAR */
1986 RETVAL = newRV_noinc(vmg_wizard_sv_new(w));
1990 SV *cast(SV *sv, SV *wiz, ...)
1991 PROTOTYPE: \[$@%&*]$@
1993 const vmg_wizard *w = NULL;
2002 wiz = SvRV_const(wiz);
2003 w = vmg_wizard_from_sv(wiz);
2006 croak(vmg_invalid_wiz);
2007 RETVAL = newSVuv(vmg_cast(SvRV(sv), w, wiz, args, i));
2012 getdata(SV *sv, SV *wiz)
2013 PROTOTYPE: \[$@%&*]$
2015 const vmg_wizard *w = NULL;
2019 w = vmg_wizard_from_sv(SvRV_const(wiz));
2021 croak(vmg_invalid_wiz);
2022 data = vmg_data_get(SvRV(sv), w);
2028 SV *dispell(SV *sv, SV *wiz)
2029 PROTOTYPE: \[$@%&*]$
2031 const vmg_wizard *w = NULL;
2034 w = vmg_wizard_from_sv(SvRV_const(wiz));
2036 croak(vmg_invalid_wiz);
2037 RETVAL = newSVuv(vmg_dispell(SvRV(sv), w));