FFI and apicall/cdecl type

nicolas cellier ncellier at ifrance.com
Wed Jul 19 07:08:04 UTC 2006


Le Mercredi 19 Juillet 2006 08:24, mathieu a écrit :
> Andreas Raab a écrit :
> > Since this is the second time you're posting a question in this area
> > (which makes me assume that you're actually interested in getting an
> > answer) I'll point you gently to:
> >
> >   http://linuxmafia.com/faq/Essays/smart-questions.html
> >
> > Please read it and note in particular the parts about "Be precise and
> > informative about your problem" and "Be explicit about your question".
>
> ok sorry.
>
> So what the ExternalType served for?
>

ExternalType is Smalltalk class used to handle external function argument 
types and function return types. It is necessary to handle automatic 
conversions from Smalltalk Objects to C Objects et vice et versa.

> I don't understand why you have to ask in 2 different way the
> ExternalType (xType := descriptorClass atomicTypeNamed: here and xType
>

This is a list of tests:

1) check if there is an atomic type whose name is here (atomic types are non 
composed types like float double short long int etc...)
2) if the answer is nil, then here is not an atomic type, try to see if it is 
the name of a structure type...

You must understand that returning nil is a simple way to indicate a failure 
(a kind of C-style programming).

> := descriptorClass structTypeNamed: sym)?
>
> What the mssage #hasInterned:ifTrue: served for in this case?

here is aString (a token parsed by the parser). It should be the name of a 
known type.

If here is a known type, then corresponding symbol (here asSymbol) must 
already exist in the image.

That is exactly what #hasInterned:ifTrue: is cheking: does a Symbol having 
same characters as here exist in the image, and if true, it will try to set 
the type to be a structure...

Once again, structTypeNamed: will do the job of checking if (here asSymbol) is 
really the name of an ExternalStructure (most Symbols are not the name of a 
structure). If not, it will answer nil.

If there is no symbol like here, then we are sure that there is currently no 
such ExternalStructure declared in the Smalltalk image, and it's not even 
necessary to check if an ExternalStructure does have that name.

>
> > Cheers,
> >   - Andreas
> >
> > mathieu wrote:
> >> Hi,
> >>
> >> I was wondering how the type was handle when apicall or cdecl occur.
> >> I have read how it is parse by the compiler and don't understand the
> >> following:
> >>
> >> ======================================
> >> Parser>>externalType: descriptorClass
> >>     "Parse an return an external type"
> >>
> >>     | xType |
> >>
> >>     xType _ descriptorClass atomicTypeNamed: here.
> >>     xType == nil ifTrue:["Look up from class scope"
> >> *here*        Symbol hasInterned: here ifTrue:[:sym|
> >> *here*            xType _ descriptorClass structTypeNamed: sym]].
> >>     xType == nil ifTrue:[
> >>         "Raise an error if user is there"
> >> *here*        self interactive ifTrue:[^nil].

An error is detected; no such named type does exist in the image, neither a 
known atomic type, nor a known structure type:
If compilation isInteractive (that is user did select accept menu in the 
browser), then it's better to warn the user because he is trying to enter 
some incorrect code. Answering nil here will be checked at upper level and 
raise a SyntaxError notified to the user.

If not interactive (as for example he is loading code from a file, Monticello 
or whatever source), it's bad to warn the user for something he is not 
responsible for. And maybe, the type is not known yet, but is defined further 
in the file...
In this case, force the type to be recognized as a structure (I let you 
inquire how).

> >>         "otherwise go over it silently"
> >> *here*        xType _ descriptorClass forceTypeNamed: here].
> >>     self advance.
> >>     (self matchToken:#*)
> >>         ifTrue:[^xType asPointerType]
> >>         ifFalse:[^xType]
> >> =============================================
> >>
> >> So if somebody can explain it to me thanks :)
> >>
> >> Math




More information about the Squeak-dev mailing list