21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
29 import static jdk.nashorn.internal.lookup.Lookup.MH;
30
31 import java.lang.invoke.MethodHandle;
32 import java.lang.invoke.MethodHandles;
33
34 import jdk.nashorn.api.scripting.NashornException;
35 import jdk.nashorn.internal.objects.annotations.Attribute;
36 import jdk.nashorn.internal.objects.annotations.Constructor;
37 import jdk.nashorn.internal.objects.annotations.Function;
38 import jdk.nashorn.internal.objects.annotations.Property;
39 import jdk.nashorn.internal.objects.annotations.ScriptClass;
40 import jdk.nashorn.internal.objects.annotations.Where;
41 import jdk.nashorn.internal.objects.ScriptFunctionImpl;
42 import jdk.nashorn.internal.runtime.ECMAException;
43 import jdk.nashorn.internal.runtime.JSType;
44 import jdk.nashorn.internal.runtime.PropertyMap;
45 import jdk.nashorn.internal.runtime.ScriptObject;
46 import jdk.nashorn.internal.runtime.ScriptFunction;
47 import jdk.nashorn.internal.runtime.ScriptRuntime;
48
49 /**
50 * ECMA 15.11 Error Objects
51 */
52 @ScriptClass("Error")
53 public final class NativeError extends ScriptObject {
54
55 static final MethodHandle GET_COLUMNNUMBER = findOwnMH("getColumnNumber", Object.class, Object.class);
56 static final MethodHandle SET_COLUMNNUMBER = findOwnMH("setColumnNumber", Object.class, Object.class, Object.class);
57 static final MethodHandle GET_LINENUMBER = findOwnMH("getLineNumber", Object.class, Object.class);
58 static final MethodHandle SET_LINENUMBER = findOwnMH("setLineNumber", Object.class, Object.class, Object.class);
59 static final MethodHandle GET_FILENAME = findOwnMH("getFileName", Object.class, Object.class);
60 static final MethodHandle SET_FILENAME = findOwnMH("setFileName", Object.class, Object.class, Object.class);
61 static final MethodHandle GET_STACK = findOwnMH("getStack", Object.class, Object.class);
76
77 /** Message property name */
78 @Property(name = NativeError.MESSAGE)
79 public Object instMessage;
80
81 /** ECMA 15.11.4.2 Error.prototype.name */
82 @Property(attributes = Attribute.NOT_ENUMERABLE, where = Where.PROTOTYPE)
83 public Object name;
84
85 /** ECMA 15.11.4.3 Error.prototype.message */
86 @Property(attributes = Attribute.NOT_ENUMERABLE, where = Where.PROTOTYPE)
87 public Object message;
88
89 // initialized by nasgen
90 private static PropertyMap $nasgenmap$;
91
92 static PropertyMap getInitialMap() {
93 return $nasgenmap$;
94 }
95
96 private NativeError(final Object msg, final ScriptObject proto, final PropertyMap map) {
97 super(proto, map);
98 if (msg != UNDEFINED) {
99 this.instMessage = JSType.toString(msg);
100 } else {
101 this.delete(NativeError.MESSAGE, false);
102 }
103 }
104
105 NativeError(final Object msg, final Global global) {
106 this(msg, global.getErrorPrototype(), global.getErrorMap());
107 }
108
109 private NativeError(final Object msg) {
110 this(msg, Global.instance());
111 }
112
113 @Override
114 public String getClassName() {
115 return "Error";
116 }
117
118 /**
119 * ECMA 15.11.2 The Error Constructor
120 *
121 * @param newObj true if this is being instantiated with a new
122 * @param self self reference
123 * @param msg error message
124 *
125 * @return NativeError instance
126 */
127 @Constructor
128 public static Object constructor(final boolean newObj, final Object self, final Object msg) {
129 return new NativeError(msg);
130 }
131
132 /**
133 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
134 *
135 * @param self self reference
136 * @param errorObj the error object
137 * @return undefined
138 */
139 @SuppressWarnings("unused")
140 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
141 public static Object captureStackTrace(final Object self, final Object errorObj) {
142 Global.checkObject(errorObj);
143 final ScriptObject sobj = (ScriptObject)errorObj;
144 new ECMAException(sobj, null); //constructor has side effects
145 sobj.delete("stack", false);
146 final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", GET_STACK);
147 final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", SET_STACK);
148 sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
149 return UNDEFINED;
150 }
151
152 /**
153 * Nashorn extension: Error.dumpStack
154 * dumps the stack of the current thread.
155 *
156 * @param self self reference
157 *
158 * @return undefined
159 */
160 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
161 public static Object dumpStack(final Object self) {
162 Thread.dumpStack();
163 return UNDEFINED;
164 }
165
166 /**
167 * Nashorn extension: Error.prototype.printStackTrace
168 * prints stack trace associated with the exception (if available).
209 *
210 * @return line number from which error was thrown
211 */
212 public static Object getLineNumber(final Object self) {
213 Global.checkObject(self);
214 final ScriptObject sobj = (ScriptObject)self;
215 return sobj.has(LINENUMBER) ? sobj.get(LINENUMBER) : ECMAException.getLineNumber(sobj);
216 }
217
218 /**
219 * Nashorn extension: Error.prototype.lineNumber
220 *
221 * @param self self reference
222 * @param value value of line number
223 *
224 * @return value that was set
225 */
226 public static Object setLineNumber(final Object self, final Object value) {
227 Global.checkObject(self);
228 final ScriptObject sobj = (ScriptObject)self;
229 sobj.set(LINENUMBER, value, false);
230 return value;
231 }
232
233 /**
234 * Nashorn extension: Error.prototype.columnNumber
235 *
236 * @param self self reference
237 *
238 * @return column number from which error was thrown
239 */
240 public static Object getColumnNumber(final Object self) {
241 Global.checkObject(self);
242 final ScriptObject sobj = (ScriptObject)self;
243 return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
244 }
245
246 /**
247 * Nashorn extension: Error.prototype.columnNumber
248 *
249 * @param self self reference
250 * @param value value of column number
251 *
252 * @return value that was set
253 */
254 public static Object setColumnNumber(final Object self, final Object value) {
255 Global.checkObject(self);
256 final ScriptObject sobj = (ScriptObject)self;
257 sobj.set(COLUMNNUMBER, value, false);
258 return value;
259 }
260
261 /**
262 * Nashorn extension: Error.prototype.fileName
263 *
264 * @param self self reference
265 *
266 * @return file name from which error was thrown
267 */
268 public static Object getFileName(final Object self) {
269 Global.checkObject(self);
270 final ScriptObject sobj = (ScriptObject)self;
271 return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject)self);
272 }
273
274 /**
275 * Nashorn extension: Error.prototype.fileName
276 *
277 * @param self self reference
278 * @param value value of file name
279 *
280 * @return value that was set
281 */
282 public static Object setFileName(final Object self, final Object value) {
283 Global.checkObject(self);
284 final ScriptObject sobj = (ScriptObject)self;
285 sobj.set(FILENAME, value, false);
286 return value;
287 }
288
289 /**
290 * Nashorn extension: Error.prototype.stack
291 * "stack" property is a string typed value containing JavaScript stack frames.
292 * Each frame information is separated bv "\n" character.
293 *
294 * @param self self reference
295 *
296 * @return value of "stack" property
297 */
298 public static Object getStack(final Object self) {
299 Global.checkObject(self);
300 final ScriptObject sobj = (ScriptObject)self;
301 if (sobj.has(STACK)) {
302 return sobj.get(STACK);
303 }
304
305 final Object exception = ECMAException.getException(sobj);
306 if (exception instanceof Throwable) {
307 return getScriptStackString(sobj, (Throwable)exception);
308 }
309
310 return "";
311 }
312
313 /**
314 * Nashorn extension
315 * Accessed from {@link Global} while setting up the Error.prototype
316 *
317 * @param self self reference
318 * @param value value to set "stack" property to, must be {@code ScriptObject}
319 *
320 * @return value that was set
321 */
322 public static Object setStack(final Object self, final Object value) {
323 Global.checkObject(self);
324 final ScriptObject sobj = (ScriptObject)self;
325 sobj.set(STACK, value, false);
326 return value;
327 }
328
329 /**
330 * ECMA 15.11.4.4 Error.prototype.toString ( )
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package jdk.nashorn.internal.objects;
27
28 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED;
29 import static jdk.nashorn.internal.lookup.Lookup.MH;
30
31 import java.lang.invoke.MethodHandle;
32 import java.lang.invoke.MethodHandles;
33
34 import jdk.nashorn.api.scripting.NashornException;
35 import jdk.nashorn.internal.objects.annotations.Attribute;
36 import jdk.nashorn.internal.objects.annotations.Constructor;
37 import jdk.nashorn.internal.objects.annotations.Function;
38 import jdk.nashorn.internal.objects.annotations.Property;
39 import jdk.nashorn.internal.objects.annotations.ScriptClass;
40 import jdk.nashorn.internal.objects.annotations.Where;
41 import jdk.nashorn.internal.runtime.ECMAException;
42 import jdk.nashorn.internal.runtime.JSType;
43 import jdk.nashorn.internal.runtime.PropertyMap;
44 import jdk.nashorn.internal.runtime.ScriptObject;
45 import jdk.nashorn.internal.runtime.ScriptFunction;
46 import jdk.nashorn.internal.runtime.ScriptRuntime;
47
48 /**
49 * ECMA 15.11 Error Objects
50 */
51 @ScriptClass("Error")
52 public final class NativeError extends ScriptObject {
53
54 static final MethodHandle GET_COLUMNNUMBER = findOwnMH("getColumnNumber", Object.class, Object.class);
55 static final MethodHandle SET_COLUMNNUMBER = findOwnMH("setColumnNumber", Object.class, Object.class, Object.class);
56 static final MethodHandle GET_LINENUMBER = findOwnMH("getLineNumber", Object.class, Object.class);
57 static final MethodHandle SET_LINENUMBER = findOwnMH("setLineNumber", Object.class, Object.class, Object.class);
58 static final MethodHandle GET_FILENAME = findOwnMH("getFileName", Object.class, Object.class);
59 static final MethodHandle SET_FILENAME = findOwnMH("setFileName", Object.class, Object.class, Object.class);
60 static final MethodHandle GET_STACK = findOwnMH("getStack", Object.class, Object.class);
75
76 /** Message property name */
77 @Property(name = NativeError.MESSAGE)
78 public Object instMessage;
79
80 /** ECMA 15.11.4.2 Error.prototype.name */
81 @Property(attributes = Attribute.NOT_ENUMERABLE, where = Where.PROTOTYPE)
82 public Object name;
83
84 /** ECMA 15.11.4.3 Error.prototype.message */
85 @Property(attributes = Attribute.NOT_ENUMERABLE, where = Where.PROTOTYPE)
86 public Object message;
87
88 // initialized by nasgen
89 private static PropertyMap $nasgenmap$;
90
91 static PropertyMap getInitialMap() {
92 return $nasgenmap$;
93 }
94
95 @SuppressWarnings("LeakingThisInConstructor")
96 private NativeError(final Object msg, final ScriptObject proto, final PropertyMap map) {
97 super(proto, map);
98 if (msg != UNDEFINED) {
99 this.instMessage = JSType.toString(msg);
100 } else {
101 this.delete(NativeError.MESSAGE, false);
102 }
103 initException(this);
104 }
105
106 NativeError(final Object msg, final Global global) {
107 this(msg, global.getErrorPrototype(), global.getErrorMap());
108 }
109
110 private NativeError(final Object msg) {
111 this(msg, Global.instance());
112 }
113
114 @Override
115 public String getClassName() {
116 return "Error";
117 }
118
119 /**
120 * ECMA 15.11.2 The Error Constructor
121 *
122 * @param newObj true if this is being instantiated with a new
123 * @param self self reference
124 * @param msg error message
125 *
126 * @return NativeError instance
127 */
128 @Constructor
129 public static Object constructor(final boolean newObj, final Object self, final Object msg) {
130 return new NativeError(msg);
131 }
132
133 // This is called NativeError, NativeTypeError etc. to
134 // associate a ECMAException with the ECMA Error object.
135 @SuppressWarnings("unused")
136 static void initException(final ScriptObject self) {
137 // ECMAException constructor has side effects
138 new ECMAException(self, null);
139 }
140
141 /**
142 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
143 *
144 * @param self self reference
145 * @param errorObj the error object
146 * @return undefined
147 */
148 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
149 public static Object captureStackTrace(final Object self, final Object errorObj) {
150 Global.checkObject(errorObj);
151 final ScriptObject sobj = (ScriptObject)errorObj;
152 initException(sobj);
153 sobj.delete(STACK, false);
154 if (! sobj.has("stack")) {
155 final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", GET_STACK);
156 final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", SET_STACK);
157 sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
158 }
159 return UNDEFINED;
160 }
161
162 /**
163 * Nashorn extension: Error.dumpStack
164 * dumps the stack of the current thread.
165 *
166 * @param self self reference
167 *
168 * @return undefined
169 */
170 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
171 public static Object dumpStack(final Object self) {
172 Thread.dumpStack();
173 return UNDEFINED;
174 }
175
176 /**
177 * Nashorn extension: Error.prototype.printStackTrace
178 * prints stack trace associated with the exception (if available).
219 *
220 * @return line number from which error was thrown
221 */
222 public static Object getLineNumber(final Object self) {
223 Global.checkObject(self);
224 final ScriptObject sobj = (ScriptObject)self;
225 return sobj.has(LINENUMBER) ? sobj.get(LINENUMBER) : ECMAException.getLineNumber(sobj);
226 }
227
228 /**
229 * Nashorn extension: Error.prototype.lineNumber
230 *
231 * @param self self reference
232 * @param value value of line number
233 *
234 * @return value that was set
235 */
236 public static Object setLineNumber(final Object self, final Object value) {
237 Global.checkObject(self);
238 final ScriptObject sobj = (ScriptObject)self;
239 if (sobj.hasOwnProperty(LINENUMBER)) {
240 sobj.put(LINENUMBER, value, false);
241 } else {
242 sobj.addOwnProperty(LINENUMBER, Attribute.NOT_ENUMERABLE, value);
243 }
244 return value;
245 }
246
247 /**
248 * Nashorn extension: Error.prototype.columnNumber
249 *
250 * @param self self reference
251 *
252 * @return column number from which error was thrown
253 */
254 public static Object getColumnNumber(final Object self) {
255 Global.checkObject(self);
256 final ScriptObject sobj = (ScriptObject)self;
257 return sobj.has(COLUMNNUMBER) ? sobj.get(COLUMNNUMBER) : ECMAException.getColumnNumber((ScriptObject)self);
258 }
259
260 /**
261 * Nashorn extension: Error.prototype.columnNumber
262 *
263 * @param self self reference
264 * @param value value of column number
265 *
266 * @return value that was set
267 */
268 public static Object setColumnNumber(final Object self, final Object value) {
269 Global.checkObject(self);
270 final ScriptObject sobj = (ScriptObject)self;
271 if (sobj.hasOwnProperty(COLUMNNUMBER)) {
272 sobj.put(COLUMNNUMBER, value, false);
273 } else {
274 sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
275 }
276 return value;
277 }
278
279 /**
280 * Nashorn extension: Error.prototype.fileName
281 *
282 * @param self self reference
283 *
284 * @return file name from which error was thrown
285 */
286 public static Object getFileName(final Object self) {
287 Global.checkObject(self);
288 final ScriptObject sobj = (ScriptObject)self;
289 return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject)self);
290 }
291
292 /**
293 * Nashorn extension: Error.prototype.fileName
294 *
295 * @param self self reference
296 * @param value value of file name
297 *
298 * @return value that was set
299 */
300 public static Object setFileName(final Object self, final Object value) {
301 Global.checkObject(self);
302 final ScriptObject sobj = (ScriptObject)self;
303 if (sobj.hasOwnProperty(FILENAME)) {
304 sobj.put(FILENAME, value, false);
305 } else {
306 sobj.addOwnProperty(FILENAME, Attribute.NOT_ENUMERABLE, value);
307 }
308 return value;
309 }
310
311 /**
312 * Nashorn extension: Error.prototype.stack
313 * "stack" property is a string typed value containing JavaScript stack frames.
314 * Each frame information is separated bv "\n" character.
315 *
316 * @param self self reference
317 *
318 * @return value of "stack" property
319 */
320 public static Object getStack(final Object self) {
321 Global.checkObject(self);
322 final ScriptObject sobj = (ScriptObject)self;
323 if (sobj.has(STACK)) {
324 return sobj.get(STACK);
325 }
326
327 final Object exception = ECMAException.getException(sobj);
328 if (exception instanceof Throwable) {
329 Object value = getScriptStackString(sobj, (Throwable)exception);
330 sobj.put(STACK, value, false);
331 return value;
332 }
333
334 return UNDEFINED;
335 }
336
337 /**
338 * Nashorn extension
339 * Accessed from {@link Global} while setting up the Error.prototype
340 *
341 * @param self self reference
342 * @param value value to set "stack" property to, must be {@code ScriptObject}
343 *
344 * @return value that was set
345 */
346 public static Object setStack(final Object self, final Object value) {
347 Global.checkObject(self);
348 final ScriptObject sobj = (ScriptObject)self;
349 sobj.set(STACK, value, false);
350 return value;
351 }
352
353 /**
354 * ECMA 15.11.4.4 Error.prototype.toString ( )
|