FFI / Primitive question

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

FFI / Primitive question

Joshua Gargus-2
Hi all,

I have a question about mixing FFI types with primitive invocations.  
For my example, let's say that I want to pass a MacRect as a  
primitive argument.  The Slang method looks like:

primitiveFrobulate: macRect
        | rectPtr |
        self primitive: 'primitiveFrobulate' parameters: #(MacRect).
        rectPtr := interpreterProxy firstIndexableField: macRect.
        self stFrobMacRect: rectPtr


The function stFrobMacRect() is hand-written C-code that looks  
something like:

void stFrobMacRect(int ptr)
{
        Rect* rectPtr = (Rect*) ptr;
        /* now, frobulate the rect */
}


This doesn't work (I don't see the expected values while  
frobulating).  What would be the right expression to write in the  
Slang code to get a pointer to the Rect to pass to stFrobMacRect()?

Thanks,
Josh

Reply | Threaded
Open this post in threaded view
|

Re: FFI / Primitive question

Joshua Gargus-2
I think that I was a bit confused.  If I now understand correctly,  
the second argument to #primitive:parameters: doesn't do anything  
special for FFI types (i.e. subclasses of ExternalObject); it is just  
a dynamic type test that can be applied to any type of Squeak object.

Is it true that the slang code must explicitly handle the two cases  
where the 'handle' iVar of an ExternalObject refers to:
        - the binary data for the object (when the handle is a ByteArray)
        - a pointer to data on the C-heap (when the handle is an  
ExternalAddress)   ?
Or, is there some feature of Slang, FFI, etc. to make it easier to  
work with ExternalObjects?  I suspect that there isn't.

Thanks,
Josh


On Jun 10, 2007, at 5:14 PM, Joshua Gargus wrote:

> Hi all,
>
> I have a question about mixing FFI types with primitive  
> invocations.  For my example, let's say that I want to pass a  
> MacRect as a primitive argument.  The Slang method looks like:
>
> primitiveFrobulate: macRect
> | rectPtr |
> self primitive: 'primitiveFrobulate' parameters: #(MacRect).
> rectPtr := interpreterProxy firstIndexableField: macRect.
> self stFrobMacRect: rectPtr
>
>
> The function stFrobMacRect() is hand-written C-code that looks  
> something like:
>
> void stFrobMacRect(int ptr)
> {
> Rect* rectPtr = (Rect*) ptr;
> /* now, frobulate the rect */
> }
>
>
> This doesn't work (I don't see the expected values while  
> frobulating).  What would be the right expression to write in the  
> Slang code to get a pointer to the Rect to pass to stFrobMacRect()?
>
> Thanks,
> Josh
>


Reply | Threaded
Open this post in threaded view
|

Re: FFI / Primitive question

johnmci
If you download Sophie you'll find lots of FFI calls especially in  
the area of navigation Services and QuickTime each of which require  
using much more
complex structures that Mac Rectangles.

On Jun 10, 2007, at 7:43 PM, Joshua Gargus wrote:

> I think that I was a bit confused.  If I now understand correctly,  
> the second argument to #primitive:parameters: doesn't do anything  
> special for FFI types (i.e. subclasses of ExternalObject); it is  
> just a dynamic type test that can be applied to any type of Squeak  
> object.
>
> Is it true that the slang code must explicitly handle the two cases  
> where the 'handle' iVar of an ExternalObject refers to:
> - the binary data for the object (when the handle is a ByteArray)
> - a pointer to data on the C-heap (when the handle is an  
> ExternalAddress)   ?
> Or, is there some feature of Slang, FFI, etc. to make it easier to  
> work with ExternalObjects?  I suspect that there isn't.
>
> Thanks,
> Josh
>

--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: FFI / Primitive question

Joshua Gargus-2
Hi John,

Thanks for the pointers.  I had looked at the QuickTime code before,  
and checking again I still can't see any cases where primitives are  
invoked with an instance of an FFI type as an argument (this was the  
topic of my original question).

In fact, it seems like  
#primitiveSetGWorldPtrOntoSurface:width:height:rowBytes:depth:movie:  
might be intentionally avoiding passing a MacRect; it's sole sender  
first extracts the height and width from a MacRect.  This isn't  
surprising, since I now think that it's not such a bright idea to try  
to mix FFI types with primitive invocations.

