UFFI code problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

UFFI code problem

shawon58
i am trying UFFI code the code is : FFICContantExamples
class>>absMinusFortyTwo
^ self ffiCall: #( int abs ( TheAnswer ) ) module: LibC
but it shows error. how to solve this ?

<http://forum.world.st/file/t372453/Capture.png>



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: UFFI code problem

tbrunz
Where did you get this example?

The straightforward answer is that "TheAnswer" appears to be undefined.  In
this context, 'ffiCall:' is expecting a valid C expression that indicates
the type of a formal argument, followed by the formal argument.

Based on the examples in the UFFI booklet, I would expect that to be
something like 'int -42' rather than "TheAnswer".

But knowing more of the context of where you got your code would help...

-t



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: UFFI code problem

shawon58
that means i cant able to use Theanswer ? i attach my screenshot how i
initialize the value in variable .



<http://forum.world.st/file/t372453/Capture.png>



--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: UFFI code problem

tbrunz
Try using this expression:

^ self ffiCall: #( int abs ( int TheAnswer ) ) module: LibC

i.e., add 'int' to tell Pharo that 'TheAnswer' is to be interpreted as a C
integer.




--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html

Reply | Threaded
Open this post in threaded view
|

Re: UFFI code problem

EstebanLM
There has been a change in how the constant values needs to be declared (because in a 64bit environment, you cannot “guess” the type by its value: 42 can be (int32)42 or (int64)42.
Hence, you need to be explicit on the time.

That’s why @tbrunz suggestion is accurate.

Now, I remember writing that example years ago, but I do not remember where :P
Can you point me to the source, so I can correct it ? (it should have been done before, but better later than never ;)

Esteban

> On 1 Apr 2020, at 06:57, tbrunz <[hidden email]> wrote:
>
> Try using this expression:
>
> ^ self ffiCall: #( int abs ( int TheAnswer ) ) module: LibC
>
> i.e., add 'int' to tell Pharo that 'TheAnswer' is to be interpreted as a C
> integer.
>
>
>
>
> --
> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>


Reply | Threaded
Open this post in threaded view
|

Re: UFFI code problem

Guillermo Polito


> El 1 abr 2020, a las 9:31, Esteban Lorenzano <[hidden email]> escribió:
>
> There has been a change in how the constant values needs to be declared (because in a 64bit environment, you cannot “guess” the type by its value: 42 can be (int32)42 or (int64)42.
> Hence, you need to be explicit on the time.

Parentheses are not needed anymore (yet they are still supported I guess) :)

You can now do

int -42

Since there is no ambiguity possible, and this is consistent with

int x

;)

>
> That’s why @tbrunz suggestion is accurate.
>
> Now, I remember writing that example years ago, but I do not remember where :P
> Can you point me to the source, so I can correct it ? (it should have been done before, but better later than never ;)

I wonder if for Pharo9 we should put the UFFI parser in strict mode.
Strict mode should
 - not let people use constants without type declarations (because not doing so is a source of bugs)
 - not let people use String declarations without declaring encodings (because not doing so is a source of bugs)

Of course, strictness is just a flag that people could turn off, at their own risk...

> Esteban
>
>> On 1 Apr 2020, at 06:57, tbrunz <[hidden email]> wrote:
>>
>> Try using this expression:
>>
>> ^ self ffiCall: #( int abs ( int TheAnswer ) ) module: LibC
>>
>> i.e., add 'int' to tell Pharo that 'TheAnswer' is to be interpreted as a C
>> integer.
>>
>>
>>
>>
>> --
>> Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html
>>
>
>