Skip to content

Instantly share code, notes, and snippets.

@okrean
Created December 26, 2018 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save okrean/5d6978bac15f257b90f581f371769935 to your computer and use it in GitHub Desktop.
Save okrean/5d6978bac15f257b90f581f371769935 to your computer and use it in GitHub Desktop.
function test(jsArray) {
print(jsArray);
jsArray.forEach(function(a) {
print("- " + a);
});
jsArray.push(3);
}
public static final String jsSource = "src/main/resources/any.js";
public static void main(String[] args) {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("graal.js");
Path file = Paths.get(jsSource);
try (BufferedReader in = Files.newBufferedReader(file)) {
engine.eval(in);
} catch (ScriptException | IOException e) {
System.out.println(e);
return;
}
Invocable inv = (Invocable) engine;
List jsArray = new ArrayList();
jsArray.add(1);
jsArray.add(2);
try {
inv.invokeFunction("test", jsArray);
} catch (NoSuchMethodException | ScriptException e) {
System.out.println(e);
}
}
@okrean
Copy link
Author

okrean commented Dec 26, 2018

it prints:

[1, 2]

  • 1
  • 2
    javax.script.ScriptException: org.graalvm.polyglot.PolyglotException: TypeError: INVOKE on JavaObject[[1, 2] (java.util.ArrayList)] failed due to: Unknown identifier: push

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