]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - Magic.xs
Improve support for custom ops
[perl/modules/Variable-Magic.git] / Magic.xs
index f3ed0d6775754f061744351420ca66de67445011..69ad981a726d862427de4e88d96de34b3a689432 100644 (file)
--- a/Magic.xs
+++ b/Magic.xs
@@ -62,7 +62,9 @@
 #endif
 
 #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))
-# define VMG_THREADSAFE 1
+# ifndef VMG_THREADSAFE
+#  define VMG_THREADSAFE 1
+# endif
 # ifndef MY_CXT_CLONE
 #  define MY_CXT_CLONE \
     dMY_CXT_SV;                                                      \
@@ -71,6 +73,7 @@
     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
 # endif
 #else
+# undef  VMG_THREADSAFE
 # define VMG_THREADSAFE 0
 # undef  dMY_CXT
 # define dMY_CXT      dNOOP
 # define IN_PERL_COMPILETIME (PL_curcop == &PL_compiling)
 #endif
 
+#ifndef OP_NAME
+# define OP_NAME(O) (PL_op_name[(O)->op_type])
+#endif
+
+#ifndef OP_CLASS
+# define OP_CLASS(O) (PL_opargs[(O)->op_type] & OA_CLASS_MASK)
+#endif
+
+#ifdef DEBUGGING
+# define VMG_ASSERT(C) assert(C)
+#else
+# define VMG_ASSERT(C)
+#endif
+
 /* uvar magic and Hash::Util::FieldHash were commited with 28419, but we only
  * enable them on 5.10 */
 #if VMG_HAS_PERL(5, 10, 0)
@@ -382,6 +399,9 @@ typedef enum {
  OPc_COP,
 #if VMG_HAS_PERL(5, 21, 5)
  OPc_METHOP,
+#endif
+#if VMG_HAS_PERL(5, 21, 7)
+ OPc_UNOP_AUX,
 #endif
  OPc_MAX
 } opclass;
@@ -401,18 +421,27 @@ static const char *const vmg_opclassnames[] = {
  "B::COP",
 #if VMG_HAS_PERL(5, 21, 5)
  "B::METHOP",
+#endif
+#if VMG_HAS_PERL(5, 21, 7)
+ "B::UNOP_AUX",
 #endif
  NULL
 };
 
