Pass C string into Squeak

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

Pass C string into Squeak

Ang BeePeng
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?

Thanks.

Ang Beepeng
Reply | Threaded
Open this post in threaded view
|

Re: Pass C string into Squeak

Igor Stasenko
2010/1/6 Ang BeePeng <[hidden email]>:

>
> 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.

> Thanks.
>
> Ang Beepeng
> --
> View this message in context: http://n4.nabble.com/Pass-C-string-into-Squeak-tp999636p999636.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>
>



--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: Pass C string into Squeak

Vagelis Savvas
In reply to this post by Ang BeePeng
On Wed, 06 Jan 2010 08:42:10 +0200, Ang BeePeng <[hidden email]>  
wrote:

>
> 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?
>
> Thanks.
>
> Ang Beepeng

Hi, you can also use FFI for that. If your C routine is exported in a DLL
(or .so, etc.) and returns a C null terminated string you are almost there.
Then you just need to call this function and FFI will take care of  
returning
a String for you. Just note that you will get back a copy of your C string.

Cheers
  - Vagelis

Reply | Threaded
Open this post in threaded view
|

Re: Pass C string into Squeak

David T. Lewis
In reply to this post by Igor Stasenko
On Wed, Jan 06, 2010 at 11:52:45AM +0200, Igor Stasenko wrote:

> 2010/1/6 Ang BeePeng <[hidden email]>:
> >
> > 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
 

Reply | Threaded
Open this post in threaded view
|

Re: Pass C string into Squeak

Ang BeePeng
This post was updated on .
Hi all.

Thanks for all the helps.

I saw this function ffiReturnCStringFrom(), there is this "classString" you guys mention.

Is it true that recent version of Squeak, function instantiateClassindexableSize() can be access through interpreterProxy->instantiateClassindexableSize() in some cases, in plugins I believe. Therefore, if I'm working with numbered primitive for example, I won't need to use interpreterProxy->instantiateClassindexableSize() (and the same to similar functions). I can use instantiateClassindexableSize() instead.

Say if I want returnCStringFrom() as a numbered primitive in my system, any advice?

instantiateClassindexableSize(classPointer, size) takes arguments classPointer and size. In older Squeak, eg. splObj(6) is used as argument for classPointer. splObj() return an instance of the class? or the class?

Sorry for the mess.

Thank you so much.

Ang Beepeng