Laurent Sansonetti
lsans****@apple*****
Wed Mar 14 09:23:24 JST 2007
Hi guys, I recently committed support for function-pointers arguments and return value of functions and methods. It means that you can now use them from RubyCocoa. How does it work? gen_bridge_metadata.rb has been modified to detect them and provide some additional XML markup. RubyCocoa at runtime detects the markup and will convert Ruby Proc objects into function- pointers, by creating an intermediate Libffi closure. Practically: $ irb -r osx/foundation >> include OSX => Object >> ary = NSMutableArray.alloc.init => #<OSX::NSCFArray:0x83d768 class='NSCFArray' id=0x125fd20> >> [5,3,2,4,1].each { |i| ary.addObject(i) } => [5, 3, 2, 4, 1] >> ary.sortUsingFunction_context(proc { |x, y, ctx| x.intValue <=> y.intValue }, nil) => nil >> ary.to_a.map { |x| x.to_i } => [1, 2, 3, 4, 5] Notes: - for functions/methods accepting only one function-pointer argument, we might want to accept a block directly (so, ``ary.sortUsingFunction_context(nil) { |x, y, ctx| ... }''). - RubyCocoa doesn't convert function-pointer return value into Ruby Proc objects yet. Enjoy :) Laurent