Ruby FFI

3,868 views

Published on

A small lightning talk on Ruby's FFI module for integrating system libraries.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,868
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Ruby FFI

    1. 1. Ruby FFIThe Foreign Function Interface for accessing system wide shared libraries in Ruby Angel Pizarro angel@upenn.edu
    2. 2. Ruby C Extension API• Ruby has a C extension mechanism • Use SWIG to wrap a C API • Non-idiomatic Ruby :( • Also IO struct changed between 1.8 and 1.9, SWIG needs new typemap • Write your own C modules using this API • Other Ruby VM’s out in the cold
    3. 3. Ruby FFI• Foreign Function Interface, based on libffiA foreign function interface [...] allows code written inone language to call code written in another language.The libffi library really only provides the lowest,machine dependent layer of a fully featured foreignfunction interface. A layer must exist above libffi thathandles type conversions for values passed between thetwo languages.• MRI, MacRuby, Rubinius and JRuby support
    4. 4. Given a C API : libcairo
    5. 5. Mapping Functions
    6. 6. Function Input Args
    7. 7. Function Input ArgsThat sucks! We lose all method signature information!
    8. 8. Types, Enums & Constants
    9. 9. Types, Enums & Constants still sucks
    10. 10. Types, Enums & Constants still sucks Alias a type: Method sig now becomes nicer:
    11. 11. Structs
    12. 12. Managed Structs DANGER WILL ROBINSON!Only use if you REALLY want Ruby GC to manage references and memory
    13. 13. Final Notes• Beware library calls that open STIN / STDOUT to buffer writes • Specifically, they may close the socket and Ruby IO becomes FUBAR• Multi-dimensional arrays in structs not supported (“unroll” the previous dimension)• Do not use ManagedStruct if you just want type casting on a pointer ref• Essentially still using C idioms to code (e.g. memory management)

    ×