-static opclass vmg_opclass(const OP *o) {
+static opclass vmg_opclass(pTHX_ const OP *o) {
+#define vmg_opclass(O) vmg_opclass(aTHX_ (O))
 #if 0
  if (!o)
   return OPc_NULL;
 #endif
 
- if (o->op_type == 0)
+ if (o->op_type == 0) {
+#if VMG_HAS_PERL(5, 21, 7)
+  if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE)
+   return OPc_COP;
+#endif
   return (o->op_flags & OPf_KIDS) ? OPc_UNOP : OPc_BASEOP;
+ }
 
  if (o->op_type == OP_SASSIGN)
   return ((o->op_private & OPpASSIGN_BACKWARDS) ? OPc_UNOP : OPc_BINOP);
@@ -435,7 +464,7 @@ static opclass vmg_opclass(const OP *o) {
   return OPc_PADOP;
 #endif
 
- switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
+ switch (OP_CLASS(o)) {
   case OA_BASEOP:
    return OPc_BASEOP;
   case OA_UNOP:
@@ -453,7 +482,16 @@ static opclass vmg_opclass(const OP *o) {
   case OA_PADOP:
    return OPc_PADOP;
   case OA_PVOP_OR_SVOP:
-   return (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)) ? OPc_SVOP : OPc_PVOP;
+   return (
+#if VMG_HAS_PERL(5, 13, 7)
+           (o->op_type != OP_CUSTOM) &&
+#endif
+           (o->op_private & (OPpTRANS_TO_UTF|OPpTRANS_FROM_UTF)))
+#if defined(USE_ITHREADS) && VMG_HAS_PERL(5, 8, 9)
+           ? OPc_PADOP : OPc_PVOP;
+#else
+           ? OPc_SVOP : OPc_PVOP;
+#endif
   case OA_LOOP:
    return OPc_LOOP;
   case OA_COP:
@@ -477,6 +515,10 @@ static opclass vmg_opclass(const OP *o) {
 #if VMG_HAS_PERL(5, 21, 5)
   case OA_METHOP:
    return OPc_METHOP;
+#endif
+#if VMG_HAS_PERL(5, 21, 7)
+  case OA_UNOP_AUX:
+   return OPc_UNOP_AUX;
 #endif
  }
 
@@ -485,9 +527,8 @@ static opclass vmg_opclass(const OP *o) {
 
 /* --- Error messages ------------------------------------------------------ */
 
-static const char vmg_invalid_wiz[]    = "Invalid wizard object";
-static const char vmg_wrongargnum[]    = "Wrong number of arguments";
-static const char vmg_argstorefailed[] = "Error while storing arguments";
+static const char vmg_invalid_wiz[] = "Invalid wizard object";
+static const char vmg_wrongargnum[] = "Wrong number of arguments";
 
 /* --- Context-safe global data -------------------------------------------- */
 
@@ -1115,8 +1156,12 @@ static SV *vmg_op_info(pTHX_ unsigned int opinfo) {
 
  switch (opinfo) {
   case VMG_OP_INFO_NAME: {
-   OPCODE t = PL_op->op_type;
-   return sv_2mortal(newSVpvn(PL_op_name[t], vmg_op_name_len[t]));
+   const char *name;
+   STRLEN      name_len;
+   OPCODE      t = PL_op->op_type;
+   name     = OP_NAME(PL_op);
+   name_len = (t == OP_CUSTOM) ? strlen(name) : vmg_op_name_len[t];
+   return sv_2mortal(newSVpvn(name, name_len));
   }
   case VMG_OP_INFO_OBJECT: {
    dMY_CXT;
@@ -1243,11 +1288,18 @@ static int vmg_cb_call(pTHX_ SV *cb, unsigned int flags, SV *sv, ...) {
  svr = POPs;
  if (SvOK(svr))
   ret = (int) SvIV(svr);
+ if (SvROK(svr))
+  SvREFCNT_inc(svr);
+ else
+  svr = NULL;
  PUTBACK;
 
  FREETMPS;
  LEAVE;
 
+ if (svr && !SvTEMP(svr))
+  sv_2mortal(svr);
+
  if (chain) {
   vmg_dispell_guard_new(*chain);
   *chain = NULL;
@@ -1766,13 +1818,15 @@ static void vmg_global_teardown_late_locked(pTHX) {
  MUTEX_DESTROY(&vmg_op_name_init_mutex);
  MUTEX_DESTROY(&vmg_vtable_refcount_mutex);
 
- vmg_loaded = 0;
-
  return;
 }
 
 static int vmg_global_teardown_free(pTHX_ SV *sv, MAGIC *mg) {
- vmg_global_teardown_late_locked();
+ VMG_LOADED_LOCK;
+
+ if (vmg_loaded == 0)
+  vmg_global_teardown_late_locked();
+
  VMG_LOADED_UNLOCK;
 
  return 0;
@@ -1834,32 +1888,27 @@ static signed char vmg_destruct_level(pTHX) {
 
 #endif /* VMG_THREADSAFE */
 
-static void vmg_teardown(pTHX_ void *interp) {
+static void vmg_teardown(pTHX_ void *param) {
  dMY_CXT;
 
-#if VMG_MULTIPLICITY
- if (aTHX != interp)
-  return;
-#endif
-
 #if VMG_THREADSAFE
  VMG_LOADED_LOCK;
 
- if (vmg_loaded <= 1) {
-  assert(vmg_loaded == 1);
+ if (vmg_loaded == 1) {
+  vmg_loaded = 0;
   if (vmg_destruct_level() == 0) {
    vmg_global_teardown_late_locked();
-   VMG_LOADED_UNLOCK;
   } else {
    if (!PL_strtab)
     PL_strtab = newHV();
    vmg_sv_magicext((SV *) PL_strtab, NULL, &vmg_global_teardown_vtbl, NULL, 0);
-   /* Lock until vmg_global_teardown_free() is called */
   }
  } else {
+  VMG_ASSERT(vmg_loaded > 1);
   --vmg_loaded;
-  VMG_LOADED_UNLOCK;
  }
+
+ VMG_LOADED_UNLOCK;
 #endif
 
  if (MY_CXT.depth == 0 && MY_CXT.freed_tokens) {
@@ -1879,12 +1928,14 @@ static void vmg_setup(pTHX) {
 #if VMG_THREADSAFE
  VMG_LOADED_LOCK;
 
- if (vmg_loaded <= 0) {
-  assert(vmg_loaded == 0);
+ if (vmg_loaded == 0) {
   MUTEX_INIT(&vmg_vtable_refcount_mutex);
   MUTEX_INIT(&vmg_op_name_init_mutex);
+  vmg_loaded = 1;
+ } else {
+  VMG_ASSERT(vmg_loaded > 0);
+  ++vmg_loaded;
  }
- ++vmg_loaded;
 
  VMG_LOADED_UNLOCK;
 #endif
@@ -1930,11 +1981,7 @@ static void vmg_setup(pTHX) {
  newCONSTSUB(stash, "VMG_OP_INFO_NAME",    newSVuv(VMG_OP_INFO_NAME));
  newCONSTSUB(stash, "VMG_OP_INFO_OBJECT",  newSVuv(VMG_OP_INFO_OBJECT));
 
-#if VMG_MULTIPLICITY
- call_atexit(vmg_teardown, aTHX);
-#else
  call_atexit(vmg_teardown, NULL);
-#endif
 
  return;
 }
@@ -2020,7 +2067,7 @@ PPCODE:
   MY_CXT.depth        = old_depth;
   MY_CXT.freed_tokens = NULL;
   VMG_LOADED_LOCK;
-  assert(vmg_loaded > 0);
+  VMG_ASSERT(vmg_loaded > 0);
   ++vmg_loaded;
   VMG_LOADED_UNLOCK;
  }