Duby dynamic dispatch has landed!
Charles Oliver Nutter
headius at headius.com
Wed Mar 24 19:17:59 PDT 2010
I love Duby sometimes, I really do.
I've just landed full dynamic dispatch support. Here's an example
script and session:
~/projects/duby ➔ cat examples/dynamic.duby
# Example of using a dynamic type in a method definition
def foo(a:dynamic)
puts "I got a #{a.getClass.getName} of size #{a.size}"
end
class SizeThing
def initialize(size:int)
@size = size
end
def size
@size
end
end
foo([1,2,3])
foo(SizeThing.new(12))
~/projects/duby ➔ bin/duby examples/dynamic.duby
I got a java.util.Collections$UnmodifiableRandomAccessList of size 3
I got a examples.SizeThing of size 12
I like this example because it's two totally disjoint types that
happen to both have a "size" method, and Duby + indy + dynalang
handles them just fine.
So far I'm doing it like C#, specifying a "dynamic" type that
propagates as-is through the type inference phase(s), but for bytecode
and signature generation it gets erased to java.lang.Object.
Here's log output of the number of lines added/removed for the entire
"dynamic" type feature in Duby's compiler chain:
http://gist.github.com/343072
Maybe a couple hundred lines of code? Of course I'm cheating by using
dynalang and indy, but hey, that's the right way to build software,
isn't it?
I'm pretty stoked how well this is working.
For Attila: I had to remove a spreadArguments handle you used for
re-binding the method...not sure why. Here's the diff:
Index: src/org/dynalang/dynalink/support/DynamicLinkerImpl.java
===================================================================
--- src/org/dynalang/dynalink/support/DynamicLinkerImpl.java (revision 232)
+++ src/org/dynalang/dynalink/support/DynamicLinkerImpl.java (working copy)
@@ -115,8 +115,6 @@
final MethodType genericType = invocation.type().generic();
final MethodHandle genericizedInvocation =
MethodHandles.convertArguments(invocation, genericType);
- final MethodHandle spreadInvocation = MethodHandles.spreadArguments(
- genericizedInvocation, SPREAD_GENERIC_INVOCATION);
- return MethodHandles.invoke(spreadInvocation, arguments);
+ return MethodHandles.invoke(genericizedInvocation, arguments);
}
}
I committed a built version with this hack...no tests, etc yet for
Duby's "dynamic" type, but that will come soon.
BTW, what's the current state of the art for emitting .java with an
invokedynamic in it? Duby also has a .java backend, so I'll need to
add indy support there as well (somehow).
- Charlie
More information about the mlvm-dev
mailing list