コード例 #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: global.input (shell-interactive-mode-only) Read one or more lines of input
  * from the standard input till the given end marker is seen in standard input.
  *
  * @param self self reference
  * @param endMarker String used as end marker for input
  * @param prompt String used as input prompt
  * @return line that was read
  * @throws IOException if an exception occurs
  */
 public static Object input(final Object self, final Object endMarker, final Object prompt)
     throws IOException {
   final String endMarkerStr = (endMarker != UNDEFINED) ? JSType.toString(endMarker) : "";
   final String promptStr = (prompt != UNDEFINED) ? JSType.toString(prompt) : ">> ";
   final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
   final StringBuilder buf = new StringBuilder();
   while (true) {
     System.out.print(promptStr);
     final String line = reader.readLine();
     if (line == null || line.equals(endMarkerStr)) {
       break;
     }
     buf.append(line);
     buf.append('\n');
   }
   return buf.toString();
 }
コード例 #3
0
 @SuppressWarnings("LeakingThisInConstructor")
 private NativeError(final Object msg, final ScriptObject proto, final PropertyMap map) {
   super(proto, map);
   if (msg != UNDEFINED) {
     this.instMessage = JSType.toString(msg);
   } else {
     this.delete(NativeError.MESSAGE, false);
   }
   initException(this);
 }
コード例 #4
0
 public String toJson(Object aObj) {
   assert writeJsonFunc != null : SCRIPT_NOT_INITIALIZED;
   if (aObj instanceof Undefined) { // nashorn JSON parser could not work with undefined.
     aObj = null;
   }
   if (aObj instanceof JSObject
       || aObj instanceof CharSequence
       || aObj instanceof Number
       || aObj instanceof Boolean
       || aObj instanceof ScriptObject
       || aObj == null) {
     return JSType.toString(writeJsonFunc.call(null, new Object[] {aObj}));
   } else {
     throw new IllegalArgumentException("Java object couldn't be converted to JSON!");
   }
 }
コード例 #5
0
 private static String getScriptStackString(final ScriptObject sobj, final Throwable exp) {
   return JSType.toString(sobj) + "\n" + NashornException.getScriptStackString(exp);
 }
コード例 #6
0
 /**
  * Fetch String value of node.
  *
  * @return String value of node.
  */
 public String getString() {
   return JSType.toString(value);
 }
コード例 #7
0
 @Override
 public String getPropertyName() {
   return JSType.toString(getObject());
 }