I'm writing an FFI interface for an existing library (written in C).
The library uses a good number of opaque structures, so I defined a few ExternalStructures
(with no fields) to use as void*
.
Now I've seen two ways (or four?) of interfacing with the library:
Having an ExternalLibrary
with a method per exported function: This could have the method in the instance class, and then use a singleton pattern to have a single instance. Or implement the methods in the class side with the "more complex" syntax including the moduleName
in the FFI pragma like in:
ffiTestFloats: f1 with: f2
"FFITestLibrary ffiTestFloats: $A with: 65.0"
<cdecl: float 'ffiTestFloats' (float float) module:'SqueakFFIPrims'>
^self externalCallFailed
What's better?
Additionally I've seen other way of doing this, not having an ExternalLibrary
at all, and implementing the methods directly in the ExternalStructure
. I like this second part better, however, all the FFI interface definition is spread through several classes, and maintaining and porting to other platforms, Smalltalk dialects or library versions could be more complex.
So, what's the "right" way of doing it?