[MacRuby] #529: Using a Proc as a ‘callback function’
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: -------------------------------------+-------------------------------------- Some Cocoa methods, or C functions, take pointers to functions which can be used as callbacks. For instance, FSEventStreamCreate: http://bit.ly/8p70Yw. RubyCocoa, in conjunction with BridgeSupport, supported this by allowing the user to give a proc that would be used as the callback. MacRuby should support this too. Here's a spec example: {{{ describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context_pointer| context = context_pointer[0].chr + context_pointer[1].chr + context_pointer[2].chr x <=> y if context == 'foo' end array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1] array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}} Don't know if this is feasible, but it would be great if the arguments given to the proc, like the context argument, were no Pointer objects, but the object they actually point to: {{{ describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context| x <=> y if context == 'foo' end array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1] array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/529> MacRuby <http://macruby.org/>
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: -------------------------------------+-------------------------------------- Comment(by eloy.de.enige@…): Actually, the latter examples should probably be more like: {{{ describe "BridgeSupport, where a pointer to a callback function is required" do it "allows the use of a proc as callback" do array = [5, 3, 2, 4, 1] proc = Proc.new { |x, y, _| x <=> y } array.sortUsingFunction(proc, context: nil) array.should == [1, 2, 3, 4, 5] end it "passes the actual objects as arguments instead of the pointers to the arguments" do result = '' proc = Proc.new { |_, __, context| result = context } [1].sortUsingFunction(proc, context: 'foo') result.should == 'foo' end end }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:1> MacRuby <http://macruby.org/>
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: lsansonetti@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: -------------------------------------+-------------------------------------- Comment(by eloy.de.enige@…): And here's the RubyCocoa code I want to be able to use on MacRuby: http://github.com/alloy/kicker/blob/master/vendor/rucola/fsevents.rb#L108 -- Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:2> MacRuby <http://macruby.org/>
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: martinlagardette@… Type: defect | Status: new Priority: blocker | Milestone: Component: MacRuby | Keywords: -------------------------------------+-------------------------------------- Changes (by lsansonetti@…): * owner: lsansonetti@… => martinlagardette@… Comment: Thibault, this could be something for you. The challenge here is to JIT compile at runtime for a given Proc a trampoline function that will call its callback, then passing a pointer to the trampoline to the dispatcher. Also, we must keep a reference to the trampoline inside the internal proc structure so that we can avoid generating it more than once and free the machine code once the proc is finalized by the GC. -- Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:3> MacRuby <http://macruby.org/>
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: martinlagardette@… Type: defect | Status: assigned Priority: blocker | Milestone: Component: MacRuby | Keywords: -------------------------------------+-------------------------------------- Changes (by martinlagardette@…): * status: new => assigned -- Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:4> MacRuby <http://macruby.org/>
#529: Using a Proc as a ‘callback function’ -------------------------------------+-------------------------------------- Reporter: eloy.de.enige@… | Owner: martinlagardette@… Type: defect | Status: closed Priority: blocker | Milestone: MacRuby 0.6 Component: MacRuby | Resolution: fixed Keywords: | -------------------------------------+-------------------------------------- Changes (by martinlagardette@…): * cc: eloy.de.enige@… (added) * status: assigned => closed * resolution: => fixed * milestone: => MacRuby 0.6 Old description:
Some Cocoa methods, or C functions, take pointers to functions which can be used as callbacks. For instance, FSEventStreamCreate: http://bit.ly/8p70Yw.
RubyCocoa, in conjunction with BridgeSupport, supported this by allowing the user to give a proc that would be used as the callback. MacRuby should support this too.
Here's a spec example:
{{{ describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context_pointer| context = context_pointer[0].chr + context_pointer[1].chr + context_pointer[2].chr x <=> y if context == 'foo' end
array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1]
array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}}
Don't know if this is feasible, but it would be great if the arguments given to the proc, like the context argument, were no Pointer objects, but the object they actually point to:
{{{ describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context| x <=> y if context == 'foo' end
array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1]
array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}}
New description: Some Cocoa methods, or C functions, take pointers to functions which can be used as callbacks. For instance, FSEventStreamCreate: http://bit.ly/8p70Yw. RubyCocoa, in conjunction with BridgeSupport, supported this by allowing the user to give a proc that would be used as the callback. MacRuby should support this too. Here's a spec example: {{{ #!ruby describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context_pointer| context = context_pointer[0].chr + context_pointer[1].chr + context_pointer[2].chr x <=> y if context == 'foo' end array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1] array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}} Don't know if this is feasible, but it would be great if the arguments given to the proc, like the context argument, were no Pointer objects, but the object they actually point to: {{{ #!ruby describe "BridgeSupport" do it "bridges a proc to be used where a pointer to a callback function is required" do array = [5, 3, 2, 4, 1] proc = Proc.new do |x, y, context| x <=> y if context == 'foo' end array.sortUsingFunction(proc, context: 'bar') array.should == [5, 3, 2, 4, 1] array.sortUsingFunction(proc, context: 'foo') array.should == [1, 2, 3, 4, 5] end end }}} -- Comment: Implemented with r3847 :-) {{{ #!ruby framework 'Foundation' array = [1, 42, 6, 2, 3] proc = Proc.new { |a, b, _| a <=> b } array.sortedArrayUsingFunction(proc, context: nil) # [1, 2, 3, 6, 42] }}} -- Ticket URL: <http://www.macruby.org/trac/ticket/529#comment:5> MacRuby <http://macruby.org/>
participants (1)
-
MacRuby