How one refers to an external global variable in Squeak FFI

Viewed 67

For interfacing HDF5, I need to get a reference to/value of an external variable exported by the library, and pass it to other external functions.

For example, there is a variable representing the native double type: H5T_NATIVE_DOUBLE_g.

In VW, DLLCC, this is achievable thru a declarative annotation:

H5T_NATIVE_DOUBLE_g
    <C: hid_t H5T_NATIVE_DOUBLE_g>

Is there anything similar in Squeak FFI? Is there any support for such use case?

1 Answers

After inquiry, there seem to be at least a basic support in the (Threaded)FFIPlugin:

ExternalAddress class>>loadSymbol: moduleSymbol module: module 
    <primitive: 'primitiveLoadSymbolFromModule' module: 'SqueakFFIPrims'>
    ^ self primitiveFailed

So we might create an ExternalData, get its address via above message (passing the global variable name and a given ExternalLibrary as module argument), and specify its type.
We can then use this ExternalData to pass the address if the external function expects a pointer.
To pass the value, one needs to dereference the address, not sure that it is automated by the plugin...

In my case, I know I will have to pass the value, so I may directly de-reference the address to get the value and store that (assuming that the global variable is assigned once at initialization and won't change afterward and assuming that initialization has already occured at library load time - lots of application specific assumptions...).

Related