This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
DProf tweak
[perl5.git] / scope.h
diff --git a/scope.h b/scope.h
index aa865bf..efaf589 100644 (file)
--- a/scope.h
+++ b/scope.h
 #define ENTER                                                  \
     STMT_START {                                               \
        push_scope();                                           \
-       DEBUG_l(WITH_THR(deb("ENTER scope %ld at %s:%d\n",      \
+       DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n",   \
                    PL_scopestack_ix, __FILE__, __LINE__)));    \
     } STMT_END
 #define LEAVE                                                  \
     STMT_START {                                               \
-       DEBUG_l(WITH_THR(deb("LEAVE scope %ld at %s:%d\n",      \
+       DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n",   \
                    PL_scopestack_ix, __FILE__, __LINE__)));    \
        pop_scope();                                            \
     } STMT_END
 #define SAVEGENERICSV(s)       save_generic_svref((SV**)&(s))
 #define SAVEDELETE(h,k,l) \
          save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
-#ifdef PERL_OBJECT
-#define CALLDESTRUCTOR this->*SSPOPDPTR
+#define CALLDESTRUCTOR (*SSPOPDPTR)
 #define SAVEDESTRUCTOR(f,p) \
-         save_destructor((DESTRUCTORFUNC)(FUNC_NAME_TO_PTR(f)),        \
-                         SOFT_CAST(void*)(p))
-#else
-#define CALLDESTRUCTOR *SSPOPDPTR
-#define SAVEDESTRUCTOR(f,p) \
-         save_destructor(SOFT_CAST(void(*)_((void*)))(FUNC_NAME_TO_PTR(f)), \
-                         SOFT_CAST(void*)(p))
-#endif
+         save_destructor((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
 
 #define SAVESTACK_POS() \
     STMT_START {                               \
 
 struct jmpenv {
     struct jmpenv *    je_prev;
-    Sigjmp_buf         je_buf;         
-    int                        je_ret;         /* return value of last setjmp() */
-    bool               je_mustcatch;   /* longjmp()s must be caught locally */
+    Sigjmp_buf         je_buf;         /* only for use if !je_throw */
+    int                        je_ret;         /* last exception thrown */
+    bool               je_mustcatch;   /* need to call longjmp()? */
+    void               (*je_throw)(int v); /* last for bincompat */
 };
 
 typedef struct jmpenv JMPENV;
 
+/*
+ * Function that catches/throws, and its callback for the
+ *  body of protected processing.
+ */
+typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
+typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ int *, protect_body_t, ...);
+
+/*
+ * How to build the first jmpenv.
+ *
+ * top_env needs to be non-zero. It points to an area
+ * in which longjmp() stuff is stored, as C callstack
+ * info there at least is thread specific this has to
+ * be per-thread. Otherwise a 'die' in a thread gives
+ * that thread the C stack of last thread to do an eval {}!
+ */
+
+#define JMPENV_BOOTSTRAP \
+    STMT_START {                               \
+       PL_start_env.je_prev = NULL;            \
+       PL_start_env.je_throw = NULL;           \
+       PL_start_env.je_ret = -1;               \
+       PL_start_env.je_mustcatch = TRUE;       \
+       PL_top_env = &PL_start_env;             \
+    } STMT_END
+
 #ifdef OP_IN_REGISTER
 #define OP_REG_TO_MEM  PL_opsave = op
 #define OP_MEM_TO_REG  op = PL_opsave
@@ -162,30 +181,94 @@ typedef struct jmpenv JMPENV;
 #define OP_MEM_TO_REG  NOOP
 #endif
 
+/*
+ * These exception-handling macros are split up to
+ * ease integration with C++ exceptions.
+ *
+ * To use C++ try+catch to catch Perl exceptions, an extension author
+ * needs to first write an extern "C" function to throw an appropriate
+ * exception object; typically it will be or contain an integer,
+ * because Perl's internals use integers to track exception types:
+ *    extern "C" { static void thrower(int i) { throw i; } }
+ *
+ * Then (as shown below) the author needs to use, not the simple
+ * JMPENV_PUSH, but several of its constitutent macros, to arrange for
+ * the Perl internals to call thrower() rather than longjmp() to
+ * report exceptions:
+ *
+ *    dJMPENV;
+ *    JMPENV_PUSH_INIT(thrower);
+ *    try {
+ *        ... stuff that may throw exceptions ...
+ *    }
+ *    catch (int why) {  // or whatever matches thrower()
+ *        JMPENV_POST_CATCH;
+ *        EXCEPT_SET(why);
+ *        switch (why) {
+ *          ... // handle various Perl exception codes
+ *        }
+ *    }
+ *    JMPENV_POP;  // don't forget this!
+ */
+
 #define dJMPENV                JMPENV cur_env
-#define JMPENV_PUSH(v) \
+
+#define JMPENV_PUSH_INIT_ENV(cur_env,THROWFUNC) \
     STMT_START {                                       \
+       cur_env.je_throw = (THROWFUNC);                 \
+       cur_env.je_ret = -1;                            \
+       cur_env.je_mustcatch = FALSE;                   \
        cur_env.je_prev = PL_top_env;                   \
+       PL_top_env = &cur_env;                          \
        OP_REG_TO_MEM;                                  \
-       cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, 1);    \
+    } STMT_END
+
+#define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(cur_env,THROWFUNC) 
+
+#define JMPENV_POST_CATCH_ENV(cur_env) \
+    STMT_START {                                       \
        OP_MEM_TO_REG;                                  \
        PL_top_env = &cur_env;                          \
-       cur_env.je_mustcatch = FALSE;                   \
-       (v) = cur_env.je_ret;                           \
     } STMT_END
-#define JMPENV_POP \
+
+#define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(cur_env)
+
+
+#define JMPENV_PUSH_ENV(cur_env,v) \
+    STMT_START {                                       \
+       JMPENV_PUSH_INIT_ENV(cur_env,NULL);                             \
+       EXCEPT_SET_ENV(cur_env,PerlProc_setjmp(cur_env.je_buf, 1));     \
+       JMPENV_POST_CATCH_ENV(cur_env);                         \
+       (v) = EXCEPT_GET_ENV(cur_env);                          \
+    } STMT_END
+
+#define JMPENV_PUSH(v) JMPENV_PUSH_ENV(cur_env,v) 
+
+#define JMPENV_POP_ENV(cur_env) \
     STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
+
+#define JMPENV_POP  JMPENV_POP_ENV(cur_env) 
+
 #define JMPENV_JUMP(v) \
     STMT_START {                                               \
        OP_REG_TO_MEM;                                          \
-       if (PL_top_env->je_prev)                                        \
-           PerlProc_longjmp(PL_top_env->je_buf, (v));                  \
+       if (PL_top_env->je_prev) {                              \
+           if (PL_top_env->je_throw)                           \
+               PL_top_env->je_throw(v);                        \
+           else                                                \
+               PerlProc_longjmp(PL_top_env->je_buf, (v));      \
+       }                                                       \
        if ((v) == 2)                                           \
-           PerlProc_exit(STATUS_NATIVE_EXPORT);                                \
+           PerlProc_exit(STATUS_NATIVE_EXPORT);                \
        PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");     \
-       PerlProc_exit(1);                                               \
+       PerlProc_exit(1);                                       \
     } STMT_END
-   
+
+#define EXCEPT_GET_ENV(cur_env)        (cur_env.je_ret)
+#define EXCEPT_GET EXCEPT_GET_ENV(cur_env)
+#define EXCEPT_SET_ENV(cur_env,v)      (cur_env.je_ret = (v))
+#define EXCEPT_SET(v) EXCEPT_SET_ENV(cur_env,v)
+
 #define CATCH_GET      (PL_top_env->je_mustcatch)
 #define CATCH_SET(v)   (PL_top_env->je_mustcatch = (v))