[squeak-dev] Pass C string into Squeak

David T. Lewis lewis at mail.msen.com
Wed Jan 6 13:23:28 UTC 2010


On Wed, Jan 06, 2010 at 11:52:45AM +0200, Igor Stasenko wrote:
> 2010/1/6 Ang BeePeng <beepeng86 at yahoo.com>:
> >
> > Hi.
> >
> > I wish I can get some help on this. I want to pass a C string into Squeak. I
> > saw "primitiveStringAtPut", "primitiveStringReplace" etc.. I'm thinking of
> > call a primitive in smalltalk, pass an empty String object onto stack, then
> > have interpreter/primitive to fill in the String. Any better way to achieve
> > that? What precaution should I take, to prevent C string from causing error
> > once it is in Squeak?
> >
> 
> There is an interface to _create_ string objects in primitive, without
> modifying existing one.
> Load VMMaker into your image and look for senders of stringClass (or
> byteStringClass , if i'm not mistaken).
> You can see an example how to create a strings object and fill with
> with contents from C string.

Actually it is #classString. In addition to VMMaker, you can find
examples in OSProcessPlugin (load from SqueakSource, or just evaluate
"VMMaker updateFromServer" after loading VMMaker).

The general approach is to allocate a new String object and copy
the contents of the C sting into that object. A caution is that
allocating the new String can trigger garbage collection, which
can invalidate other object references in the primitive. Use
#pushRemappableOop: and #popRemappableOop to protect against this.

See UnixOSProcessPlugin>>primitiveRealPath for an example of
a primitive that does what you want, including the #pushRemappableOop:

Dave
 



More information about the Squeak-dev mailing list