[Cuis-dev] FFI example

Mark Volkmann r.mark.volkmann at gmail.com
Sun Sep 29 17:54:29 PDT 2024


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> wrote:

> This looks helpful too
>
>
> 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
>
> All the best,
>
>
> *Ron Teitelbaum*
> *Chief Executive Officer**3D Immersive Collaboration Corp*
> ron at 3dicc.com
> 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> wrote:
>
>> This may help too:
>>
>> https://wiki.squeak.org/squeak/1414
>>
>> All the best,
>>
>>
>> *Ron Teitelbaum*
>> *Chief Executive Officer**3D Immersive Collaboration Corp*
>> ron at 3dicc.com
>> 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> wrote:
>>
>>> Maybe this:
>>>
>>> <
>>> 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
>>> >
>>>
>>>
>>> All the best,
>>>
>>>
>>> *Ron Teitelbaum*
>>> *Chief Executive Officer**3D Immersive Collaboration Corp*
>>> ron at 3dicc.com
>>> 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> wrote:
>>>
>>>> Which documentation are you referring to? I don't see a separate
>>>> repository related to FFI at 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
>>>>
>>>> On Sun, Sep 29, 2024 at 2:15 PM Andres Valloud via Cuis-dev <
>>>> 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
>>>>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>>>>>
>>>>
>>>>
>>>> --
>>>> R. Mark Volkmann
>>>> Object Computing, Inc.
>>>> --
>>>> Cuis-dev mailing list
>>>> Cuis-dev at lists.cuis.st
>>>> https://lists.cuis.st/mailman/listinfo/cuis-dev
>>>>
>>>

-- 
R. Mark Volkmann
Object Computing, Inc.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240929/fac90ced/attachment-0001.htm>


More information about the Cuis-dev mailing list