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