[MacRuby-devel] Exposing c structs and functions

Laurent Sansonetti lsansonetti at apple.com
Fri Dec 19 12:27:35 PST 2008


Hi Jim,

Sorry for the late response!

Indeed the best way to make your C API available from MacRuby is to  
cover it with a BridgeSupport file and load it, MacRuby will  
reconstruct the corresponding API in the Ruby world. BridgeSupport  
descriptions are language agnostic and can be loaded in other  
scripting languages too.

On Dec 19, 2008, at 12:03 PM, Jim Getzen wrote:

> OK, I decided to devote a little time to see if I could make any  
> headway with BridgeSupport. It doesn't seem too terribly  
> complicated, but I have run into a problem.
>
> My framework is called ZenGL, so I first created a struct in ZenGL.h  
> based on NSPoint:
> typedef struct _ZPoint {
> 	float x;
> 	float y;
> } ZPoint;
>
> I also created a ZPointMake function:
> ZPoint ZPointMake(float x, float y);
>
> Then, I rebuilt the framework. Fine so far.
>
> Next, I created an XML ZenGL.bridgesupport file using Foundation.xml  
> (in /Library/BridgeSupport/) as an example. Here are the contents:
> <?xml version='1.0'?>
> <!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd 
> ">
> <signatures version='0.9'>
> 	<struct name='ZPoint'  
> encoding='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'  
> type='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'/>
> 	<function name='ZPointMake'>
>    	<function_arg type='f'/>
>    	<function_arg type='f'/>
>    	<function_retval type='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'/>
>  	</function>
> </signatures>
> I placed ZenGL.bridgesupport in the folder with my built MacRuby app  
> and added this code to load it:
> load_bridge_support_file './ZenGL.bridgesupport'
>
> (Going back to my xml, I originally did not include a 'type'  
> attribute for the struct since the NSPoint example in Foundation.xml  
> did not have one, but I received an error when running the load_...  
> command. Not sure why.)
>
> All seems well, but when I try this code from MacRuby:
> zp = ZPointMake(10.0, 10.0)
> I get this error:
> `ZPointMake': wrong number of arguments (2 for 0) (ArgumentError)
>
> My ZPointMake function definition in the xml sure looks right to me,  
> based on the examples in Foundation.xml, so what have I done wrong?

Looks like your XML description is not correct, function_arg and  
function_retval are invalid elements.

Try:

<?xml version='1.0'?>
<!DOCTYPE signatures SYSTEM "file://localhost/System/Library/DTDs/BridgeSupport.dtd 
">
<signatures version='0.9'>
	<struct name='ZPoint'  
encoding='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'  
type='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'/>
	<function name='ZPointMake'>
		<arg type='f'/>
		<arg type='f'/>
		<retval type='{_ZPoint=&quot;x&quot;f&quot;y&quot;f}'/>
  	</function>
</signatures>

You can learn more about the BridgeSupport format by reading the  
gen_bridge_metadata(1) and BridgeSupport(5) man pages.

Laurent


More information about the MacRuby-devel mailing list