6.28.6 Callbacks

In some cases you have to pass a function pointer to a C function, i.e., the library wants to call back to your application (and the pointed-to function is called a callback function). You can pass the address of an existing C function (that you get with lib-sym, see Low-Level C Interface Words), but if there is no appropriate C function, you probably want to define the function as a Forth word. Then you need to generate a callback as described below:

You can generate C callbacks from Forth code with c-callback.

c-callback ( "forth-name" "{type}" "—" "type" –  ) gforth-1.0 “c-callback”

Define a callback instantiator with the given signature. The callback instantiator forth-name ( xt -- addr ) takes an xt, and returns the address of the C function handling that callback.

c-callback-thread ( "forth-name" "{type}" "—" "type" –  ) gforth-1.0 “c-callback-thread”

Define a callback instantiator with the given signature. The callback instantiator forth-name ( xt -- addr ) takes an xt, and returns the address of the C function handling that callback. This callback is safe when called from another thread

This precompiles a number of callback functions (up to the value callback#). The prototype of the C function is deduced from its Forth signature. If this is not sufficient, you can add types in curly braces after the Forth type.

c-callback vector4double: f f f f -- void
c-callback vector4single: f{float} f{float} f{float} f{float} -- void