source: project/wiki/Callbacks @ 12863

Last change on this file since 12863 was 7273, checked in by felix winkelmann, 12 years ago

updates

File size: 3.9 KB
Line 
1[[tags: manual]]
2[[toc:]]
3
4== Callbacks
5
6
7To enable an external C function to call back to Scheme, the form
8{{foreign-safe-lambda}} (or {{foreign-safe-lambda*}})
9has to be used. This generates special code to save and restore important
10state information during execution of C code. There are two ways of
11calling Scheme procedures from C: the first is to invoke the runtime
12function {{C_callback}} with the closure to be called and the number
13of arguments.  The second is to define an externally visible wrapper
14function around a Scheme procedure with the {{define-external}}
15form.
16
17Note: the names of all functions, variables and macros exported by the
18CHICKEN runtime system start with {{C_}}. It is advisable to
19use a different naming scheme for your own code to avoid name clashes.
20Callbacks (defined by {{define-external}})
21do not capture the lexical environment.
22
23Non-local exits leaving the scope of the invocation of a callback from Scheme into C
24will not remove the C call-frame from the stack (and will result in a memory
25leak).
26
27
28
29=== define-external
30
31 [syntax] (define-external [QUALIFIERS] (NAME (ARGUMENTTYPE1 VARIABLE1) ...) RETURNTYPE BODY ...)
32 [syntax] (define-external NAME TYPE [INIT])
33
34The first form defines an externally callable Scheme
35procedure. {{NAME}} should be a symbol, which, when converted to a
36string, represents a legal C identifier. {{ARGUMENTTYPE1 ...}} and
37{{RETURNTYPE}} are foreign type specifiers for the argument variables
38{{VAR1 ...}} and the result, respectively.  {{QUALIFIERS}}
39is an optional qualifier for the foreign procedure definition, like
40{{__stdcall}}.
41
42<enscript highlight=scheme>
43(define-external (foo (c-string x)) int (string-length x))
44</enscript>
45
46The second form of {{define-external}} can be used to define
47variables that are accessible from foreign code. It declares
48a global variable named by the symbol {{NAME}} that
49has the type {{TYPE}}. {{INIT}} can be an arbitrary
50expression that is used to initialize the variable. {{NAME}} is
51accessible from Scheme just like any other foreign variable defined by
52{{define-foreign-variable}}. 
53
54<enscript highlight=scheme>
55(define-external foo int 42)
56((foreign-lambda* int ()
57  "C_return(foo);"))           ==> 42
58</enscript>
59
60'''Note:''' don't be tempted to
61assign strings or bytevectors to external variables. Garbage collection
62moves those objects around, so it is very bad idea to assign pointers
63to heap-data. If you have to do so, then copy the data object into
64statically allocated memory (for example by using {{object-evict}}).
65
66Results of type {{scheme-object}} returned by {{define-external}}
67are always allocated in the secondary heap, that is, not in the stack.
68=== C_callback
69
70 [C function] C_word C_callback (C_word closure, int argc)
71
72This function can be used to invoke the Scheme procedure {{closure}}.
73{{argc}} should contain the number of arguments that are passed to
74the procedure on the temporary stack. Values are put onto the temporary
75stack with the {{C_save}} macro.
76
77=== C_callback_adjust_stack
78
79 [C function] void C_callback_adjust_stack (C_word *ptr, int size)
80
81The runtime-system uses the stack as a special allocation area and
82internally holds pointers to estimated limits to distinguish between
83Scheme data objects inside the stack from objects outside of it.  If
84you invoke callbacks at wildly differing stack-levels, these limits
85may shift from invocation to invocation. Callbacks defined with
86{{define-external}} will perform appropriate adjustments
87automatically, but if you invoke {{C_callback}} manually, you should
88perform a {{C_callback_adjust_stack}} to make sure the internal limits
89are set properly. {{ptr}} should point to some data object on the
90stack and {{size}} is the number of words contained in the data object
91(or some estimate). The call will make sure the limits are adjusted so
92that the value pointed to by {{ptr}} is located in the stack.
93
94Previous: [[Embedding]]
95
96Next: [[Locations]]
Note: See TracBrowser for help on using the repository browser.