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