![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
GNU Smalltalk internally maps every object except Integers to a data structure named an OOP (which is short for Ordinary Object Pointer). An OOP is a pointer to an internal data structure; this data structure basically adds a level of indirection in the representation of objects, since it contains
This additional level of indirection makes garbage collection very efficient, since the collector is free to move an object in memory without updating every reference to that object in the heap, thereby keeping the heap fully compact and allowing very fast allocation of new objects. However, it makes C code that wants to deal with objects even more messy than it would be without; if you want some examples, look at the hairy code in GNU Smalltalk that deals with processes.
To shield you as much as possible from the complications of doing object-oriented programming in a non-object-oriented environment like C, GNU Smalltalk provides friendly functions to map between common Smalltalk objects and C types. This way you can simply declare OOP variables and then use these functions to treat their contents like C data.
These functions are passed to a module via the VMProxy
struct, a
pointer to which is passed to the module, as shown in Linking your libraries to the virtual machine. They can be
divided in two groups, those that map from Smalltalk objects to C
data types and those that map from C data types to Smalltalk
objects.
Here are those in the former group (Smalltalk to C); you can see that
they all begin with OOPTo
:
signed long
for that integer.
double
for that object.
long double
for that object.
!= 0
) if
the given OOP is the true
object, false (i.e. == 0
)
otherwise.
char
for that integer.
wchar_t
for that integer.
char *
with the same contents. It is
the caller's responsibility to free the pointer and to handle possible
`NUL' characters inside the Smalltalk object.
wchar_t *
with the same contents. It is
the caller's responsibility to free the pointer and to handle possible
`NUL' characters inside the Smalltalk object.
char *
with the same contents, without
null-terminating it. It is the caller's responsibility to free the
pointer.
PTR
to the C data pointed to by the object. The
caller should not free the pointer, nor assume anything about its size
and contents, unless it exactly knows what it's doing. A PTR
is a void *
if supported, or otherwise a char *
.
nil
, true
, false
,
character, integer). If the OOP is nil
, it answers 0; else the
mapping for each object is exactly the same as for the above functions.
Note that, even though the function is declared as returning a
long
, you might need to cast it to either a char *
or PTR
.
While special care is needed to use the functions above (you will
probably want to know at least the type of the Smalltalk object you're
converting), the functions below, which convert C data to Smalltalk
objects, are easier to use and also put objects in the incubator so that
they are not swept by a garbage collection (see section 5.10 Incubator support). These
functions all end with ToOOP
, except
cObjectToTypedOOP
:
Integer
which contains the same value as
the passed C long
. Note that Smalltalk integers are always
signed and have a bit less of precision with respect to C longs. On 32
bit machines, their precision is 30 bits (if unsigned) or 31 bits (if
signed); on 64 bit machines, their precision is 62 bits (if unsigned) or
63 bits (if signed).
OOPToId
. The OOP will be the same that was passed to
OOPToId
only if the original OOP has not been garbage-collected
since the call to OOPToId
.
FloatD
which contains the same value as
the passed double
. Unlike Integers, FloatDs have exactly the same
precision as C doubles.
FloatQ
which contains the same value as
the passed long double
. Unlike Integers, FloatQs have exactly the same
precision as C long doubles.
Boolean
which contains the same boolean
value as the passed C int
. That is, the returned OOP is the sole
instance of either False
or True
, depending on where the
parameter is zero or not.
Character
which represents the same char
as the passed C char
.
Character
or UnicodeCharacter
which represents the same char as the passed C wchar_t
.
Smalltalk
dictionary. NULL
is returned if the class is
not found.
This method is slow; you can safely cache its result.
nil
if the parameter points to
address 0 (zero).
nil
if the parameter points to
address 0 (zero).
nil
is returned if the first
parameter points to address 0 (zero).
nil
if the parameter points to
address 0 (zero).
nil
if the parameter points to address 0 (zero).
The returned value has no precise CType assigned. To assign one, use
cObjectToTypedOOP
.
nil
if the parameter points to address 0 (zero).
The returned value has the second parameter as its type; to get possible
types you can use typeNameToOOP
.
cIntType = typeNameToOOP("CIntType"); myOwnCStructType = typeNameToOOP("MyOwnCStruct type"); |
This method is primarily used by msgSendf
(see section 5.5 Calls from C to Smalltalk),
but it can be useful if you use lower level call-in methods. This method
is slow too; you can safely cache its result.
As said above, the C to Smalltalk layer automatically puts the objects it creates in the incubator which prevents objects from being collected as garbage. A plugin, however, has limited control on the incubator, and the incubator itself is not at all useful when objects should be kept registered for a relatively long time, and whose lives in the registry typically overlap.
To avoid garbage collection of such object, you can use these functions, which access a separate registry:
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |