FFI questions

Joshua 'Schwa' Gargus schwa at cc.gatech.edu
Fri Sep 5 01:20:23 UTC 2003


I'm running into some problems with FFI.  This is the method I am using:

cgCreateProgram: ctx with: programtype with: program with: profile with: entry with: args
	"This method was automatically generated."
	"CGprogram cgCreateProgram(CGcontext ctx, CGenum program_type, const char *program, CGprofile profile, const char *entry, const char **args);"
	<cdecl: ulong 'cgCreateProgram' (ulong ulong byte* ulong byte* byte** ) module: 'Cg'>
	FFIPlugin
	^ self externalCallFailed


If I get ahold of the resulting CompiledMethod and look at the first
literal, it is an ExternalLibraryFunction corresponding to the FFI
spec.  One thing that is odd is the contents of the 'argTypes' variable
of the ELFunc: #(ulong ulong ulong byte* ulong byte* byte void).  Why 
might the 'void' be there?

To try to debug this, I called the CompiledMethod with #invokeWithArguments:, 
which I modified to include the line:

	FFIPlugin doPrimitive: 'primitiveCalloutWithArgs'. 

So that I could try to figure out what was going on.  Sure enough, it was choking
on the arg array, which wasn't as long as expected because of the extra 'void' in 
'argTypes'.  I manually got rid of the 'void' (argTypes := argTypes first: 7) and
tried again.  This time I got a different error, about not being able to coerce
the arguments.  Yay!  At least this one seems like my fault.

This brings me to my next question(s):  
- Will FFI know how to coerce a String to a 'byte*'?
  - if not, should I be using 'char*' instead?
  - or, just convert the String to a ByteArray manually? (adding a terminating null)
    (ByteArray withAll: ('string' copyWith: (Character value: 0)))
- How can I find out these coercion rules?
- What the heck do I do for 'byte**'?  
  (The C function expects an array of null-terminated strings)


Thanks,
Joshua



More information about the Squeak-dev mailing list