Don't know if it deserves a ticket, so I'm posting here first. I'm trying to use ObjectiveFlickr from my MacRuby project: http://github.com/czak/flickrtest One of the ObjectiveFlickr delegate methods receives NSUIntegers from the library: http://github.com/lukhnos/objectiveflickr/blob/b480aa39fa0e96f40a1b44523e0b6... When I implement the delegate method in ruby (even an empty implementation): http://github.com/czak/flickrtest/blob/00ad51b155b65221770aa4ab1661fd99957a6... ...I get a crash saying: Program received signal: “EXC_BAD_ACCESS”. warning: Could not find object file "/private/tmp/mrr/trunk/array.o" - no debug information available for "array.c". warning: Could not find object file "/private/tmp/mrr/trunk/bignum.o" - no debug information available for "bignum.c". warning: Could not find object file "/private/tmp/mrr/trunk/class.o" - no debug information available for "class.c". ... (a total of about 80 warnings for missing *.o files) Lukhnos (the author of ObjectiveFlickr) suggests it might be a case of primitive C types being passed to the delegate. Please advise - is it a bug in MacRuby that I should submit a ticket for or is there a way to avoid that? -- Regards, Łukasz Adamczak
My question boils down to a simpler case: Ruby: -------- class Dog def bark(num = 1) num.times { puts "woof!" } end end Objective-C: ---------------- id dog = [[MacRuby sharedRuntime] evaluateString:@"Dog.new"]; [dog bark:3]; Passing Objective-C int to a Ruby method crashes it. Assuming I don't have access to the Objective-C side (the caller) - how do I make it work? Thanks! -- Regards, Łukasz Adamczak
Hi Łukasz, Since the bark method is defined in Ruby, it means its return value and arguments are objects (id). So, if you want to call the method from Objective-C, you must pass an Objective-C object. In this case, an NSNumber object should do it. If the bark method was actually an Objective-C method overwritten in Ruby, then passing the C integer would have worked. Also, it is generally safer to use the -[performRubySelector:] method when calling specialized Ruby methods (those with optional or splat arguments). The Ruby method calling semantics differ a little bit from Objective-C so it won't always work. HTH, Laurent 2009/5/21 Łukasz Adamczak <lukasz@czak.pl>:
My question boils down to a simpler case:
Ruby: -------- class Dog def bark(num = 1) num.times { puts "woof!" } end end
Objective-C: ---------------- id dog = [[MacRuby sharedRuntime] evaluateString:@"Dog.new"]; [dog bark:3];
Passing Objective-C int to a Ruby method crashes it.
Assuming I don't have access to the Objective-C side (the caller) - how do I make it work?
Thanks!
-- Regards, Łukasz Adamczak _______________________________________________ MacRuby-devel mailing list MacRuby-devel@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
an NSNumber object should do it.
Laurent, thanks for the answer. I believed that was the case. What about the delegate method case, though? I mean, the framework I'm playing with expects a certain delegate method and passes C unsigned ints to it: http://github.com/lukhnos/objectiveflickr/blob/b480aa39fa0e96f40a1b44523e0b6... In this case I have no say in the Objective-C side of things. I write only Ruby code. The only thing I can do is to not implement the method at all (and lose some functionality provided by the framework). Obviously in an open-source environment I'm free to patch the library to my liking, but that's not optimal and not always possible. There may be more cases like this. Unless there is a policy on not using primitive types in delegates (is there?), more libraries (or maybe Cocoa itself?) can have problems with MacRuby. -- Regards, Łukasz Adamczak
Hi Łukasz, On May 22, 2009, at 1:58 AM, Łukasz Adamczak wrote:
an NSNumber object should do it.
Laurent, thanks for the answer. I believed that was the case.
What about the delegate method case, though? I mean, the framework I'm playing with expects a certain delegate method and passes C unsigned ints to it:
http://github.com/lukhnos/objectiveflickr/blob/b480aa39fa0e96f40a1b44523e0b6...
In this case I have no say in the Objective-C side of things. I write only Ruby code. The only thing I can do is to not implement the method at all (and lose some functionality provided by the framework). Obviously in an open-source environment I'm free to patch the library to my liking, but that's not optimal and not always possible.
There may be more cases like this. Unless there is a policy on not using primitive types in delegates (is there?), more libraries (or maybe Cocoa itself?) can have problems with MacRuby.
MacRuby can deal with specialized delegate methods like this one, but you need to generate a bridgesupport file for the framework you're targeting. Most frameworks that ship with Mac OS X are already covered, but in your case you might want to manually cover ObjectiveFlickr too. You can learn more by reading the gen_bridge_metadata(1) or BridgeSupport(5) man-pages. What's going to happen is that informal protocols will be described and at runtime MacRuby will appropriately use the right method encoding in case the selector matches. HTH, Laurent
MacRuby can deal with specialized delegate methods like this one, but you need to generate a bridgesupport file for the framework you're targeting. Most frameworks that ship with Mac OS X are already covered, but in your case you might want to manually cover ObjectiveFlickr too.
Wow. This is way better than I expected. I didn't know bridgesupport was so well integrated with OS X. After a few hours of hacking, ObjectiveFlickr now has BridgeSupport and is MacRuby compliant :) http://github.com/lukhnos/objectiveflickr However, gen_bridge_metadata didn't work out-of-the box. I had to hack it a little to get a good dump. Some of the headers were parsed incorrectly. I guess I'll try to contact someone @ http://bridgesupport.macosforge.org about that. Thanks again for the help! -- Regards, Łukasz Adamczak
Hi Łukasz, On May 24, 2009, at 3:39 AM, Łukasz Adamczak wrote:
MacRuby can deal with specialized delegate methods like this one, but you need to generate a bridgesupport file for the framework you're targeting. Most frameworks that ship with Mac OS X are already covered, but in your case you might want to manually cover ObjectiveFlickr too.
Wow. This is way better than I expected.
I didn't know bridgesupport was so well integrated with OS X. After a few hours of hacking, ObjectiveFlickr now has BridgeSupport and is MacRuby compliant :) http://github.com/lukhnos/objectiveflickr
However, gen_bridge_metadata didn't work out-of-the box. I had to hack it a little to get a good dump. Some of the headers were parsed incorrectly. I guess I'll try to contact someone @ http://bridgesupport.macosforge.org about that.
Thanks again for the help!
Excellent! Regarding the BridgeSupport hack, I'm in fact the author & maintainer of this project too. So feel free to send me your patch and I will integrate it in the next possible Mac OS X version. To be honest I do not like the way gen_bridge_metadata is implemented and I plan to re-write it using clang in the future. Laurent
participants (3)
-
Laurent Sansonetti
-
Laurent Sansonetti
-
Łukasz Adamczak