FFI to call Mac Carbon functions

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

FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
I want to call DCSGetTermRangeInString[1]:

CFRange DCSGetTermRangeInString (
   DCSDictionaryRef dictionary,
   CFStringRef textString,
   CFIndex offset
);

The problem is I have no idea what to do about the return value and arguments. That is:
<apicall: ??? 'DCSGetTermRangeInString' ???>

How do I call this function using FFI? Where can I see an example?

Thanks.
Sean

[1] Full doc at http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/DictionaryServicesRef/Reference/reference.html
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Levente Uzonyi-2
On Sun, 8 May 2011, Sean P. DeNigris wrote:

> I want to call DCSGetTermRangeInString[1]:
>
> CFRange DCSGetTermRangeInString (
>   DCSDictionaryRef dictionary,
>   CFStringRef textString,
>   CFIndex offset
> );
>
> The problem is I have no idea what to do about the return value and
> arguments. That is:
> &lt;apicall: ??? 'DCSGetTermRangeInString' ???&gt;
>
> How do I call this function using FFI? Where can I see an example?

You have to implement them (most of them). For example CFRange is a
struct, so you have to create a subclass of ExternalStructure for it. You
implement the class side #fields method which defines its fields, then
perform the class side #defineFields method to create the accessors for
them. For example CFRange is defined as[1]:

struct CFRange {
    CFIndex location;
    CFIndex length;
};
typedef struct CFRange CFRange;

So it has two fields, length and location, both have CFIndex as type.
CFIndex is signed long [2], so you'll implement #fields as:

CFRange class >> #fields

  ^#(
  (length 'long')
  (location 'long'))

Then evaluate [CFRange defineFields]. If this is done, then you can use
the class CFRange in the FFI call like:

<cdecl: CFRange 'DCSGetTermRangeInString' (... ... long) module: ...>

You don't have to implement C structs which you won't use in Squeak, just
pass around various C functions. In this case you can declare them as
void* and their class will be ExternalData. Don't forget that you have to
deallocate stuff allocated by C functions, to avoid memory leaks.


Levente

[1] <a href="http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFRange">http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFRange
[2] <a href="http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFIndex">http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBaseUtils/Reference/reference.html#//apple_ref/c/tdef/CFIndex

>
> Thanks.
> Sean
>
> [1] Full doc at
> http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/DictionaryServicesRef/Reference/reference.html
>
> --
> View this message in context: http://forum.world.st/FFI-to-call-Mac-Carbon-functions-tp3507842p3507842.html
> Sent from the Squeak - Dev mailing list archive at Nabble.com.
>
>

Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
Levente Uzonyi-2 wrote
You don't have to implement C structs which you won't use in Squeak, just
pass around various C functions. In this case you can declare them as
void* and their class will be ExternalData.
Thanks Levente. I tried the above with a Foundation function, with no success:

After I put a link to the Foundation lib in the VM's Resources folder, I compiled:

CarbonFunctions>>NSMakeRectWithX: xFloat y: yFloat w: widthFloat h: hFloat
        <apicall: void* 'NSMakeRect' (float float float float) module: 'Foundation'>
        ^self externalCallFailed

DoIt:
CarbonFunctions
        NSMakeRectWithX: 0.0
        y: 0.0
        w: 100.0
        h: 100.0.

Error: Unable to find function address

Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Igor Stasenko
On 20 May 2011 02:26, Sean P. DeNigris <[hidden email]> wrote:

>
> Levente Uzonyi-2 wrote:
>>
>> You don't have to implement C structs which you won't use in Squeak, just
>> pass around various C functions. In this case you can declare them as
>> void* and their class will be ExternalData.
>>
>
> Thanks Levente. I tried the above with a Foundation function, with no
> success:
>
> After I put a link to the Foundation lib in the VM's Resources folder, I
> compiled:
>
> CarbonFunctions>>NSMakeRectWithX: xFloat y: yFloat w: widthFloat h: hFloat
>        &lt;apicall: void* 'NSMakeRect' (float float float float) module:
> 'Foundation'&gt;
>        ^self externalCallFailed
>
> DoIt:
> CarbonFunctions
>        NSMakeRectWithX: 0.0
>        y: 0.0
>        w: 100.0
>        h: 100.0.
>
> Error: Unable to find function address
>

