8006412: Improve toString method of ScriptObjectMirror class
Reviewed-by: jlaskey, lagergren
This commit is contained in:
parent
c6c424f27a
commit
d35ae9ab05
@ -38,6 +38,7 @@ import java.util.concurrent.Callable;
|
||||
import jdk.nashorn.internal.runtime.Context;
|
||||
import jdk.nashorn.internal.runtime.ScriptFunction;
|
||||
import jdk.nashorn.internal.runtime.ScriptObject;
|
||||
import jdk.nashorn.internal.runtime.ScriptRuntime;
|
||||
import netscape.javascript.JSObject;
|
||||
|
||||
/**
|
||||
@ -67,6 +68,16 @@ final class ScriptObjectMirror extends JSObject implements Map<Object, Object> {
|
||||
return sobj.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return inGlobal(new Callable<String>() {
|
||||
@Override
|
||||
public String call() {
|
||||
return ScriptRuntime.safeToString(sobj);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private <V> V inGlobal(final Callable<V> callable) {
|
||||
final ScriptObject oldGlobal = Context.getGlobal();
|
||||
final boolean globalChanged = (oldGlobal != global);
|
||||
|
@ -46,6 +46,7 @@ import javax.script.ScriptEngineManager;
|
||||
import javax.script.ScriptException;
|
||||
import jdk.nashorn.internal.runtime.Version;
|
||||
import netscape.javascript.JSObject;
|
||||
import org.testng.Assert;
|
||||
import org.testng.TestNG;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@ -859,4 +860,25 @@ public class ScriptEngineTest {
|
||||
fail(t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void scriptObjectMirrorToStringTest() {
|
||||
final ScriptEngineManager m = new ScriptEngineManager();
|
||||
final ScriptEngine e = m.getEngineByName("nashorn");
|
||||
try {
|
||||
Object obj = e.eval("new TypeError('wrong type')");
|
||||
Assert.assertEquals(obj.toString(), "TypeError: wrong type", "toString returns wrong value");
|
||||
} catch (final Throwable t) {
|
||||
t.printStackTrace();
|
||||
fail(t.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
Object obj = e.eval("function func() { print('hello'); }");
|
||||
Assert.assertEquals(obj.toString(), "function func() { print('hello'); }", "toString returns wrong value");
|
||||
} catch (final Throwable t) {
|
||||
t.printStackTrace();
|
||||
fail(t.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user