Exemple #1
0
  @Override
  public Object call(ExecutionContext context, Object self, Object... args) {
    // 15.4.4.8
    JSObject o = Types.toObject(context, self);
    long len = Types.toUint32(context, o.get(context, "length"));

    long middle = (long) Math.floor(len / 2);
    long lower = 0;

    while (lower != middle) {
      long upper = len - lower - 1;

      Object lowerValue = o.get(context, "" + lower);
      Object upperValue = o.get(context, "" + upper);

      boolean lowerExists = o.hasProperty(context, "" + lower);
      boolean upperExists = o.hasProperty(context, "" + upper);

      if (lowerExists && upperExists) {
        o.put(context, "" + lower, upperValue, true);
        o.put(context, "" + upper, lowerValue, true);
      } else if (upperExists) {
        o.put(context, "" + lower, upperValue, true);
        o.delete(context, "" + upper, true);
      } else if (lowerExists) {
        o.put(context, "" + upper, lowerValue, true);
        o.delete(context, "" + lower, true);
      } else {
        // no action required
      }
      ++lower;
    }

    return o;
  }
Exemple #2
0
  @Override
  public Object call(ExecutionContext context, Object self, Object... args) {
    // 15.4.4.10
    JSObject o = Types.toObject(context, self);

    JSObject a = BuiltinArray.newArray(context);
    long len = Types.toUint32(context, o.get(context, "length"));

    long relativeStart = Types.toInteger(context, args[0]);
    long k = Math.min(relativeStart, len);

    if (relativeStart < 0) {
      k = Math.max(len + relativeStart, 0);
    }

    long relativeEnd = len;
    if (args[1] != Types.UNDEFINED) {
      relativeEnd = Types.toInteger(context, args[1]);
    }

    long finalPos = Math.min(relativeEnd, len);

    if (relativeEnd < 0) {
      finalPos = Math.max(len + relativeEnd, 0);
    }

    long n = 0;

    while (k < finalPos) {
      boolean kPresent = o.hasProperty(context, "" + k);
      if (kPresent) {
        final Object kValue = o.get(context, "" + k);
        a.defineOwnProperty(
            context,
            "" + n,
            PropertyDescriptor.newDataPropertyDescriptor(kValue, true, true, true),
            false);
      }
      ++k;
      ++n;
    }

    return a;
  }