Search code examples
eclipse-plugingraalvmgraaljs

Add Custom Logger for GraalVM JS Script Engine through Java ScriptEngineManager


There is an eclipse plugin (Name: A), which internally calls JS script Engine to evaluate some conditions. The consumer of the plugin has no knowledge of the JS Scripting implementation code and is free to choose their custom custom JS scripting implementation.

We import the 'A' plugin in our plugin (B) and We are using GraalVM JS scripting engine. Seems like GraalVM uses TruffleLogger for logging and writes to the console

we would like to write to the Eclipse error log. Unfortunately, I'm unable to find any way.

any pointer how to achieve the same?

Any custom logger that can be attached without the knowledge of the code used to initialize the ScriptEngine.

Any programmatic way to set the TruffleLogger?

ScriptEngineManager engineManager = new ScriptEngineManager();
this.scriptEngine = engineManager.getEngineByName("JS");

Thanks, Pal


Solution

  • code that answer your requirement:

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("js");
    Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    bindings.put("polyglot.js.allowHostAccess", true);
    bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
    bindings.put("truffleLogger", new TruffleLogger());
    engine.eval("(truffleLogger instanceof Java.type('java.lang.TruffleLogger'));");