# HG changeset patch # User sundar # Date 1375115200 -19800 # Node ID 41b35dc6bab7198440e020a73ac41862d255874b # Parent a1076f5a3961bc52d23ee89edbc807fa1e208566# Parent 721939a7971cd1ffe32b7192f9ac3d7d3b834ffc Merge diff -r a1076f5a3961 -r 41b35dc6bab7 nashorn/src/jdk/nashorn/api/scripting/resources/engine.js --- a/nashorn/src/jdk/nashorn/api/scripting/resources/engine.js Mon Jul 29 10:28:03 2013 +0530 +++ b/nashorn/src/jdk/nashorn/api/scripting/resources/engine.js Mon Jul 29 21:56:40 2013 +0530 @@ -39,12 +39,20 @@ } }); -function print(str) { +function print() { var writer = context.getWriter(); if (! (writer instanceof java.io.PrintWriter)) { writer = new java.io.PrintWriter(writer); } - writer.println(String(str)); + + var buf = new java.lang.StringBuilder(); + for (var i = 0; i < arguments.length; i++) { + if (i != 0) { + buf.append(' '); + } + buf.append(String(arguments[i])); + } + writer.println(buf.toString()); } /** diff -r a1076f5a3961 -r 41b35dc6bab7 nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java --- a/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Mon Jul 29 10:28:03 2013 +0530 +++ b/nashorn/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Mon Jul 29 21:56:40 2013 +0530 @@ -1220,4 +1220,38 @@ Assert.assertEquals(itf.test1(42, "a", "b"), "i == 42, strings instanceof java.lang.String[] == true, strings == [a, b]"); Assert.assertEquals(itf.test2(44, "c", "d", "e"), "arguments[0] == 44, arguments[1] instanceof java.lang.String[] == true, arguments[1] == [c, d, e]"); } + + @Test + // check that print function prints arg followed by newline char + public void printTest() { + final ScriptEngineManager m = new ScriptEngineManager(); + final ScriptEngine e = m.getEngineByName("nashorn"); + final StringWriter sw = new StringWriter(); + e.getContext().setWriter(sw); + try { + e.eval("print('hello')"); + } catch (final Throwable t) { + t.printStackTrace(); + fail(t.getMessage()); + } + + assertEquals(sw.toString(), "hello\n"); + } + + @Test + // check that print prints all arguments (more than one) + public void printManyTest() { + final ScriptEngineManager m = new ScriptEngineManager(); + final ScriptEngine e = m.getEngineByName("nashorn"); + final StringWriter sw = new StringWriter(); + e.getContext().setWriter(sw); + try { + e.eval("print(34, true, 'hello')"); + } catch (final Throwable t) { + t.printStackTrace(); + fail(t.getMessage()); + } + + assertEquals(sw.toString(), "34 true hello\n"); + } }