version 1.179, 2007/06/01 18:41:07
|
version 1.180, 2007/06/09 15:55:44
|
Line 406 C Interface
|
Line 406 C Interface
|
|
|
* Calling C Functions:: |
* Calling C Functions:: |
* Declaring C Functions:: |
* Declaring C Functions:: |
|
* Calling C function pointers:: |
* Callbacks:: |
* Callbacks:: |
* C interface internals:: |
* C interface internals:: |
* Low-Level C Interface Words:: |
* Low-Level C Interface Words:: |
Line 11835 as well as a way of declaring structs, u
|
Line 11836 as well as a way of declaring structs, u
|
@menu |
@menu |
* Calling C Functions:: |
* Calling C Functions:: |
* Declaring C Functions:: |
* Declaring C Functions:: |
|
* Calling C function pointers:: |
* Callbacks:: |
* Callbacks:: |
* C interface internals:: |
* C interface internals:: |
* Low-Level C Interface Words:: |
* Low-Level C Interface Words:: |
Line 11908 calling the appropriate word for the des
|
Line 11910 calling the appropriate word for the des
|
|
|
|
|
|
|
@node Declaring C Functions, Callbacks, Calling C Functions, C Interface |
@node Declaring C Functions, Calling C function pointers, Calling C Functions, C Interface |
@subsection Declaring C Functions |
@subsection Declaring C Functions |
@cindex C functions, declarations |
@cindex C functions, declarations |
@cindex declaring C functions |
@cindex declaring C functions |
Line 12011 interface; you can find some examples fo
|
Line 12013 interface; you can find some examples fo
|
in @file{lib.fs}. |
in @file{lib.fs}. |
|
|
|
|
|
@node Calling C function pointers, Callbacks, Declaring C Functions, C Interface |
|
@subsection Calling C function pointers from Forth |
|
@cindex C function pointers, calling from Forth |
|
|
|
If you come across a C function pointer (e.g., in some C-constructed |
|
structure) and want to call it from your Forth program, you can also |
|
use the features explained until now to achieve that, as follows: |
|
|
|
Let us assume that there is a C function pointer type @code{func1} |
|
defined in some header file @file{func1.h}, and you know that these |
|
functions take one integer argument and return an integer result; and |
|
you want to call functions through such pointers. Just define |
|
|
@node Callbacks, C interface internals, Declaring C Functions, C Interface |
@example |
|
\c #include <func1.h> |
|
\c #define call_func1(par1,fptr) ((func1)fptr)(par1) |
|
c-function call-func1 call_func1 n func -- n |
|
@end example |
|
|
|
and then you can call a function pointed to by, say @code{func1a} as |
|
follows: |
|
|
|
@example |
|
-5 func1a call-func1 . |
|
@end example |
|
|
|
In the C part, @code{call_func} is defined as a macro to avoid having |
|
to declare the exact parameter and return types, so the C compiler |
|
knows them from the declaration of @code{func1}. |
|
|
|
The Forth word @code{call-func1} is similar to @code{execute}, except |
|
that it takes a C @code{func1} pointer instead of a Forth execution |
|
token, and it is specific to @code{func1} pointers. For each type of |
|
function pointer you want to call from Forth, you have to define |
|
a separate calling word. |
|
|
|
|
|
@node Callbacks, C interface internals, Calling C function pointers, C Interface |
@subsection Callbacks |
@subsection Callbacks |
@cindex Callback functions written in Forth |
@cindex Callback functions written in Forth |
@cindex C function pointers to Forth words |
@cindex C function pointers to Forth words |