Example #1
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;
  }
  @Override
  public Object interpret(ExecutionContext context) {
    DynArray array = BuiltinArray.newArray(context);

    int numElements = this.exprs.size();
    int len = 0;
    for (int i = 0; i < numElements; ++i) {
      Expression each = this.exprs.get(i);
      Object value = null;
      if (each != null) {
        value = getValue(this.exprGets.get(i), context, each.interpret(context));
        array.defineOwnProperty(
            context,
            "" + i,
            PropertyDescriptor.newPropertyDescriptorForObjectInitializer(value),
            false);
      }
      ++len;
    }
    array.put(context, "length", (long) len, true);

    return (array);
  }