Add a test for [perl #17753].
[p5sagit/p5-mst-13.2.git] / XSUB.h
CommitLineData
eb1102fc 1/* XSUB.h
2 *
4c79ee7a 3 * Copyright (c) 1997-2003, Larry Wall
eb1102fc 4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
8 */
9
b4ba0ab9 10#ifndef _INC_PERL_XSUB_H
11#define _INC_PERL_XSUB_H 1
12
954c1994 13/* first, some documentation for xsubpp-generated items */
14
15/*
ccfc67b7 16=head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
17
954c1994 18=for apidoc Amn|char*|CLASS
19Variable which is setup by C<xsubpp> to indicate the
20class name for a C++ XS constructor. This is always a C<char*>. See C<THIS>.
21
22=for apidoc Amn|(whatever)|RETVAL
23Variable which is setup by C<xsubpp> to hold the return value for an
24XSUB. This is always the proper type for the XSUB. See
25L<perlxs/"The RETVAL Variable">.
26
27=for apidoc Amn|(whatever)|THIS
28Variable which is setup by C<xsubpp> to designate the object in a C++
29XSUB. This is always the proper type for the C++ object. See C<CLASS> and
30L<perlxs/"Using XS With C++">.
31
9f2ea798 32=for apidoc Amn|I32|ax
33Variable which is setup by C<xsubpp> to indicate the stack base offset,
34used by the C<ST>, C<XSprePUSH> and C<XSRETURN> macros. The C<dMARK> macro
35must be called prior to setup the C<MARK> variable.
36
954c1994 37=for apidoc Amn|I32|items
38Variable which is setup by C<xsubpp> to indicate the number of
39items on the stack. See L<perlxs/"Variable-length Parameter Lists">.
40
41=for apidoc Amn|I32|ix
42Variable which is setup by C<xsubpp> to indicate which of an
43XSUB's aliases was used to invoke it. See L<perlxs/"The ALIAS: Keyword">.
44
45=for apidoc Am|SV*|ST|int ix
46Used to access elements on the XSUB's stack.
47
48=for apidoc AmU||XS
49Macro to declare an XSUB and its C parameter list. This is handled by
50C<xsubpp>.
51
9f2ea798 52=for apidoc Ams||dAX
53Sets up the C<ax> variable.
54This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
55
56=for apidoc Ams||dITEMS
57Sets up the C<items> variable.
58This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
59
954c1994 60=for apidoc Ams||dXSARGS
9f2ea798 61Sets up stack and mark pointers for an XSUB, calling dSP and dMARK.
62Sets up the C<ax> and C<items> variables by calling C<dAX> and C<dITEMS>.
63This is usually handled automatically by C<xsubpp>.
954c1994 64
65=for apidoc Ams||dXSI32
66Sets up the C<ix> variable for an XSUB which has aliases. This is usually
67handled automatically by C<xsubpp>.
68
69=cut
70*/
71
3280af22 72#define ST(off) PL_stack_base[ax + (off)]
a0d0e21e 73
d308986b 74#if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
acfe0abc 75# define XS(name) __declspec(dllexport) void name(pTHX_ CV* cv)
cea2e8a9 76#else
acfe0abc 77# define XS(name) void name(pTHX_ CV* cv)
a0d0e21e 78#endif
79
349b520e 80#define dAX I32 ax = MARK - PL_stack_base + 1
9f2ea798 81
82#define dITEMS I32 items = SP - MARK
83
a0d0e21e 84#define dXSARGS \
3c78fafa 85 dSP; dMARK; \
9f2ea798 86 dAX; dITEMS
a0d0e21e 87
8a7fc0dc 88#define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
89 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
90
b26a54d0 91/* Should be used before final PUSHi etc. if not in PPCODE section. */
92#define XSprePUSH (sp = PL_stack_base + ax - 1)
93
a0d0e21e 94#define XSANY CvXSUBANY(cv)
95
96#define dXSI32 I32 ix = XSANY.any_i32
97
cfc02341 98#ifdef __cplusplus
99# define XSINTERFACE_CVT(ret,name) ret (*name)(...)
100#else
101# define XSINTERFACE_CVT(ret,name) ret (*name)()
102#endif
103#define dXSFUNCTION(ret) XSINTERFACE_CVT(ret,XSFUNCTION)
2554589c 104#define XSINTERFACE_FUNC(ret,cv,f) ((XSINTERFACE_CVT(ret,))(f))
cfc02341 105#define XSINTERFACE_FUNC_SET(cv,f) \
acfe0abc 106 CvXSUBANY(cv).any_dptr = (void (*) (pTHX_ void*))(f)
cfc02341 107
748a9306 108/* Simple macros to put new mortal values onto the stack. */
109/* Typically used to return values from XS functions. */
954c1994 110
111/*
ccfc67b7 112=head1 Stack Manipulation Macros
113
954c1994 114=for apidoc Am|void|XST_mIV|int pos|IV iv
115Place an integer into the specified position C<pos> on the stack. The
116value is stored in a new mortal SV.
117
118=for apidoc Am|void|XST_mNV|int pos|NV nv
119Place a double into the specified position C<pos> on the stack. The value
120is stored in a new mortal SV.
121
122=for apidoc Am|void|XST_mPV|int pos|char* str
123Place a copy of a string into the specified position C<pos> on the stack.
124The value is stored in a new mortal SV.
125
126=for apidoc Am|void|XST_mNO|int pos
127Place C<&PL_sv_no> into the specified position C<pos> on the
128stack.
129
130=for apidoc Am|void|XST_mYES|int pos
131Place C<&PL_sv_yes> into the specified position C<pos> on the
132stack.
133
134=for apidoc Am|void|XST_mUNDEF|int pos
135Place C<&PL_sv_undef> into the specified position C<pos> on the
136stack.
137
138=for apidoc Am|void|XSRETURN|int nitems
139Return from XSUB, indicating number of items on the stack. This is usually
140handled by C<xsubpp>.
141
142=for apidoc Am|void|XSRETURN_IV|IV iv
143Return an integer from an XSUB immediately. Uses C<XST_mIV>.
144
145=for apidoc Am|void|XSRETURN_NV|NV nv
d1be9408 146Return a double from an XSUB immediately. Uses C<XST_mNV>.
954c1994 147
148=for apidoc Am|void|XSRETURN_PV|char* str
149Return a copy of a string from an XSUB immediately. Uses C<XST_mPV>.
150
151=for apidoc Ams||XSRETURN_NO
152Return C<&PL_sv_no> from an XSUB immediately. Uses C<XST_mNO>.
153
154=for apidoc Ams||XSRETURN_YES
155Return C<&PL_sv_yes> from an XSUB immediately. Uses C<XST_mYES>.
156
157=for apidoc Ams||XSRETURN_UNDEF
158Return C<&PL_sv_undef> from an XSUB immediately. Uses C<XST_mUNDEF>.
159
160=for apidoc Ams||XSRETURN_EMPTY
161Return an empty list from an XSUB immediately.
162
ccfc67b7 163=head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
164
954c1994 165=for apidoc AmU||newXSproto
166Used by C<xsubpp> to hook up XSUBs as Perl subs. Adds Perl prototypes to
167the subs.
168
169=for apidoc AmU||XS_VERSION
170The version identifier for an XS module. This is usually
171handled automatically by C<ExtUtils::MakeMaker>. See C<XS_VERSION_BOOTCHECK>.
172
173=for apidoc Ams||XS_VERSION_BOOTCHECK
174Macro to verify that a PM module's $VERSION variable matches the XS
175module's C<XS_VERSION> variable. This is usually handled automatically by
176C<xsubpp>. See L<perlxs/"The VERSIONCHECK: Keyword">.
177
178=cut
179*/
180
4633a7c4 181#define XST_mIV(i,v) (ST(i) = sv_2mortal(newSViv(v)) )
182#define XST_mNV(i,v) (ST(i) = sv_2mortal(newSVnv(v)) )
183#define XST_mPV(i,v) (ST(i) = sv_2mortal(newSVpv(v,0)))
79cb57f6 184#define XST_mPVN(i,v,n) (ST(i) = sv_2mortal(newSVpvn(v,n)))
3280af22 185#define XST_mNO(i) (ST(i) = &PL_sv_no )
186#define XST_mYES(i) (ST(i) = &PL_sv_yes )
187#define XST_mUNDEF(i) (ST(i) = &PL_sv_undef)
954c1994 188
189#define XSRETURN(off) \
190 STMT_START { \
191 PL_stack_sp = PL_stack_base + ax + ((off) - 1); \
192 return; \
193 } STMT_END
194
80b92232 195#define XSRETURN_IV(v) STMT_START { XST_mIV(0,v); XSRETURN(1); } STMT_END
196#define XSRETURN_NV(v) STMT_START { XST_mNV(0,v); XSRETURN(1); } STMT_END
197#define XSRETURN_PV(v) STMT_START { XST_mPV(0,v); XSRETURN(1); } STMT_END
954c1994 198#define XSRETURN_PVN(v,n) STMT_START { XST_mPVN(0,v,n); XSRETURN(1); } STMT_END
80b92232 199#define XSRETURN_NO STMT_START { XST_mNO(0); XSRETURN(1); } STMT_END
200#define XSRETURN_YES STMT_START { XST_mYES(0); XSRETURN(1); } STMT_END
201#define XSRETURN_UNDEF STMT_START { XST_mUNDEF(0); XSRETURN(1); } STMT_END
202#define XSRETURN_EMPTY STMT_START { XSRETURN(0); } STMT_END
382b8d97 203
37120919 204#define newXSproto(a,b,c,d) sv_setpv((SV*)newXS(a,b,c), d)
720fb644 205
206#ifdef XS_VERSION
c6af7a1a 207# define XS_VERSION_BOOTCHECK \
774d564b 208 STMT_START { \
2d8e6c8d 209 SV *tmpsv; STRLEN n_a; \
210 char *vn = Nullch, *module = SvPV(ST(0),n_a); \
774d564b 211 if (items >= 2) /* version supplied as bootstrap arg */ \
6b88bc9c 212 tmpsv = ST(1); \
774d564b 213 else { \
46fc3d4c 214 /* XXX GV_ADDWARN */ \
cea2e8a9 215 tmpsv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
864dbfa3 216 vn = "XS_VERSION"), FALSE); \
6b88bc9c 217 if (!tmpsv || !SvOK(tmpsv)) \
cea2e8a9 218 tmpsv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
864dbfa3 219 vn = "VERSION"), FALSE); \
774d564b 220 } \
2d8e6c8d 221 if (tmpsv && (!SvOK(tmpsv) || strNE(XS_VERSION, SvPV(tmpsv, n_a)))) \
894356b3 222 Perl_croak(aTHX_ "%s object version %s does not match %s%s%s%s %"SVf,\
ae66e5c8 223 module, XS_VERSION, \
224 vn ? "$" : "", vn ? module : "", vn ? "::" : "", \
6b88bc9c 225 vn ? vn : "bootstrap parameter", tmpsv); \
80b92232 226 } STMT_END
720fb644 227#else
c6af7a1a 228# define XS_VERSION_BOOTCHECK
720fb644 229#endif
76e3520e 230
6a31061a 231/*
232 The DBM_setFilter & DBM_ckFilter macros are only used by
233 the *DB*_File modules
234*/
235
236#define DBM_setFilter(db_type,code) \
237 { \
238 if (db_type) \
239 RETVAL = sv_mortalcopy(db_type) ; \
240 ST(0) = RETVAL ; \
241 if (db_type && (code == &PL_sv_undef)) { \
242 SvREFCNT_dec(db_type) ; \
243 db_type = NULL ; \
244 } \
245 else if (code) { \
246 if (db_type) \
247 sv_setsv(db_type, code) ; \
248 else \
249 db_type = newSVsv(code) ; \
250 } \
251 }
252
253#define DBM_ckFilter(arg,type,name) \
254 if (db->type) { \
255 if (db->filtering) { \
256 croak("recursion detected in %s", name) ; \
257 } \
258 ENTER ; \
259 SAVETMPS ; \
260 SAVEINT(db->filtering) ; \
261 db->filtering = TRUE ; \
262 SAVESPTR(DEFSV) ; \
263 DEFSV = arg ; \
264 SvTEMP_off(arg) ; \
265 PUSHMARK(SP) ; \
266 PUTBACK ; \
267 (void) perl_call_sv(db->type, G_DISCARD); \
268 SPAGAIN ; \
269 PUTBACK ; \
270 FREETMPS ; \
271 LEAVE ; \
272 }
273
51371543 274#if 1 /* for compatibility */
dc9e4912 275# define VTBL_sv &PL_vtbl_sv
276# define VTBL_env &PL_vtbl_env
277# define VTBL_envelem &PL_vtbl_envelem
278# define VTBL_sig &PL_vtbl_sig
279# define VTBL_sigelem &PL_vtbl_sigelem
280# define VTBL_pack &PL_vtbl_pack
281# define VTBL_packelem &PL_vtbl_packelem
282# define VTBL_dbline &PL_vtbl_dbline
283# define VTBL_isa &PL_vtbl_isa
284# define VTBL_isaelem &PL_vtbl_isaelem
285# define VTBL_arylen &PL_vtbl_arylen
286# define VTBL_glob &PL_vtbl_glob
287# define VTBL_mglob &PL_vtbl_mglob
288# define VTBL_nkeys &PL_vtbl_nkeys
289# define VTBL_taint &PL_vtbl_taint
290# define VTBL_substr &PL_vtbl_substr
291# define VTBL_vec &PL_vtbl_vec
292# define VTBL_pos &PL_vtbl_pos
293# define VTBL_bm &PL_vtbl_bm
294# define VTBL_fm &PL_vtbl_fm
295# define VTBL_uvar &PL_vtbl_uvar
296# define VTBL_defelem &PL_vtbl_defelem
297# define VTBL_regexp &PL_vtbl_regexp
298# define VTBL_regdata &PL_vtbl_regdata
299# define VTBL_regdatum &PL_vtbl_regdatum
300# ifdef USE_LOCALE_COLLATE
301# define VTBL_collxfrm &PL_vtbl_collxfrm
302# endif
9e7bc3e8 303# define VTBL_amagic &PL_vtbl_amagic
304# define VTBL_amagicelem &PL_vtbl_amagicelem
dc9e4912 305#endif
306
6f4183fe 307#include "perlapi.h"
c6af7a1a 308
e8ee3774 309#if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_GET_CONTEXT) && !defined(PERL_CORE)
c5be433b 310# undef aTHX
311# undef aTHX_
54aff467 312# define aTHX PERL_GET_THX
313# define aTHX_ aTHX,
54aff467 314#endif
315
acfe0abc 316#if defined(PERL_IMPLICIT_SYS) && !defined(PERL_CORE)
c6af7a1a 317# ifndef NO_XSLOCKS
2986a63f 318# if defined (NETWARE) && defined (USE_STDIO)
319# define times PerlProc_times
320# define setuid PerlProc_setuid
321# define setgid PerlProc_setgid
322# define getpid PerlProc_getpid
323# define pause PerlProc_pause
324# define exit PerlProc_exit
325# define _exit PerlProc__exit
326# else
c6af7a1a 327# undef closedir
328# undef opendir
329# undef stdin
330# undef stdout
331# undef stderr
332# undef feof
333# undef ferror
334# undef fgetpos
335# undef ioctl
336# undef getlogin
337# undef setjmp
338# undef getc
339# undef ungetc
340# undef fileno
341
cb69f87a 342/* Following symbols were giving redefinition errors while building extensions - sgp 17th Oct 2000 */
2986a63f 343#ifdef NETWARE
344# undef readdir
345# undef fstat
346# undef stat
347# undef longjmp
348# undef endhostent
349# undef endnetent
350# undef endprotoent
351# undef endservent
352# undef gethostbyaddr
353# undef gethostbyname
354# undef gethostent
355# undef getnetbyaddr
356# undef getnetbyname
357# undef getnetent
358# undef getprotobyname
359# undef getprotobynumber
360# undef getprotoent
361# undef getservbyname
362# undef getservbyport
363# undef getservent
364# undef inet_ntoa
365# undef sethostent
366# undef setnetent
367# undef setprotoent
368# undef setservent
369#endif /* NETWARE */
370
1018e26f 371# undef socketpair
372
c6af7a1a 373# define mkdir PerlDir_mkdir
374# define chdir PerlDir_chdir
375# define rmdir PerlDir_rmdir
376# define closedir PerlDir_close
377# define opendir PerlDir_open
378# define readdir PerlDir_read
379# define rewinddir PerlDir_rewind
380# define seekdir PerlDir_seek
381# define telldir PerlDir_tell
382# define putenv PerlEnv_putenv
383# define getenv PerlEnv_getenv
b2af26b1 384# define uname PerlEnv_uname
197357d0 385# define stdin PerlSIO_stdin
386# define stdout PerlSIO_stdout
387# define stderr PerlSIO_stderr
f9415d23 388# define fopen PerlSIO_fopen
389# define fclose PerlSIO_fclose
390# define feof PerlSIO_feof
391# define ferror PerlSIO_ferror
b6d726d6 392# define clearerr PerlSIO_clearerr
f9415d23 393# define getc PerlSIO_getc
394# define fputc PerlSIO_fputc
395# define fputs PerlSIO_fputs
396# define fflush PerlSIO_fflush
397# define ungetc PerlSIO_ungetc
398# define fileno PerlSIO_fileno
399# define fdopen PerlSIO_fdopen
400# define freopen PerlSIO_freopen
401# define fread PerlSIO_fread
402# define fwrite PerlSIO_fwrite
22a46b6e 403# define setbuf PerlSIO_setbuf
404# define setvbuf PerlSIO_setvbuf
405# define setlinebuf PerlSIO_setlinebuf
1f59ddd9 406# define stdoutf PerlSIO_stdoutf
407# define vfprintf PerlSIO_vprintf
f9415d23 408# define ftell PerlSIO_ftell
409# define fseek PerlSIO_fseek
410# define fgetpos PerlSIO_fgetpos
411# define fsetpos PerlSIO_fsetpos
412# define frewind PerlSIO_rewind
413# define tmpfile PerlSIO_tmpfile
c6af7a1a 414# define access PerlLIO_access
415# define chmod PerlLIO_chmod
416# define chsize PerlLIO_chsize
417# define close PerlLIO_close
418# define dup PerlLIO_dup
419# define dup2 PerlLIO_dup2
420# define flock PerlLIO_flock
421# define fstat PerlLIO_fstat
422# define ioctl PerlLIO_ioctl
423# define isatty PerlLIO_isatty
6b980173 424# define link PerlLIO_link
c6af7a1a 425# define lseek PerlLIO_lseek
426# define lstat PerlLIO_lstat
427# define mktemp PerlLIO_mktemp
428# define open PerlLIO_open
429# define read PerlLIO_read
430# define rename PerlLIO_rename
431# define setmode PerlLIO_setmode
4f49e16e 432# define stat(buf,sb) PerlLIO_stat(buf,sb)
c6af7a1a 433# define tmpnam PerlLIO_tmpnam
434# define umask PerlLIO_umask
435# define unlink PerlLIO_unlink
436# define utime PerlLIO_utime
437# define write PerlLIO_write
438# define malloc PerlMem_malloc
439# define realloc PerlMem_realloc
440# define free PerlMem_free
441# define abort PerlProc_abort
442# define exit PerlProc_exit
443# define _exit PerlProc__exit
444# define execl PerlProc_execl
445# define execv PerlProc_execv
446# define execvp PerlProc_execvp
447# define getuid PerlProc_getuid
448# define geteuid PerlProc_geteuid
449# define getgid PerlProc_getgid
450# define getegid PerlProc_getegid
451# define getlogin PerlProc_getlogin
452# define kill PerlProc_kill
453# define killpg PerlProc_killpg
454# define pause PerlProc_pause
455# define popen PerlProc_popen
456# define pclose PerlProc_pclose
457# define pipe PerlProc_pipe
458# define setuid PerlProc_setuid
459# define setgid PerlProc_setgid
460# define sleep PerlProc_sleep
461# define times PerlProc_times
462# define wait PerlProc_wait
463# define setjmp PerlProc_setjmp
464# define longjmp PerlProc_longjmp
465# define signal PerlProc_signal
7766f137 466# define getpid PerlProc_getpid
57ab3dfe 467# define gettimeofday PerlProc_gettimeofday
c6af7a1a 468# define htonl PerlSock_htonl
469# define htons PerlSock_htons
470# define ntohl PerlSock_ntohl
471# define ntohs PerlSock_ntohs
472# define accept PerlSock_accept
473# define bind PerlSock_bind
474# define connect PerlSock_connect
475# define endhostent PerlSock_endhostent
476# define endnetent PerlSock_endnetent
477# define endprotoent PerlSock_endprotoent
478# define endservent PerlSock_endservent
479# define gethostbyaddr PerlSock_gethostbyaddr
480# define gethostbyname PerlSock_gethostbyname
481# define gethostent PerlSock_gethostent
482# define gethostname PerlSock_gethostname
483# define getnetbyaddr PerlSock_getnetbyaddr
484# define getnetbyname PerlSock_getnetbyname
485# define getnetent PerlSock_getnetent
486# define getpeername PerlSock_getpeername
487# define getprotobyname PerlSock_getprotobyname
488# define getprotobynumber PerlSock_getprotobynumber
489# define getprotoent PerlSock_getprotoent
490# define getservbyname PerlSock_getservbyname
491# define getservbyport PerlSock_getservbyport
492# define getservent PerlSock_getservent
493# define getsockname PerlSock_getsockname
494# define getsockopt PerlSock_getsockopt
495# define inet_addr PerlSock_inet_addr
496# define inet_ntoa PerlSock_inet_ntoa
497# define listen PerlSock_listen
498# define recv PerlSock_recv
499# define recvfrom PerlSock_recvfrom
500# define select PerlSock_select
501# define send PerlSock_send
502# define sendto PerlSock_sendto
503# define sethostent PerlSock_sethostent
504# define setnetent PerlSock_setnetent
505# define setprotoent PerlSock_setprotoent
506# define setservent PerlSock_setservent
507# define setsockopt PerlSock_setsockopt
508# define shutdown PerlSock_shutdown
509# define socket PerlSock_socket
510# define socketpair PerlSock_socketpair
2986a63f 511# endif /* NETWARE && USE_STDIO */
21c5e947 512
513# ifdef USE_SOCKETS_AS_HANDLES
514# undef fd_set
515# undef FD_SET
516# undef FD_CLR
517# undef FD_ISSET
518# undef FD_ZERO
519# define fd_set Perl_fd_set
520# define FD_SET(n,p) PERL_FD_SET(n,p)
521# define FD_CLR(n,p) PERL_FD_CLR(n,p)
522# define FD_ISSET(n,p) PERL_FD_ISSET(n,p)
523# define FD_ZERO(p) PERL_FD_ZERO(p)
524# endif /* USE_SOCKETS_AS_HANDLES */
525
c6af7a1a 526# endif /* NO_XSLOCKS */
acfe0abc 527#endif /* PERL_IMPLICIT_SYS && !PERL_CORE */
b4ba0ab9 528
cfeeb022 529#endif /* _INC_PERL_XSUB_H */ /* include guard */