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