J2V8 icon indicating copy to clipboard operation
J2V8 copied to clipboard

method overloading support?

Open racyon opened this issue 9 years ago • 4 comments

Android Object Binding Sample

V8Object v8Obj = new V8Object(v8);

v8.add("tv", v8Obj);

v8Obj.registerJavaMethod(tvResult, "setTextSize", "setTextSize", new Class<?>[]{float.class});
v8Obj.registerJavaMethod(tvResult, "setTextSize", "setTextSize", new Class<?>[]{int.class, float.class});

v8Obj.release();


tv.setTextSize(50);

Caused by: java.lang.IllegalArgumentException: argument type mismatch at com.eclipsesource.v8.V8.checkArgs(V8.java:800) at com.eclipsesource.v8.V8.callVoidJavaMethod(V8.java:783) at com.eclipsesource.v8.V8._executeVoidScript(Native Method) 

Please support the method overloading. Thanks!

racyon avatar Nov 26 '15 08:11 racyon

This isn't exactly method overriding, but auto typecasting. In this case you are trying to call setTextSize with a int, but that signature requires a float. I don't think we can support generic method overriding because there are no type information in JS, but this might be doable.

irbull avatar Nov 27 '15 05:11 irbull

Is there progress on this? Is it still auto typecasting when one version of a method has no arguments and another has? For example:

V8Object v8Obj = new V8Object(v8);
v8.add("object", v8Obj);
v8Obj.registerJavaMethod(source, "method", "method", new Class<?>[]{});
v8Obj.registerJavaMethod(source, "method", "method", new Class<?>[]{String.class});

v8Obj.release();

will throw the argument type mismatch exception, when the script "method()" is executed. Does this mean the first registration gets overwritten by the 2nd?

Edit: I apologize. Method overloading, like the one in my example, is working. Thanks and great work :)

adam-law avatar Feb 18 '18 13:02 adam-law

Edit: I apologize. Method overloading, like the one in my example, is working. Thanks and great work :)

This is not working for me, it happens as you say that the second registration seems to override the first one. When the method is called in javascript, the callVoidJavaMethod (in V8 class) is always called with the same methodId of the last registration regardless of the parameters given in javascript.

JSConsole console = new JSConsole(); V8Object v8Console = new V8Object(runtime); runtime.add("console", v8Console); v8Console.registerJavaMethod(console, "log", "log", new Class<?>[] {}); v8Console.registerJavaMethod(console, "log", "log", new Class<?>[] { Object.class });

JS call console.log("string"); console.log();

I am using the j2v8_win32_x86_64 build version 4.6.0

Dr3ll avatar Sep 02 '19 10:09 Dr3ll

I've found a solution to this problem that worked for me.

My solution was to use a function with variadic arguments. For example:

    public void myFunction(Object... arguments) {
        //do stuff
    }

register method:

        Method[] methods = this.getClass().getMethods();
        for (Method method : methods) {
            v8Obj.registerJavaMethod(this, method.getName(), method.getName(), method.getParameterTypes());
        }

and in javascript you can use:

    object.myFuction();
    object.myFuction(1);
    object.myFuction(1, "YES");

I hope it works for you too.

ezefire avatar Feb 09 '22 10:02 ezefire