graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

com.oracle.truffle.polyglot.PolyglotMap is Not found

Open 0xddy opened this issue 3 years ago • 4 comments

image

import package class error not found PolyglotMap ,The type cannot be converted,How can I solve this problem? Can you help me ,thank you!!

  • JDK version: graalvm-ce-java11_22.0.02

build.gradle

dependencies {
    implementation 'org.graalvm.truffle:truffle-api:22.0.0.2'
    implementation 'org.graalvm.js:js:22.0.0.2'
    implementation 'org.graalvm.js:js-scriptengine:22.0.0.2'
}

Main.kt

fun main(args: Array<String>) {

    val context = Context.newBuilder()
        .allowExperimentalOptions(true)
        .allowPolyglotAccess(PolyglotAccess.ALL)
        .allowHostAccess(HostAccess.ALL)
        .allowAllAccess(true)
        .allowHostClassLoading(true)
        .allowHostClassLookup {
            true
        }

    GraalJSScriptEngine.create(null, context)?.apply {
        val bindings = getBindings(ScriptContext.ENGINE_SCOPE)
        bindings["VERSION"] = "1.0"
        bindings["File"] = File::class.java
        val fn = eval(
            """
                function run(){
                    let name = "KKK"
                    return { name:name }
                }
            """
        )
        val ret = invokeFunction("run")

        // com.oracle.truffle.polyglot.PolyglotMap
        // import com.oracle.truffle.polyglot.PolyglotMap
        // Idea red error  not found
        println(ret.javaClass)

    }```



0xddy avatar Mar 13 '22 17:03 0xddy

PolyglotMap is an internal implementation detail. It is not even a public class. You are not supposed to use it directly.

iamstolis avatar Mar 14 '22 07:03 iamstolis

PolyglotMap是内部实现细节。它甚至不是公共课。你不应该直接使用它。

The returned result type of invokefunction is polyglotmap

How to correctly return results for JS Object { name:name } ,Can I only use Java Map or other Java Object to return results in JS ?

like this

bindings["HashMap"] = HashMap::class.java
eval(
            """
                function run(){
                    let name = "KKK"
                    let map = new HashMap<String,String>()
                    map.put("name",name)
                    return map
                }
            """
        )

0xddy avatar Mar 14 '22 07:03 0xddy

The returned result type of invokefunction is polyglotmap

Yes, that's correct but there is no reason to cast the result to PolyglotMap, you can cast to java.util.Map. For example,

((java.util.Map) invokeFunction("run")).get("name")

iamstolis avatar Mar 14 '22 08:03 iamstolis

The returned result type of invokefunction is polyglotmap

Yes, that's correct but there is no reason to cast the result to PolyglotMap, you can cast to java.util.Map. For example,

((java.util.Map) invokeFunction("run")).get("name")

@iamstolis Oh, thank you very much

0xddy avatar Mar 14 '22 08:03 0xddy