Javet
Javet copied to clipboard
Built-in JavetProxyConverter: Better error reporting
Hey so when I first started using the very first versions of Javet back in version 0.x.x I needed to proxy a java API so it could be called from javascript. Back then, Javet did not have this feature, so I created my own way to do it, which was very time consuming and error prone. Nowadays I was reading the Javet documentation I noticed that Javet now supports this with JavetProxyConverter which is great! I noticed it even supports function overloading even though javascript doesn't, by making all functions in javascript take varArgs and then resolving them at runtime to the appropriate java method.
However it doesn't report any errors or warnings and doesn't even throw any exceptions when calling a method with missmatched parameter count or wrong parameter types. I understand this can't be detected on javascript side because all proxied methods take varArgs, but it can be detected on the java side. And in my opinion it should, otherwise this hides many errors.
An example:
public class TestObj {
public void test(){
System.out.println("Called test");
}
public void test(int v){
System.out.println("Called test2 ");
}
}
try (NodeRuntime v8Runtime = V8Host.getNodeInstance().createV8Runtime()) {
File f = new File("main.js");
JavetProxyConverter c = new JavetProxyConverter();
v8Runtime.setConverter(c);
TestObj t = new TestObj();
v8Runtime.getGlobalObject().set("testObj", t);
v8Runtime.getExecutor(f).execute();
}
main.js:
testObj.test(1,2,3); //silently fails with no error or warning and continues execution
testObj.test();
testObj.test(2);
console.log("After test");
This prints: "Called test" "Called test2" "After test"
In my opinion it should stop execution on the first call to testObj.test(1,2,3) because no such method exists and it should report an error message about wrong parameter count and the same should be done if the parameter types are the wrong type. Like I said I understand this is not possible to do on Javascript script side, because all proxied functions are varArgs but on the Java side in ScoredExecutable.java, it seems like it's already doing all those checks, but when the totalScore is 0 it doesn't report why its 0. I think all cases where totalScore = 0, it should report the corresponding error and stop execution. It would be very helpful to have the line number aswell where that error happened too.
I understand we can already create our own JavetProxyConverter but my suggestion is about improving the current built-in proxy converter. What do you think?