Method/Block Invocation Proposal

0 views
Skip to first unread message

dreamhead

unread,
Apr 13, 2007, 9:37:25 PM4/13/07
to xruby...@googlegroups.com
The following is the code snippet of Integer#times, where receiver is Fixnum:
int value = ((RubyFixnum) receiver).intValue();
for (int i = 0; i < value; ++i) {
RubyValue v = block.invoke(receiver, new
RubyArray(ObjectFactory.createFixnum(i)));
if (block.breakedOrReturned()) {
return v;
}
}

To invoke the block(yield) , we must create a new RubyArray just for
the interface. In fact, only one arg is required. So we must get the
arg in method/block like this.
arg = args.get(0);

I don't think the conversion is required. I hope we can call
method/block like this.
block.invoke(receiver, arg);

With this approach, the interface is cleaner. If we do more work, the
conversion may be omitted so that we can improve the performance.

The following is my proposal about method/block invocation.

As you know, we have add some facility class like RubyNoArgMethod,
RubyOneArgMethod etc. These facility class is just for method/block
writer now. I think they can help method/block caller.

We can provide more methods in RubyMethod/RubyBlock for invoke. So the
caller interface can be simplified.
// For multi-arg
RubyValue invoke(RubyValue receiver, RubyArray args, RubyBlock block) ;

// For no-arg
RubyValue invoke(RubyValue receiver, RubyBlock block);

// For one-arg
RubyValue invoke(RubyValue receiver, RubyValue arg, RubyBlock block);

// For two-arg
RubyValue invoke(RubyValue receiver, RubyValue arg1, RubyValue arg2,
RubyBlock block);
...

On the other hand, RubyMethod should provide some corresponding run
method, such as,
// For one-arg
RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block);

Default implementation will call the multi-arg run like this.
RubyValue run(RubyValue receiver, RubyValue arg, RubyBlock block) {
run(receiver, new RubyArray(arg), block);
}

Only multi-arg run method is required for subclass. RubyOneArgMethod
instance overrides one-arg run. If caller invoke this method with
one-arg invoke, there's no conversion.

In current implementation, we don't need to modify any other method
code to support this idea. But we need to rewrite code generation to
generate code with this idea.

This is a big modification, so I'd better discuss with others. Any
question or a better approach is welcome!

--
Everything is simple!

Xue Yong Zhi

unread,
Apr 14, 2007, 11:20:34 PM4/14/07
to xruby...@googlegroups.com
Yes, I have planned to do this, just can not find any time.

Actually it is very expensive to create a new array for one arg then throw
it away. Most benchmark tests will run much faster after this change.

>As you know, we have add some facility class like RubyNoArgMethod,
>RubyOneArgMethod etc. These facility class is just for method/block
>writer now. I think they can help method/block caller.
>

_________________________________________________________________
Exercise your brain! Try Flexicon.
http://games.msn.com/en/flexicon/default.htm?icid=flexicon_hmemailtaglineapril07

femto

unread,
Apr 18, 2007, 5:00:56 AM4/18/07
to xruby-devel
and for RubyAPI, callPublicOneArgMethod, callOneArgMethod etc.

Reply all
Reply to author
Forward
0 new messages