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