Esempio n. 1
0
 @Test
 public void testSplitString() {
   JSObject result = (JSObject) eval("'bob,lance,qmx'.split(',')");
   assertThat(result.get(getContext(), "length")).isEqualTo(3L);
   assertThat(result.get(getContext(), "0")).isEqualTo("bob");
   assertThat(result.get(getContext(), "1")).isEqualTo("lance");
   assertThat(result.get(getContext(), "2")).isEqualTo("qmx");
 }
Esempio n. 2
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;
  }
Esempio n. 3
0
  public void initialize(GlobalObject globalObject, JSObject proto) {
    // String.foo()
    defineNonEnumerableProperty(this, "fromCharCode", new FromCharCode(globalObject));

    // String.prototype.foo()
    proto.setPrototype(globalObject.getPrototypeFor("Object"));
    defineNonEnumerableProperty(proto, "constructor", this);
    defineNonEnumerableProperty(proto, "toString", new ToString(globalObject));
    defineNonEnumerableProperty(proto, "valueOf", new ValueOf(globalObject));
    defineNonEnumerableProperty(proto, "charAt", new CharAt(globalObject));
    defineNonEnumerableProperty(proto, "charCodeAt", new CharCodeAt(globalObject));
    defineNonEnumerableProperty(proto, "concat", new Concat(globalObject));
    defineNonEnumerableProperty(proto, "indexOf", new IndexOf(globalObject));
    defineNonEnumerableProperty(proto, "lastIndexOf", new LastIndexOf(globalObject));
    defineNonEnumerableProperty(proto, "localeCompare", new LocaleCompare(globalObject));
    defineNonEnumerableProperty(proto, "match", new Match(globalObject));
    defineNonEnumerableProperty(proto, "search", new Search(globalObject));
    defineNonEnumerableProperty(proto, "slice", new Slice(globalObject));
    defineNonEnumerableProperty(proto, "split", new Split(globalObject));
    defineNonEnumerableProperty(proto, "substring", new Substring(globalObject));
    defineNonEnumerableProperty(
        proto, "substr", new Substr(globalObject)); // Alias, 'cause node likes this
    defineNonEnumerableProperty(proto, "toLowerCase", new ToLowerCase(globalObject));
    defineNonEnumerableProperty(proto, "toUpperCase", new ToUpperCase(globalObject));
    defineNonEnumerableProperty(proto, "toLocaleLowerCase", new ToLocaleLowerCase(globalObject));
    defineNonEnumerableProperty(proto, "toLocaleUpperCase", new ToLocaleUpperCase(globalObject));
    defineNonEnumerableProperty(proto, "trim", new Trim(globalObject));
    defineNonEnumerableProperty(
        proto, "replace", new Replace(globalObject)); // http://es5.github.com/#x15.5.4.11
  }
Esempio n. 4
0
 protected static void defineReadOnlyFunction(
     final JSObject on, final GlobalObject globalObject, String name, final Object value) {
   PropertyDescriptor desc = new PropertyDescriptor();
   desc.set(Names.VALUE, value);
   desc.set(Names.WRITABLE, false);
   desc.set(Names.ENUMERABLE, false);
   desc.set(Names.CONFIGURABLE, false);
   on.defineOwnProperty(null, name, desc, false);
 }
Esempio n. 5
0
 @Override
 public void initialize(GlobalObject globalObject, JSObject proto) {
   proto.setPrototype(globalObject.getPrototypeFor("Object"));
   defineNonEnumerableProperty(proto, "constructor", this);
   defineNonEnumerableProperty(proto, "toString", new ToString(globalObject));
   defineNonEnumerableProperty(proto, "apply", new Apply(globalObject));
   defineNonEnumerableProperty(proto, "call", new Call(globalObject));
   defineNonEnumerableProperty(proto, "bind", new Bind(globalObject));
 }
Esempio n. 6
0
  @Override
  public Object call(ExecutionContext context, Object self, Object... args) {
    JSObject o = Types.toObject(context, self);
    Object tv = Types.toPrimitive(context, o, "Number");

    if (tv instanceof Number) {
      if (Double.isInfinite(((Number) tv).doubleValue())) {
        return Types.NULL;
      }
    }

    Object toISO = o.get(context, "toISOString");
    if (!(toISO instanceof JSFunction)) {
      throw new ThrowException(context, context.createTypeError("toISOString must be a function"));
    }

    return context.call((JSFunction) toISO, o);
  }
Esempio n. 7
0
  @Override
  public void initialize(GlobalObject globalObject, JSObject proto) {
    proto.setPrototype(globalObject.getPrototypeFor("Object"));

    defineNonEnumerableProperty(proto, "constructor", this);
    defineNonEnumerableProperty(proto, "toString", new ToString(globalObject));
    defineNonEnumerableProperty(proto, "toLocaleString", new ToString(globalObject));
    defineNonEnumerableProperty(proto, "valueOf", new ValueOf(globalObject));
    defineNonEnumerableProperty(proto, "toFixed", new ToFixed(globalObject));
    defineNonEnumerableProperty(proto, "toExponential", new ToExponential(globalObject));
    defineNonEnumerableProperty(proto, "toPrecision", new ToPrecision(globalObject));

    defineReadOnlyProperty(this, globalObject, "NaN", Double.NaN);
    defineReadOnlyProperty(this, globalObject, "POSITIVE_INFINITY", Double.POSITIVE_INFINITY);
    defineReadOnlyProperty(this, globalObject, "NEGATIVE_INFINITY", Double.NEGATIVE_INFINITY);
    defineReadOnlyProperty(this, globalObject, "MIN_VALUE", Double.MIN_VALUE);
    defineReadOnlyProperty(this, globalObject, "MAX_VALUE", Double.MAX_VALUE);
    defineReadOnlyProperty(globalObject, globalObject, "NaN", Double.NaN);
    defineReadOnlyProperty(globalObject, globalObject, "Infinity", Double.POSITIVE_INFINITY);
  }
Esempio n. 8
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;
  }