Skip to content

GraalJS with non-GraalVM fails to read/parse script in BufferedReader #202

@rschmunk

Description

@rschmunk

I"m exploring what it will take to migrate from Nashorn to GraalJS for whenever it is that Nashorn is removed from Java. I'm trying to use the GraalJS jars in conjunction with a non-Graal VM, and have followed instructions in the docs at https://github.com/graalvm/graaljs/blob/master/docs/user/RunOnJDK.md and https://github.com/graalvm/graaljs/blob/master/docs/user/ScriptEngine.md

(There are, BTW, a couple errors on the RunOnJDK.md page regarding necessary jars that I had to figure out.)

My Project allows for the user to either manually enter Javascript commands line by line, or to provide the path to a script file to read in, parse and execute. The former seems to work just fine. However, the latter fails.

Omitting a bunch of try-catch code, I create the engine, feed it a BufferedReader with the script like the following.

   ScriptEngine engine = new ScriptEngineManager ( ).getEngineByName ("javascript");
   Bindings bindings = engine.createBindings ( );
   bindings.put ("polyglot.js.allowAllAccess", true);
   engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);

   File sfile = new File (spath);   // spath is the path to the script file
   FileInputStream fstream  = new FileInputStream (sfile);    
   InputStreamReader isr = new InputStreamReader (fstream, "UTF-8");
   BufferedReader breader = new BufferedReader (isr);

   engine.eval ( breader );

That last line comes back with an exception thrown by GraalJSScriptEngine that the stream was closed.

javax.script.ScriptException: java.io.IOException: Stream closed
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.createSource(GraalJSScriptEngine.java:317)
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:310)
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:249)
	at myapp.CLParser.evaluateScript(CLParser.java:682)
	at myapp.CLParser.evaluateScript(CLParser.java:616)
	at myapp.CLParser.<init>(CLParser.java:174)
	at myapp.CLParser.main(CLParser.java:113)
Caused by: java.io.IOException: Stream closed
	at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder.java:46)
	at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:148)
	at java.io.InputStreamReader.read(InputStreamReader.java:184)
	at java.io.BufferedReader.fill(BufferedReader.java:161)
	at java.io.BufferedReader.read1(BufferedReader.java:212)
	at java.io.BufferedReader.read(BufferedReader.java:286)
	at java.io.Reader.read(Reader.java:140)
	at com.oracle.truffle.api.source.Source.read(Source.java:1209)
	at com.oracle.truffle.api.source.Source.buildSource(Source.java:1134)
	at com.oracle.truffle.api.source.Source$SourceBuilder.build(Source.java:1667)
	at com.oracle.truffle.api.source.Source$LiteralBuilder.build(Source.java:1801)
	at com.oracle.truffle.polyglot.PolyglotSource.build(PolyglotSource.java:296)
	at org.graalvm.polyglot.Source$Builder.build(Source.java:920)
	at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.createSource(GraalJSScriptEngine.java:315)
	... 6 more

Any ideas on whether this is something I can fix in my own code? Am I omitting something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions