3 * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
11 #ifndef _INC_PERL_XSUB_H
12 #define _INC_PERL_XSUB_H 1
14 /* first, some documentation for xsubpp-generated items */
17 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
19 =for apidoc Amn|char*|CLASS
20 Variable which is setup by C<xsubpp> to indicate the
21 class name for a C++ XS constructor. This is always a C<char*>. See C<THIS>.
23 =for apidoc Amn|(whatever)|RETVAL
24 Variable which is setup by C<xsubpp> to hold the return value for an
25 XSUB. This is always the proper type for the XSUB. See
26 L<perlxs/"The RETVAL Variable">.
28 =for apidoc Amn|(whatever)|THIS
29 Variable which is setup by C<xsubpp> to designate the object in a C++
30 XSUB. This is always the proper type for the C++ object. See C<CLASS> and
31 L<perlxs/"Using XS With C++">.
33 =for apidoc Amn|I32|ax
34 Variable which is setup by C<xsubpp> to indicate the stack base offset,
35 used by the C<ST>, C<XSprePUSH> and C<XSRETURN> macros. The C<dMARK> macro
36 must be called prior to setup the C<MARK> variable.
38 =for apidoc Amn|I32|items
39 Variable which is setup by C<xsubpp> to indicate the number of
40 items on the stack. See L<perlxs/"Variable-length Parameter Lists">.
42 =for apidoc Amn|I32|ix
43 Variable which is setup by C<xsubpp> to indicate which of an
44 XSUB's aliases was used to invoke it. See L<perlxs/"The ALIAS: Keyword">.
46 =for apidoc Am|SV*|ST|int ix
47 Used to access elements on the XSUB's stack.
50 Macro to declare an XSUB and its C parameter list. This is handled by
54 Sets up the C<ax> variable.
55 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
57 =for apidoc Ams||dITEMS
58 Sets up the C<items> variable.
59 This is usually handled automatically by C<xsubpp> by calling C<dXSARGS>.
61 =for apidoc Ams||dXSARGS
62 Sets up stack and mark pointers for an XSUB, calling dSP and dMARK.
63 Sets up the C<ax> and C<items> variables by calling C<dAX> and C<dITEMS>.
64 This is usually handled automatically by C<xsubpp>.
66 =for apidoc Ams||dXSI32
67 Sets up the C<ix> variable for an XSUB which has aliases. This is usually
68 handled automatically by C<xsubpp>.
73 #define ST(off) PL_stack_base[ax + (off)]
75 #if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
76 # define XS(name) __declspec(dllexport) void name(pTHX_ CV* cv)
78 # define XS(name) void name(pTHX_ CV* cv)
81 #define dAX I32 ax = MARK - PL_stack_base + 1
83 #define dITEMS I32 items = SP - MARK
89 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
90 ? PAD_SV(PL_op->op_targ) : sv_newmortal())
92 /* Should be used before final PUSHi etc. if not in PPCODE section. */
93 #define XSprePUSH (sp = PL_stack_base + ax - 1)
95 #define XSANY CvXSUBANY(cv)
97 #define dXSI32 I32 ix = XSANY.any_i32
100 # define XSINTERFACE_CVT(ret,name) ret (*name)(...)
102 # define XSINTERFACE_CVT(ret,name) ret (*name)()
104 #define dXSFUNCTION(ret) XSINTERFACE_CVT(ret,XSFUNCTION)
105 #define XSINTERFACE_FUNC(ret,cv,f) ((XSINTERFACE_CVT(ret,))(f))
106 #define XSINTERFACE_FUNC_SET(cv,f) \
107 CvXSUBANY(cv).any_dxptr = (void (*) (pTHX_ void*))(f)
109 /* Simple macros to put new mortal values onto the stack. */
110 /* Typically used to return values from XS functions. */
113 =head1 Stack Manipulation Macros
115 =for apidoc Am|void|XST_mIV|int pos|IV iv
116 Place an integer into the specified position C<pos> on the stack. The
117 value is stored in a new mortal SV.
119 =for apidoc Am|void|XST_mNV|int pos|NV nv
120 Place a double into the specified position C<pos> on the stack. The value
121 is stored in a new mortal SV.
123 =for apidoc Am|void|XST_mPV|int pos|char* str
124 Place a copy of a string into the specified position C<pos> on the stack.
125 The value is stored in a new mortal SV.
127 =for apidoc Am|void|XST_mNO|int pos
128 Place C<&PL_sv_no> into the specified position C<pos> on the
131 =for apidoc Am|void|XST_mYES|int pos
132 Place C<&PL_sv_yes> into the specified position C<pos> on the
135 =for apidoc Am|void|XST_mUNDEF|int pos
136 Place C<&PL_sv_undef> into the specified position C<pos> on the
139 =for apidoc Am|void|XSRETURN|int nitems
140 Return from XSUB, indicating number of items on the stack. This is usually
141 handled by C<xsubpp>.
143 =for apidoc Am|void|XSRETURN_IV|IV iv
144 Return an integer from an XSUB immediately. Uses C<XST_mIV>.
146 =for apidoc Am|void|XSRETURN_UV|IV uv
147 Return an integer from an XSUB immediately. Uses C<XST_mUV>.
149 =for apidoc Am|void|XSRETURN_NV|NV nv
150 Return a double from an XSUB immediately. Uses C<XST_mNV>.
152 =for apidoc Am|void|XSRETURN_PV|char* str
153 Return a copy of a string from an XSUB immediately. Uses C<XST_mPV>.
155 =for apidoc Ams||XSRETURN_NO
156 Return C<&PL_sv_no> from an XSUB immediately. Uses C<XST_mNO>.
158 =for apidoc Ams||XSRETURN_YES
159 Return C<&PL_sv_yes> from an XSUB immediately. Uses C<XST_mYES>.
161 =for apidoc Ams||XSRETURN_UNDEF
162 Return C<&PL_sv_undef> from an XSUB immediately. Uses C<XST_mUNDEF>.
164 =for apidoc Ams||XSRETURN_EMPTY
165 Return an empty list from an XSUB immediately.
167 =head1 Variables created by C<xsubpp> and C<xsubpp> internal functions
169 =for apidoc AmU||newXSproto
170 Used by C<xsubpp> to hook up XSUBs as Perl subs. Adds Perl prototypes to
173 =for apidoc AmU||XS_VERSION
174 The version identifier for an XS module. This is usually
175 handled automatically by C<ExtUtils::MakeMaker>. See C<XS_VERSION_BOOTCHECK>.
177 =for apidoc Ams||XS_VERSION_BOOTCHECK
178 Macro to verify that a PM module's $VERSION variable matches the XS
179 module's C<XS_VERSION> variable. This is usually handled automatically by
180 C<xsubpp>. See L<perlxs/"The VERSIONCHECK: Keyword">.
185 #define XST_mIV(i,v) (ST(i) = sv_2mortal(newSViv(v)) )
186 #define XST_mUV(i,v) (ST(i) = sv_2mortal(newSVuv(v)) )
187 #define XST_mNV(i,v) (ST(i) = sv_2mortal(newSVnv(v)) )
188 #define XST_mPV(i,v) (ST(i) = sv_2mortal(newSVpv(v,0)))
189 #define XST_mPVN(i,v,n) (ST(i) = sv_2mortal(newSVpvn(v,n)))
190 #define XST_mNO(i) (ST(i) = &PL_sv_no )
191 #define XST_mYES(i) (ST(i) = &PL_sv_yes )
192 #define XST_mUNDEF(i) (ST(i) = &PL_sv_undef)
194 #define XSRETURN(off) \
196 PL_stack_sp = PL_stack_base + ax + ((off) - 1); \
200 #define XSRETURN_IV(v) STMT_START { XST_mIV(0,v); XSRETURN(1); } STMT_END
201 #define XSRETURN_UV(v) STMT_START { XST_mUV(0,v); XSRETURN(1); } STMT_END
202 #define XSRETURN_NV(v) STMT_START { XST_mNV(0,v); XSRETURN(1); } STMT_END
203 #define XSRETURN_PV(v) STMT_START { XST_mPV(0,v); XSRETURN(1); } STMT_END
204 #define XSRETURN_PVN(v,n) STMT_START { XST_mPVN(0,v,n); XSRETURN(1); } STMT_END
205 #define XSRETURN_NO STMT_START { XST_mNO(0); XSRETURN(1); } STMT_END
206 #define XSRETURN_YES STMT_START { XST_mYES(0); XSRETURN(1); } STMT_END
207 #define XSRETURN_UNDEF STMT_START { XST_mUNDEF(0); XSRETURN(1); } STMT_END
208 #define XSRETURN_EMPTY STMT_START { XSRETURN(0); } STMT_END
210 #define newXSproto(a,b,c,d) sv_setpv((SV*)newXS(a,b,c), d)
213 # define XS_VERSION_BOOTCHECK \
215 SV *tmpsv; STRLEN n_a; \
216 char *vn = Nullch, *module = SvPV(ST(0),n_a); \
217 if (items >= 2) /* version supplied as bootstrap arg */ \
220 /* XXX GV_ADDWARN */ \
221 tmpsv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
222 vn = "XS_VERSION"), FALSE); \
223 if (!tmpsv || !SvOK(tmpsv)) \
224 tmpsv = get_sv(Perl_form(aTHX_ "%s::%s", module, \
225 vn = "VERSION"), FALSE); \
227 if (tmpsv && (!SvOK(tmpsv) || strNE(XS_VERSION, SvPV(tmpsv, n_a)))) \
228 Perl_croak(aTHX_ "%s object version %s does not match %s%s%s%s %"SVf,\
229 module, XS_VERSION, \
230 vn ? "$" : "", vn ? module : "", vn ? "::" : "", \
231 vn ? vn : "bootstrap parameter", tmpsv); \
234 # define XS_VERSION_BOOTCHECK
238 The DBM_setFilter & DBM_ckFilter macros are only used by
239 the *DB*_File modules
242 #define DBM_setFilter(db_type,code) \
245 RETVAL = sv_mortalcopy(db_type) ; \
247 if (db_type && (code == &PL_sv_undef)) { \
248 SvREFCNT_dec(db_type) ; \
253 sv_setsv(db_type, code) ; \
255 db_type = newSVsv(code) ; \
259 #define DBM_ckFilter(arg,type,name) \
261 if (db->filtering) { \
262 croak("recursion detected in %s", name) ; \
266 SAVEINT(db->filtering) ; \
267 db->filtering = TRUE ; \
273 (void) perl_call_sv(db->type, G_DISCARD); \
280 #if 1 /* for compatibility */
281 # define VTBL_sv &PL_vtbl_sv
282 # define VTBL_env &PL_vtbl_env
283 # define VTBL_envelem &PL_vtbl_envelem
284 # define VTBL_sig &PL_vtbl_sig
285 # define VTBL_sigelem &PL_vtbl_sigelem
286 # define VTBL_pack &PL_vtbl_pack
287 # define VTBL_packelem &PL_vtbl_packelem
288 # define VTBL_dbline &PL_vtbl_dbline
289 # define VTBL_isa &PL_vtbl_isa
290 # define VTBL_isaelem &PL_vtbl_isaelem
291 # define VTBL_arylen &PL_vtbl_arylen
292 # define VTBL_glob &PL_vtbl_glob
293 # define VTBL_mglob &PL_vtbl_mglob
294 # define VTBL_nkeys &PL_vtbl_nkeys
295 # define VTBL_taint &PL_vtbl_taint
296 # define VTBL_substr &PL_vtbl_substr
297 # define VTBL_vec &PL_vtbl_vec
298 # define VTBL_pos &PL_vtbl_pos
299 # define VTBL_bm &PL_vtbl_bm
300 # define VTBL_fm &PL_vtbl_fm
301 # define VTBL_uvar &PL_vtbl_uvar
302 # define VTBL_defelem &PL_vtbl_defelem
303 # define VTBL_regexp &PL_vtbl_regexp
304 # define VTBL_regdata &PL_vtbl_regdata
305 # define VTBL_regdatum &PL_vtbl_regdatum
306 # ifdef USE_LOCALE_COLLATE
307 # define VTBL_collxfrm &PL_vtbl_collxfrm
309 # define VTBL_amagic &PL_vtbl_amagic
310 # define VTBL_amagicelem &PL_vtbl_amagicelem
315 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_GET_CONTEXT) && !defined(PERL_CORE)
318 # define aTHX PERL_GET_THX
322 #if defined(PERL_IMPLICIT_SYS) && !defined(PERL_CORE)
324 # if defined (NETWARE) && defined (USE_STDIO)
325 # define times PerlProc_times
326 # define setuid PerlProc_setuid
327 # define setgid PerlProc_setgid
328 # define getpid PerlProc_getpid
329 # define pause PerlProc_pause
330 # define exit PerlProc_exit
331 # define _exit PerlProc__exit
348 /* Following symbols were giving redefinition errors while building extensions - sgp 17th Oct 2000 */
358 # undef gethostbyaddr
359 # undef gethostbyname
364 # undef getprotobyname
365 # undef getprotobynumber
367 # undef getservbyname
368 # undef getservbyport
379 # define mkdir PerlDir_mkdir
380 # define chdir PerlDir_chdir
381 # define rmdir PerlDir_rmdir
382 # define closedir PerlDir_close
383 # define opendir PerlDir_open
384 # define readdir PerlDir_read
385 # define rewinddir PerlDir_rewind
386 # define seekdir PerlDir_seek
387 # define telldir PerlDir_tell
388 # define putenv PerlEnv_putenv
389 # define getenv PerlEnv_getenv
390 # define uname PerlEnv_uname
391 # define stdin PerlSIO_stdin
392 # define stdout PerlSIO_stdout
393 # define stderr PerlSIO_stderr
394 # define fopen PerlSIO_fopen
395 # define fclose PerlSIO_fclose
396 # define feof PerlSIO_feof
397 # define ferror PerlSIO_ferror
398 # define clearerr PerlSIO_clearerr
399 # define getc PerlSIO_getc
400 # define fputc PerlSIO_fputc
401 # define fputs PerlSIO_fputs
402 # define fflush PerlSIO_fflush
403 # define ungetc PerlSIO_ungetc
404 # define fileno PerlSIO_fileno
405 # define fdopen PerlSIO_fdopen
406 # define freopen PerlSIO_freopen
407 # define fread PerlSIO_fread
408 # define fwrite PerlSIO_fwrite
409 # define setbuf PerlSIO_setbuf
410 # define setvbuf PerlSIO_setvbuf
411 # define setlinebuf PerlSIO_setlinebuf
412 # define stdoutf PerlSIO_stdoutf
413 # define vfprintf PerlSIO_vprintf
414 # define ftell PerlSIO_ftell
415 # define fseek PerlSIO_fseek
416 # define fgetpos PerlSIO_fgetpos
417 # define fsetpos PerlSIO_fsetpos
418 # define frewind PerlSIO_rewind
419 # define tmpfile PerlSIO_tmpfile
420 # define access PerlLIO_access
421 # define chmod PerlLIO_chmod
422 # define chsize PerlLIO_chsize
423 # define close PerlLIO_close
424 # define dup PerlLIO_dup
425 # define dup2 PerlLIO_dup2
426 # define flock PerlLIO_flock
427 # define fstat PerlLIO_fstat
428 # define ioctl PerlLIO_ioctl
429 # define isatty PerlLIO_isatty
430 # define link PerlLIO_link
431 # define lseek PerlLIO_lseek
432 # define lstat PerlLIO_lstat
433 # define mktemp PerlLIO_mktemp
434 # define open PerlLIO_open
435 # define read PerlLIO_read
436 # define rename PerlLIO_rename
437 # define setmode PerlLIO_setmode
438 # define stat(buf,sb) PerlLIO_stat(buf,sb)
439 # define tmpnam PerlLIO_tmpnam
440 # define umask PerlLIO_umask
441 # define unlink PerlLIO_unlink
442 # define utime PerlLIO_utime
443 # define write PerlLIO_write
444 # define malloc PerlMem_malloc
445 # define realloc PerlMem_realloc
446 # define free PerlMem_free
447 # define abort PerlProc_abort
448 # define exit PerlProc_exit
449 # define _exit PerlProc__exit
450 # define execl PerlProc_execl
451 # define execv PerlProc_execv
452 # define execvp PerlProc_execvp
453 # define getuid PerlProc_getuid
454 # define geteuid PerlProc_geteuid
455 # define getgid PerlProc_getgid
456 # define getegid PerlProc_getegid
457 # define getlogin PerlProc_getlogin
458 # define kill PerlProc_kill
459 # define killpg PerlProc_killpg
460 # define pause PerlProc_pause
461 # define popen PerlProc_popen
462 # define pclose PerlProc_pclose
463 # define pipe PerlProc_pipe
464 # define setuid PerlProc_setuid
465 # define setgid PerlProc_setgid
466 # define sleep PerlProc_sleep
467 # define times PerlProc_times
468 # define wait PerlProc_wait
469 # define setjmp PerlProc_setjmp
470 # define longjmp PerlProc_longjmp
471 # define signal PerlProc_signal
472 # define getpid PerlProc_getpid
473 # define gettimeofday PerlProc_gettimeofday
474 # define htonl PerlSock_htonl
475 # define htons PerlSock_htons
476 # define ntohl PerlSock_ntohl
477 # define ntohs PerlSock_ntohs
478 # define accept PerlSock_accept
479 # define bind PerlSock_bind
480 # define connect PerlSock_connect
481 # define endhostent PerlSock_endhostent
482 # define endnetent PerlSock_endnetent
483 # define endprotoent PerlSock_endprotoent
484 # define endservent PerlSock_endservent
485 # define gethostbyaddr PerlSock_gethostbyaddr
486 # define gethostbyname PerlSock_gethostbyname
487 # define gethostent PerlSock_gethostent
488 # define gethostname PerlSock_gethostname
489 # define getnetbyaddr PerlSock_getnetbyaddr
490 # define getnetbyname PerlSock_getnetbyname
491 # define getnetent PerlSock_getnetent
492 # define getpeername PerlSock_getpeername
493 # define getprotobyname PerlSock_getprotobyname
494 # define getprotobynumber PerlSock_getprotobynumber
495 # define getprotoent PerlSock_getprotoent
496 # define getservbyname PerlSock_getservbyname
497 # define getservbyport PerlSock_getservbyport
498 # define getservent PerlSock_getservent
499 # define getsockname PerlSock_getsockname
500 # define getsockopt PerlSock_getsockopt
501 # define inet_addr PerlSock_inet_addr
502 # define inet_ntoa PerlSock_inet_ntoa
503 # define listen PerlSock_listen
504 # define recv PerlSock_recv
505 # define recvfrom PerlSock_recvfrom
506 # define select PerlSock_select
507 # define send PerlSock_send
508 # define sendto PerlSock_sendto
509 # define sethostent PerlSock_sethostent
510 # define setnetent PerlSock_setnetent
511 # define setprotoent PerlSock_setprotoent
512 # define setservent PerlSock_setservent
513 # define setsockopt PerlSock_setsockopt
514 # define shutdown PerlSock_shutdown
515 # define socket PerlSock_socket
516 # define socketpair PerlSock_socketpair
517 # endif /* NETWARE && USE_STDIO */
519 # ifdef USE_SOCKETS_AS_HANDLES
525 # define fd_set Perl_fd_set
526 # define FD_SET(n,p) PERL_FD_SET(n,p)
527 # define FD_CLR(n,p) PERL_FD_CLR(n,p)
528 # define FD_ISSET(n,p) PERL_FD_ISSET(n,p)
529 # define FD_ZERO(p) PERL_FD_ZERO(p)
530 # endif /* USE_SOCKETS_AS_HANDLES */
532 # endif /* NO_XSLOCKS */
533 #endif /* PERL_IMPLICIT_SYS && !PERL_CORE */
535 #endif /* _INC_PERL_XSUB_H */ /* include guard */