Run Javascript Nodes With GraalJS

41 views
Skip to first unread message

Thomas Passin

unread,
Mar 22, 2020, 2:46:42 PM3/22/20
to leo-editor
 A few days ago, Edward (@ekr) showed how he runs Rust programs from Leo:


It's possible to run Javascript programs from a Leo node, too.  First, get GraalJS, which is available for all the major OSs.  See


Once it's on your machine, find its languages directory.  In there, you want the languages/js/bin/js executable.  On my Windows computer it's at

D:\usr\graalvm-ce-java11-20.0.0\languages\js\bin\js.exe

The following Python code will submit to GraalJS the code in the node selected in the outline, and display anything that was printed to STDOUT.  Of course, adjust the path to the js executable.

from subprocess import run

graal = r"D:\usr\graalvm-ce-java11-20.0.0\languages\js\bin\js.exe"
raw = p.b
progfile = 'arg1.txt'

# Remove all lines starting with "@language"
lines = raw.split('\n')
prog = '\n'.join([line for line in lines if not
                line.startswith('@language')])

with open(progfile, 'w') as f:
    f.write(prog)

cmd = f'{graal} {progfile}'
result = run(cmd, capture_output=True, text=True, encoding='utf-8')

g.es(result.stdout or 'no output')


I have this in a @command node, hot keyed to F6.  Since GraalJS is built on a java foundation, you might think it would be slow to load.  But not so - I'm finding it just about instantaneous to run a tiny program.

Note that this example doesn't capture STDERR, but that's easy enough to arrange.

It should be straightforward to adapt for Viewrendered3.




Thomas Passin

unread,
Mar 22, 2020, 3:00:41 PM3/22/20
to leo-e...@googlegroups.com
GraalJS can use classes in jar files, and it even supports nodejs, but I haven't learned how yet. That is, the GRAAL system itself, which includes its own JIT compiler and other things, when used for running java code, can use jar files specified on its CLASSPATH.  I don't know if any of them are needed for javascript interpretation or not.

Edward K. Ream

unread,
Mar 23, 2020, 3:04:20 PM3/23/20
to leo-editor
On Sun, Mar 22, 2020 at 1:46 PM Thomas Passin <tbp1...@gmail.com> wrote:

> A few days ago, Edward (@ekr) showed how he runs Rust programs from Leo: [snip]

> It's possible to run Javascript programs from a Leo node, too [with] GraalJS.

Excellent. As I've implied in recent posts, libs like Graal and Bokeh are the way to create bridges between Leo and other worlds. Many thanks for telling us about Graal. I'll look forward to seeing how VR3 supports Graal.

Edward
Reply all
Reply to author
Forward
0 new messages