You referred to "navigation services"... are you talking about the  
Sophie-MacServices package?  If so, I'll take a close look at that  
code.  The ServicesPlugin code isn't in SophieSource; should I be  
looking in the squeakvm.org svn repository, or elsewhere?

(BTW, I'm not particularly interested in MacRects, I just wanted to  
base my example on a simple FFI type that already exists in the  
image.  I don't plan to do much frobulating, either ;-)  )

Thanks again,
Josh


On Jun 10, 2007, at 9:52 PM, John M McIntosh wrote:

> If you download Sophie you'll find lots of FFI calls especially in  
> the area of navigation Services and QuickTime each of which require  
> using much more
> complex structures that Mac Rectangles.
>
> On Jun 10, 2007, at 7:43 PM, Joshua Gargus wrote:
>
>> I think that I was a bit confused.  If I now understand correctly,  
>> the second argument to #primitive:parameters: doesn't do anything  
>> special for FFI types (i.e. subclasses of ExternalObject); it is  
>> just a dynamic type test that can be applied to any type of Squeak  
>> object.
>>
>> Is it true that the slang code must explicitly handle the two  
>> cases where the 'handle' iVar of an ExternalObject refers to:
>> - the binary data for the object (when the handle is a ByteArray)
>> - a pointer to data on the C-heap (when the handle is an  
>> ExternalAddress)   ?
>> Or, is there some feature of Slang, FFI, etc. to make it easier to  
>> work with ExternalObjects?  I suspect that there isn't.
>>
>> Thanks,
>> Josh
>>
>
> --
> ======================================================================
> =====
> John M. McIntosh <[hidden email]>
> Corporate Smalltalk Consulting Ltd.  http://
> www.smalltalkconsulting.com
> ======================================================================
> =====
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: FFI / Primitive question

johnmci

On Jun 10, 2007, at 10:35 PM, Joshua Gargus wrote:

> Hi John,
>
> Thanks for the pointers.  I had looked at the QuickTime code  
> before, and checking again I still can't see any cases where  
> primitives are invoked with an instance of an FFI type as an  
> argument (this was the topic of my original question).
>
> In fact, it seems like  
> #primitiveSetGWorldPtrOntoSurface:width:height:rowBytes:depth:movie: m
> ight be intentionally avoiding passing a MacRect; it's sole sender  
> first extracts the height and width from a MacRect.  This isn't  
> surprising, since I now think that it's not such a bright idea to  
> try to mix FFI types with primitive invocations.

SophieImageReadWriter>>buildComponentAndDrawCGContextUsing:osType:

uses MacRect

We use Quicktime's image reader/writer logic to read arbitrary image  
data into Sophie and transform them into PNG data, or bitmaps via  
GCContexts (os-x) or QuickDraw surfaces (Window). Or I'm afraid under  
linux or deprived Windows machines we fallback to Squeak's limited  
set of graphics imports.

>
> You referred to "navigation services"... are you talking about the  
> Sophie-MacServices package?  If so, I'll take a close look at that  
> code.  The ServicesPlugin code isn't in SophieSource; should I be  
> looking in the squeakvm.org svn repository, or elsewhere?

ServicesPlugin is in SVN since it's not Sophe specific, more like mac  
specific. Mind I've not heard of anyone actually exploiting it.

You want to look at the class MacNavigationDialogServices, somehow I  
thought we broke that out into a different MC package, but I see it's  
in Sophie-FFI-Mac

>
> (BTW, I'm not particularly interested in MacRects, I just wanted to  
> base my example on a simple FFI type that already exists in the  
> image.  I don't plan to do much frobulating, either ;-)  )
>
> Thanks again,
> Josh

--
========================================================================
===
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
========================================================================
===



Reply | Threaded
Open this post in threaded view
|

Re: FFI / Primitive question

Bert Freudenberg
In reply to this post by Joshua Gargus-2
On Jun 11, 2007, at 7:35 , Joshua Gargus wrote:

>  I now think that it's not such a bright idea to try to mix FFI  
> types with primitive invocations.

Indeed ;)

- Bert -