[graalvm-users] TypeError

Andreas Mueller am at iit.de
Wed Mar 25 05:33:47 PDT 2020


Hi,

I got it working without any type mapping. Although I had to change a bunch of scripts.

IMO a problem occurs when you have multiple methods with the same name but different type of parameters. Such as the Long below or Integer.parseInt(..). For example, I have a custom class that has these methods:

myclass.remove(int)
myclass.remove(string)

The caller decides which method is called by either using a string or an int type. This doesn’t seem to work with GraalVM. Once you use type mapping from String to Integer it throws an exception. Expected behavior would be to use the String method when the parameter is a String and use type mapping only if a method with that type isn’t found. 

Thanks,
Andreas

-- 
Andreas Mueller
IIT Software GmbH
https://urldefense.com/v3/__http://www.swiftmq.com__;!!GqivPVa7Brio!PNs0Qd8pB5eyd85XGiLxrtm2zqzuoUFK-KVCcXfD9W1qD52wYLE3aprmEN58TPZiePsw9A$  <https://urldefense.com/v3/__http://www.swiftmq.com__;!!GqivPVa7Brio!PNs0Qd8pB5eyd85XGiLxrtm2zqzuoUFK-KVCcXfD9W1qD52wYLE3aprmEN58TPZiePsw9A$ > 



On 24. Mar 2020, at 17:57, Christian Wirth <christian.wirth at oracle.com <mailto:christian.wirth at oracle.com> > wrote:

Hi Andreas,

>From the error message, I assume you are calling  `var Long = Java.type('java.lang.Long'); new Long('1585059851090')`.

In that case, as you use targetTypeMapping String=>Long, it seems we fail to give precedence to the base (unmapped) type, String in this case. We identify that it is ambigous whether to call the `Long(String)` or the `Long(long)` (using targetTypeMapping) constructor, and thus you get the error. 

I would think this is an error on our side. Commenting out the String->Long targetTypeMapping resolved the problem (and calls the `Long(String)` constructor). But I'll have to check with our expert on that matter.

Best,
Christian Wirth





Am 24.03.2020 um 15:45 schrieb Andreas Mueller:
Hi,

I’m testing existing Nashorn JS scripts running on GraalVM 20.0.0. I’m getting this exception while instantiating a java.lang.Long object from JS code:

TypeError: instantiate on JavaClass[java.lang.Long] failed due to: Multiple applicable overloads found for method name java.lang.Long (candidates: [Method[public java.lang.Long(java.lang.String) throws java.lang.NumberFormatException], Method[public java.lang.Long(long)]], arguments: [1585059851090 (String)])

The argument I pass is a String and there is a Long constructor for this so I don’t understand why this is thrown.

My ScriptEngine with a mapping String -> long is created as follows:

public class GraalSetup {
    private static HostAccess getHostAccess() {
        HostAccess.Builder builder = HostAccess.newBuilder(HostAccess.ALL);
        builder.targetTypeMapping(String.class, Integer.class, null, (v) -> Integer.parseInt(v));
        builder.targetTypeMapping(String.class, Long.class, null, (v) -> Long.parseLong(v));
        builder.targetTypeMapping(String.class, Double.class, null, (v) -> Double.parseDouble(v));
        builder.targetTypeMapping(String.class, Float.class, null, (v) -> Float.parseFloat(v));
        builder.targetTypeMapping(Integer.class, Long.class, null, (v) -> v.longValue());
        return builder.build();
    }

    public static ScriptEngine engine() throws Exception {
        return GraalJSScriptEngine.create(null,Context.newBuilder("js").allowAllAccess(true).allowHostAccess(getHostAccess()));
    }
}

Any hints?

Thanks,
Andreas
-- 
Andreas Mueller
IIT Software GmbH
https://urldefense.com/v3/__http://www.swiftmq.com__;!!GqivPVa7Brio!PNs0Qd8pB5eyd85XGiLxrtm2zqzuoUFK-KVCcXfD9W1qD52wYLE3aprmEN58TPZiePsw9A$  <https://urldefense.com/v3/__http://www.swiftmq.com__;!!GqivPVa7Brio!PNs0Qd8pB5eyd85XGiLxrtm2zqzuoUFK-KVCcXfD9W1qD52wYLE3aprmEN58TPZiePsw9A$ > 

<swiftmq_logo_positiv.png>



_______________________________________________
GraalVM-Users mailing list

GraalVM-Users at oss.oracle.com
https://oss.oracle.com/mailman/listinfo/graalvm-users
_______________________________________________
GraalVM-Users mailing list
GraalVM-Users at oss.oracle.com <mailto:GraalVM-Users at oss.oracle.com> 
https://oss.oracle.com/mailman/listinfo/graalvm-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://oss.oracle.com/pipermail/graalvm-users/attachments/20200325/ebbaf878/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: swiftmq_logo_positiv.png
Type: image/png
Size: 10241 bytes
Desc: not available
Url : http://oss.oracle.com/pipermail/graalvm-users/attachments/20200325/ebbaf878/attachment-0001.png 


More information about the GraalVM-Users mailing list