[Cuis-dev] FFI example

Andres Valloud ten at smallinteger.com
Sun Sep 29 18:27:26 PDT 2024


Just this from Linux world already has multiple red flags.

https://www.man7.org/linux/man-pages/man3/abs.3.html

POSIX (everything is a macro, can't use FFI).  GCC handles that as 
built-ins (so no function may actually exist, or the one you call is not 
the one GCC gives you).

The man page from macOS says 'abs' complies with C99, but even though it 
calls it a "function", you still have to ask yourself if POSIX isn't 
involved somewhere anyway because then everything is a macro.

At the very least, calling 'abs' from FFI is not portable.

Also on macOS, 'abs' takes and returns ints, not longs.  And even if 
'abs' is a function today and forever, what is that attribute __const__ 
that you're not really supposed to know about?

This is all asking for bad FFI karma.

On 9/29/24 6:13 PM, Andres Valloud via Cuis-dev wrote:
> So, how do you know that 'abs' thing is actually a function?
> 
> On 9/29/24 5:54 PM, Mark Volkmann via Cuis-dev wrote:
>> Ron, thanks so much for the links! I think they got me a bit farther, 
>> but I don't have a working example yet. I know this is a somewhat 
>> complex topic, but I'm surprised I haven't been able to find a basic 
>> working example. I'd love to see something like a C function that 
>> takes two numbers and returns their sum. And then see how to call that 
>> function from Cuis Smalltalk.
>>
>> Here's something I tried to calculate the absolute value of a number 
>> using the abs function provided by C and running in macOS where the C 
>> library is provided in libSystem.dylib.
>>
>> Define the following class:
>>
>> ExternalLibrary subclass: #FFIDemo
>>      instanceVariableNames: ''
>>      classVariableNames: ''
>>      poolDictionaries: ''
>>      category: 'Volkmann'
>>
>> Define the following class method:
>>
>> moduleName
>>      ^ 'libSystem.dylib'
>>
>> Define the following instance methods:
>>
>> ffiAbs: anInteger
>>      "<calltype: returnType 'functionName' (parameter types list) 
>> module: 'libraryName'>"
>>      <cdecl: long 'abs' (long) module: 'libSystem.dylib'>
>>      ^self externalCallFailed
>>
>> callIt: aNumber
>>      | meth |
>> meth := ExternalLibraryFunction
>> name: 'ffiAbs'
>> module: FFIDemo moduleName
>> callType: 0
>> returnType: ExternalType int32
>> argumentTypes: #(ExternalType int32).
>>   ^ meth invokeWith: aNumber.
>>
>> Test it like this:
>>
>> demo := FFIDemo new.
>> (demo callIt: -2) print. "expecting 2"
>>
>> This gives "Error: Unable to find function address".
>>
>> On Sun, Sep 29, 2024 at 5:00 PM Ron Teitelbaum <ron at 3dicc.com 
>> <mailto:ron at 3dicc.com>> wrote:
>>
>>     This looks helpful too
>>
>>     http://forum.world.st/About-Squeak-s-FFI-package-ported-to-Cuis-
>>     td4663852.html <http://forum.world.st/About-Squeak-s-FFI-package-
>>     ported-to-Cuis-td4663852.html>
>>
>>     and this:
>>
>>     https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/master/
>>     Packages/System/Tests-FFI.pck.st <https://github.com/Cuis-Smalltalk/
>>     Cuis-Smalltalk-Dev/blob/master/Packages/System/Tests-FFI.pck.st>
>>
>>     All the best,
>>
>>     *Ron Teitelbaum
>>     */Chief Executive Officer
>>     /*3D Immersive Collaboration Corp*
>>     ron at 3dicc.com <mailto:ron at 3dicc.com>
>>     www.3dicc.com <http://www.3dicc.com/>
>>
>>     <https://www.facebook.com/3DICC> <https://twitter.com/RonTeitelbaum>
>>     <https://www.linkedin.com/in/ronteitelbaum>
>>
>>
>>
>>     On Sun, Sep 29, 2024 at 5:54 PM Ron Teitelbaum <ron at 3dicc.com
>>     <mailto:ron at 3dicc.com>> wrote:
>>
>>         This may help too:
>>
>>         https://wiki.squeak.org/squeak/1414 <https://wiki.squeak.org/
>>         squeak/1414>
>>
>>         All the best,
>>
>>         *Ron Teitelbaum
>>         */Chief Executive Officer
>>         /*3D Immersive Collaboration Corp*
>>         ron at 3dicc.com <mailto:ron at 3dicc.com>
>>         www.3dicc.com <http://www.3dicc.com/>
>>
>>         <https://www.facebook.com/3DICC> <https://twitter.com/
>>         RonTeitelbaum> <https://www.linkedin.com/in/ronteitelbaum>
>>
>>
>>
>>         On Sun, Sep 29, 2024 at 5:50 PM Ron Teitelbaum <ron at 3dicc.com
>>         <mailto:ron at 3dicc.com>> wrote:
>>
>>             Maybe this:
>>
>>             <https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/
>>             master/Documentation/Technical/
>>             AQuickOrientationToFFIFromSmalltalk.md <https://github.com/
>>             Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/master/Documentation/
>>             Technical/AQuickOrientationToFFIFromSmalltalk.md>>
>>
>>             and
>>
>>             <https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/
>>             master/Documentation/Technical/FFIorVMPluginsWhatToUse.md
>>             <https://github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/
>>             master/Documentation/Technical/FFIorVMPluginsWhatToUse.md>>
>>
>>
>>             All the best,
>>
>>             *Ron Teitelbaum
>>             */Chief Executive Officer
>>             /*3D Immersive Collaboration Corp*
>>             ron at 3dicc.com <mailto:ron at 3dicc.com>
>>             www.3dicc.com <http://www.3dicc.com/>
>>
>>             <https://www.facebook.com/3DICC> <https://twitter.com/
>>             RonTeitelbaum> <https://www.linkedin.com/in/ronteitelbaum>
>>
>>
>>
>>             On Sun, Sep 29, 2024 at 5:47 PM Mark Volkmann via Cuis-dev
>>             <cuis-dev at lists.cuis.st <mailto:cuis-dev at lists.cuis.st>> 
>> wrote:
>>
>>                 Which documentation are you referring to? I don't see a
>>                 separate repository related to FFI at https://
>>                 github.com/Cuis-Smalltalk <https://github.com/Cuis-
>>                 Smalltalk>.
>>                 I do see this, but no documentation in it: https://
>>                 github.com/Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/
>>                 master/Packages/System/FFI.pck.st <https://github.com/
>>                 Cuis-Smalltalk/Cuis-Smalltalk-Dev/blob/master/Packages/
>>                 System/FFI.pck.st>
>>
>>                 On Sun, Sep 29, 2024 at 2:15 PM Andres Valloud via Cuis-
>>                 dev <cuis-dev at lists.cuis.st <mailto:cuis-
>>                 dev at lists.cuis.st>> wrote:
>>
>>                     Before you start, make sure you go through the FFI
>>                     doc at github.
>>
>>                     On 9/29/24 6:58 AM, Mark Volkmann via Cuis-dev wrote:
>>                      > Can someone point me to an example of using FFI
>>                     to call a C function in
>>                      > Cuis Smalltalk?
>>                      > I've looked at the code in the FFI package, but I
>>                     don't see any examples
>>                      > or tests.
>>                      > I've also spent time searching the web for
>>                     examples and came up empty.
>>                      >
>>                      > --
>>                      > R. Mark Volkmann
>>                      > Object Computing, Inc.
>>                      >
>>
>>                     --                     Cuis-dev mailing list
>>                     Cuis-dev at lists.cuis.st <mailto:Cuis- 
>> dev at lists.cuis.st>
>>                     https://lists.cuis.st/mailman/listinfo/cuis-dev
>>                     <https://lists.cuis.st/mailman/listinfo/cuis-dev>
>>
>>
>>
>>                 --                 R. Mark Volkmann
>>                 Object Computing, Inc.
>>                 --                 Cuis-dev mailing list
>>                 Cuis-dev at lists.cuis.st <mailto:Cuis-dev at lists.cuis.st>
>>                 https://lists.cuis.st/mailman/listinfo/cuis-dev
>>                 <https://lists.cuis.st/mailman/listinfo/cuis-dev>
>>
>>
>>
>> -- 
>> R. Mark Volkmann
>> Object Computing, Inc.
>>
> 



More information about the Cuis-dev mailing list