This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cast markstack values to I32
[perl5.git] / XSUB.h
diff --git a/XSUB.h b/XSUB.h
index af42f9c..bff12e3 100644 (file)
--- a/XSUB.h
+++ b/XSUB.h
@@ -1,7 +1,7 @@
 /*    XSUB.h
  *
- *    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, by Larry Wall and others
+ *    Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
+ *    2003, 2004, 2005, 2006, 2007 by Larry Wall and others
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -54,6 +54,10 @@ C<xsubpp>.
 Sets up the C<ax> variable.
 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
 
+=for apidoc Ams||dAXMARK
+Sets up the C<ax> variable and stack marker variable C<mark>.
+This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
+
 =for apidoc Ams||dITEMS
 Sets up the C<items> variable.
 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
@@ -67,26 +71,68 @@ This is usually handled automatically by C<xsubpp>.
 Sets up the C<ix> variable for an XSUB which has aliases.  This is usually
 handled automatically by C<xsubpp>.
 
+=for apidoc Ams||dUNDERBAR
+Sets up the C<padoff_du> variable for an XSUB that wishes to use
+C<UNDERBAR>.
+
+=for apidoc AmU||UNDERBAR
+The SV* corresponding to the $_ variable. Works even if there
+is a lexical $_ in scope.
+
 =cut
 */
 
+#ifndef PERL_UNUSED_ARG
+#  if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
+#    include <note.h>
+#    define PERL_UNUSED_ARG(x) NOTE(ARGUNUSED(x))
+#  else
+#    define PERL_UNUSED_ARG(x) ((void)x)
+#  endif
+#endif
+#ifndef PERL_UNUSED_VAR
+#  define PERL_UNUSED_VAR(x) ((void)x)
+#endif
+
 #define ST(off) PL_stack_base[ax + (off)]
 
+#undef XS
 #if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
 #  define XS(name) __declspec(dllexport) void name(pTHX_ CV* cv)
-#else
-#  define XS(name) void name(pTHX_ CV* cv)
+#endif
+#if defined(__SYMBIAN32__)
+#  define XS(name) EXPORT_C void name(pTHX_ CV* cv)
+#endif
+#ifndef XS
+#  if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
+#    define XS(name) void name(pTHX_ CV* cv __attribute__unused__)
+#  else
+#    ifdef __cplusplus
+#      define XS(name) extern "C" void name(pTHX_ CV* cv)
+#    else
+#      define XS(name) void name(pTHX_ CV* cv)
+#    endif
+#  endif
 #endif
 
-#define dAX I32 ax = MARK - PL_stack_base + 1
+#define dAX const I32 ax = (I32)(MARK - PL_stack_base + 1)
 
-#define dITEMS I32 items = SP - MARK
+#define dAXMARK                                \
+       I32 ax = POPMARK;       \
+       register SV **mark = PL_stack_base + ax++
 
-#define dXSARGS                                \
-       dSP; dMARK;                     \
-       dAX; dITEMS
+#define dITEMS I32 items = (I32)(SP - MARK)
+
+#if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
+#  define dXSARGS \
+       NOTE(ARGUNUSED(cv)) \
+       dSP; dAXMARK; dITEMS
+#else
+#  define dXSARGS \
+       dSP; dAXMARK; dITEMS
+#endif
 
-#define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
+#define dXSTARG SV * const targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
                             ? PAD_SV(PL_op->op_targ) : sv_newmortal())
 
 /* Should be used before final PUSHi etc. if not in PPCODE section. */
@@ -106,6 +152,11 @@ handled automatically by C<xsubpp>.
 #define XSINTERFACE_FUNC_SET(cv,f)     \
                CvXSUBANY(cv).any_dxptr = (void (*) (pTHX_ void*))(f)
 
+#define dUNDERBAR PADOFFSET padoff_du = find_rundefsvoffset()
+#define UNDERBAR ((padoff_du == NOT_IN_PAD \
+           || PAD_COMPNAME_FLAGS_isOUR(padoff_du)) \
+       ? DEFSV : PAD_SVl(padoff_du))
+
 /* Simple macros to put new mortal values onto the stack.   */
 /* Typically used to return values from XS functions.       */
 
@@ -166,7 +217,7 @@ Return an empty list from an XSUB immediately.
 
 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
 
-=for apidoc AmU||newXSproto
+=for apidoc AmU||newXSproto|char* name|XSUBADDR_t f|char* filename|const char *proto
 Used by C<xsubpp> to hook up XSUBs as Perl subs.  Adds Perl prototypes to
 the subs.
 
@@ -179,6 +230,24 @@ Macro to verify that a PM module's $VERSION variable matches the XS
 module's C<XS_VERSION> variable.  This is usually handled automatically by
 C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
 
+=head1 Simple Exception Handling Macros
+
+=for apidoc Ams||dXCPT
+Set up necessary local variables for exception handling.
+See L<perlguts/"Exception Handling">.
+
+=for apidoc AmU||XCPT_TRY_START
+Starts a try block.  See L<perlguts/"Exception Handling">.
+
+=for apidoc AmU||XCPT_TRY_END
+Ends a try block.  See L<perlguts/"Exception Handling">.
+
+=for apidoc AmU||XCPT_CATCH
+Introduces a catch block.  See L<perlguts/"Exception Handling">.
+
+=for apidoc Ams||XCPT_RETHROW
+Rethrows a previously caught exception.  See L<perlguts/"Exception Handling">.
+
 =cut
 */
 
