Preliminary libffi progress

new topic     » topic index » view thread      » older message » newer message

I've been playing around with libffi and here's what I've come up with so far.

  • Structures: You can create new "types" (using ffi_type) that can describe direct values like int, float, etc. or you can describe structures which are basically an array of types. Then you can get back the offsets of these types using ffi_get_struct_offsets(). Combined with libffi's built-in knowledge of native C types, this allows me to describe, allocate, and then peek and poke structures with very little effort.

  • C functions: You can pass any of these new "types" you've created (including structures!) to a Call InterFace or "CIF", and then allocate the required memory for arguments and return value, then perform the call with ffi_call(). libffi will take care of figuring out how to pass and receive values with its built-in knowledge of ABIs and type sizes and alignments. And it can even pass and receive structures by value!

  • Call-backs: I haven't explored this much yet, but libffi also supports creating call-backs that can receive a variable number of arguments, as well as receive non-integer (floating point) values and structures by value. These are things that Euphoria currently does not support. I need to do more digging on this but so far it looks promising.

Internally, I've created std/ffi.e that completely replaces, but remains code-compatible with, std/dll.e. What I mean by "code compatible" is that the API doesn't change, but the ABI - implementation and underlying values (like constants) - are completely different. From the end-programmer's perspective, replacing std/dll.e with std/ffi.e should be 100% compatible as long as you weren't doing anything funny with the actual values of things like C_POINTER, etc.

The biggest change is the addition of what I'm calling define_c_type() (to parallel define_c_var/func/proc) which allows you to build a new type from existing types. I also created special types based on C_POINTER for C_STRING and C_WSTRING that will automatically allocate/poke/free string values during a function call.

Here's an example of what I have working so far (at least the basic Raylib demo works). I'm looking to solicit feedback at this stage. I feel this is more worthwhile than the existing memstruct approach. So if there's support for this, I'd like to get it worked into 4.2 now, instead of memstruct. Then we can revisit building classes and structures into the language syntax later.

include std/ffi.e 
 
-- Vector2, 2 components 
--typedef struct Vector2 { 
--    float x;                // Vector x component 
--    float y;                // Vector y component 
--} Vector2; 
public constant RL_VECTOR2 = define_c_type({ 
    C_FLOAT, -- x // Vector x component 
    C_FLOAT  -- y // Vector y component 
}) 
 
-- Color, 4 components, R8G8B8A8 (32bit) 
--typedef struct Color { 
--    unsigned char r;        // Color red value 
--    unsigned char g;        // Color green value 
--    unsigned char b;        // Color blue value 
--    unsigned char a;        // Color alpha value 
--} Color; 
public constant RL_COLOR = define_c_type({ 
    C_UCHAR, -- r // Color red value 
    C_UCHAR, -- g // Color green value 
    C_UCHAR, -- b // Color blue value 
    C_UCHAR  -- a // Color alpha value 
}) 
 
atom raylib = open_dll( "raylib.dll" ) 
 
constant xInitWindow = define_c_proc( raylib, "+InitWindow", {C_INT,C_INT,C_STRING} ) 
constant xGetWindowPosition = define_c_func( raylib, "+GetWindowPosition", {}, RL_VECTOR2 ) 
constant xClearBackground = define_c_proc( raylib, "+ClearBackground", {RL_COLOR} ) 
 
public procedure InitWindow( integer width, integer height, sequence title ) 
    c_proc( xInitWindow, {width,height,title} ) -- string is allocated/freed automatically 
end procedure 
 
public function GetWindowPosition() 
    return c_func( xGetWindowPosition, {} ) -- returns {x,y} 
end function 
 
public procedure ClearBackground( sequence color ) 
    c_proc( xClearBackground, {color} ) -- color is {r,g,b,a} 
end procedure 

-Greg

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu