src/jdk/nashorn/internal/runtime/ECMAException.java

Mon, 01 Jul 2013 17:21:09 +0530

author
sundar
date
Mon, 01 Jul 2013 17:21:09 +0530
changeset 389
47099609a48b
parent 378
39e17373d8df
child 748
a4a1d38f0294
permissions
-rw-r--r--

8019482: Number("0x0.0p0") should evaluate to NaN
Reviewed-by: lagergren

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.runtime;
jlaskey@3 27
jlaskey@3 28 import static jdk.nashorn.internal.codegen.CompilerConstants.constructorNoLookup;
jlaskey@3 29 import static jdk.nashorn.internal.codegen.CompilerConstants.virtualField;
jlaskey@3 30 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
jlaskey@3 31
jlaskey@3 32 import javax.script.ScriptException;
jlaskey@3 33 import jdk.nashorn.api.scripting.NashornException;
jlaskey@3 34 import jdk.nashorn.internal.codegen.CompilerConstants.Call;
jlaskey@3 35 import jdk.nashorn.internal.codegen.CompilerConstants.FieldAccess;
jlaskey@3 36
jlaskey@3 37 /**
jlaskey@3 38 * Exception used to implement ECMAScript "throw" from scripts. The actual thrown
jlaskey@3 39 * object from script need not be a Java exception and so it is wrapped as an
jlaskey@3 40 * instance field called "thrown" here. This exception class is also used to
jlaskey@3 41 * represent ECMA errors thrown from runtime code (for example, TypeError,
jlaskey@3 42 * ReferenceError thrown from Nashorn engine runtime).
jlaskey@3 43 */
jlaskey@3 44 @SuppressWarnings("serial")
jlaskey@3 45 public final class ECMAException extends NashornException {
jlaskey@3 46 /**
jlaskey@3 47 * Method handle pointing to the constructor {@link ECMAException#ECMAException(Object, String, int, int)},
jlaskey@3 48 */
jlaskey@3 49 public static final Call THROW_INIT = constructorNoLookup(ECMAException.class, Object.class, String.class, int.class, int.class);
jlaskey@3 50
jlaskey@3 51 /** Field handle to the{@link ECMAException#thrown} field, so that it can be accessed from generated code */
jlaskey@3 52 public static final FieldAccess THROWN = virtualField(ECMAException.class, "thrown", Object.class);
jlaskey@3 53
sundar@389 54 private static final String EXCEPTION_PROPERTY = "nashornException";
jlaskey@3 55
jlaskey@3 56 /** Object thrown. */
jlaskey@3 57 public final Object thrown;
jlaskey@3 58
jlaskey@3 59 /**
jlaskey@3 60 * Constructor. This is called from generated code to implement the {@code throw}
jlaskey@3 61 * instruction from generated script code
jlaskey@3 62 *
jlaskey@3 63 * @param thrown object to be thrown
jlaskey@3 64 * @param fileName script file name
jlaskey@3 65 * @param line line number of throw
jlaskey@3 66 * @param column column number of throw
jlaskey@3 67 */
jlaskey@3 68 public ECMAException(final Object thrown, final String fileName, final int line, final int column) {
lagergren@112 69 super(ScriptRuntime.safeToString(thrown), asThrowable(thrown), fileName, line, column);
jlaskey@3 70 this.thrown = thrown;
jlaskey@3 71 setExceptionToThrown();
jlaskey@3 72 }
jlaskey@3 73
jlaskey@3 74 /**
jlaskey@3 75 * Constructor. This is called from runtime code in Nashorn to throw things like
jlaskey@3 76 * type errors.
jlaskey@3 77 *
jlaskey@3 78 * @param thrown object to be thrown
jlaskey@3 79 * @param cause Java exception that triggered this throw
jlaskey@3 80 */
jlaskey@3 81 public ECMAException(final Object thrown, final Throwable cause) {
jlaskey@3 82 super(ScriptRuntime.safeToString(thrown), cause);
jlaskey@3 83 this.thrown = thrown;
jlaskey@3 84 setExceptionToThrown();
jlaskey@3 85 }
jlaskey@3 86
jlaskey@3 87 /**
jlaskey@3 88 * Get the thrown object
jlaskey@3 89 * @return thrown object
jlaskey@3 90 */
jlaskey@3 91 public Object getThrown() {
jlaskey@3 92 return thrown;
jlaskey@3 93 }
jlaskey@3 94
jlaskey@3 95 @Override
jlaskey@3 96 public String toString() {
jlaskey@3 97 final StringBuilder sb = new StringBuilder();
jlaskey@3 98 final String fileName = getFileName();
jlaskey@3 99 final int line = getLineNumber();
jlaskey@3 100 final int column = getColumnNumber();
jlaskey@3 101
jlaskey@3 102 if (fileName != null) {
jlaskey@3 103 sb.append(fileName);
jlaskey@3 104 if (line >= 0) {
jlaskey@3 105 sb.append(':');
jlaskey@3 106 sb.append(line);
jlaskey@3 107 }
jlaskey@3 108 if (column >= 0) {
jlaskey@3 109 sb.append(':');
jlaskey@3 110 sb.append(column);
jlaskey@3 111 }
jlaskey@3 112 sb.append(' ');
jlaskey@3 113 } else {
jlaskey@3 114 sb.append("ECMAScript Exception: ");
jlaskey@3 115 }
jlaskey@3 116
jlaskey@3 117 sb.append(getMessage());
jlaskey@3 118 return sb.toString();
jlaskey@3 119 }
jlaskey@3 120
jlaskey@3 121 /**
jlaskey@3 122 * Get the {@link ECMAException}, i.e. the underlying Java object for the
jlaskey@3 123 * JavaScript error object from a {@link ScriptObject} representing an error
jlaskey@3 124 *
jlaskey@3 125 * @param errObj the error object
jlaskey@3 126 * @return a {@link ECMAException}
jlaskey@3 127 */
jlaskey@3 128 public static Object getException(final ScriptObject errObj) {
jlaskey@3 129 return errObj.get(ECMAException.EXCEPTION_PROPERTY);
jlaskey@3 130 }
jlaskey@3 131
jlaskey@3 132 /**
jlaskey@3 133 * Print the stack trace for a {@code ScriptObject} representing an error
jlaskey@3 134 *
jlaskey@3 135 * @param errObj the error object
jlaskey@3 136 * @return undefined
jlaskey@3 137 */
jlaskey@3 138 public static Object printStackTrace(final ScriptObject errObj) {
jlaskey@3 139 final Object exception = getException(errObj);
jlaskey@3 140 if (exception instanceof Throwable) {
sundar@41 141 ((Throwable)exception).printStackTrace(Context.getCurrentErr());
jlaskey@3 142 } else {
lagergren@11 143 Context.err("<stack trace not available>");
jlaskey@3 144 }
jlaskey@3 145 return UNDEFINED;
jlaskey@3 146 }
jlaskey@3 147
jlaskey@3 148 /**
jlaskey@3 149 * Get the line number for a {@code ScriptObject} representing an error
jlaskey@3 150 *
jlaskey@3 151 * @param errObj the error object
jlaskey@3 152 * @return the line number, or undefined if wrapped exception is not a ParserException
jlaskey@3 153 */
jlaskey@3 154 public static Object getLineNumber(final ScriptObject errObj) {
jlaskey@3 155 final Object e = getException(errObj);
jlaskey@3 156 if (e instanceof NashornException) {
jlaskey@3 157 return ((NashornException)e).getLineNumber();
jlaskey@3 158 } else if (e instanceof ScriptException) {
jlaskey@3 159 return ((ScriptException)e).getLineNumber();
jlaskey@3 160 }
jlaskey@3 161
jlaskey@3 162 return UNDEFINED;
jlaskey@3 163 }
jlaskey@3 164
jlaskey@3 165 /**
jlaskey@3 166 * Get the column number for a {@code ScriptObject} representing an error
jlaskey@3 167 *
jlaskey@3 168 * @param errObj the error object
jlaskey@3 169 * @return the column number, or undefined if wrapped exception is not a ParserException
jlaskey@3 170 */
jlaskey@3 171 public static Object getColumnNumber(final ScriptObject errObj) {
jlaskey@3 172 final Object e = getException(errObj);
jlaskey@3 173 if (e instanceof NashornException) {
jlaskey@3 174 return ((NashornException)e).getColumnNumber();
jlaskey@3 175 } else if (e instanceof ScriptException) {
jlaskey@3 176 return ((ScriptException)e).getColumnNumber();
jlaskey@3 177 }
jlaskey@3 178
jlaskey@3 179 return UNDEFINED;
jlaskey@3 180 }
jlaskey@3 181
jlaskey@3 182 /**
jlaskey@3 183 * Get the file name for a {@code ScriptObject} representing an error
jlaskey@3 184 *
jlaskey@3 185 * @param errObj the error object
jlaskey@3 186 * @return the file name, or undefined if wrapped exception is not a ParserException
jlaskey@3 187 */
jlaskey@3 188 public static Object getFileName(final ScriptObject errObj) {
jlaskey@3 189 final Object e = getException(errObj);
jlaskey@3 190 if (e instanceof NashornException) {
jlaskey@3 191 return ((NashornException)e).getFileName();
jlaskey@3 192 } else if (e instanceof ScriptException) {
jlaskey@3 193 return ((ScriptException)e).getFileName();
jlaskey@3 194 }
jlaskey@3 195
jlaskey@3 196 return UNDEFINED;
jlaskey@3 197 }
jlaskey@3 198
jlaskey@3 199 /**
jlaskey@3 200 * Stateless string conversion for an error object
jlaskey@3 201 *
jlaskey@3 202 * @param errObj the error object
jlaskey@3 203 * @return string representation of {@code errObj}
jlaskey@3 204 */
jlaskey@3 205 public static String safeToString(final ScriptObject errObj) {
jlaskey@3 206 Object name = UNDEFINED;
jlaskey@3 207 try {
jlaskey@3 208 name = errObj.get("name");
jlaskey@3 209 } catch (final Exception e) {
jlaskey@3 210 //ignored
jlaskey@3 211 }
jlaskey@3 212
jlaskey@3 213 if (name == UNDEFINED) {
jlaskey@3 214 name = "Error";
jlaskey@3 215 } else {
jlaskey@3 216 name = ScriptRuntime.safeToString(name);
jlaskey@3 217 }
jlaskey@3 218
jlaskey@3 219 Object msg = UNDEFINED;
jlaskey@3 220 try {
jlaskey@3 221 msg = errObj.get("message");
jlaskey@3 222 } catch (final Exception e) {
jlaskey@3 223 //ignored
jlaskey@3 224 }
jlaskey@3 225
jlaskey@3 226 if (msg == UNDEFINED) {
jlaskey@3 227 msg = "";
jlaskey@3 228 } else {
jlaskey@3 229 msg = ScriptRuntime.safeToString(msg);
jlaskey@3 230 }
jlaskey@3 231
jlaskey@3 232 if (((String)name).isEmpty()) {
jlaskey@3 233 return (String)msg;
jlaskey@3 234 }
jlaskey@3 235
jlaskey@3 236 if (((String)msg).isEmpty()) {
jlaskey@3 237 return (String)name;
jlaskey@3 238 }
jlaskey@3 239
lagergren@137 240 return name + ": " + msg;
jlaskey@3 241 }
jlaskey@3 242
jlaskey@3 243 private static Throwable asThrowable(final Object obj) {
jlaskey@3 244 return (obj instanceof Throwable)? (Throwable)obj : null;
jlaskey@3 245 }
jlaskey@3 246
jlaskey@3 247 private void setExceptionToThrown() {
jlaskey@3 248 /*
jlaskey@3 249 * Nashorn extension: errorObject.nashornException
jlaskey@3 250 * Expose this exception via "nashornException" property of on the
jlaskey@3 251 * thrown object. This exception object can be used to print stack
jlaskey@3 252 * trace and fileName, line number etc. from script code.
jlaskey@3 253 */
jlaskey@3 254
jlaskey@3 255 if (thrown instanceof ScriptObject) {
jlaskey@3 256 final ScriptObject sobj = (ScriptObject)thrown;
jlaskey@3 257 if (!sobj.has(EXCEPTION_PROPERTY)) {
jlaskey@3 258 sobj.addOwnProperty(EXCEPTION_PROPERTY, Property.NOT_ENUMERABLE, this);
jlaskey@3 259 }
jlaskey@3 260 }
jlaskey@3 261 }
jlaskey@3 262 }

mercurial