3 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005 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)]
100 #if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
101 # define XS(name) __declspec(dllexport) void name(pTHX_ CV* cv)
103 #if defined(__SYMBIAN32__)
104 # define XS(name) EXPORT_C void name(pTHX_ CV* cv)
107 # if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
108 # define XS(name) void name(pTHX_ CV* cv __attribute__unused__)
110 # define XS(name) void name(pTHX_ CV* cv)
114 #define dAX const I32 ax = MARK - PL_stack_base + 1
118 register SV **mark = PL_stack_base + ax++
120 #define dITEMS I32 items = SP - MARK
122 #if defined(lint) && defined(S_SPLINT_S) /* www.splint.org */
124 NOTE(ARGUNUSED(cv)) \
131 #define dXSTARG SV * const targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
132 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
134 /* Should be used before final PUSHi etc. if not in PPCODE section. */
135 #define XSprePUSH (sp = PL_stack_base + ax - 1)
137 #define XSANY CvXSUBANY(cv)
139 #define dXSI32 I32 ix = XSANY.any_i32
142 # define XSINTERFACE_CVT(ret,name) ret (*name)(...)
144 # define XSINTERFACE_CVT(ret,name) ret (*name)()
146 #define dXSFUNCTION(ret) XSINTERFACE_CVT(ret,XSFUNCTION)
147 #define XSINTERFACE_FUNC(ret,cv,f) ((XSINTERFACE_CVT(ret,))(f))
148 #define XSINTERFACE_FUNC_SET(cv,f) \
149 CvXSUBANY(cv).any_dxptr = (void (*) (pTHX_ void*))(f)
151 #define dUNDERBAR PADOFFSET padoff_du = find_rundefsvoffset()
152 #define UNDERBAR ((padoff_du == NOT_IN_PAD \
153 || PAD_COMPNAME_FLAGS_isOUR(padoff_du)) \
154 ? DEFSV : PAD_SVl(padoff_du))
156 /* Simple macros to put new mortal values onto the stack. */
157 /* Typically used to return values from XS functions. */
160 =head1 Stack Manipulation Macros
162 =for apidoc Am|void|XST_mIV|int pos|IV iv
163 Place an integer into the specified position C<pos> on the stack. The
164 value is stored in a new mortal SV.
166 =for apidoc Am|void|XST_mNV|int pos|NV nv
167 Place a double into the specified position C<pos> on the stack. The value
168 is stored in a new mortal SV.
170 =for apidoc Am|void|XST_mPV|int pos|char* str
171 Place a copy of a string into the specified position C<pos> on the stack.
172 The value is stored in a new mortal SV.
174 =for apidoc Am|void|XST_mNO|int pos
175 Place C<&PL_sv_no> into the specified position C<pos> on the
178 =for apidoc Am|void|XST_mYES|int pos
179 Place C<&PL_sv_yes> into the specified position C<pos> on the
182 =for apidoc Am|void|XST_mUNDEF|int pos
183 Place C<&PL_sv_undef> into the specified position C<pos> on the
186 =for apidoc Am|void|XSRETURN|int nitems
187 Return from XSUB, indicating number of items on the stack. This is usually
188 handled by C<xsubpp>.
190 =for apidoc Am|void|XSRETURN_IV|IV iv
191 Return an integer from an XSUB immediately. Uses C<XST_mIV>.
193 =for apidoc Am|void|XSRETURN_UV|IV uv
194 Return an integer from an XSUB immediately. Uses C<XST_mUV>.
196 =for apidoc Am|void|XSRETURN_NV|NV nv
197 Return a double from an XSUB immediately. Uses C<XST_mNV>.
199 =for apidoc Am|void|XSRETURN_PV|char* str
200 Return a copy of a string from an XSUB immediately. Uses C<XST_mPV>.
202 =for apidoc Ams||XSRETURN_NO
203 Return C<&PL_sv_no> from an XSUB immediately. Uses C<XST_mNO>.
205 =for apidoc Ams||XSRETURN_YES
206 Return C<&PL_sv_yes> from an XSUB immediately. Uses C<XST_mYES>.
208 =for apidoc Ams||XSRETURN_UNDEF
209 Return C<&PL_sv_undef> from an XSUB immediately. Uses C<XST_mUNDEF>.
211 =for apidoc Ams||XSRETURN_EMPTY
212 Return an empty list from an XSUB immediately.
214 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
216 =for apidoc AmU||newXSproto|char* name|XSUBADDR_t f|char* filename|const char *proto
217 Used by C<xsubpp> to hook up XSUBs as Perl subs. Adds Perl prototypes to
220 =for apidoc AmU||XS_VERSION
221 The version identifier for an XS module. This is usually
222 handled automatically by C<ExtUtils::MakeMaker>. See C<XS_VERSION_BOOTCHECK>.
224 =for apidoc Ams||XS_VERSION_BOOTCHECK
225 Macro to verify that a PM module's $VERSION variable matches the XS
226 module's C<XS_VERSION> variable. This is usually handled automatically by
227 C<xsubpp>. See L<perlxs/"The VERSIONCHECK: Keyword">.
229 =head1 Simple Exception Handling Macros
231 =for apidoc Ams||dXCPT
232 Set up necessary local variables for exception handling.
233 See L<perlguts/"Exception Handling">.
235 =for apidoc AmU||XCPT_TRY_START
236 Starts a try block. See L<perlguts/"Exception Handling">.
238 =for apidoc AmU||XCPT_TRY_END
239 Ends a try block. See L<perlguts/"Exception Handling">.
241 =for apidoc AmU||XCPT_CATCH
242 Introduces a catch block. See L<perlguts/"Exception Handling">.
244 =for apidoc Ams||XCPT_RETHROW
245 Rethrows a previously caught exception. See L<perlguts/"Exception Handling">.
250 #define XST_mIV(i,v) (ST(i) = sv_2mortal(newSViv(v)) )
251 #define XST_mUV(i,v) (ST(i) = sv_2mortal(newSVuv(v)) )
252 #define XST_mNV(i,v) (ST(i) = sv_2mortal(newSVnv(v)) )
253 #define XST_mPV(i,v) (ST(i) = sv_2mortal(newSVpv(v,0)))
254 #define XST_mPVN(i,v,n) (ST(i) = sv_2mortal(newSVpvn(v,n)))
255 #define XST_mNO(i) (ST(i) = &PL_sv_no )
256 #define XST_mYES(i) (ST(i) = &PL_sv_yes )
257 #define XST_mUNDEF(i) (ST(i) = &PL_sv_undef)
259 #define XSRETURN(off) \
261 IV tmpXSoff = (off); \
262 PL_stack_sp = PL_stack_base + ax + (tmpXSoff - 1); \
266 #define XSRETURN_IV(v) STMT_START { XST_mIV(0,v); XSRETURN(1); } STMT_END
267 #define XSRETURN_UV(v) STMT_START { XST_mUV(0,v); XSRETURN(1); } STMT_END
268 #define XSRETURN_NV(v) STMT_START { XST_mNV(0,v); XSRETURN(1); } STMT_END
269 #define XSRETURN_PV(v) STMT_START { XST_mPV(0,v); XSRETURN(1); } STMT_END
270 #define XSRETURN_PVN(v,n) STMT_START { XST_mPVN(0,v,n); XSRETURN(1); } STMT_END
271 #define XSRETURN_NO STMT_START { XST_mNO(0); XSRETURN(1); } STMT_END
272 #define XSRETURN_YES STMT_START { XST_mYES(0); XSRETURN(1); } STMT_END
273 #define XSRETURN_UNDEF STMT_START { XST_mUNDEF(0); XSRETURN(1); } STMT_END
274 #define XSRETURN_EMPTY STMT_START { XSRETURN(0); } STMT_END
276 #define newXSproto(a,b,c,d) newXS_flags(a,b,c,d,0)
279 # define XS_VERSION_BOOTCHECK \
282 const char *vn = NULL, *module = SvPV_nolen_const(ST(0)); \
283 if (items >= 2) /* version supplied as bootstrap arg */ \
286 /* XXX GV_ADDWARN */ \
287 _sv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
288 vn = "XS_VERSION"), FALSE); \
289 if (!_sv || !SvOK(_sv)) \
290 _sv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
291 vn = "VERSION"), FALSE); \
294 SV *xssv = Perl_newSVpvf(aTHX_ "%s",XS_VERSION); \
295 xssv = new_version(xssv); \
296 if ( !sv_derived_from(_sv, "version") ) \
297 _sv = new_version(_sv); \
298 if ( vcmp(_sv,xssv) ) \
299 Perl_croak(aTHX_ "%s object version %"SVf" does not match %s%s%s%s %"SVf,\
300 module, vstringify(xssv), \
301 vn ? "$" : "", vn ? module : "", vn ? "::" : "", \
302 vn ? vn : "bootstrap parameter", vstringify(_sv));\
306 # define XS_VERSION_BOOTCHECK
310 # define dXCPT dJMPENV; int rEtV = 0
311 # define XCPT_TRY_START JMPENV_PUSH(rEtV); if (rEtV == 0)
312 # define XCPT_TRY_END JMPENV_POP;
313 # define XCPT_CATCH if (rEtV != 0)
314 # define XCPT_RETHROW JMPENV_JUMP(rEtV)
318 The DBM_setFilter & DBM_ckFilter macros are only used by
319 the *DB*_File modules
322 #define DBM_setFilter(db_type,code) \
325 RETVAL = sv_mortalcopy(db_type) ; \
327 if (db_type && (code == &PL_sv_undef)) { \
328 SvREFCNT_dec(db_type) ; \
333 sv_setsv(db_type, code) ; \
335 db_type = newSVsv(code) ; \
339 #define DBM_ckFilter(arg,type,name) \
342 if (db->filtering) { \
343 croak("recursion detected in %s", name) ; \
347 SAVEINT(db->filtering) ; \
348 db->filtering = TRUE ; \
350 if (name[7] == 's') \
351 arg = newSVsv(arg); \
356 (void) perl_call_sv(db->type, G_DISCARD); \
361 if (name[7] == 's'){ \
362 arg = sv_2mortal(arg); \
367 #if 1 /* for compatibility */
368 # define VTBL_sv &PL_vtbl_sv
369 # define VTBL_env &PL_vtbl_env
370 # define VTBL_envelem &PL_vtbl_envelem
371 # define VTBL_sig &PL_vtbl_sig
372 # define VTBL_sigelem &PL_vtbl_sigelem
373 # define VTBL_pack &PL_vtbl_pack
374 # define VTBL_packelem &PL_vtbl_packelem
375 # define VTBL_dbline &PL_vtbl_dbline
376 # define VTBL_isa &PL_vtbl_isa
377 # define VTBL_isaelem &PL_vtbl_isaelem
378 # define VTBL_arylen &PL_vtbl_arylen
379 # define VTBL_glob &PL_vtbl_glob
380 # define VTBL_mglob &PL_vtbl_mglob
381 # define VTBL_nkeys &PL_vtbl_nkeys
382 # define VTBL_taint &PL_vtbl_taint
383 # define VTBL_substr &PL_vtbl_substr
384 # define VTBL_vec &PL_vtbl_vec
385 # define VTBL_pos &PL_vtbl_pos
386 # define VTBL_bm &PL_vtbl_bm
387 # define VTBL_fm &PL_vtbl_fm
388 # define VTBL_uvar &PL_vtbl_uvar
389 # define VTBL_defelem &PL_vtbl_defelem
390 # define VTBL_regexp &PL_vtbl_regexp
391 # define VTBL_regdata &PL_vtbl_regdata
392 # define VTBL_regdatum &PL_vtbl_regdatum
393 # ifdef USE_LOCALE_COLLATE
394 # define VTBL_collxfrm &PL_vtbl_collxfrm
396 # define VTBL_amagic &PL_vtbl_amagic
397 # define VTBL_amagicelem &PL_vtbl_amagicelem
404 # define PL_madskills 0
408 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_GET_CONTEXT) && !defined(PERL_CORE)
411 # define aTHX PERL_GET_THX
415 #if defined(PERL_IMPLICIT_SYS) && !defined(PERL_CORE)
417 # if defined (NETWARE) && defined (USE_STDIO)
418 # define times PerlProc_times
419 # define setuid PerlProc_setuid
420 # define setgid PerlProc_setgid
421 # define getpid PerlProc_getpid
422 # define pause PerlProc_pause
423 # define exit PerlProc_exit
424 # define _exit PerlProc__exit
441 /* Following symbols were giving redefinition errors while building extensions - sgp 17th Oct 2000 */
451 # undef gethostbyaddr
452 # undef gethostbyname
457 # undef getprotobyname
458 # undef getprotobynumber
460 # undef getservbyname
461 # undef getservbyport
472 # define mkdir PerlDir_mkdir
473 # define chdir PerlDir_chdir
474 # define rmdir PerlDir_rmdir
475 # define closedir PerlDir_close
476 # define opendir PerlDir_open
477 # define readdir PerlDir_read
478 # define rewinddir PerlDir_rewind
479 # define seekdir PerlDir_seek
480 # define telldir PerlDir_tell
481 # define putenv PerlEnv_putenv
482 # define getenv PerlEnv_getenv
483 # define uname PerlEnv_uname
484 # define stdin PerlSIO_stdin
485 # define stdout PerlSIO_stdout
486 # define stderr PerlSIO_stderr
487 # define fopen PerlSIO_fopen
488 # define fclose PerlSIO_fclose
489 # define feof PerlSIO_feof
490 # define ferror PerlSIO_ferror
491 # define clearerr PerlSIO_clearerr
492 # define getc PerlSIO_getc
493 # define fputc PerlSIO_fputc
494 # define fputs PerlSIO_fputs
495 # define fflush PerlSIO_fflush
496 # define ungetc PerlSIO_ungetc
497 # define fileno PerlSIO_fileno
498 # define fdopen PerlSIO_fdopen
499 # define freopen PerlSIO_freopen
500 # define fread PerlSIO_fread
501 # define fwrite PerlSIO_fwrite
502 # define setbuf PerlSIO_setbuf
503 # define setvbuf PerlSIO_setvbuf
504 # define setlinebuf PerlSIO_setlinebuf
505 # define stdoutf PerlSIO_stdoutf
506 # define vfprintf PerlSIO_vprintf
507 # define ftell PerlSIO_ftell
508 # define fseek PerlSIO_fseek
509 # define fgetpos PerlSIO_fgetpos
510 # define fsetpos PerlSIO_fsetpos
511 # define frewind PerlSIO_rewind
512 # define tmpfile PerlSIO_tmpfile
513 # define access PerlLIO_access
514 # define chmod PerlLIO_chmod
515 # define chsize PerlLIO_chsize
516 # define close PerlLIO_close
517 # define dup PerlLIO_dup
518 # define dup2 PerlLIO_dup2
519 # define flock PerlLIO_flock
520 # define fstat PerlLIO_fstat
521 # define ioctl PerlLIO_ioctl
522 # define isatty PerlLIO_isatty
523 # define link PerlLIO_link
524 # define lseek PerlLIO_lseek
525 # define lstat PerlLIO_lstat
526 # define mktemp PerlLIO_mktemp
527 # define open PerlLIO_open
528 # define read PerlLIO_read
529 # define rename PerlLIO_rename
530 # define setmode PerlLIO_setmode
531 # define stat(buf,sb) PerlLIO_stat(buf,sb)
532 # define tmpnam PerlLIO_tmpnam
533 # define umask PerlLIO_umask
534 # define unlink PerlLIO_unlink
535 # define utime PerlLIO_utime
536 # define write PerlLIO_write
537 # define malloc PerlMem_malloc
538 # define realloc PerlMem_realloc
539 # define free PerlMem_free
540 # define abort PerlProc_abort
541 # define exit PerlProc_exit
542 # define _exit PerlProc__exit
543 # define execl PerlProc_execl
544 # define execv PerlProc_execv
545 # define execvp PerlProc_execvp
546 # define getuid PerlProc_getuid
547 # define geteuid PerlProc_geteuid
548 # define getgid PerlProc_getgid
549 # define getegid PerlProc_getegid
550 # define getlogin PerlProc_getlogin
551 # define kill PerlProc_kill
552 # define killpg PerlProc_killpg
553 # define pause PerlProc_pause
554 # define popen PerlProc_popen
555 # define pclose PerlProc_pclose
556 # define pipe PerlProc_pipe
557 # define setuid PerlProc_setuid
558 # define setgid PerlProc_setgid
559 # define sleep PerlProc_sleep
560 # define times PerlProc_times
561 # define wait PerlProc_wait
562 # define setjmp PerlProc_setjmp
563 # define longjmp PerlProc_longjmp
564 # define signal PerlProc_signal
565 # define getpid PerlProc_getpid
566 # define gettimeofday PerlProc_gettimeofday
567 # define htonl PerlSock_htonl
568 # define htons PerlSock_htons
569 # define ntohl PerlSock_ntohl
570 # define ntohs PerlSock_ntohs
571 # define accept PerlSock_accept
572 # define bind PerlSock_bind
573 # define connect PerlSock_connect
574 # define endhostent PerlSock_endhostent
575 # define endnetent PerlSock_endnetent
576 # define endprotoent PerlSock_endprotoent
577 # define endservent PerlSock_endservent
578 # define gethostbyaddr PerlSock_gethostbyaddr
579 # define gethostbyname PerlSock_gethostbyname
580 # define gethostent PerlSock_gethostent
581 # define gethostname PerlSock_gethostname
582 # define getnetbyaddr PerlSock_getnetbyaddr
583 # define getnetbyname PerlSock_getnetbyname
584 # define getnetent PerlSock_getnetent
585 # define getpeername PerlSock_getpeername
586 # define getprotobyname PerlSock_getprotobyname
587 # define getprotobynumber PerlSock_getprotobynumber
588 # define getprotoent PerlSock_getprotoent
589 # define getservbyname PerlSock_getservbyname
590 # define getservbyport PerlSock_getservbyport
591 # define getservent PerlSock_getservent
592 # define getsockname PerlSock_getsockname
593 # define getsockopt PerlSock_getsockopt
594 # define inet_addr PerlSock_inet_addr
595 # define inet_ntoa PerlSock_inet_ntoa
596 # define listen PerlSock_listen
597 # define recv PerlSock_recv
598 # define recvfrom PerlSock_recvfrom
599 # define select PerlSock_select
600 # define send PerlSock_send
601 # define sendto PerlSock_sendto
602 # define sethostent PerlSock_sethostent
603 # define setnetent PerlSock_setnetent
604 # define setprotoent PerlSock_setprotoent
605 # define setservent PerlSock_setservent
606 # define setsockopt PerlSock_setsockopt
607 # define shutdown PerlSock_shutdown
608 # define socket PerlSock_socket
609 # define socketpair PerlSock_socketpair
610 # endif /* NETWARE && USE_STDIO */
612 # ifdef USE_SOCKETS_AS_HANDLES
618 # define fd_set Perl_fd_set
619 # define FD_SET(n,p) PERL_FD_SET(n,p)
620 # define FD_CLR(n,p) PERL_FD_CLR(n,p)
621 # define FD_ISSET(n,p) PERL_FD_ISSET(n,p)
622 # define FD_ZERO(p) PERL_FD_ZERO(p)
623 # endif /* USE_SOCKETS_AS_HANDLES */
625 # endif /* NO_XSLOCKS */
626 #endif /* PERL_IMPLICIT_SYS && !PERL_CORE */
628 #endif /* _INC_PERL_XSUB_H */ /* include guard */
632 * c-indentation-style: bsd
634 * indent-tabs-mode: t
637 * ex: set ts=8 sts=4 sw=4 noet: