Message-Id: <200107121529.LAA23411@bottesini.harvard.edu>
[p5sagit/p5-mst-13.2.git] / XSUB.h
CommitLineData
b4ba0ab9 1#ifndef _INC_PERL_XSUB_H
2#define _INC_PERL_XSUB_H 1
3
954c1994 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 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 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 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 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 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 67#endif
68
349b520e 69#define dAX I32 ax = MARK - PL_stack_base + 1
9f2ea798 70
71#define dITEMS I32 items = SP - MARK
72
a0d0e21e 73#define dXSARGS \
3c78fafa 74 dSP; dMARK; \
9f2ea798 75 dAX; dITEMS
a0d0e21e 76
8a7fc0dc 77#define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
78 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
79
b26a54d0 80/* Should be used before final PUSHi etc. if not in PPCODE section. */
81#define XSprePUSH (sp = PL_stack_base + ax - 1)
82
a0d0e21e 83#define XSANY CvXSUBANY(cv)
84
85#define dXSI32 I32 ix = XSANY.any_i32
86
cfc02341 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)
2554589c 93#define XSINTERFACE_FUNC(ret,cv,f) ((XSINTERFACE_CVT(ret,))(f))
cfc02341 94#define XSINTERFACE_FUNC_SET(cv,f) \
51371543 95 CvXSUBANY(cv).any_dptr = (void (*) (pTHXo_ void*))(f)
cfc02341 96
748a9306 97/* Simple macros to put new mortal values onto the stack. */
98/* Typically used to return values from XS functions. */
954c1994 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 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 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 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 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 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 190
191#ifdef XS_VERSION
c6af7a1a 192# define XS_VERSION_BOOTCHECK \
774d564b 193 STMT_START { \
2d8e6c8d 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 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 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 245# define VTBL_amagic &PL_vtbl_amagic
246# define VTBL_amagicelem &PL_vtbl_amagicelem
dc9e4912 247#endif
248
6f4183fe 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 253# undef aTHX
254# undef aTHX_
54aff467 255# define aTHX PERL_GET_THX
256# define aTHX_ aTHX,
54aff467 257#endif
258
32e30700 259#if (defined(PERL_CAPI) || defined(PERL_IMPLICIT_SYS)) && !defined(PERL_CORE)
c6af7a1a 260# ifndef NO_XSLOCKS
2986a63f 261# if defined (NETWARE) && defined (USE_STDIO)
262# define times PerlProc_times
263# define setuid PerlProc_setuid
264# define setgid PerlProc_setgid
265# define getpid PerlProc_getpid
266# define pause PerlProc_pause
267# define exit PerlProc_exit
268# define _exit PerlProc__exit
269# else
c6af7a1a 270# undef closedir
271# undef opendir
272# undef stdin
273# undef stdout
274# undef stderr
275# undef feof
276# undef ferror
277# undef fgetpos
278# undef ioctl
279# undef getlogin
280# undef setjmp
281# undef getc
282# undef ungetc
283# undef fileno
284
2986a63f 285//Following symbols were giving redefinition errors while building extensions - sgp 17th Oct 2000
286#ifdef NETWARE
287# undef readdir
288# undef fstat
289# undef stat
290# undef longjmp
291# undef endhostent
292# undef endnetent
293# undef endprotoent
294# undef endservent
295# undef gethostbyaddr
296# undef gethostbyname
297# undef gethostent
298# undef getnetbyaddr
299# undef getnetbyname
300# undef getnetent
301# undef getprotobyname
302# undef getprotobynumber
303# undef getprotoent
304# undef getservbyname
305# undef getservbyport
306# undef getservent
307# undef inet_ntoa
308# undef sethostent
309# undef setnetent
310# undef setprotoent
311# undef setservent
312#endif /* NETWARE */
313
c6af7a1a 314# define mkdir PerlDir_mkdir
315# define chdir PerlDir_chdir
316# define rmdir PerlDir_rmdir
317# define closedir PerlDir_close
318# define opendir PerlDir_open
319# define readdir PerlDir_read
320# define rewinddir PerlDir_rewind
321# define seekdir PerlDir_seek
322# define telldir PerlDir_tell
323# define putenv PerlEnv_putenv
324# define getenv PerlEnv_getenv
b2af26b1 325# define uname PerlEnv_uname
c6af7a1a 326# define stdin PerlIO_stdin()
327# define stdout PerlIO_stdout()
328# define stderr PerlIO_stderr()
329# define fopen PerlIO_open
330# define fclose PerlIO_close
331# define feof PerlIO_eof
332# define ferror PerlIO_error
333# define fclearerr PerlIO_clearerr
334# define getc PerlIO_getc
335# define fputc(c, f) PerlIO_putc(f,c)
336# define fputs(s, f) PerlIO_puts(f,s)
337# define fflush PerlIO_flush
338# define ungetc(c, f) PerlIO_ungetc((f),(c))
339# define fileno PerlIO_fileno
340# define fdopen PerlIO_fdopen
341# define freopen PerlIO_reopen
342# define fread(b,s,c,f) PerlIO_read((f),(b),(s*c))
343# define fwrite(b,s,c,f) PerlIO_write((f),(b),(s*c))
344# define setbuf PerlIO_setbuf
345# define setvbuf PerlIO_setvbuf
346# define setlinebuf PerlIO_setlinebuf
347# define stdoutf PerlIO_stdoutf
348# define vfprintf PerlIO_vprintf
349# define ftell PerlIO_tell
350# define fseek PerlIO_seek
351# define fgetpos PerlIO_getpos
352# define fsetpos PerlIO_setpos
353# define frewind PerlIO_rewind
354# define tmpfile PerlIO_tmpfile
355# define access PerlLIO_access
356# define chmod PerlLIO_chmod
357# define chsize PerlLIO_chsize
358# define close PerlLIO_close
359# define dup PerlLIO_dup
360# define dup2 PerlLIO_dup2
361# define flock PerlLIO_flock
362# define fstat PerlLIO_fstat
363# define ioctl PerlLIO_ioctl
364# define isatty PerlLIO_isatty
6b980173 365# define link PerlLIO_link
c6af7a1a 366# define lseek PerlLIO_lseek
367# define lstat PerlLIO_lstat
368# define mktemp PerlLIO_mktemp
369# define open PerlLIO_open
370# define read PerlLIO_read
371# define rename PerlLIO_rename
372# define setmode PerlLIO_setmode
4f49e16e 373# define stat(buf,sb) PerlLIO_stat(buf,sb)
c6af7a1a 374# define tmpnam PerlLIO_tmpnam
375# define umask PerlLIO_umask
376# define unlink PerlLIO_unlink
377# define utime PerlLIO_utime
378# define write PerlLIO_write
379# define malloc PerlMem_malloc
380# define realloc PerlMem_realloc
381# define free PerlMem_free
382# define abort PerlProc_abort
383# define exit PerlProc_exit
384# define _exit PerlProc__exit
385# define execl PerlProc_execl
386# define execv PerlProc_execv
387# define execvp PerlProc_execvp
388# define getuid PerlProc_getuid
389# define geteuid PerlProc_geteuid
390# define getgid PerlProc_getgid
391# define getegid PerlProc_getegid
392# define getlogin PerlProc_getlogin
393# define kill PerlProc_kill
394# define killpg PerlProc_killpg
395# define pause PerlProc_pause
396# define popen PerlProc_popen
397# define pclose PerlProc_pclose
398# define pipe PerlProc_pipe
399# define setuid PerlProc_setuid
400# define setgid PerlProc_setgid
401# define sleep PerlProc_sleep
402# define times PerlProc_times
403# define wait PerlProc_wait
404# define setjmp PerlProc_setjmp
405# define longjmp PerlProc_longjmp
406# define signal PerlProc_signal
7766f137 407# define getpid PerlProc_getpid
c6af7a1a 408# define htonl PerlSock_htonl
409# define htons PerlSock_htons
410# define ntohl PerlSock_ntohl
411# define ntohs PerlSock_ntohs
412# define accept PerlSock_accept
413# define bind PerlSock_bind
414# define connect PerlSock_connect
415# define endhostent PerlSock_endhostent
416# define endnetent PerlSock_endnetent
417# define endprotoent PerlSock_endprotoent
418# define endservent PerlSock_endservent
419# define gethostbyaddr PerlSock_gethostbyaddr
420# define gethostbyname PerlSock_gethostbyname
421# define gethostent PerlSock_gethostent
422# define gethostname PerlSock_gethostname
423# define getnetbyaddr PerlSock_getnetbyaddr
424# define getnetbyname PerlSock_getnetbyname
425# define getnetent PerlSock_getnetent
426# define getpeername PerlSock_getpeername
427# define getprotobyname PerlSock_getprotobyname
428# define getprotobynumber PerlSock_getprotobynumber
429# define getprotoent PerlSock_getprotoent
430# define getservbyname PerlSock_getservbyname
431# define getservbyport PerlSock_getservbyport
432# define getservent PerlSock_getservent
433# define getsockname PerlSock_getsockname
434# define getsockopt PerlSock_getsockopt
435# define inet_addr PerlSock_inet_addr
436# define inet_ntoa PerlSock_inet_ntoa
437# define listen PerlSock_listen
438# define recv PerlSock_recv
439# define recvfrom PerlSock_recvfrom
440# define select PerlSock_select
441# define send PerlSock_send
442# define sendto PerlSock_sendto
443# define sethostent PerlSock_sethostent
444# define setnetent PerlSock_setnetent
445# define setprotoent PerlSock_setprotoent
446# define setservent PerlSock_setservent
447# define setsockopt PerlSock_setsockopt
448# define shutdown PerlSock_shutdown
449# define socket PerlSock_socket
450# define socketpair PerlSock_socketpair
2986a63f 451# endif /* NETWARE && USE_STDIO */
c6af7a1a 452# endif /* NO_XSLOCKS */
c5be433b 453#endif /* PERL_CAPI */
b4ba0ab9 454
cfeeb022 455#endif /* _INC_PERL_XSUB_H */ /* include guard */