Search code examples
smalltalkffipharosqueak

What is the syntax for the Squeak FFI in the new Squeak (5.0)


I am trying to use the Squeak Foreign Function Interface. All information I was able to find does not seem to apply to the new Squeak 5.0 because when I try e.g.:

add: a to: b
    " ( int ) add (int a, int b) ; "
    < cdecl: int 'add' ( int a, int b ) module: 'mydll'>
    ^ self externalCallFailed

which is derived from this page's:

apiInvalRect: aRect
    " ( void ) InvalRect (const Rect &star; badRect ) ; "
    < cdecl: void 'InvalRect' ( MacRect )  module: 'InterfaceLib'>   
    ^ self externalCallFailed.    

then I get the error that it expects a > right after the <.

(I am using Squeak 5.0 on Windows with SqueakFFIPrims.bundle in its resources directory.)


Solution

  • You first need to install FFI in the image, via Monticello.

    The FFI package are located at http://source.squeak.org/FFI.html

    You need to install 'FFI-Pools' first, then 'FFI-Kernel'. Then you can load 'FFI-Tests' and 'FFI-Example'.

    Once FFI is installed in image, the correct syntax would be something like this:

    add: a to: b
        " ( int ) add (int a, int b) ; "
        <cdecl: long 'add' ( long long ) module: 'mydll'>
        ^ self externalCallFailed 
    

    You don't specify the parameter names - they are implicitly taking the same position as the smalltalk method.

    You have to replace int by long - it's the same on supported 32 bits platforms.

    EDIT to load the FFI package in Squeak, you can type and execute (do it) this in a workspace:

    (Installer repository: 'http://source.squeak.org/FFI')
        install: 'FFI-Pools';
        install: 'FFI-Kernel';
        install: 'FFI-Tests';
        install: 'FFI-Examples'.