Wed, 16 Jan 2013 17:58:51 +0530
8006412: Improve toString method of ScriptObjectMirror class
Reviewed-by: jlaskey, lagergren
1.1 --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Wed Jan 16 07:06:40 2013 -0400 1.2 +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java Wed Jan 16 17:58:51 2013 +0530 1.3 @@ -38,6 +38,7 @@ 1.4 import jdk.nashorn.internal.runtime.Context; 1.5 import jdk.nashorn.internal.runtime.ScriptFunction; 1.6 import jdk.nashorn.internal.runtime.ScriptObject; 1.7 +import jdk.nashorn.internal.runtime.ScriptRuntime; 1.8 import netscape.javascript.JSObject; 1.9 1.10 /** 1.11 @@ -67,6 +68,16 @@ 1.12 return sobj.hashCode(); 1.13 } 1.14 1.15 + @Override 1.16 + public String toString() { 1.17 + return inGlobal(new Callable<String>() { 1.18 + @Override 1.19 + public String call() { 1.20 + return ScriptRuntime.safeToString(sobj); 1.21 + } 1.22 + }); 1.23 + } 1.24 + 1.25 private <V> V inGlobal(final Callable<V> callable) { 1.26 final ScriptObject oldGlobal = Context.getGlobal(); 1.27 final boolean globalChanged = (oldGlobal != global);
2.1 --- a/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Wed Jan 16 07:06:40 2013 -0400 2.2 +++ b/test/src/jdk/nashorn/api/scripting/ScriptEngineTest.java Wed Jan 16 17:58:51 2013 +0530 2.3 @@ -46,6 +46,7 @@ 2.4 import javax.script.ScriptException; 2.5 import jdk.nashorn.internal.runtime.Version; 2.6 import netscape.javascript.JSObject; 2.7 +import org.testng.Assert; 2.8 import org.testng.TestNG; 2.9 import org.testng.annotations.Test; 2.10 2.11 @@ -859,4 +860,25 @@ 2.12 fail(t.getMessage()); 2.13 } 2.14 } 2.15 + 2.16 + @Test 2.17 + public void scriptObjectMirrorToStringTest() { 2.18 + final ScriptEngineManager m = new ScriptEngineManager(); 2.19 + final ScriptEngine e = m.getEngineByName("nashorn"); 2.20 + try { 2.21 + Object obj = e.eval("new TypeError('wrong type')"); 2.22 + Assert.assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value"); 2.23 + } catch (final Throwable t) { 2.24 + t.printStackTrace(); 2.25 + fail(t.getMessage()); 2.26 + } 2.27 + 2.28 + try { 2.29 + Object obj = e.eval("function func() { print('hello'); }"); 2.30 + Assert.assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value"); 2.31 + } catch (final Throwable t) { 2.32 + t.printStackTrace(); 2.33 + fail(t.getMessage()); 2.34 + } 2.35 + } 2.36 }