[Cuis-dev] FFI example

Vanessa Freudenberg vanessa at codefrau.net
Mon Sep 30 16:05:56 PDT 2024


I put mine on the class side just so I can run it directly, but that
shouldn't make any difference.

Secondly, did you change the super class to a regular class? If it's
still ExternalLibrary then you would need to implement its "moduleName"
method.

Vanessa

On Mon, Sep 30, 2024 at 3:53 PM Mark Volkmann <r.mark.volkmann at gmail.com>
wrote:

> Thanks Vanessa! I don't actually have a specific use of FFI in mind. I'm
> just trying to understand and document as much as I can about Cuis
> Smalltalk.
> Sadly I still can't get this work. Is your greet method a class or
> instance method? I've tried both. As an instance method when I evaluate
> this:
>
> (FFIDemo new) greet
>
> I get a Debug window that says "Error: Unable to find function address".
> But I'm positive the module path to the libmylib.dylib file I supplied is
> correct. I can copy it to a terminal and run ls with it and it finds the
> file.
>
> On Mon, Sep 30, 2024 at 1:55 PM Vanessa Freudenberg <vanessa at codefrau.net>
> wrote:
>
>> For one, there's no reason to subclass ExternalLibrary.
>>
>> But otherwise, it works for me:
>>
>> [image: image.png]
>>
>> [image: image.png]
>>
>> That said, if you actually want to use C functions in your application,
>> the better way is to build a VM plugin. It's a little bit more overhead to
>> learn but fits the Smalltalk philosophy much better, where the VM provides
>> the environment for the image to live in, rather than the image talking to
>> the outside world directly. The job of the VM is to provide a safe world
>> so Smalltalk developers don't have to worry about the harsh reality. FFI
>> breaks that illusion.
>>
>> Vanessa
>>
>> On Mon, Sep 30, 2024 at 8:10 AM Mark Volkmann via Cuis-dev <
>> cuis-dev at lists.cuis.st> wrote:
>>
>>> Vanessa's getenv example works for me. Now I want to learn how to invoke
>>> my own C functions.
>>> I created the file "mylib.c" containing this:
>>>
>>> char* greet() {
>>>   return "Hello, World!";
>>> }
>>>
>>> Then I created a shared library for macOS with this:
>>>
>>> clang -dynamiclib -o libmylib.dylib mylib.c
>>>
>>> Then I created this Smalltalk class:
>>>
>>> ExternalLibrary subclass: #FFIDemo
>>>     instanceVariableNames: ''
>>>     classVariableNames: ''
>>>     poolDictionaries: ''
>>>     category: 'Volkmann'
>>>
>>> Then I added this class method:
>>>
>>> greet
>>>     <cdecl: char* 'greet' () module:
>>> '/Users/volkmannm/Documents/dev/lang/smalltalk/Cuis-Smalltalk-Volkmann/libmylib.dylib'>
>>>     ^self externalCallFailed
>>>
>>> Side note: I need to learn where this looks by default for shared
>>> libraries so I don't have to specify an absolute path.
>>>
>>> Then I entered this in a Workspace and did a "Print it".
>>>
>>> FFIDemo greet
>>>
>>> That opens a Debug window with the message "Error: Unable to find
>>> function address".
>>>
>>> Can anyone spot where I'm going wrong?
>>>
>>> On Sun, Sep 29, 2024 at 10:10 PM Andres Valloud via Cuis-dev <
>>> cuis-dev at lists.cuis.st> wrote:
>>>
>>>> Except of course getenv() is ruled by POSIX so doing that is at the
>>>> very
>>>> least not portable, if not outright wrong.
>>>>
>>>> On 9/29/24 6:29 PM, Vanessa Freudenberg via Cuis-dev wrote:
>>>> > And, defining a method myself works fine:
>>>> >
>>>> > getenv: var
>>>> >          <cdecl: char* 'getenv' (char*) module: 'libSystem.dylib'>
>>>> >          ^self externalCallFailed
>>>> >
>>>> > And then
>>>> >
>>>> >     MyClass getenv: 'HOME'
>>>> >
>>>> >
>>>> > returns
>>>> >
>>>> >       '/Users/vanessa' .
>>>> >
>>>> >
>>>> > There really isn't much more to it.
>>>> >
>>>> > Vanessa
>>>> >
>>>> > On Sun, Sep 29, 2024 at 6:26 PM Vanessa Freudenberg
>>>> > <vanessa at codefrau.net <mailto:vanessa at codefrau.net>> wrote:
>>>> >
>>>> >     I just loaded the "Tests-FFI" package and it works.
>>>> >
>>>> >     And you can e.g. run
>>>> >
>>>> >           FFITestLibrary new ffiTestFloats: 3 with: 4
>>>> >
>>>> >     which returns 7.0 because the plugin happens to implement
>>>> >     "ffiTestFloats()" as returning the sum of its two arguments.
>>>> >
>>>> >     Vanessa
>>>> >
>>>> >
>>>>
>>>> --
>>>> 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/20240930/9e6eb994/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 50559 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240930/9e6eb994/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 173898 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20240930/9e6eb994/attachment-0003.png>


More information about the Cuis-dev mailing list