8130127: streamline input parameter of Nashorn scripting $EXEC function

Handle null and undefined correctly, do not coerce them to strings

Reviewed-by: lagergren, sundar
This commit is contained in:
Michael Haupt 2015-07-01 16:26:25 +02:00
parent 891fa406cd
commit eacc58bf85
5 changed files with 105 additions and 10 deletions

View File

@ -39,9 +39,10 @@ $EXEC("ls -la")
$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"))

View File

@ -128,9 +128,9 @@ public final class ScriptingFunctions {
* 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 @@ public final class ScriptingFunctions {
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.

View File

@ -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')"))

View File

@ -0,0 +1,6 @@
hello

View File

@ -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)