From: Arno P. <ar...@pu...> - 2012-08-07 17:00:07
|
this is an interesting problem. The Java language does not permit overloading of methods whose signature only differ in the return type. However, due to type erasing it can happen that on byte-code level you end up having two methods that only differ in their return type and the JVM can handle this. There has been a debate whether this should be permissible or not and the semantics in Java 7 have changed: In Java 7, the arguments need to be different *after* type erasure. Here is a nice writeup that explains this better than I could: http://notatube.blogspot.com/2010/07/interesting-change-to-method-signature.html So, wrt Java 7, XMLVM is doing the right thing, which of course doesn't solve the Scala issue. The only way to solve this is to name-mangle the return type into the generated C function names. That is a little more than a weekend project. Arno On 8/6/12 11:03 PM, Kensuke Matsuzaki wrote: > Hi, > > I tried to convert scala program, but segmentation fault occurred. > Does xmlvm2c support method overloading by return type? > > A .class file has two method. > * public int call(); > * public java.lang.Object call(); > > Generated callee .c source file has only "int call()", > but caller uses "java.lang.Object call()". > > * test code * > package test { > import java.util.concurrent.Callable; > > class Hoge extends Callable[Int] { > def call() = 1 > } > > object Test { > def callAndPrint(c: Callable[_]) { > System.out.println(c.call()); > } > def main(args: Array[String]) { > callAndPrint(new Hoge); > } > } > } > > * result of javap -c * > Compiled from "Test.scala" > public class test.Hoge extends java.lang.Object implements java.util.concurrent.Callable,scala.ScalaObject{ > public int call(); > Code: > 0: iconst_1 > 1: ireturn > > public java.lang.Object call(); > Code: > 0: aload_0 > 1: invokevirtual #11; //Method call:()I > 4: invokestatic #17; //Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer; > 7: areturn > > public test.Hoge(); > Code: > 0: aload_0 > 1: invokespecial #23; //Method java/lang/Object."<init>":()V > 4: return > > } > > Compiled from "Test.scala" > public final class test.Test$ extends java.lang.Object implements scala.ScalaObject{ > public static final test.Test$ MODULE$; > > public static {}; > Code: > 0: new #9; //class test/Test$ > 3: invokespecial #12; //Method "<init>":()V > 6: return > > public void callAndPrint(java.util.concurrent.Callable); > Code: > 0: getstatic #20; //Field java/lang/System.out:Ljava/io/PrintStream; > 3: aload_1 > 4: invokeinterface #27, 1; //InterfaceMethod java/util/concurrent/Callable.call:()Ljava/lang/Object; > 9: invokevirtual #33; //Method java/io/PrintStream.println:(Ljava/lang/Object;)V > 12: return > > public void main(java.lang.String[]); > Code: > 0: aload_0 > 1: new #43; //class test/Hoge > 4: dup > 5: invokespecial #44; //Method test/Hoge."<init>":()V > 8: invokevirtual #46; //Method callAndPrint:(Ljava/util/concurrent/Callable;)V > 11: return > > } > |