CALLBACK:
Factor handbook » C library interface » Passing data between Factor and C » C type specifiers

Prev:UNION-STRUCT:
Next:TYPEDEF:


Vocabulary
alien.syntax

Syntax
CALLBACK: return type ( parameters )


Inputs
None

Outputs
None

Word description
Defines a new function pointer C type word type. The newly defined word works both as a C type and as a wrapper for alien-callback for callbacks that accept the given return type and parameters. The ABI of the callback is decided from the ABI of the active LIBRARY: declaration.

On ARM64, a terminal ellipsis declares a runtime variadic callback: CALLBACK: int sum-callback ( int count, ... ). Its quotation receives the named arguments followed by an argument cursor. Read the tail with va-arg from alien.varargs, using the types prescribed by the C API. An ellipsis followed by type/name pairs declares a fixed tail instead: CALLBACK: double event-callback ( int tag, ... int code, double value ). Its quotation receives those values directly.

A callback parameter explicitly typed va_list is a different C interface: import the ABI-aware type from alien.varargs. Its value is a borrowed cursor on ARM64. Cursors, including copies, are usable only during their originating callback execution; they cannot be retained after return or accessed from a nested callback. The outer callback may resume using its cursor after the nested call returns.

A cursor has no discoverable length or argument types. Supplying the wrong type or reading beyond the C caller's tail is invalid.

Examples
CALLBACK: bool FakeCallback ( int message, void* payload ) : MyFakeCallback ( -- alien ) [| message payload | "message #" write message number>string write " received" print t ] FakeCallback ;


Definition