Download Larceny User Manual

Transcript
Larceny User Manual
between the two domains.
10.19.3. Marshalling via ffi-attributes
This section describes the marshalling protocol defined in lib/Base/std-ffi.sch.
Foreign functions automatically marshal their inputs and outputs according to type-descriptors attached to
each foreign function.
Type-descriptors are S-expressons formed according to the following grammar:
TypeDesc ::= CoreAttr | ArrowT | MaybeT | OneOfT
CoreAttr ::= PrimAttr | VoidStar | --PrimAttr ::= CurrentPrimAttr | DeprecatedPrimAttr
CurrentPrimAttr
::= int | uint | byte | short | ushort | char | uchar
| long | ulong | longlong | ulonglong
| size_t | float | double | bool | string | void
DeprecatedPrimAttr
::= unsigned | boxed
VoidStar ::= void* | --ArrowT
::= (-> (TypeDesc ...) TypeDesc)
MaybeT
::= (maybe TypeDesc)
OneOfT
::= (oneof (Any Fixnum) ... TypeDesc)
where --- represents a user-extensible part of the grammar (see below), Any represents any Scheme
value, and Fixnum represents any word-sized integer.
A central registry maps CoreAttr's to a foreign representation and two conversion routines: one to
convert a Scheme value to a foreign argument, and another to convert a foreign result back back to a
Scheme value. The denoted components are collectively referred to as a type within the FFI
documentation. The registry is extensible; the ffi-add-attribute-core-entry! procedure adds new
CoreAttr's to the registry, and one can alternatively add short-hands for type-descriptors via the
ffi-add-alias-of-attribute-entry! procedure. Finally, one can add new VoidStar productions
(subtypes of the void* type-descriptor) via the ffi-install-void*-subtype procedure (defined in the
lib/Standard/foreign-stdlib.sch library).
10.19.3.1. Primitive Attribute Types
The following is a list of the accepted types and their conversions at the boundary between Scheme and
foreign code:
int
Exact integer values in the range [-231,231-1]. Scheme integers in that range are converted to and from
C "int".
uint
Exact integer values in the range [0,232-1]. Scheme integers in that ranges are converted to and from C
"unsigned int".
61