Mercurial > hg > jdk9-shenandoah > nashorn
changeset 1340:d95394322204
8130127: streamline input parameter of Nashorn scripting $EXEC function
Summary: handle null and undefined correctly, do not coerce them to strings
Reviewed-by: lagergren, sundar
author | mhaupt |
---|---|
date | Wed, 01 Jul 2015 16:26:25 +0200 |
parents | 0b60cae91ec6 |
children | 1172aca37bba |
files | samples/exec.js src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java test/script/nosecurity/JDK-8130127.js test/script/nosecurity/JDK-8130127.js.EXPECTED test/script/nosecurity/readprint.js |
diffstat | 5 files changed, 105 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- a/samples/exec.js Tue Jun 30 08:51:06 2015 -0700 +++ b/samples/exec.js Wed Jul 01 16:26:25 2015 +0200 @@ -39,9 +39,10 @@ $EXEC("cat", "Hello, world!") // Additional arguments can be passed after the stdin argument, as an array of -// strings, or a sequence of varargs: -$EXEC("ls", "" /* no stdin */, "-l", "-a") -$EXEC("ls", "" /* no stdin */, ["-l", "-a"]) +// strings, or a sequence of varargs (if there is no stdin, null or undefined +// can be passed): +$EXEC("ls", undefined, "-l", "-a") +$EXEC("ls", undefined, ["-l", "-a"]) // Output of running external commands is returned from $EXEC: print($EXEC("ls"))
--- a/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java Tue Jun 30 08:51:06 2015 -0700 +++ b/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java Wed Jul 01 16:26:25 2015 +0200 @@ -128,9 +128,9 @@ * Nashorn extension: exec a string in a separate process. * * @param self self reference - * @param args string to execute, input and additional arguments, to be appended to {@code string}. Additional arguments can be passed as - * either one JavaScript array, whose elements will be converted to strings; or as a sequence of - * varargs, each of which will be converted to a string. + * @param args string to execute, input and additional arguments, to be appended to {@code string}. Additional + * arguments can be passed as either one JavaScript array, whose elements will be converted to + * strings; or as a sequence of varargs, each of which will be converted to a string. * * @return output string from the request * @@ -215,13 +215,13 @@ errThread.start(); // If input is present, pass on to process. - try (OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream())) { - if (input != UNDEFINED) { + if (!JSType.nullOrUndefined(input)) { + try (OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream())) { final String in = JSType.toString(input); outputStream.write(in, 0, in.length()); + } catch (final IOException ex) { + // Process was not expecting input. May be normal state of affairs. } - } catch (final IOException ex) { - // Process was not expecting input. May be normal state of affairs. } // Wait for the process to complete.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/nosecurity/JDK-8130127.js Wed Jul 01 16:26:25 2015 +0200 @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * JDK-8130127: streamline input parameter of Nashorn scripting $EXEC function + * + * Test different variants of stdin passing to $EXEC. + * + * @test + * @option -scripting + * @run + */ + +var File = java.io.File, + sep = File.separator, + System = java.lang.System, + os = System.getProperty("os.name"), + win = os.startsWith("Windows"), + jjsName = "jjs" + (win ? ".exe" : ""), + javaHome = System.getProperty("java.home") + +var jjs = javaHome + "/../bin/".replace(/\//g, sep) + jjsName +if (!new File(jjs).isFile()) { + jjs = javaHome + "/bin/".replace(/\//g, sep) + jjsName +} + +var jjsCmd = jjs + " readprint.js" + +print($EXEC(jjsCmd)) +print($EXEC(jjsCmd, null)) +print($EXEC(jjsCmd, undefined)) +print($EXEC(jjsCmd, "")) + +print($EXEC(jjs, "print('hello')")) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/nosecurity/JDK-8130127.js.EXPECTED Wed Jul 01 16:26:25 2015 +0200 @@ -0,0 +1,6 @@ + + + + +hello +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/script/nosecurity/readprint.js Wed Jul 01 16:26:25 2015 +0200 @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * This is a dummy script accompanying JDK-8130127.js. + * + * @subtest + * @run ignore supplemental + */ + +var l = readLine() +print(l) +