@@ -208,13 +277,13 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
 #define XSRETURN_UNDEF STMT_START { XST_mUNDEF(0); XSRETURN(1); } STMT_END
 #define XSRETURN_EMPTY STMT_START {                XSRETURN(0); } STMT_END
 
-#define newXSproto(a,b,c,d)    sv_setpv((SV*)newXS(a,b,c), d)
+#define newXSproto(a,b,c,d)    newXS_flags(a,b,c,d,0)
 
 #ifdef XS_VERSION
 #  define XS_VERSION_BOOTCHECK \
     STMT_START {                                                       \
-       SV *_sv; STRLEN n_a;                                            \
-       char *vn = Nullch, *module = SvPV(ST(0),n_a);                   \
+       SV *_sv;                                                        \
+       const char *vn = NULL, *module = SvPV_nolen_const(ST(0));       \
        if (items >= 2)  /* version supplied as bootstrap arg */        \
            _sv = ST(1);                                                \
        else {                                                          \
@@ -225,23 +294,37 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
                _sv = get_sv(Perl_form(aTHX_ "%s::%s", module,  \
                                    vn = "VERSION"), FALSE);            \
        }                                                               \
-       if (_sv && (!SvOK(_sv) || strNE(XS_VERSION, SvPV(_sv, n_a))))   \
-           Perl_croak(aTHX_ "%s object version %s does not match %s%s%s%s %"SVf,\
-                 module, XS_VERSION,                                   \
-                 vn ? "$" : "", vn ? module : "", vn ? "::" : "",      \
-                 vn ? vn : "bootstrap parameter", _sv);                \
+       if (_sv) {                                                      \
+           SV *xssv = Perl_newSVpv(aTHX_ XS_VERSION, 0);               \
+           xssv = new_version(xssv);                                   \
+           if ( !sv_derived_from(_sv, "version") )                     \
+               _sv = new_version(_sv);                         \
+           if ( vcmp(_sv,xssv) )                                       \
+               Perl_croak(aTHX_ "%s object version %"SVf" does not match %s%s%s%s %"SVf,\
+                     module, SVfARG(vstringify(xssv)),                 \
+                     vn ? "$" : "", vn ? module : "", vn ? "::" : "",  \
+                     vn ? vn : "bootstrap parameter", SVfARG(vstringify(_sv)));\
+       }                                                               \
     } STMT_END
 #else
 #  define XS_VERSION_BOOTCHECK
 #endif
 
+#ifdef NO_XSLOCKS
+#  define dXCPT             dJMPENV; int rEtV = 0
+#  define XCPT_TRY_START    JMPENV_PUSH(rEtV); if (rEtV == 0)
+#  define XCPT_TRY_END      JMPENV_POP;
+#  define XCPT_CATCH        if (rEtV != 0)
+#  define XCPT_RETHROW      JMPENV_JUMP(rEtV)
+#endif
+
 /* 
    The DBM_setFilter & DBM_ckFilter macros are only used by 
    the *DB*_File modules 
 */
 
 #define DBM_setFilter(db_type,code)                            \
-       {                                                       \
+       STMT_START {                                            \
            if (db_type)                                        \
                RETVAL = sv_mortalcopy(db_type) ;               \
            ST(0) = RETVAL ;                                    \
@@ -255,9 +338,10 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
                else                                            \
                    db_type = newSVsv(code) ;                   \
            }                                                   \
-       }
+       } STMT_END
 
 #define DBM_ckFilter(arg,type,name)                            \
+        STMT_START {                                           \
        if (db->type) {                                         \
            if (db->filtering) {                                \
                croak("recursion detected in %s", name) ;       \
@@ -267,6 +351,8 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
            SAVEINT(db->filtering) ;                            \
            db->filtering = TRUE ;                              \
            SAVESPTR(DEFSV) ;                                   \
+            if (name[7] == 's')                                 \
+                arg = newSVsv(arg);                             \
            DEFSV = arg ;                                       \
            SvTEMP_off(arg) ;                                   \
            PUSHMARK(SP) ;                                      \
@@ -276,7 +362,10 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
            PUTBACK ;                                           \
            FREETMPS ;                                          \
            LEAVE ;                                             \
-       }
+            if (name[7] == 's'){                                \
+                arg = sv_2mortal(arg);                          \
+            }                                                   \
+       } } STMT_END                                                     
 
 #if 1          /* for compatibility */
 #  define VTBL_sv              &PL_vtbl_sv
@@ -312,6 +401,12 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
 #endif
 
 #include "perlapi.h"
+#ifndef PERL_MAD
+#  undef PL_madskills
+#  undef PL_xmlfp
+#  define PL_madskills 0
+#  define PL_xmlfp 0
+#endif
 
 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_GET_CONTEXT) && !defined(PERL_CORE)
 #  undef aTHX
@@ -534,3 +629,13 @@ C<xsubpp>.  See L<perlxs/"The VERSIONCHECK: Keyword">.
 #endif  /* PERL_IMPLICIT_SYS && !PERL_CORE */
 
 #endif /* _INC_PERL_XSUB_H */          /* include guard */
+
+/*
+ * Local variables:
+ * c-indentation-style: bsd
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set ts=8 sts=4 sw=4 noet:
+ */