All Manuals > LispWorks Foreign Language Interface User Guide and Reference Manual > 4 Advanced Uses of the FLI


4.8 Block objects in C (foreign blocks)

This section applies to LispWorks for Macintosh, only.

Foreign blocks are objects that correspond to the opaque "Block" object in C and derived languages that are introduced in CLANG and used by Apple Computer, Inc.

A "Block" in C is similar to a closure in Lisp. It encapsulates a piece of code, and potentially some variables (which may be local), and allows invocation of this code.

LispWorks foreign blocks allows your Lisp program to call into and get called by code that uses blocks.

A foreign block is represented in LispWorks by a foreign pointer with pointer type foreign-block-pointer. Even though these are foreign pointers, these objects should be regarded as opaque, and should not be manipulated or used except as described below.

You use a foreign block by passing it to a foreign function that is defined to take a block as an argument, or by invoking a block that is received from a foreign function. The argument type needs to be specified as foreign-block-pointer.

When a foreign function invokes a block which was created in Lisp (or a copy of it), this invocation calls a Lisp function which the programmer supplied to the creating function or macro. When Lisp invokes a block that came from foreign code, it invokes some (unknown) foreign code.

Blocks can be used to run code via the Grand Central Dispatch mechanism (GCD) in Mac OS X 10.6 and later (see Apple documentation). There is a simple example in:

(sys:example-file "misc/grand-central-dispatch.lisp")

4.8.1 Calling foreign code that receives a block as argument

4.8.2 Operations on foreign blocks

4.8.3 Scope of invocation

LispWorks Foreign Language Interface User Guide and Reference Manual - 7 Dec 2011