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