src/jdk/nashorn/internal/objects/NativeError.java

Print this page




 112      * @param self   self reference
 113      * @param msg    error message
 114      *
 115      * @return NativeError instance
 116      */
 117     @Constructor
 118     public static Object constructor(final boolean newObj, final Object self, final Object msg) {
 119         return new NativeError(msg);
 120     }
 121 
 122     /**
 123      * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 124      *
 125      * @param self self reference
 126      */
 127     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 128     public static Object captureStackTrace(final Object self, final Object errorObj) {
 129         Global.checkObject(errorObj);
 130         final ScriptObject sobj = (ScriptObject)errorObj;
 131         final ECMAException exp = new ECMAException(sobj, null);
 132         sobj.set("stack", NashornException.getScriptStackString(exp), false);
 133         return UNDEFINED;
 134     }
 135 
 136     /**
 137      * Nashorn extension: Error.dumpStack
 138      * dumps the stack of the current thread.
 139      *
 140      * @param self self reference
 141      *
 142      * @return undefined
 143      */
 144     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 145     public static Object dumpStack(final Object self) {
 146         Thread.dumpStack();
 147         return UNDEFINED;
 148     }
 149 
 150     /**
 151      * Nashorn extension: Error.prototype.printStackTrace
 152      * prints stack trace associated with the exception (if available).


 271     }
 272 
 273     /**
 274      * Nashorn extension: Error.prototype.stack
 275      * "stack" property is a string typed value containing JavaScript stack frames.
 276      * Each frame information is separated bv "\n" character.
 277      *
 278      * @param self  self reference
 279      *
 280      * @return      value of "stack" property
 281      */
 282     public static Object getStack(final Object self) {
 283         Global.checkObject(self);
 284         final ScriptObject sobj = (ScriptObject)self;
 285         if (sobj.has(STACK)) {
 286             return sobj.get(STACK);
 287         }
 288 
 289         final Object exception = ECMAException.getException(sobj);
 290         if (exception instanceof Throwable) {
 291             return NashornException.getScriptStackString((Throwable)exception);
 292         } else {
 293             return "";
 294         }
 295     }
 296 
 297     /**
 298      * Nashorn extension
 299      * Accessed from {@link Global} while setting up the Error.prototype
 300      *
 301      * @param self   self reference
 302      * @param value  value to set "stack" property to, must be {@code ScriptObject}
 303      *
 304      * @return value that was set
 305      */
 306     public static Object setStack(final Object self, final Object value) {
 307         Global.checkObject(self);
 308         final ScriptObject sobj = (ScriptObject)self;
 309         sobj.set(STACK, value, false);
 310         return value;
 311     }


 344 
 345         // Step 8 : if name is empty, return msg
 346         if (((String)name).isEmpty()) {
 347             return msg;
 348         }
 349 
 350         // Step 9 : if message is empty, return name
 351         if (((String)msg).isEmpty()) {
 352             return name;
 353         }
 354         // Step 10 : return name + ": " + msg
 355         return name + ": " + msg;
 356     }
 357 
 358     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 359         try {
 360             return MethodHandles.lookup().findStatic(NativeError.class, name, MH.type(rtype, types));
 361         } catch (final NoSuchMethodException | IllegalAccessException e) {
 362             throw new MethodHandleFactory.LookupException(e);
 363         }




 364     }
 365 }


 112      * @param self   self reference
 113      * @param msg    error message
 114      *
 115      * @return NativeError instance
 116      */
 117     @Constructor
 118     public static Object constructor(final boolean newObj, final Object self, final Object msg) {
 119         return new NativeError(msg);
 120     }
 121 
 122     /**
 123      * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided.
 124      *
 125      * @param self self reference
 126      */
 127     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 128     public static Object captureStackTrace(final Object self, final Object errorObj) {
 129         Global.checkObject(errorObj);
 130         final ScriptObject sobj = (ScriptObject)errorObj;
 131         final ECMAException exp = new ECMAException(sobj, null);
 132         sobj.set("stack", getScriptStackString(sobj, exp), false);
 133         return UNDEFINED;
 134     }
 135 
 136     /**
 137      * Nashorn extension: Error.dumpStack
 138      * dumps the stack of the current thread.
 139      *
 140      * @param self self reference
 141      *
 142      * @return undefined
 143      */
 144     @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 145     public static Object dumpStack(final Object self) {
 146         Thread.dumpStack();
 147         return UNDEFINED;
 148     }
 149 
 150     /**
 151      * Nashorn extension: Error.prototype.printStackTrace
 152      * prints stack trace associated with the exception (if available).


 271     }
 272 
 273     /**
 274      * Nashorn extension: Error.prototype.stack
 275      * "stack" property is a string typed value containing JavaScript stack frames.
 276      * Each frame information is separated bv "\n" character.
 277      *
 278      * @param self  self reference
 279      *
 280      * @return      value of "stack" property
 281      */
 282     public static Object getStack(final Object self) {
 283         Global.checkObject(self);
 284         final ScriptObject sobj = (ScriptObject)self;
 285         if (sobj.has(STACK)) {
 286             return sobj.get(STACK);
 287         }
 288 
 289         final Object exception = ECMAException.getException(sobj);
 290         if (exception instanceof Throwable) {
 291             return getScriptStackString(sobj, (Throwable)exception);
 292         } else {
 293             return "";
 294         }
 295     }
 296 
 297     /**
 298      * Nashorn extension
 299      * Accessed from {@link Global} while setting up the Error.prototype
 300      *
 301      * @param self   self reference
 302      * @param value  value to set "stack" property to, must be {@code ScriptObject}
 303      *
 304      * @return value that was set
 305      */
 306     public static Object setStack(final Object self, final Object value) {
 307         Global.checkObject(self);
 308         final ScriptObject sobj = (ScriptObject)self;
 309         sobj.set(STACK, value, false);
 310         return value;
 311     }


 344 
 345         // Step 8 : if name is empty, return msg
 346         if (((String)name).isEmpty()) {
 347             return msg;
 348         }
 349 
 350         // Step 9 : if message is empty, return name
 351         if (((String)msg).isEmpty()) {
 352             return name;
 353         }
 354         // Step 10 : return name + ": " + msg
 355         return name + ": " + msg;
 356     }
 357 
 358     private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
 359         try {
 360             return MethodHandles.lookup().findStatic(NativeError.class, name, MH.type(rtype, types));
 361         } catch (final NoSuchMethodException | IllegalAccessException e) {
 362             throw new MethodHandleFactory.LookupException(e);
 363         }
 364     }
 365 
 366     private static String getScriptStackString(final ScriptObject sobj, final Throwable exp) {
 367         return JSType.toString(sobj) + "\n" + NashornException.getScriptStackString(exp);
 368     }
 369 }