3 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 * 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 #ifndef _INC_PERL_XSUB_H
12 #define _INC_PERL_XSUB_H 1
14 /* first, some documentation for xsubpp-generated items */
17 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
19 =for apidoc Amn|char*|CLASS
20 Variable which is setup by C<xsubpp> to indicate the
21 class name for a C++ XS constructor. This is always a C<char*>. See C<THIS>.
23 =for apidoc Amn|(whatever)|RETVAL
24 Variable which is setup by C<xsubpp> to hold the return value for an
25 XSUB. This is always the proper type for the XSUB. See
26 L<perlxs/"The RETVAL Variable">.
28 =for apidoc Amn|(whatever)|THIS
29 Variable which is setup by C<xsubpp> to designate the object in a C++
30 XSUB. This is always the proper type for the C++ object. See C<CLASS> and
31 L<perlxs/"Using XS With C++">.
33 =for apidoc Amn|I32|ax
34 Variable which is setup by C<xsubpp> to indicate the stack base offset,
35 used by the C<ST>, C<XSprePUSH> and C<XSRETURN> macros. The C<dMARK> macro
36 must be called prior to setup the C<MARK> variable.
38 =for apidoc Amn|I32|items
39 Variable which is setup by C<xsubpp> to indicate the number of
40 items on the stack. See L<perlxs/"Variable-length Parameter Lists">.
42 =for apidoc Amn|I32|ix
43 Variable which is setup by C<xsubpp> to indicate which of an
44 XSUB's aliases was used to invoke it. See L<perlxs/"The ALIAS: Keyword">.
46 =for apidoc Am|SV*|ST|int ix
47 Used to access elements on the XSUB's stack.
50 Macro to declare an XSUB and its C parameter list. This is handled by
54 Sets up the C<ax> variable.
55 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
57 =for apidoc Ams||dAXMARK
58 Sets up the C<ax> variable and stack marker variable C<mark>.
59 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
61 =for apidoc Ams||dITEMS
62 Sets up the C<items> variable.
63 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
65 =for apidoc Ams||dXSARGS
66 Sets up stack and mark pointers for an XSUB, calling dSP and dMARK.
67 Sets up the C<ax> and C<items> variables by calling C<dAX> and C<dITEMS>.
68 This is usually handled automatically by C<xsubpp>.
70 =for apidoc Ams||dXSI32
71 Sets up the C<ix> variable for an XSUB which has aliases. This is usually
72 handled automatically by C<xsubpp>.
74 =for apidoc Ams||dUNDERBAR
75 Sets up the C<padoff_du> variable for an XSUB that wishes to use
78 =for apidoc AmU||UNDERBAR
79 The SV* corresponding to the $_ variable. Works even if there
80 is a lexical $_ in scope.
85 #ifndef PERL_UNUSED_ARG
86 # if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
88 # define PERL_UNUSED_ARG(x) NOTE(ARGUNUSED(x))
90 # define PERL_UNUSED_ARG(x) ((void)x)
93 #ifndef PERL_UNUSED_VAR
94 # define PERL_UNUSED_VAR(x) ((void)x)
97 #define ST(off) PL_stack_base[ax + (off)]
99 /* XSPROTO() is also used by SWIG like this:
101 * typedef XSPROTO(SwigPerlWrapper);
102 * typedef SwigPerlWrapper *SwigPerlWrapperPtr;
104 * This code needs to be compilable under both C and C++.
106 * Don't forget to change the __attribute__unused__ version of XS()
107 * below too if you change XSPROTO() here.
109 #define XSPROTO(name) void name(pTHX_ CV* cv)
112 #if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
113 # define XS(name) __declspec(dllexport) XSPROTO(name)
115 #if defined(__SYMBIAN32__)
116 # define XS(name) EXPORT_C XSPROTO(name)
119 # if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
120 # define XS(name) void name(pTHX_ CV* cv __attribute__unused__)
123 # define XS(name) extern "C" XSPROTO(name)
125 # define XS(name) XSPROTO(name)
130 #define dAX const I32 ax = (I32)(MARK - PL_stack_base + 1)
134 register SV **mark = PL_stack_base + ax++
136 #define dITEMS I32 items = (I32)(SP - MARK)
138 #if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
140 NOTE(ARGUNUSED(cv)) \
147 #define dXSTARG SV * const targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
148 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
150 /* Should be used before final PUSHi etc. if not in PPCODE section. */
151 #define XSprePUSH (sp = PL_stack_base + ax - 1)
153 #define XSANY CvXSUBANY(cv)
155 #define dXSI32 I32 ix = XSANY.any_i32
158 # define XSINTERFACE_CVT(ret,name) ret (*name)(...)
159 # define XSINTERFACE_CVT_ANON(ret) ret (*)(...)
161 # define XSINTERFACE_CVT(ret,name) ret (*name)()
162 # define XSINTERFACE_CVT_ANON(ret) ret (*)()
164 #define dXSFUNCTION(ret) XSINTERFACE_CVT(ret,XSFUNCTION)
165 #define XSINTERFACE_FUNC(ret,cv,f) ((XSINTERFACE_CVT_ANON(ret))(f))
166 #define XSINTERFACE_FUNC_SET(cv,f) \
167 CvXSUBANY(cv).any_dxptr = (void (*) (pTHX_ void*))(f)
169 #define dUNDERBAR PADOFFSET padoff_du = find_rundefsvoffset()
170 #define UNDERBAR ((padoff_du == NOT_IN_PAD \
171 || PAD_COMPNAME_FLAGS_isOUR(padoff_du)) \
172 ? DEFSV : PAD_SVl(padoff_du))
174 /* Simple macros to put new mortal values onto the stack. */
175 /* Typically used to return values from XS functions. */
178 =head1 Stack Manipulation Macros
180 =for apidoc Am|void|XST_mIV|int pos|IV iv
181 Place an integer into the specified position C<pos> on the stack. The
182 value is stored in a new mortal SV.
184 =for apidoc Am|void|XST_mNV|int pos|NV nv
185 Place a double into the specified position C<pos> on the stack. The value
186 is stored in a new mortal SV.
188 =for apidoc Am|void|XST_mPV|int pos|char* str
189 Place a copy of a string into the specified position C<pos> on the stack.
190 The value is stored in a new mortal SV.
192 =for apidoc Am|void|XST_mNO|int pos
193 Place C<&PL_sv_no> into the specified position C<pos> on the
196 =for apidoc Am|void|XST_mYES|int pos
197 Place C<&PL_sv_yes> into the specified position C<pos> on the
200 =for apidoc Am|void|XST_mUNDEF|int pos
201 Place C<&PL_sv_undef> into the specified position C<pos> on the
204 =for apidoc Am|void|XSRETURN|int nitems
205 Return from XSUB, indicating number of items on the stack. This is usually
206 handled by C<xsubpp>.
208 =for apidoc Am|void|XSRETURN_IV|IV iv
209 Return an integer from an XSUB immediately. Uses C<XST_mIV>.
211 =for apidoc Am|void|XSRETURN_UV|IV uv
212 Return an integer from an XSUB immediately. Uses C<XST_mUV>.
214 =for apidoc Am|void|XSRETURN_NV|NV nv
215 Return a double from an XSUB immediately. Uses C<XST_mNV>.
217 =for apidoc Am|void|XSRETURN_PV|char* str
218 Return a copy of a string from an XSUB immediately. Uses C<XST_mPV>.
220 =for apidoc Ams||XSRETURN_NO
221 Return C<&PL_sv_no> from an XSUB immediately. Uses C<XST_mNO>.
223 =for apidoc Ams||XSRETURN_YES
224 Return C<&PL_sv_yes> from an XSUB immediately. Uses C<XST_mYES>.
226 =for apidoc Ams||XSRETURN_UNDEF
227 Return C<&PL_sv_undef> from an XSUB immediately. Uses C<XST_mUNDEF>.
229 =for apidoc Ams||XSRETURN_EMPTY
230 Return an empty list from an XSUB immediately.
232 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
234 =for apidoc AmU||newXSproto|char* name|XSUBADDR_t f|char* filename|const char *proto
235 Used by C<xsubpp> to hook up XSUBs as Perl subs. Adds Perl prototypes to
238 =for apidoc AmU||XS_VERSION
239 The version identifier for an XS module. This is usually
240 handled automatically by C<ExtUtils::MakeMaker>. See C<XS_VERSION_BOOTCHECK>.
242 =for apidoc Ams||XS_VERSION_BOOTCHECK
243 Macro to verify that a PM module's $VERSION variable matches the XS
244 module's C<XS_VERSION> variable. This is usually handled automatically by
245 C<xsubpp>. See L<perlxs/"The VERSIONCHECK: Keyword">.
247 =head1 Simple Exception Handling Macros
249 =for apidoc Ams||dXCPT
250 Set up necessary local variables for exception handling.
251 See L<perlguts/"Exception Handling">.
253 =for apidoc AmU||XCPT_TRY_START
254 Starts a try block. See L<perlguts/"Exception Handling">.
256 =for apidoc AmU||XCPT_TRY_END
257 Ends a try block. See L<perlguts/"Exception Handling">.
259 =for apidoc AmU||XCPT_CATCH
260 Introduces a catch block. See L<perlguts/"Exception Handling">.
262 =for apidoc Ams||XCPT_RETHROW
263 Rethrows a previously caught exception. See L<perlguts/"Exception Handling">.
268 #define XST_mIV(i,v) (ST(i) = sv_2mortal(newSViv(v)) )
269 #define XST_mUV(i,v) (ST(i) = sv_2mortal(newSVuv(v)) )
270 #define XST_mNV(i,v) (ST(i) = sv_2mortal(newSVnv(v)) )
271 #define XST_mPV(i,v) (ST(i) = sv_2mortal(newSVpv(v,0)))
272 #define XST_mPVN(i,v,n) (ST(i) = newSVpvn_flags(v,n, SVs_TEMP))
273 #define XST_mNO(i) (ST(i) = &PL_sv_no )
274 #define XST_mYES(i) (ST(i) = &PL_sv_yes )
275 #define XST_mUNDEF(i) (ST(i) = &PL_sv_undef)
277 #define XSRETURN(off) \
279 const IV tmpXSoff = (off); \
280 PL_stack_sp = PL_stack_base + ax + (tmpXSoff - 1); \
284 #define XSRETURN_IV(v) STMT_START { XST_mIV(0,v); XSRETURN(1); } STMT_END
285 #define XSRETURN_UV(v) STMT_START { XST_mUV(0,v); XSRETURN(1); } STMT_END
286 #define XSRETURN_NV(v) STMT_START { XST_mNV(0,v); XSRETURN(1); } STMT_END
287 #define XSRETURN_PV(v) STMT_START { XST_mPV(0,v); XSRETURN(1); } STMT_END
288 #define XSRETURN_PVN(v,n) STMT_START { XST_mPVN(0,v,n); XSRETURN(1); } STMT_END
289 #define XSRETURN_NO STMT_START { XST_mNO(0); XSRETURN(1); } STMT_END
290 #define XSRETURN_YES STMT_START { XST_mYES(0); XSRETURN(1); } STMT_END
291 #define XSRETURN_UNDEF STMT_START { XST_mUNDEF(0); XSRETURN(1); } STMT_END
292 #define XSRETURN_EMPTY STMT_START { XSRETURN(0); } STMT_END
294 #define newXSproto(a,b,c,d) newXS_flags(a,b,c,d,0)
297 # define XS_VERSION_BOOTCHECK \
300 const char *vn = NULL, *module = SvPV_nolen_const(ST(0)); \
301 if (items >= 2) /* version supplied as bootstrap arg */ \
304 /* XXX GV_ADDWARN */ \
305 _sv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
306 vn = "XS_VERSION"), FALSE); \
307 if (!_sv || !SvOK(_sv)) \
308 _sv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
309 vn = "VERSION"), FALSE); \
312 SV *xssv = Perl_newSVpv(aTHX_ XS_VERSION, 0); \
313 xssv = new_version(xssv); \
314 if ( !sv_derived_from(_sv, "version") ) \
315 _sv = new_version(_sv); \
316 if ( vcmp(_sv,xssv) ) \
317 Perl_croak(aTHX_ "%s object version %"SVf" does not match %s%s%s%s %"SVf,\
318 module, SVfARG(vstringify(xssv)), \
319 vn ? "$" : "", vn ? module : "", vn ? "::" : "", \
320 vn ? vn : "bootstrap parameter", SVfARG(vstringify(_sv)));\
324 # define XS_VERSION_BOOTCHECK
328 # define dXCPT dJMPENV; int rEtV = 0
329 # define XCPT_TRY_START JMPENV_PUSH(rEtV); if (rEtV == 0)
330 # define XCPT_TRY_END JMPENV_POP;
331 # define XCPT_CATCH if (rEtV != 0)
332 # define XCPT_RETHROW JMPENV_JUMP(rEtV)
336 The DBM_setFilter & DBM_ckFilter macros are only used by
337 the *DB*_File modules
340 #define DBM_setFilter(db_type,code) \
343 RETVAL = sv_mortalcopy(db_type) ; \
345 if (db_type && (code == &PL_sv_undef)) { \
346 SvREFCNT_dec(db_type) ; \
351 sv_setsv(db_type, code) ; \
353 db_type = newSVsv(code) ; \
357 #define DBM_ckFilter(arg,type,name) \
360 if (db->filtering) { \
361 croak("recursion detected in %s", name) ; \
365 SAVEINT(db->filtering) ; \
366 db->filtering = TRUE ; \
368 if (name[7] == 's') \
369 arg = newSVsv(arg); \
374 (void) perl_call_sv(db->type, G_DISCARD); \
379 if (name[7] == 's'){ \
380 arg = sv_2mortal(arg); \
384 #if 1 /* for compatibility */
385 # define VTBL_sv &PL_vtbl_sv
386 # define VTBL_env &PL_vtbl_env
387 # define VTBL_envelem &PL_vtbl_envelem
388 # define VTBL_sig &PL_vtbl_sig
389 # define VTBL_sigelem &PL_vtbl_sigelem
390 # define VTBL_pack &PL_vtbl_pack
391 # define VTBL_packelem &PL_vtbl_packelem
392 # define VTBL_dbline &PL_vtbl_dbline
393 # define VTBL_isa &PL_vtbl_isa
394 # define VTBL_isaelem &PL_vtbl_isaelem
395 # define VTBL_arylen &PL_vtbl_arylen
396 # define VTBL_glob &PL_vtbl_glob
397 # define VTBL_mglob &PL_vtbl_mglob
398 # define VTBL_nkeys &PL_vtbl_nkeys
399 # define VTBL_taint &PL_vtbl_taint
400 # define VTBL_substr &PL_vtbl_substr
401 # define VTBL_vec &PL_vtbl_vec
402 # define VTBL_pos &PL_vtbl_pos
403 # define VTBL_bm &PL_vtbl_bm
404 # define VTBL_fm &PL_vtbl_fm
405 # define VTBL_uvar &PL_vtbl_uvar
406 # define VTBL_defelem &PL_vtbl_defelem
407 # define VTBL_regexp &PL_vtbl_regexp
408 # define VTBL_regdata &PL_vtbl_regdata
409 # define VTBL_regdatum &PL_vtbl_regdatum
410 # ifdef USE_LOCALE_COLLATE
411 # define VTBL_collxfrm &PL_vtbl_collxfrm
413 # define VTBL_amagic &PL_vtbl_amagic
414 # define VTBL_amagicelem &PL_vtbl_amagicelem
419 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_GET_CONTEXT) && !defined(PERL_CORE)
422 # define aTHX PERL_GET_THX
426 #if defined(PERL_IMPLICIT_SYS) && !defined(PERL_CORE)
428 # if defined (NETWARE) && defined (USE_STDIO)
429 # define times PerlProc_times
430 # define setuid PerlProc_setuid
431 # define setgid PerlProc_setgid
432 # define getpid PerlProc_getpid
433 # define pause PerlProc_pause
434 # define exit PerlProc_exit
435 # define _exit PerlProc__exit
452 /* Following symbols were giving redefinition errors while building extensions - sgp 17th Oct 2000 */
462 # undef gethostbyaddr
463 # undef gethostbyname
468 # undef getprotobyname
469 # undef getprotobynumber
471 # undef getservbyname
472 # undef getservbyport
483 # define mkdir PerlDir_mkdir
484 # define chdir PerlDir_chdir
485 # define rmdir PerlDir_rmdir
486 # define closedir PerlDir_close
487 # define opendir PerlDir_open
488 # define readdir PerlDir_read
489 # define rewinddir PerlDir_rewind
490 # define seekdir PerlDir_seek
491 # define telldir PerlDir_tell
492 # define putenv PerlEnv_putenv
493 # define getenv PerlEnv_getenv
494 # define uname PerlEnv_uname
495 # define stdin PerlSIO_stdin
496 # define stdout PerlSIO_stdout
497 # define stderr PerlSIO_stderr
498 # define fopen PerlSIO_fopen
499 # define fclose PerlSIO_fclose
500 # define feof PerlSIO_feof
501 # define ferror PerlSIO_ferror
502 # define clearerr PerlSIO_clearerr
503 # define getc PerlSIO_getc
504 # define fputc PerlSIO_fputc
505 # define fputs PerlSIO_fputs
506 # define fflush PerlSIO_fflush
507 # define ungetc PerlSIO_ungetc
508 # define fileno PerlSIO_fileno
509 # define fdopen PerlSIO_fdopen
510 # define freopen PerlSIO_freopen
511 # define fread PerlSIO_fread
512 # define fwrite PerlSIO_fwrite
513 # define setbuf PerlSIO_setbuf
514 # define setvbuf PerlSIO_setvbuf
515 # define setlinebuf PerlSIO_setlinebuf
516 # define stdoutf PerlSIO_stdoutf
517 # define vfprintf PerlSIO_vprintf
518 # define ftell PerlSIO_ftell
519 # define fseek PerlSIO_fseek
520 # define fgetpos PerlSIO_fgetpos
521 # define fsetpos PerlSIO_fsetpos
522 # define frewind PerlSIO_rewind
523 # define tmpfile PerlSIO_tmpfile
524 # define access PerlLIO_access
525 # define chmod PerlLIO_chmod
526 # define chsize PerlLIO_chsize
527 # define close PerlLIO_close
528 # define dup PerlLIO_dup
529 # define dup2 PerlLIO_dup2
530 # define flock PerlLIO_flock
531 # define fstat PerlLIO_fstat
532 # define ioctl PerlLIO_ioctl
533 # define isatty PerlLIO_isatty
534 # define link PerlLIO_link
535 # define lseek PerlLIO_lseek
536 # define lstat PerlLIO_lstat
537 # define mktemp PerlLIO_mktemp
538 # define open PerlLIO_open
539 # define read PerlLIO_read
540 # define rename PerlLIO_rename
541 # define setmode PerlLIO_setmode
542 # define stat(buf,sb) PerlLIO_stat(buf,sb)
543 # define tmpnam PerlLIO_tmpnam
544 # define umask PerlLIO_umask
545 # define unlink PerlLIO_unlink
546 # define utime PerlLIO_utime
547 # define write PerlLIO_write
548 # define malloc PerlMem_malloc
549 # define realloc PerlMem_realloc
550 # define free PerlMem_free
551 # define abort PerlProc_abort
552 # define exit PerlProc_exit
553 # define _exit PerlProc__exit
554 # define execl PerlProc_execl
555 # define execv PerlProc_execv
556 # define execvp PerlProc_execvp
557 # define getuid PerlProc_getuid
558 # define geteuid PerlProc_geteuid
559 # define getgid PerlProc_getgid
560 # define getegid PerlProc_getegid
561 # define getlogin PerlProc_getlogin
562 # define kill PerlProc_kill
563 # define killpg PerlProc_killpg
564 # define pause PerlProc_pause
565 # define popen PerlProc_popen
566 # define pclose PerlProc_pclose
567 # define pipe PerlProc_pipe
568 # define setuid PerlProc_setuid
569 # define setgid PerlProc_setgid
570 # define sleep PerlProc_sleep
571 # define times PerlProc_times
572 # define wait PerlProc_wait
573 # define setjmp PerlProc_setjmp
574 # define longjmp PerlProc_longjmp
575 # define signal PerlProc_signal
576 # define getpid PerlProc_getpid
577 # define gettimeofday PerlProc_gettimeofday
578 # define htonl PerlSock_htonl
579 # define htons PerlSock_htons
580 # define ntohl PerlSock_ntohl
581 # define ntohs PerlSock_ntohs
582 # define accept PerlSock_accept
583 # define bind PerlSock_bind
584 # define connect PerlSock_connect
585 # define endhostent PerlSock_endhostent
586 # define endnetent PerlSock_endnetent
587 # define endprotoent PerlSock_endprotoent
588 # define endservent PerlSock_endservent
589 # define gethostbyaddr PerlSock_gethostbyaddr
590 # define gethostbyname PerlSock_gethostbyname
591 # define gethostent PerlSock_gethostent
592 # define gethostname PerlSock_gethostname
593 # define getnetbyaddr PerlSock_getnetbyaddr
594 # define getnetbyname PerlSock_getnetbyname
595 # define getnetent PerlSock_getnetent
596 # define getpeername PerlSock_getpeername
597 # define getprotobyname PerlSock_getprotobyname
598 # define getprotobynumber PerlSock_getprotobynumber
599 # define getprotoent PerlSock_getprotoent
600 # define getservbyname PerlSock_getservbyname
601 # define getservbyport PerlSock_getservbyport
602 # define getservent PerlSock_getservent
603 # define getsockname PerlSock_getsockname
604 # define getsockopt PerlSock_getsockopt
605 # define inet_addr PerlSock_inet_addr
606 # define inet_ntoa PerlSock_inet_ntoa
607 # define listen PerlSock_listen
608 # define recv PerlSock_recv
609 # define recvfrom PerlSock_recvfrom
610 # define select PerlSock_select
611 # define send PerlSock_send
612 # define sendto PerlSock_sendto
613 # define sethostent PerlSock_sethostent
614 # define setnetent PerlSock_setnetent
615 # define setprotoent PerlSock_setprotoent
616 # define setservent PerlSock_setservent
617 # define setsockopt PerlSock_setsockopt
618 # define shutdown PerlSock_shutdown
619 # define socket PerlSock_socket
620 # define socketpair PerlSock_socketpair
621 # endif /* NETWARE && USE_STDIO */
623 # ifdef USE_SOCKETS_AS_HANDLES
629 # define fd_set Perl_fd_set
630 # define FD_SET(n,p) PERL_FD_SET(n,p)
631 # define FD_CLR(n,p) PERL_FD_CLR(n,p)
632 # define FD_ISSET(n,p) PERL_FD_ISSET(n,p)
633 # define FD_ZERO(p) PERL_FD_ZERO(p)
634 # endif /* USE_SOCKETS_AS_HANDLES */
636 # endif /* NO_XSLOCKS */
637 #endif /* PERL_IMPLICIT_SYS && !PERL_CORE */
639 #endif /* _INC_PERL_XSUB_H */ /* include guard */
643 * c-indentation-style: bsd
645 * indent-tabs-mode: t
648 * ex: set ts=8 sts=4 sw=4 noet: