[squeak-dev] Re: [smalltalk-research] Aliens and CObject

Paolo Bonzini bonzini at gnu.org
Mon Mar 8 15:29:10 UTC 2010

On 03/08/2010 04:07 PM, Gilad Bracha wrote:
> "It" is ambiguous here.  From what I could see,  CObjects already has
> some facilities for automagically deriving bindings from C headers, and
> in that sense is higher level.

Not so much automagic, but yes---you can say something like

CStruct subclass: #SdlCursor
     declaration: #(
         (#area (#ptr #CObject))
         (#hotX #short)
         (#hotY #short)
         (#data (#ptr #CObject))
         (#mask (#ptr #CObject))
         (#save (#ptr #CObject))
         (#wmCursor (#ptr #CObject)))
     classVariableNames: ''
     poolDictionaries: ''
     category: 'LibSDL-Core'

and the SdlCursor class will have all the required accessors.  (There is 
similarly CUnion).

The other more high-level feature of CObject, as far as I can see, is 
that it includes classes like CInt, CDouble, CString, CPtr or CArray. 
These embed the ABI knowledge; they support C-like pointer arithmetic 
(i.e. incrementing a CInt is equivalent to incrementing an int *).  Like 
an Alien, they can represent any of malloced/GCed/foreign storage, 
though the arithmetic has bounds checking only if the backing store for 
the CObject is garbage collected.  They are also the basic building 
blocks for CStruct/CUnion.

> On the other hand, the big advantage of aliens is that they do not
> require a special primitive-like  syntactic construct. The Alien
> implementation itself requires a primitive, but you don't need special
> syntax to use aliens.

Though there is a special primitive-like construct in GNU Smalltalk, it 
is just a shortcut that is expanded at compile time to a normal 
primitive.  In particular it expands to one of these templates

          descr callInto: nil. ^self
          ^(descr callInto: ValueHolder now) value
          ^(descr callInto: ValueHolder now) value narrow

depending on the return type of the call (respectively void, anything 
but CObject, and CObject), where descr is a compile-time literal and 
CFunctionDescriptor>>#callInto: returns its argument.  This however is 
considered an implementation detail, hence the special syntax.


More information about the Squeak-dev mailing list