[MacRuby-devel] calling Objective-C method with block parameter

7 views
Skip to first unread message

Alan Skipp

unread,
Nov 15, 2010, 5:51:00 AM11/15/10
to MacRuby development discussions.
Hello everyone,
I'm attempting to call a method on an Objective-C object which takes a block as its parameter, but I'm not having much luck.  I can happily create the object in Macruby and send the message with a Proc. The NSLog call within the Objective-C method body succeeds, but the 'block()' doesn't. Am I doing something obviously wrong here? (I'm using a nightly build from sometime last week).


This is the Objective-C method:

- (void)callBlock:(void (^)())block;
{
NSLog(@"block: %@", block);
block();
}

Here is the ruby code:

b = TestBlock.new
b.callBlock( Proc.new { puts "hello" } )


The output is as follows:

block: #<Proc:0x2005c9b80>
Program received signal:  “EXC_BAD_ACCESS”.

Matt Aimonetti

unread,
Nov 16, 2010, 5:10:40 PM11/16/10
to MacRuby development discussions.
Did you install BridgeSupport preview 1?  http://www.macruby.org/blog/2010/10/08/bridgesupport-preview.html
It is required to use C blocks.

Thanks,

- Matt


_______________________________________________
MacRuby-devel mailing list
MacRub...@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel


Thibault Martin-Lagardette

unread,
Nov 16, 2010, 5:23:12 PM11/16/10
to MacRuby development discussions.
Also, it is to note that if the block lives inside a framework you've made (or downladed – one that is not part of the system), you'll have to generate the BridgeSupport files yourselves.
This is important because the runtime needs to know that you're trying to use blocks, and you instruct it to use them by creating and using the said BridgeSupport files :-)

-- 
Thibault Martin-Lagardette


Alan Skipp

unread,
Nov 17, 2010, 8:32:34 AM11/17/10
to MacRuby development discussions.
Thanks for the info. I'd wrongly assumed that as blocks can be treated as Objective-C objects that I could just go ahead and use them in Macruby.
I have it working now, which is great, though I do have one more question. 
Initially I was receiving the same errors after I'd included the framework with a bridgesupport file into my project. I finally got it working after invoking, 'load_bridge_support_file' in my controller class. Is there something I should be doing that would enable macruby to automatically detect and load the bridgesupport file included in my framework?

Cheers,
Al

Eloy Duran

unread,
Nov 17, 2010, 8:42:55 AM11/17/10
to MacRuby development discussions.
The BridgeSupport file should be in a BridgeSupport directory inside the framework’s Resources directory. For example, Foundation’s BridgeSupport file is at: /System/Library/Frameworks/Foundation.framework/Resources/BridgeSupport/Foundation.bridgesupport

Alan Skipp

unread,
Nov 17, 2010, 9:34:13 AM11/17/10
to MacRuby development discussions.
Thanks for the reply. I have the bridgesupport file located in the '/Resources/BridgeSupport/' folder of the framework, but currently it is not automatically loaded. I've tried making a clean build to no avail and I've checked the built app package and the framework is copied and the bridgesupport file is present. I also created a new test project in Xcode (just in case something had went awry with the other project) and I had to explicitly call 'load_bridge_support_file' to get the framework working there also.


On 17 Nov 2010, at 13:42, Eloy Duran wrote:

> The BridgeSupport file should be in a BridgeSupport directory inside the framework’s Resources directory. For example, Foundation’s BridgeSupport file is at: /System/Library/Frameworks/Foundation.framework/Resources/BridgeSupport/Foundation.bridgesupport
>
> On Nov 17, 2010, at 2:32 PM, Alan Skipp wrote:
>
>> Thanks for the info. I'd wrongly assumed that as blocks can be treated as Objective-C objects that I could just go ahead and use them in Macruby.
>> I have it working now, which is great, though I do have one more question.
>> Initially I was receiving the same errors after I'd included the framework with a bridgesupport file into my project. I finally got it working after invoking, 'load_bridge_support_file' in my controller class. Is there something I should be doing that would enable macruby to automatically detect and load the bridgesupport file included in my framework?
>>
>> Cheers,
>> Al

_______________________________________________

Eloy Duran

unread,
Nov 17, 2010, 9:39:17 AM11/17/10
to MacRuby development discussions.
That’s no good. Can you file a ticket? (Preferably with the test framework attached)

Alan Skipp

unread,
Nov 17, 2010, 12:05:12 PM11/17/10
to MacRuby development discussions.
Ticket #1000 has been filed.
Does the 1000th ticket get a special prize?

Matt Aimonetti

unread,
Nov 17, 2010, 12:26:52 PM11/17/10
to MacRuby development discussions.
Congratulations!! You won a lifetime license to use MacRuby 0.7.1 for free ;)

I tested your framework and I wasn't able to reproduce the bug mentioned. I closed the ticket https://www.macruby.org/trac/ticket/1000#comment:2
But feel free to investigate more and reopen it if you find something new.

Thanks,

- Matt

Alan Skipp

unread,
Nov 18, 2010, 4:52:23 AM11/18/10
to MacRuby development discussions.
A wonderful prize indeed!

Regarding the bug, I think I've been led astray by Xcode. If I add the framework in the usual way to an Xcode project, the class defintions within the framework become accessible to macruby. Therefore I assumed that by linking to the framework, macruby automatically loads it because I am able to create objects from the framework's header file. The failure only occurs when attempting to call methods that require a bridgesupport file.

If I explicitly load the framework in 'rb_main.rb', everything works correctly. I hadn't realised this step was necessary, as simply linking to the framework in Xcode appears to load it and make it available to macruby code.

Eloy Duran

unread,
Nov 18, 2010, 4:59:01 AM11/18/10
to MacRuby development discussions.
Ah yes, this is one for a FAQ. Linking against a framework does not automatically make MacRuby read the BridgeSupport files that might be there. But if you use the MacRuby Kernel::framework method, it will do both. (Though it’s not necessary to do it from rb_main.rb, just before the code where you will use the framework.)

Laurent Sansonetti

unread,
Nov 21, 2010, 9:51:36 PM11/21/10
to MacRuby development discussions.
Hi Alan,

Indeed, if you link a specific framework into your project's executable in Xcode, its BridgeSupport file won't be loaded at runtime by MacRuby. I re-opened #1000 as I think MacRuby should do it for you, nevertheless. I believe this issue might hit other persons as MacRuby will gain popularity, so let's try to get it fixed for the upcoming release.

Laurent
Reply all
Reply to author
Forward
0 new messages