How to explicitly select a Java method overload? · Issue #37 · oracle/graaljs · GitHub
Skip to content

How to explicitly select a Java method overload? #37

Closed

Description

@jonathanperret

When calling a Java method from JS, if multiple overloads are applicable, the runtime correctly complains that it can't choose (please forgive the nonsensical example):

$ js --jvm
> Graal
{language: "JavaScript", versionJS: "1.0", versionGraalVM: "1.0.0-rc5", isGraalRuntime: true}
> java.lang.Runtime.getRuntime().exec(null)
TypeError: INVOKE on JavaObject[java.lang.Runtime@799f10e1 (java.lang.Runtime)] failed due to: java.lang.IllegalArgumentException: Multiple applicable overloads found for method name exec (candidates: [Method[public java.lang.Process java.lang.Runtime.exec(java.lang.String[]) throws java.io.IOException], Method[public java.lang.Process java.lang.Runtime.exec(java.lang.String) throws java.io.IOException]], arguments: [DynamicObject<null>@18078bef (DynamicObjectBasic)])
	at <js> :program(<shell>:3:1:0-35)

This is in line with Nashorn:

$ jjs -version
nashorn 1.8.0_121
jjs> java.lang.Runtime.getRuntime().exec(null)
java.lang.RuntimeException: java.lang.NoSuchMethodException: Can't unambiguously select between fixed arity signatures [(java.lang.String), (java.lang.String[])] of the method java.lang.Runtime.exec for argument types [null]

However Nashorn lets us pick an overload to resolve the ambiguity:

jjs> java.lang.Runtime.getRuntime()["exec(java.lang.String)"](null)
java.lang.NullPointerException

Trying the same syntax in GraalJS doesn't work:

> java.lang.Runtime.getRuntime()["exec(java.lang.String)"](null)
TypeError: (intermediate value).lang.Runtime.getRuntime(...)["exec(java.lang.String)"] is not a function
	at <js> :program(<shell>:8:1:56-61)

The lookupOverloadByParamTypeString method:
https://github.com/graalvm/graaljs/blob/69bc58643bf77ed6eaaaffca3e79beb4b473a664/graal-js/src/com.oracle.truffle.js.runtime/src/com/oracle/truffle/js/runtime/interop/JavaMethod.java#L504

looks promising but I have no idea how to invoke it. Ideally, the Nashorn syntax would be supported…

Activity

  1. added
    interopPolyglot Language Interoperability
    on Mar 8, 2019
  2. woess commented on Dec 7, 2020

    @woess
    Member

    java.lang.Runtime.getRuntime()["exec(java.lang.String)"] is supported since 21.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestinteropPolyglot Language Interoperability

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions