diff options
author | sundar <none@none> | 2013-12-02 18:19:26 +0530 |
---|---|---|
committer | sundar <none@none> | 2013-12-02 18:19:26 +0530 |
commit | 7208c45db1d08a01bc209fbae38afb6087459d6c (patch) | |
tree | ff99f4b1985688cb47f92defbfa9066a9751bbe1 /src | |
parent | f4c565cba7f46df3a714663e7b93c619b7523736 (diff) | |
download | nashorn-7208c45db1d08a01bc209fbae38afb6087459d6c.tar.gz |
8029364: NashornException to expose thrown object
Reviewed-by: lagergren, jlaskey
Diffstat (limited to 'src')
4 files changed, 50 insertions, 4 deletions
diff --git a/src/jdk/nashorn/api/scripting/NashornException.java b/src/jdk/nashorn/api/scripting/NashornException.java index d5ec5bb4..f570ca71 100644 --- a/src/jdk/nashorn/api/scripting/NashornException.java +++ b/src/jdk/nashorn/api/scripting/NashornException.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.List; import jdk.nashorn.internal.codegen.CompilerConstants; import jdk.nashorn.internal.runtime.ECMAErrors; +import jdk.nashorn.internal.runtime.ScriptObject; /** * This is base exception for all Nashorn exceptions. These originate from @@ -49,6 +50,8 @@ public abstract class NashornException extends RuntimeException { private final int line; // script column number private final int column; + // underlying ECMA error object - lazily initialized + private Object ecmaError; /** script source name used for "engine.js" */ public static final String ENGINE_SCRIPT_SOURCE_NAME = "nashorn:engine/resources/engine.js"; @@ -188,4 +191,33 @@ public abstract class NashornException extends RuntimeException { } return buf.toString(); } + + protected Object getThrown() { + return null; + } + + protected NashornException initEcmaError(final ScriptObject global) { + if (ecmaError != null) { + return this; // initialized already! + } + + final Object thrown = getThrown(); + if (thrown instanceof ScriptObject) { + ecmaError = ScriptObjectMirror.wrap(thrown, global); + } else { + ecmaError = thrown; + } + + return this; + } + + /** + * Return the underlying ECMA error object, if available. + * + * @return underlying ECMA Error object's mirror or whatever was thrown + * from script such as a String, Number or a Boolean. + */ + public Object getEcmaError() { + return ecmaError; + } } diff --git a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java index 83b0bee8..4113e14c 100644 --- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java +++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java @@ -478,16 +478,19 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C private Object invokeImpl(final Object selfObject, final String name, final Object... args) throws ScriptException, NoSuchMethodException { name.getClass(); // null check + ScriptObject invokeGlobal = null; ScriptObjectMirror selfMirror = null; if (selfObject instanceof ScriptObjectMirror) { selfMirror = (ScriptObjectMirror)selfObject; if (! isOfContext(selfMirror.getHomeGlobal(), nashornContext)) { throw new IllegalArgumentException(getMessage("script.object.from.another.engine")); } + invokeGlobal = selfMirror.getHomeGlobal(); } else if (selfObject instanceof ScriptObject) { // invokeMethod called from script code - in which case we may get 'naked' ScriptObject // Wrap it with oldGlobal to make a ScriptObjectMirror for the same. final ScriptObject oldGlobal = Context.getGlobal(); + invokeGlobal = oldGlobal; if (oldGlobal == null) { throw new IllegalArgumentException(getMessage("no.current.nashorn.global")); } @@ -500,6 +503,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C } else if (selfObject == null) { // selfObject is null => global function call final ScriptObject ctxtGlobal = getNashornGlobalFrom(context); + invokeGlobal = ctxtGlobal; selfMirror = (ScriptObjectMirror)ScriptObjectMirror.wrap(ctxtGlobal, ctxtGlobal); } @@ -511,7 +515,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C if (cause instanceof NoSuchMethodException) { throw (NoSuchMethodException)cause; } - throwAsScriptException(e); + throwAsScriptException(e, invokeGlobal); throw new AssertionError("should not reach here"); } } @@ -545,7 +549,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C } return ScriptObjectMirror.translateUndefined(ScriptObjectMirror.wrap(ScriptRuntime.apply(script, ctxtGlobal), ctxtGlobal)); } catch (final Exception e) { - throwAsScriptException(e); + throwAsScriptException(e, ctxtGlobal); throw new AssertionError("should not reach here"); } finally { if (globalChanged) { @@ -554,7 +558,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C } } - private static void throwAsScriptException(final Exception e) throws ScriptException { + private static void throwAsScriptException(final Exception e, final ScriptObject global) throws ScriptException { if (e instanceof ScriptException) { throw (ScriptException)e; } else if (e instanceof NashornException) { @@ -562,6 +566,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C final ScriptException se = new ScriptException( ne.getMessage(), ne.getFileName(), ne.getLineNumber(), ne.getColumnNumber()); + ne.initEcmaError(global); se.initCause(e); throw se; } else if (e instanceof RuntimeException) { @@ -607,7 +612,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C return nashornContext.compileScript(source, newGlobal); } catch (final Exception e) { - throwAsScriptException(e); + throwAsScriptException(e, newGlobal); throw new AssertionError("should not reach here"); } finally { if (globalChanged) { diff --git a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java index 911f1663..01f72c00 100644 --- a/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java +++ b/src/jdk/nashorn/api/scripting/ScriptObjectMirror.java @@ -108,6 +108,8 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin } throw new RuntimeException("not a function: " + toString()); + } catch (final NashornException ne) { + throw ne.initEcmaError(global); } catch (final RuntimeException | Error e) { throw e; } catch (final Throwable t) { @@ -135,6 +137,8 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin } throw new RuntimeException("not a constructor: " + toString()); + } catch (final NashornException ne) { + throw ne.initEcmaError(global); } catch (final RuntimeException | Error e) { throw e; } catch (final Throwable t) { @@ -182,6 +186,8 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin } throw new NoSuchMethodException("No such function " + functionName); + } catch (final NashornException ne) { + throw ne.initEcmaError(global); } catch (final RuntimeException | Error e) { throw e; } catch (final Throwable t) { @@ -717,6 +723,8 @@ public final class ScriptObjectMirror extends AbstractJSObject implements Bindin } try { return callable.call(); + } catch (final NashornException ne) { + throw ne.initEcmaError(global); } catch (final RuntimeException e) { throw e; } catch (final Exception e) { diff --git a/src/jdk/nashorn/internal/runtime/ECMAException.java b/src/jdk/nashorn/internal/runtime/ECMAException.java index a32e721c..ff524bc0 100644 --- a/src/jdk/nashorn/internal/runtime/ECMAException.java +++ b/src/jdk/nashorn/internal/runtime/ECMAException.java @@ -88,6 +88,7 @@ public final class ECMAException extends NashornException { * Get the thrown object * @return thrown object */ + @Override public Object getThrown() { return thrown; } |