5

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.)

ben
  • 452
  • 3
  • 10
  • FFI is not installed by default in the image, did you try anything to install it? – aka.nice Oct 18 '15 at 20:07
  • @aka.nice It is not? I only saw that my image has a SqueakFFIPrims.bundle in its directory and that i none of the websites I visited had any info that it had to be installed (and how?), also not in squeakMap. I would be very glad if you tell us how to install it and I hope that also solves the syntax error (I guess the plugin changes the parsing rules too?) – ben Oct 18 '15 at 20:18
  • I usually load the latest FFI packages with Monticello from http://source.squeak.org/FFI.html that would be 'FFI-Kernel-dtl.28.mcz' 'FFI-Tests-EstebanLorenzano.8.mcz' 'FFI-Examples-ar.1.mcz'. There must be some Installer script which does the same work, but I don't remember where to find it, maybe ask google... – aka.nice Oct 18 '15 at 20:30
  • thanks i will try that - will report in a few minutes – ben Oct 18 '15 at 20:35
  • There are some dependencies I am trying to resolve now... – ben Oct 18 '15 at 20:44
  • Ah, the right order is 'FFI-Pools' 'FFI-Kernel' 'FFI-Tests' 'FFI-Examples' – aka.nice Oct 18 '15 at 20:50
  • that works thank you! please put the info into your answer and i will accept it. source.squeak.org/FFI.html and right order is 'FFI-Pools' 'FFI-Kernel' 'FFI-Tests' 'FFI-Examples' – ben Oct 18 '15 at 20:56

1 Answers1

3

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'.
aka.nice
  • 9,100
  • 1
  • 28
  • 40