tag:blogger.com,1999:blog-4704664917418794835.post1804825522200804514..comments2023-07-01T05:41:30.469-07:00Comments on Headius: FFI for Ruby Now Availableheadiushttp://www.blogger.com/profile/15717357218364947795noreply@blogger.comBlogger23125tag:blogger.com,1999:blog-4704664917418794835.post-57575803163293178342014-04-03T08:11:54.380-07:002014-04-03T08:11:54.380-07:00The first code id a bit disappointing when I ran i...The first code id a bit disappointing when I ran it in jirb.<br />Here's why.<br />LoadError: no library specified<br /> from C:/jruby-1.7.11/lib/ruby/shared/ffi/library.rb:114:in `ffi_libraries'<br /> from C:/jruby-1.7.11/lib/ruby/shared/ffi/library.rb:164:in `attach_function'<br /> from (irb):7:in `GetPid'<br /> from (irb):4:in `evaluate'<br /> from org/jruby/RubyKernel.java:1121:in `eval'<br /> from org/jruby/RubyKernel.java:1521:in `loop'<br /> from org/jruby/RubyKernel.java:1284:in `catch'<br /> from org/jruby/RubyKernel.java:1284:in `catch'<br /> from C:\jruby-1.7.11\/bin/jirb_swing:53:in `(root)'<br />NoMethodError: undefined method `getpid' for GetPid:Module<br /> from (irb):10:in `evaluate'<br /> from org/jruby/RubyKernel.java:1121:in `eval'<br /> from org/jruby/RubyKernel.java:1521:in `loop'<br /> from org/jruby/RubyKernel.java:1284:in `catch'<br /> from org/jruby/RubyKernel.java:1284:in `catch'<br /> from C:\jruby-1.7.11\/bin/jirb_swing:53:in `(root)'Douglashttps://www.blogger.com/profile/03486876006429318487noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-85006731633645598272008-10-31T17:10:00.000-07:002008-10-31T17:10:00.000-07:00Just out of interest: have you talked to any of th...Just out of interest: have you talked to any of the other implementations yet? I don't know about GemStone/S, but both Parrot and .NET have some sort of FFI and so it shouldn't probably too hard to add FFI to Cardinal, Ruby.NET or IronRuby.<br><br>Also, have you talked to Matz about maybe getting FFI into the Ruby Spec, maybe even deprecating the Extension interface for, say, the YARV 2.0 or Rite timeframe and maybe the upcoming ISO Spec?jwmittaghttp://joerg.mittag.meinguter.name/noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-75981419278802659062008-10-31T17:12:00.000-07:002008-10-31T17:12:00.000-07:00Really sounds like a great move! I hope the MRI gu...Really sounds like a great move! I hope the MRI guys will follow even if unfortunately a few guys might do their best to avoid confrontation with JRuby on purpose and prefer a C extensions balkanization to consolidate their small influence circles. Hope the general interest will win here.<br><br>A question I have is: do you know if the Jython guys are benefiting from this work and in which ways? I would really see great Python softwares such as OpenERP to run on the JVM (Java would get a killer ERP; best oss potential SAP killer would just run on Java), Jython seems good enough at this stage but the C libraries support seems even a greater issue with Jython than it is with JRuby given that the 'maturity' of Python brought it way to much to the native extensions trend and almost every real application would require C bindings at some point. Any idea?<br><br>And aside from Jython, might all the other JVM languages, including the Java language, benefit from that kind of lib to set up bridges with the non Java world? Could you imagine a Swing application backed with GTK using such a lib (just curious, I consider Swing better even if its momentum isn't as good)?<br><br><br>Finally, I assume no C library call might fit in the Java security sandbox, correct? Java might have lost the RIA battle. But, given the recent plugin progresses for instance or the Flash VM limitations that would just be a shame. But RIA means a fine grained security sandbox model. Would that be an all or nothing model: like JNA calls are non secured or could we imagine somethings like JNA calls to signed+certified C libs might be allowed: like a C call to GTK or anything else stable and secured would be allowed. Sorry if that's a dumb question, but could such things be done?<br><br><br>Congrats as usual anyway.<br><br>Raphaël Valyi.Raphaël Valyihttp://www.blogger.com/profile/01805258585519968165noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-45206164899019991692008-10-31T17:44:00.000-07:002008-10-31T17:44:00.000-07:00Awesome work indeed!FFI for C Ruby yay!I only foun...Awesome work indeed!<br><br>FFI for C Ruby yay!<br><br>I only found problematic to wotk with broken kenai site and workaround hardcoded stuff in the building process.<br><br>Of course, I'm talking to make FFI work on Windows, which is pretty much working for libffi.<br><br>Anyhow, great work from Wayne!Luishttp://www.blogger.com/profile/13019468621368759952noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-79408039611088449942008-10-31T17:47:00.000-07:002008-10-31T17:47:00.000-07:00I agree with jwmittag. A *standard* FFI interface...I agree with jwmittag. A *standard* FFI interface would be a big thing for Ruby.<br><br>Look at how every Common Lisp implementation has its own FFI (which is now thankfully handled by UFFI, but it took a long while), and see what a mess you can have when you don't have a standard FFI.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-65008512410724603732008-10-31T22:56:00.000-07:002008-10-31T22:56:00.000-07:00jsmittag: I think it's come up in discussions ...jsmittag: I think it's come up in discussions with John Lam of IronRuby, and I don't suspect they'll have any trouble building an FFI of their own. Also, I brought up FFI to Matz this past spring and he didn't seem opposed to it. I think he needs to be shown how it's better than DL, which I didn't have a chance to do at the time. I think the API Evan and we have assembled will easily become a de-facto standard, at any rate.<br><br>raphaël: I think the release of the gem pretty much guarantees that FFI will become a commonly-used API now. Even if there are anti-Java or anti-JRuby zealots out there, the API is nice enough to draw them in anyway. And the more libraries we see backed by FFI, the better for all implementations.<br><br>We are also working closely with the Jython guys. Wayne has spoken with them about JNA and JFFI use, and it's almost certain they'll work with him to get Python's ctypes library functioning as well.<br><br>I don't expect that C calls will ever be kosher inside a secure container, but for many other domains you mention--like GUI development--they'll probably prove very useful. And honestly, I think it's time that Java included a standard library for calling C libs. Hopefully this will help push things in that direction.<br><br>luis: I've never had any trouble with Kenai, but do report issues you see. They're standing by and fixing issues as fast as they can while simultaneously building out features. I think it's going to be a great site.<br><br>As for building on Windows, please, please submit patches back. We definitely want to make the gem as solid as possible on all platforms, and none of us are well-versed in Windows dev. (I did a bunch of it years ago, but it's all been pushed out to make room for new information)<br><br>anonymous: With this gem, the same FFI code will now work on C Ruby, JRuby, and Rubinius, and I suspect that IronRuby and others will follow suit. We're now well on the path toward a standard FFI API, even if it's not currently "blessed" as such.Charles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-41413777447731979972008-11-01T01:25:00.000-07:002008-11-01T01:25:00.000-07:00Can't wait to give it a try, great work!Can't wait to give it a try, great work!rledge21http://www.blogger.com/profile/07280624261775331061noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-68407216379915754952008-11-01T07:39:00.000-07:002008-11-01T07:39:00.000-07:00Thanks Charles!I had some random errors while usin...Thanks Charles!<br><br>I had some random errors while using kenai, but seems these errors got fixed (was 3 weeks ago, which are ages in this field, hehehe).<br><br>Indeed I'll supply some patches, as soon get a healthy way to build the C extensions more independently of the platform.Luishttp://www.blogger.com/profile/13019468621368759952noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-80781409608596984592008-11-02T11:28:00.000-08:002008-11-02T11:28:00.000-08:00This sounds nice, I've tried a few times to ex...This sounds nice, I've tried a few times to extend ruby programs with C++ code, which is even harder, since you have to write a c layer that sits between ruby and c++ which handles c++'s OO features.<br><br>It takes quite some time and it feels like 'dumb' work.<br><br>So is FFI able to handle c++ libraries as well? How does it wrap the C++ classes in that case?<br><br>And on a different subject... Haskell has an FFI too (even called FFI). Is this just a coincidence or is this project inspired by haskell's? any thoughts on calling haskell code from ruby?Bluescreen303http://www.blogger.com/profile/05543339741560532560noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-23876073656161190152008-11-02T12:21:00.000-08:002008-11-02T12:21:00.000-08:00Far as I'm concerned, this is huge. Quite pos...Far as I'm concerned, this is huge. Quite possibly one of the most significant contributions JRuby has made thus far. Awesome, awesome work, and many thanks.Bob Amanhttp://www.blogger.com/profile/08770372909350717238noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-20861181726154786912008-11-02T17:42:00.000-08:002008-11-02T17:42:00.000-08:00This is beautiful! Beautiful!The examples work as...This is beautiful! Beautiful!<br><br>The examples work as advertised.<br><br>I am going to check out the code to see what is happening so that I understand how to use it. It looks like there is a winner here. I have played around with writing ruby extensions, but the current system (before ffi) were pretty nasty.<br><br>There are a lot of libraries out there that need to be wrapped.Christopher Parkerhttp://www.blogger.com/profile/07916027985463625919noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-91880338902104593492008-11-03T01:55:00.000-08:002008-11-03T01:55:00.000-08:00One thing that's also not clear is how to regi...One thing that's also not clear is how to register a custom free function (from the C side) for memory that also has been allocated from by the C library, so we can let the garbage collector handle freeing that memory. <br><br>For example in SDL you need to do <br><br>image = SDL_LoadBMP(file_name);<br>...<br>SDL_FreeSurface(image);<br><br>However, it's undesirable to have to do this manually in a garbage collected language.<br><br>C Ruby 'dl' can do this, but can FFI do this too? If so how? And if not, I think you will need this.<br><br>Anyway, looks like an interesting beginning. Keep up the good work.Beoranhttp://www.blogger.com/profile/08114449806890952256noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-58915746810502757942008-11-03T02:09:00.000-08:002008-11-03T02:09:00.000-08:00Beoran: What's wrong with putting it in a fina...Beoran: What's wrong with putting it in a finalizer? The finalizer proc holds a reference to the the memory to be freed, and frees it once that happens.<br><br>If not, feel free to submit a patch that adds the functionality to Ruby-FFI in a way that will work across implementations. :)Charles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-83840220207945988852008-11-03T03:14:00.000-08:002008-11-03T03:14:00.000-08:00What do you mean by setting a finalizer? Maybe we&...What do you mean by setting a finalizer? Maybe we're talking about the same thing? In DL on ruby 1.9.x , you'd do something like: <br><br>require 'dl/import'<br>module SDL<br>extend DL::Importer<br>extern "void * SDL_LoadBMP(char *)"<br>FREE_SURFACE = extern "void SDL_FreeSurface(void *)"<br>end<br><br>surfp = SDL.SDL_LoadBMP('cute.bmp')<br>surfp.free = SDL::FREE_SURFACE<br># Now, I don't have to worry about calling SFL_FreeSurface manually anymore<br>surfp = nil<br># Garbage collector knows what to do.<br><br>Perhaps this is already possible in ffi, but I couldn't find it.<br><br>And yes, I'm willing to help you implement this if it's not already there.Beoranhttp://www.blogger.com/profile/08114449806890952256noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-37928036966601467502008-11-03T12:23:00.000-08:002008-11-03T12:23:00.000-08:00N.B.: The 0.1.1 gem doesn't appear to have the...N.B.: The 0.1.1 gem doesn't appear to have the "templating system to generate bindings based on the current platform's header files". <br><br>FFI::Generator does exist in the Hg repo, though.Matthew Kinghttp://www.blogger.com/profile/03223931423148258501noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-67072576563726524572008-11-03T14:24:00.000-08:002008-11-03T14:24:00.000-08:00@Wayne:Well, yes, that would be nice, I guess, but...@Wayne:<br><br>Well, yes, that would be nice, I guess, but wouldn't it be even better if you could simply call .free on an instance of MemoryPointer, passing in either a block callback or a symbol of the function to call? <br><br>I'm interesting in wrapping SDL though FFI so I can also use it on JRuby for my big game project. Of course, I want an easy API. To be honest, I kinda like the DL API because it's easy to use once you get to know it. It can already parse C quite well, so perhaps you can find something to borrow in it's Ruby code? <br><br><br>Anyway, if you'd like me to help you out a bit, I already subscribed as an observer to the FFI project with username BeorAegul on the Kenai site, so perhaps we could talk further that way if you like.Beoranhttp://www.blogger.com/profile/08114449806890952256noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-34024091368522519032008-11-03T23:25:00.000-08:002008-11-03T23:25:00.000-08:00How can I compile the following code under Linux t...How can I compile the following code under Linux that is avaible under jruby ffi?<br> /* File : example.c */<br><br> /* A global variable */<br> double Foo = 3.0;<br><br> int gcd(int x, int y) {<br> int g;<br> g = y;<br> while (x > 0) {<br> g = x;<br> x = y % x;<br> y = g;<br> }<br> return g;<br> }Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-44741621191179807602008-11-04T04:06:00.000-08:002008-11-04T04:06:00.000-08:00The gcd C Code is only a example. I try make a lib...The gcd C Code is only a example. I try make a library for JRuby. I did<br><br>gcc -c -fPIC gcd.c -o gcd.o <br>gcc -shared -o libgcd.so gcd.o -lc<br><br>and my Ruby code looks like this<br>----------------------------------<br>require 'ffi'<br><br>module Test<br> extend FFI::Library<br> attach_function :gcd, [:int, :int], :int<br>end<br><br>Test.gcd(3,3) <br>---------------------------------<br><br>But if start jruby I get following error:<br> /home/flashdog/jruby-1.1.4/lib/ruby/site_ruby/1.8/ffi/ffi.rb:328:in `create_invoker': Function 'gcd' not found! (Looking in 'c' or this process) (FFI::NotFoundError)<br> from /home/Anonymous/jruby-1.1.4/lib/ruby/site_ruby/1.8/ffi/ffi.rb:380:in `attach_function'<br> from gcd.rb:5 <br><br>What do I wrong?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-79075130034994411272008-11-04T10:32:00.000-08:002008-11-04T10:32:00.000-08:00anonymous: I believe you just need to get the libr...anonymous: I believe you just need to get the library loaded, which you could do via<br><br>ffi_lib "gcd"<br><br>in the module body. If you have further issues, I recommend asking on the Ruby FFI mailing lists :)Charles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-71570727846824976302008-11-05T19:36:00.000-08:002008-11-05T19:36:00.000-08:00I have a small patch, but am I just missing it or ...I have a small patch, but am I just missing it or is there really no way to submit bugs/patches without signing up for this Kenai thing? Do not want.Brianhttp://www.codekitchen.net/noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-562750850615701652008-11-05T21:57:00.000-08:002008-11-05T21:57:00.000-08:00Brian: Kenai is just the project hosting site, lik...Brian: Kenai is just the project hosting site, like RubyForge, SourceForge, etc. As with those services, if you want to submit a bug or patch you generally need to sign up.Charles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-43752756942385462452009-09-21T13:13:47.742-07:002009-09-21T13:13:47.742-07:00I'd like to know whom to contact about Ruby-FF...I'd like to know whom to contact about Ruby-FFI. I have started to test this and it looks good, but the fact that I have not found full documentation and there seems to be no project "owner" does not inspire confidence.<br><br>Also, there are gaps, one of which I have filled ad hoc with the following code:<br><br>module FFI<br> class Pointer<br><br> def read_double<br> get_float64(0)<br> end<br> def write_double(obj)<br> put_float64(0, obj)<br> end<br><br> def read_array_of_double(length)<br> get_array_of_double(0, length)<br> end<br><br> def write_array_of_double(ary)<br> put_array_of_double(0, ary)<br> end<br><br> end<br>end<br><br>Feel free to contact me on ak90@zoom1000.com<br><br>-AlexAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-4704664917418794835.post-78233421505114672372009-10-06T11:33:56.511-07:002009-10-06T11:33:56.511-07:00Derek: You should check out the ruby-ffi project o...Derek: You should check out the ruby-ffi project on kenai.com. Your problem may be a 32/64-bit issue or you may not have the signatures matched up quite right. Folks on the ruby-ffi mailing list will be able to assist you.Charles Oliver Nutterhttp://www.blogger.com/profile/06400331959739924670noreply@blogger.com