/**
   * If value represents Uint32 index, make it available through ScriptRuntime.lastUint32Result(cx)
   * and return null. Otherwise return the same value as toXMLName(cx, value).
   */
  XMLName toXMLNameOrIndex(Context cx, Object value) {
    XMLName result;

    if (value instanceof XMLName) {
      result = (XMLName) value;
    } else if (value instanceof String) {
      String str = (String) value;
      long test = ScriptRuntime.testUint32String(str);
      if (test >= 0) {
        ScriptRuntime.storeUint32Result(cx, test);
        result = null;
      } else {
        result = toXMLNameFromString(cx, str);
      }
    } else if (value instanceof Number) {
      double d = ((Number) value).doubleValue();
      long l = (long) d;
      if (l == d && 0 <= l && l <= 0xFFFFFFFFL) {
        ScriptRuntime.storeUint32Result(cx, l);
        result = null;
      } else {
        throw badXMLName(value);
      }
    } else if (value instanceof QName) {
      QName qname = (QName) value;
      String uri = qname.uri();
      boolean number = false;
      result = null;
      if (uri != null && uri.length() == 0) {
        // Only in this case qname.toString() can resemble uint32
        long test = ScriptRuntime.testUint32String(uri);
        if (test >= 0) {
          ScriptRuntime.storeUint32Result(cx, test);
          number = true;
        }
      }
      if (!number) {
        result = XMLName.formProperty(uri, qname.localName());
      }
    } else if (value instanceof Boolean || value == Undefined.instance || value == null) {
      throw badXMLName(value);
    } else {
      String str = ScriptRuntime.toString(value);
      long test = ScriptRuntime.testUint32String(str);
      if (test >= 0) {
        ScriptRuntime.storeUint32Result(cx, test);
        result = null;
      } else {
        result = toXMLNameFromString(cx, str);
      }
    }

    return result;
  }