Btw, make sure that the lib you are using is 32bit.

> Sean
>
> --
> View this message in context: http://forum.world.st/FFI-to-call-Mac-Carbon-functions-tp3507842p3537296.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: FFI to call Mac Carbon functions

Bert Freudenberg

On 20.05.2011, at 10:09, Igor Stasenko wrote:

> On 20 May 2011 02:26, Sean P. DeNigris <[hidden email]> wrote:
>>
>> Levente Uzonyi-2 wrote:
>>>
>>> You don't have to implement C structs which you won't use in Squeak, just
>>> pass around various C functions. In this case you can declare them as
>>> void* and their class will be ExternalData.
>>>
>>
>> Thanks Levente. I tried the above with a Foundation function, with no
>> success:
>>
>> After I put a link to the Foundation lib in the VM's Resources folder, I
>> compiled:
>>
>> CarbonFunctions>>NSMakeRectWithX: xFloat y: yFloat w: widthFloat h: hFloat
>>        &lt;apicall: void* 'NSMakeRect' (float float float float) module:
>> 'Foundation'&gt;
>>        ^self externalCallFailed
>>
>> DoIt:
>> CarbonFunctions
>>        NSMakeRectWithX: 0.0
>>        y: 0.0
>>        w: 100.0
>>        h: 100.0.
>>
>> Error: Unable to find function address

Seems broken. On the interpreter VM (4.2.5) I can't even call libc functions. 2 of the FFI tests fail.

On Cog (r2348), all tests are green, libc examples work, and I can call some Carbon functions (from the 'ApplicationServices' module, no linking necessary. Not sure if NS* functions are supposed to work.

> Btw, make sure that the lib you are using is 32bit.

MacOS uses fat binaries, so that's no problem:

cd /System/Library/Frameworks/Foundation.framework
file Foundation
Foundation: Mach-O universal binary with 3 architectures
Foundation (for architecture x86_64): Mach-O 64-bit dynamically linked shared library x86_64
Foundation (for architecture i386): Mach-O dynamically linked shared library i386
Foundation (for architecture ppc7400): Mach-O dynamically linked shared library ppc

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
Bert Freudenberg wrote
Seems broken. On the interpreter VM (4.2.5) I can't even call libc functions. 2 of the FFI tests fail.
I have libc working with 4.2.5. Did you either:
* put a link to the lib in the VM's resources folder? (that's how I got it to work)
* or, change the VM's Info.plist "SqueakPluginsBuiltInOrLocalOnly" key from "true" to "false"

Bert Freudenberg wrote
Not sure if NS* functions are supposed to work.
I thought they were just C functions. Is there anything special about them that would make them not work?

Thanks.
Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Bert Freudenberg

On 20.05.2011, at 16:17, Sean P. DeNigris wrote:

>
> Bert Freudenberg wrote:
>>
>> Seems broken. On the interpreter VM (4.2.5) I can't even call libc
>> functions. 2 of the FFI tests fail.
>>
>
> I have libc working with 4.2.5. Did you either:
> * put a link to the lib in the VM's resources folder? (that's how I got it
> to work)
> * or, change the VM's Info.plist "SqueakPluginsBuiltInOrLocalOnly" key from
> "true" to "false"

No. That explains it.

> Bert Freudenberg wrote:
>>
>> Not sure if NS* functions are supposed to work.
>>
> I thought they were just C functions. Is there anything special about them
> that would make them not work?

I guess I confused them with Objective-C methods. No, should be fine.

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Igor Stasenko
On 20 May 2011 16:47, Bert Freudenberg <[hidden email]> wrote:

>
> On 20.05.2011, at 16:17, Sean P. DeNigris wrote:
>
>>
>> Bert Freudenberg wrote:
>>>
>>> Seems broken. On the interpreter VM (4.2.5) I can't even call libc
>>> functions. 2 of the FFI tests fail.
>>>
>>
>> I have libc working with 4.2.5. Did you either:
>> * put a link to the lib in the VM's resources folder? (that's how I got it
>> to work)
>> * or, change the VM's Info.plist "SqueakPluginsBuiltInOrLocalOnly" key from
>> "true" to "false"
>
> No. That explains it.

