[Cuis-dev] I need some advice with FFI

Andres Valloud ten at smallinteger.com
Tue Nov 9 09:49:20 PST 2021


Happy to help! :)

On 11/9/21 9:27 AM, Nicola Mingotti wrote:
> 
> Hi Andres,
> 
> Super valuable info, I had no idea of that !
> 
> I just supposed what i can call from C as a function I can call from FFI.
> 
> I see my supposition was wrong. Thank you ! you may have saved me 
> several hours
> of hopeless attempts.
> 
> Nicola
> 
> 
> 
> 
> 
> 
> On 11/9/21 18:13, Andres Valloud via Cuis-dev wrote:
>> You can't call that function via FFI because that function is 
>> documented in POSIX / Single Unix Specification, and virtually every 
>> function in POSIX / SUS is allowed to be declared as a macro.  Of 
>> course, you can't call a macro using an FFI.  Moreover, the macro may 
>> have the same name as the function, so there may be a function called 
>> localtime(), but if you write something like
>>
>> {
>>     .
>>     .
>>     .
>>     localtime(...);
>>     .
>>     .
>>     .
>> }
>>
>> in C, the C compiler will substitute the macro localtime() before it 
>> ever calls the function localtime().  So, even if a function called 
>> localtime() is there, you still should not call it via FFI.
>>
>> This is just one example of how macros of all kinds (C preprocessor, 
>> linker macros, etc) can interpose themselves.  It could even be the 
>> case, as I've seen in some Unix systems, that a function such as 
>> memmove() does not even exist.  Of course, compilation works because 
>> there's a macro called memmove(). Obviously, the implementation of all 
>> such macros is private and cannot be depended on.
>>
>> The point is that the *only* correct way to call these things is by 
>> writing C code, because the alternative is undefined behavior, 
>> irreproducible bugs, and all sorts of maintenance headaches.  So, I'd 
>> just write a plugin.
>>
>> Andres.
>>
>> On 11/9/21 2:47 AM, Nicola Mingotti wrote:
>>>
>>> On 11/7/21 23:08, Andres Valloud via Cuis-dev wrote:
>>>> Are you trying to call localtime()? Like, this function?
>>>>
>>>> https://linux.die.net/man/3/localtime
>>>>
>>>> On 11/6/21 11:01 AM, Nicola Mingotti via Cuis-dev wrote:
>>>>> Hi guys,
>>>>>
>>>>> I was trying to develop my first FFI calls in Cuis today but
>>>>> I am stuck, i hope some of you can give me some guidance.
>>>>>
>>>>> In the attached package there are implemented 2 foreign functions
>>>>> TestClass (class) >> time         # working
>>>>> TestClass (class) >> localtime    # broken
>>>>>
>>>>> ---- interactions ----------
>>>>> TestClass time.                                      " ok "
>>>>> TestClass localtime2: (TestClass time).              " broken "
>>>>> -----------------------------
>>>>>
>>>>> The error i get is "can not use integer as pointer".
>>>>>
>>>>> Any ideas ?
>>>>>
>>>>> bye
>>>>> Nicola
>>>>>
>>>>>
>>>
>>>
>>> Hi Andres,
>>>
>>> yes, absolutely that function ;)
>>>
>>> The motivation spurs from my mail to this list of 3-Nov title 
>>> "ATTENTION. maybe nasty bug ..... "
>>>
>>> I am going to update on that thread today or tomorrow, time permitting.
>>>
>>> bye
>>> Nicola
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
> 


More information about the Cuis-dev mailing list