コード例 #1
0
  /**
   * ECMA 15.11.4.4 Error.prototype.toString ( )
   *
   * @param self self reference
   * @return this NativeError as a string
   */
  @Function(attributes = Attribute.NOT_ENUMERABLE)
  public static Object toString(final Object self) {
    // Step 1 and 2 : check if 'self' is object it not throw TypeError
    final ScriptObject sobj = Global.checkObject(self);

    // Step 3 & 4 : get "name" and convert to String.
    // But if message is undefined make it "Error".
    Object name = sobj.get("name");
    if (name == UNDEFINED) {
      name = "Error";
    } else {
      name = JSType.toString(name);
    }

    // Steps 5, 6, & 7 : get "message" and convert to String.
    // if 'message' is undefined make it "" (empty String).
    Object msg = sobj.get("message");
    if (msg == UNDEFINED) {
      msg = "";
    } else {
      msg = JSType.toString(msg);
    }

    // Step 8 : if name is empty, return msg
    if (((String) name).isEmpty()) {
      return msg;
    }

    // Step 9 : if message is empty, return name
    if (((String) msg).isEmpty()) {
      return name;
    }
    // Step 10 : return name + ": " + msg
    return name + ": " + msg;
  }
コード例 #2
0
 /**
  * Nashorn extension Accessed from {@link Global} while setting up the Error.prototype
  *
  * @param self self reference
  * @param value value to set "stack" property to, must be {@code ScriptObject}
  * @return value that was set
  */
 public static Object setStack(final Object self, final Object value) {
   final ScriptObject sobj = Global.checkObject(self);
   if (sobj.hasOwnProperty(STACK)) {
     sobj.put(STACK, value, false);
   } else {
     sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
   }
   return value;
 }
コード例 #3
0
 /**
  * Nashorn extension: Error.prototype.fileName
  *
  * @param self self reference
  * @param value value of file name
  * @return value that was set
  */
 public static Object setFileName(final Object self, final Object value) {
   final ScriptObject sobj = Global.checkObject(self);
   if (sobj.hasOwnProperty(FILENAME)) {
     sobj.put(FILENAME, value, false);
   } else {
     sobj.addOwnProperty(FILENAME, Attribute.NOT_ENUMERABLE, value);
   }
   return value;
 }
コード例 #4
0
 /**
  * Nashorn extension: Error.prototype.columnNumber
  *
  * @param self self reference
  * @param value value of column number
  * @return value that was set
  */
 public static Object setColumnNumber(final Object self, final Object value) {
   final ScriptObject sobj = Global.checkObject(self);
   if (sobj.hasOwnProperty(COLUMNNUMBER)) {
     sobj.put(COLUMNNUMBER, value, false);
   } else {
     sobj.addOwnProperty(COLUMNNUMBER, Attribute.NOT_ENUMERABLE, value);
   }
   return value;
 }
コード例 #5
0
 /**
  * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the
  * Error object provided.
  *
  * @param self self reference
  * @param errorObj the error object
  * @return undefined
  */
 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
 public static Object captureStackTrace(final Object self, final Object errorObj) {
   final ScriptObject sobj = Global.checkObject(errorObj);
   initException(sobj);
   sobj.delete(STACK, false);
   if (!sobj.has("stack")) {
     final ScriptFunction getStack = ScriptFunctionImpl.makeFunction("getStack", GET_STACK);
     final ScriptFunction setStack = ScriptFunctionImpl.makeFunction("setStack", SET_STACK);
     sobj.addOwnProperty("stack", Attribute.NOT_ENUMERABLE, getStack, setStack);
   }
   return UNDEFINED;
 }
コード例 #6
0
  /**
   * Nashorn extension: Error.prototype.getStackTrace() "stack" property is an array typed value
   * containing {@link StackTraceElement} objects of JavaScript stack frames.
   *
   * @param self self reference
   * @return stack trace as a script array.
   */
  @Function(attributes = Attribute.NOT_ENUMERABLE)
  public static Object getStackTrace(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    final Object exception = ECMAException.getException(sobj);
    Object[] res;
    if (exception instanceof Throwable) {
      res = NashornException.getScriptFrames((Throwable) exception);
    } else {
      res = ScriptRuntime.EMPTY_ARRAY;
    }

    return new NativeArray(res);
  }
コード例 #7
0
  /**
   * Nashorn extension: Error.prototype.stack "stack" property is a string typed value containing
   * JavaScript stack frames. Each frame information is separated bv "\n" character.
   *
   * @param self self reference
   * @return value of "stack" property
   */
  public static Object getStack(final Object self) {
    final ScriptObject sobj = Global.checkObject(self);
    if (sobj.has(STACK)) {
      return sobj.get(STACK);
    }

    final Object exception = ECMAException.getException(sobj);
    if (exception instanceof Throwable) {
      final Object value = getScriptStackString(sobj, (Throwable) exception);
      if (sobj.hasOwnProperty(STACK)) {
        sobj.put(STACK, value, false);
      } else {
        sobj.addOwnProperty(STACK, Attribute.NOT_ENUMERABLE, value);
      }

      return value;
    }

    return UNDEFINED;
  }
コード例 #8
0
 /**
  * Nashorn extension: Error.prototype.fileName
  *
  * @param self self reference
  * @return file name from which error was thrown
  */
 public static Object getFileName(final Object self) {
   final ScriptObject sobj = Global.checkObject(self);
   return sobj.has(FILENAME) ? sobj.get(FILENAME) : ECMAException.getFileName((ScriptObject) self);
 }
コード例 #9
0
 /**
  * Nashorn extension: Error.prototype.columnNumber
  *
  * @param self self reference
  * @return column number from which error was thrown
  */
 public static Object getColumnNumber(final Object self) {
   final ScriptObject sobj = Global.checkObject(self);
   return sobj.has(COLUMNNUMBER)
       ? sobj.get(COLUMNNUMBER)
       : ECMAException.getColumnNumber((ScriptObject) self);
 }
コード例 #10
0
 /**
  * Nashorn extension: Error.prototype.lineNumber
  *
  * @param self self reference
  * @return line number from which error was thrown
  */
 public static Object getLineNumber(final Object self) {
   final ScriptObject sobj = Global.checkObject(self);
   return sobj.has(LINENUMBER) ? sobj.get(LINENUMBER) : ECMAException.getLineNumber(sobj);
 }
コード例 #11
0
 /**
  * Nashorn extension: Error.prototype.printStackTrace prints stack trace associated with the
  * exception (if available). to the standard error stream.
  *
  * @param self self reference
  * @return result of {@link ECMAException#printStackTrace(ScriptObject)}, which is typically
  *     undefined
  */
 @Function(attributes = Attribute.NOT_ENUMERABLE)
 public static Object printStackTrace(final Object self) {
   return ECMAException.printStackTrace(Global.checkObject(self));
 }