[Pharo-users] FFI beginner question

Guillermo Polito guillermopolito at gmail.com
Tue Sep 24 04:43:46 EDT 2019

> El 24 sept 2019, a las 4:35, Richard O'Keefe <raoknz at gmail.com> escribió:
> What I want to do is to create a binding for the SoftPosit library.
> A typical function has an interface like
>        quire16_t q16_fdp_add(quire16_t, posit16_t, posit16_t);
> where
>        typedef struct { uint16_t v; } posit16_t;
>        typedef struct { uint64_t v[2]; } quire16_t;
> A "quire" is a dot product accumulator.
> A "posit" is a number.
> {quire,posit}{8,16,32} are defined, and a number of other types.

Ok, so IIUC, what you need is:

1) define type aliases (e.g., typedef posit16_t uint_t)
2) define a couple of structs based on those types

I see in the booklet no documentation about type aliases so far, but in the latest pharo version there are a couple of examples you can look at.
Type aliases can be defined as class variables in the class using the type, or in a shared pool, if you mean to use those types in several places.
Take a look at FT2Types class side for an example, where the type FT_Error is defined

FT2Types class>>initialize
	FT_Error := 'long’.

and then used in a callout like this

FT2Face >> ffiGetKerningLeft: left_glyph right: right_glyph result: akerning
	self ffiCall: #(FT_Error FT_Get_Kerning(self, FT_UInt left_glyph, FT_UInt right_glyph, 2, FT_Vector  *akerning ))

Then, to use those defined types in a struct definition, you only need to make those type aliases (class vars) available in your struct, and use them in the struct field definition
See for example FTMatrix

FTMatrix class >> fieldsDesc
	^ #(    
		FT_Fixed  	xx
		FT_Fixed 	xy
		FT_Fixed  	yx 
		FT_Fixed 	yy

Hope this helps,

More information about the Pharo-users mailing list