[x3d-public] Calling JavaScript from Java to return an X3D object for manipulation
John Carlson
yottzumm at gmail.com
Wed Mar 27 16:14:53 PDT 2024
Here's the Gradle dependencies I used (I install X3DJSAIL in my
~/.m2/repository) that actually make the code work:
implementation 'org.graalvm.sdk:collections:24.0.0'
//implementation 'org.graalvm.js:js:23.0.3'
implementation 'org.graalvm.polyglot:js-community:24.0.0'
implementation 'org.graalvm.js:js-launcher:24.0.0'
implementation 'org.graalvm.js:js-scriptengine:24.0.0'
implementation 'org.graalvm.sdk:graal-sdk:24.0.0'
implementation 'org.graalvm:launcher-common:1.0.0-rc7'
implementation 'org.graalvm.sdk:nativeimage:24.0.0'
implementation 'org.graalvm.polyglot:polyglot:24.0.0'
implementation 'org.graalvm.regex:regex:24.0.0'
implementation 'org.graalvm.truffle:truffle-api:24.0.0'
implementation 'org.graalvm.truffle:truffle-compiler:24.0.0'
implementation 'org.graalvm.shadowed:icu4j:24.0.0'
implementation 'org.graalvm.truffle:truffle-runtime:24.0.0'
implementation 'org.web3d.x3d:X3DJSAIL:4.0-SNAPSHOT'
So with this I will now test from NetBeans. Works, except perhaps
JavaScript compilation is not supported.
Here's my project:
coderextreme/jaminate: High level animation tool for generating chained
event ROUTEs and nodes in VRML. (github.com)
<https://github.com/coderextreme/Jaminate>
On Wed, Mar 27, 2024 at 5:37 PM John Carlson <yottzumm at gmail.com> wrote:
> Here's some steps for calling JavaScript from Java which calls JavaScript
>
> Use these imports:
>
> import org.graalvm.polyglot.Context;
> import org.graalvm.polyglot.HostAccess;
> import org.graalvm.polyglot.Engine;
> import org.web3d.x3d.jsail.Core.X3D;
>
> Here is example Java to load and execute JavaScript. It is based on
> DISWebGateway code, but VERY different!
>
> public X3D JavaScriptExec(String javaScriptString)
> {
> X3D X3D0 = null;
> try
> {
> Engine engine = Engine.newBuilder("js").build();
> Context context = Context.newBuilder("js")
> .engine(engine)
> .allowHostAccess(HostAccess.ALL)
> .allowHostClassLookup(className -> true)
> .arguments("js", new String[]{"--jvm",
> "--vm.classpath=lib/X3DJSAIL.4.0.full.jar;lib/saxon-he-12.1.jar"})
> .option("js.ecmascript-version", "2022").build();
> context.eval("js", javaScriptString).asHostObject();
> X3D0 = context.eval("js", "X3D0").asHostObject();
> context.close();
> engine.close();
> } catch(Exception e) {
> e.printStackTrace(System.err);
> }
> return X3D0;
> }
> private void loadJsFile(GenericTableModel model, File selectedFile) {
> this.model = model;
> System.err.println("Opening file "+selectedFile);
> try (FileInputStream fis = new FileInputStream(selectedFile)){
> try (BufferedReader br = new BufferedReader(new
> InputStreamReader(fis, "UTF-8"))) {
> StringBuilder sb = new StringBuilder();
> String line;
> while ((line = br.readLine()) != null) {
> sb.append(line);
> sb.append("\n");
> }
> String jsCode = sb.toString();
> X3D X3D0 = this.JavaScriptExec(jsCode);
> System.err.println("Version: "+X3D0.getVersion());
> System.err.println("Profile: "+X3D0.getProfile());
>
> } catch (IOException e) {
> e.printStackTrace(System.err);
> }
> System.err.println("Number of rows in reader
> "+model.getModel().getRowCount());
> } catch (IOException e) {
> e.printStackTrace(System.err);
> }
> }
>
> ============================================================
> I compile with Gradle, but don't run inside Gradle.
>
> Here's some Gradle config:
>
> // https://mvnrepository.com/artifact/org.graalvm.sdk/graal-sdk
> implementation 'org.graalvm.sdk:graal-sdk:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm.js/js-launcher
> implementation 'org.graalvm.js:js-launcher:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm.js/js-scriptengine
> implementation 'org.graalvm.js:js-scriptengine:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm.js/js
> implementation 'org.graalvm.js:js:23.0.3'
> // implementation 'org.graalvm.js:js:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm.truffle/truffle-api
> implementation 'org.graalvm.truffle:truffle-api:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm.shadowed/icu4j
> implementation 'org.graalvm.shadowed:icu4j:24.0.0'
> // https://mvnrepository.com/artifact/org.graalvm/launcher-common
> implementation 'org.graalvm:launcher-common:1.0.0-rc7'
>
> //
> https://mvnrepository.com/artifact/org.graalvm.regex/regex
>
> implementation
> 'org.graalvm.regex:regex:24.0.0'
>
> implementation
> 'org.web3d.x3d:X3DJSAIL:4.0-SNAPSHOT'
>
>
>
>
>
>
>
> //implementation 'org.graalvm.tools:profiler:24.0.0'
>
>
> //implementation 'org.graalvm.tools:chromeinspector:24.0.0'
>
> // needs newer
>
>
> //implementation
> 'org.graalvm.compiler:compiler:24.0.0'
>
> //
> https://mvnrepository.com/artifact/org.graalvm.truffle/truffle-compiler
>
> implementation
> 'org.graalvm.truffle:truffle-compiler:24.0.0'
>
>
>
>
>
>
>
> implementation 'org.graalvm.polyglot:polyglot:24.0.0'
>
>
> implementation 'org.graalvm.sdk:collections:24.0.0'
>
> // implementation
> 'org.graalvm.sdk:nativeimage:24.0.0'
>
>
>
>
> // implementation 'org.graalvm.polyglot:js:24.0.0'
>
>
> // Select tools: profiler, inspect, coverage, dap, tools
>
> implementation
> 'org.graalvm.polyglot:tools:24.0.0'
>
>
>
>
> //
> https://mvnrepository.com/artifact/org.graalvm.polyglot/js-community
>
> implementation
> 'org.graalvm.polyglot:js-community:24.0.0'
> ============================================================
> Here's how I run my program:
>
> /c/graalvm-jdk-22_windows-x64_bin/graalvm-jdk-22+36.1/bin/java.exe -cp
> "build/classes/java/main;C:/Users/john/pythonSAI/X3DJSAIL.4.0.full.jar;build/install/app/lib/tagsoup-1.2.1.jar;lib/graal/collections-24.0.0.jar;lib/graal/graaljs.jar;lib/graal/graaljs-launcher.jar;lib/graal/graaljs-scriptengine.jar;lib/graal/graal-sdk.jar;lib/graal/launcher-common.jar;lib/graal/nativeimage-24.0.0.jar;lib/graal/polyglot-24.0.0.jar;lib/graal/tregex.jar;lib/graal/truffle-api.jar;lib/graal/truffle-compiler-24.0.0.jar;lib/graal/truffle-icu4j.jar;lib/graal/truffle-runtime-24.0.0.jar;."
> net.coderextreme.jaminate.TableLoadSave
>
> Here's my lib folder:
>
> lib/graal/collections-24.0.0.jar
> lib/graal/graal-sdk.jar
> lib/graal/graaljs-launcher.jar
> lib/graal/graaljs-scriptengine.jar
> lib/graal/graaljs.jar
> lib/graal/launcher-common.jar
> lib/graal/nativeimage-24.0.0.jar
> lib/graal/polyglot-24.0.0.jar
> lib/graal/tregex.jar
> lib/graal/truffle-api.jar
> lib/graal/truffle-compiler-24.0.0.jar
> lib/graal/truffle-icu4j.jar
> lib/graal/truffle-runtime-24.0.0.jar
> lib/saxon-he-12.1.jar
> lib/X3DJSAIL.4.0.full.jar
>
> ===========================================================
> Once I get this working in Gradle, I will repost.
>
> Note that I've not been able to load a JavaScript file from JavaScript in
> this scenario, so I've incorporated the X3DJSAIL-JavaScript interface
> directly in JavaScript code.
>
> John
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20240327/a736d790/attachment-0001.html>
More information about the x3d-public
mailing list