Btw, what this flag supposed to do?
Because name are really confusing.

>
>> Bert Freudenberg wrote:
>>>
>>> Not sure if NS* functions are supposed to work.
>>>
>> I thought they were just C functions. Is there anything special about them
>> that would make them not work?
>
> I guess I confused them with Objective-C methods. No, should be fine.
>
> - Bert -
>
>
>
>



--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Bert Freudenberg

On 20.05.2011, at 16:53, Igor Stasenko wrote:

> On 20 May 2011 16:47, Bert Freudenberg <[hidden email]> wrote:
>>
>> On 20.05.2011, at 16:17, Sean P. DeNigris wrote:
>>
>>>
>>> Bert Freudenberg wrote:
>>>>
>>>> Seems broken. On the interpreter VM (4.2.5) I can't even call libc
>>>> functions. 2 of the FFI tests fail.
>>>>
>>>
>>> I have libc working with 4.2.5. Did you either:
>>> * put a link to the lib in the VM's resources folder? (that's how I got it
>>> to work)
>>> * or, change the VM's Info.plist "SqueakPluginsBuiltInOrLocalOnly" key from
>>> "true" to "false"
>>
>> No. That explains it.
>
> Btw, what this flag supposed to do?
> Because name are really confusing.

It makes sure only plugins from the app bundle get loaded. Those are either built-in, or "local" (in the Resources folder).

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Igor Stasenko
On 20 May 2011 17:07, Bert Freudenberg <[hidden email]> wrote:

>
> On 20.05.2011, at 16:53, Igor Stasenko wrote:
>
>> On 20 May 2011 16:47, Bert Freudenberg <[hidden email]> wrote:
>>>
>>> On 20.05.2011, at 16:17, Sean P. DeNigris wrote:
>>>
>>>>
>>>> Bert Freudenberg wrote:
>>>>>
>>>>> Seems broken. On the interpreter VM (4.2.5) I can't even call libc
>>>>> functions. 2 of the FFI tests fail.
>>>>>
>>>>
>>>> I have libc working with 4.2.5. Did you either:
>>>> * put a link to the lib in the VM's resources folder? (that's how I got it
>>>> to work)
>>>> * or, change the VM's Info.plist "SqueakPluginsBuiltInOrLocalOnly" key from
>>>> "true" to "false"
>>>
>>> No. That explains it.
>>
>> Btw, what this flag supposed to do?
>> Because name are really confusing.
>
> It makes sure only plugins from the app bundle get loaded. Those are either built-in, or "local" (in the Resources folder).
>

IMO, plugins should be looked up only in Resources folder on macs.
But not for FFI. Because apparently it kills an idea of having FFI.

> - Bert -
>
>
>
>



--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
Igor Stasenko wrote
IMO, plugins should be looked up only in Resources folder on macs.
But not for FFI. Because apparently it kills an idea of having FFI.
Yes it's a PITA to find the exact location of system libs and then link them into the Resources folder.

Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
In reply to this post by Bert Freudenberg
Bert Freudenberg wrote
Seems broken. On the interpreter VM (4.2.5) I can't even call libc functions. 2 of the FFI tests fail.

On Cog (r2348), all tests are green, libc examples work, and I can call some Carbon functions (from the 'ApplicationServices' module, no linking necessary.
Who should know about this (i.e. should I report an issue somewhere)? It seems like the interpreter plugin is the problem, right? Is anyone maintaining it?

Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Bert Freudenberg

On 21.05.2011, at 20:53, Sean P. DeNigris wrote:

>
> Bert Freudenberg wrote:
>>
>> Seems broken. On the interpreter VM (4.2.5) I can't even call libc
>> functions. 2 of the FFI tests fail.
>>
>> On Cog (r2348), all tests are green, libc examples work, and I can call
>> some Carbon functions (from the 'ApplicationServices' module, no linking
>> necessary.
>>
>
> Who should know about this (i.e. should I report an issue somewhere)? It
> seems like the interpreter plugin is the problem, right? Is anyone
> maintaining it?

vm-dev would be the right list to report issues.

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: FFI to call Mac Carbon functions

Sean P. DeNigris
Administrator
Bert Freudenberg wrote
vm-dev would be the right list to report issues.
Done, thanks Bert.
Cheers,
Sean