chickadee » jiffi » define-callback

(define-callback ...)syntax

Create a C function using Scheme code, that can be called from C code or used as a C callback. See the Callbacks in Jiffi guide.

Usage:

(define-callback (SCHEME-NAME C_NAME)   ; or SCHEME-NAME
  quals:  "QUALIFIER ..."               ; optional
  return: RETURN-TYPE                   ; optional
  args:   ((ARG-TYPE ARG-NAME) ...)     ; optional
  BODY
  ...)

SCHEME-NAME is a variable to define in Scheme. It will hold a pointer to the C function that this macro creates. It can be passed to a C function that expects a callback function pointer, for example.

C_NAME is a non-quoted symbol or string specifying the name of the function to define in C. It must be a valid C function name. In addition to being defined as a C function, C_NAME is also defined as a Scheme procedure.

If you don't care what C_NAME to define, you can write SCHEME-NAME instead of (SCHEME-NAME C_NAME). A valid C_NAME will be automatically generated. It will be unpredictable to help avoid name collisions.

"QUALIFIER ..." is an optional string containing C type qualifiers for the function, such as "__stdcall const". If the quals: clause is omitted, no qualifiers are used.

RETURN-TYPE is a foreign type specifier describing the return value. If the return: clause is omitted, the return type is void.

Each ARG-TYPE is a foreign type specifier describing the argument. Each ARG-NAME is a non-quoted symbol which is used as the variable name within the body. If the args: clause is omitted, the function accepts no arguments.

Each BODY is a Scheme expression. The value of the final BODY expression must the match the return type.

This macro uses define-external to define the C function, and location to get the C function pointer.