Search in sources :

Example 26 with ArrayObject

use of com.github.anba.es6draft.runtime.types.builtins.ArrayObject in project es6draft by anba.

the class ObjectConstructor method GetOwnPropertySymbols.

/**
     * 19.1.2.8.1 Runtime Semantics: GetOwnPropertyKeys ( O, Type ), with Type = Symbol
     * 
     * @param cx
     *            the execution context
     * @param o
     *            the script object
     * @return the own symbol-valued property keys of <var>o</var>
     */
public static ArrayObject GetOwnPropertySymbols(ExecutionContext cx, Object o) {
    /* steps 1-2 */
    ScriptObject obj = ToObject(cx, o);
    /* steps 3-4 */
    List<?> keys = obj.ownPropertyKeys(cx);
    /* step 5 */
    int initialSize = Math.min(8, keys.size());
    ArrayList<Symbol> nameList = new ArrayList<>(initialSize);
    /* step 6 */
    for (Object key : keys) {
        if (key instanceof Symbol) {
            nameList.add((Symbol) key);
        }
    }
    /* step 7 */
    return CreateArrayFromList(cx, nameList);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Symbol(com.github.anba.es6draft.runtime.types.Symbol) ArrayList(java.util.ArrayList) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ImmutablePrototypeObject(com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 27 with ArrayObject

use of com.github.anba.es6draft.runtime.types.builtins.ArrayObject in project es6draft by anba.

the class PromiseConstructor method PerformPromiseAll.

/**
     * 25.4.4.1.1 Runtime Semantics: PerformPromiseAll( iteratorRecord, constructor,
     * resultCapability)
     * 
     * @param <PROMISE>
     *            the promise type
     * @param cx
     *            the execution context
     * @param iterator
     *            the iterator object
     * @param constructor
     *            the constructor object
     * @param resultCapability
     *            the new promise capability record
     * @return the new promise object
     */
public static <PROMISE extends ScriptObject> PROMISE PerformPromiseAll(ExecutionContext cx, ScriptIterator<?> iterator, ScriptObject constructor, PromiseCapability<PROMISE> resultCapability) {
    /* steps 1-2 (not applicable) */
    /* step 3 */
    ArrayList<Object> values = new ArrayList<>();
    /* step 4 */
    AtomicInteger remainingElementsCount = new AtomicInteger(1);
    /* step 5 */
    int index = 0;
    /* step 6 */
    while (iterator.hasNext()) {
        /* steps 6.a-c, 6.e-g */
        Object nextValue = iterator.next();
        /* step 6.h */
        // Using 'null' instead of undefined to be able to verify that no values are overwritten
        values.add(null);
        /* steps 6.i-j */
        Object nextPromise = Invoke(cx, constructor, "resolve", nextValue);
        /* steps 6.k-p */
        PromiseAllResolveElementFunction resolveElement = new PromiseAllResolveElementFunction(cx.getRealm(), new AtomicBoolean(false), index, values, resultCapability, remainingElementsCount);
        /* step 6.q */
        remainingElementsCount.incrementAndGet();
        /* steps 6.r-s */
        Invoke(cx, nextPromise, "then", resolveElement, resultCapability.getReject());
        /* step 6.t */
        index += 1;
    }
    /* step 6.d */
    if (remainingElementsCount.decrementAndGet() == 0) {
        ArrayObject valuesArray = CreateArrayFromList(cx, values);
        resultCapability.getResolve().call(cx, UNDEFINED, valuesArray);
    }
    return resultCapability.getPromise();
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 28 with ArrayObject

use of com.github.anba.es6draft.runtime.types.builtins.ArrayObject in project es6draft by anba.

the class RegExpPrototype method RegExpSplit.

/**
     * Internal {@code RegExp.prototype.split()} function.
     * 
     * @param cx
     *            the execution context
     * @param rx
     *            the regular expression object
     * @param s
     *            the string
     * @param unicodeMatching
     *            {@code true} for unicode matching
     * @param lim
     *            the split limit
     * @return the split result array
     */
private static ArrayObject RegExpSplit(ExecutionContext cx, RegExpObject rx, String s, boolean unicodeMatching, long lim) {
    /* steps 1-12, 17-18 (not applicable) */
    /* step 15 */
    ArrayObject a = ArrayCreate(cx, 0);
    /* step 16 */
    int lengthA = 0;
    /* step 19 */
    int size = s.length();
    /* step 20 */
    int p = 0;
    /* step 21 */
    if (lim == 0) {
        return a;
    }
    /* step 22 */
    if (size == 0) {
        MatchState matcher = rx.getRegExpMatcher().matcher(s);
        if (matcher.find(0)) {
            RegExpConstructor.storeLastMatchResult(cx, s, matcher.toMatchResult());
            return a;
        }
        CreateDataProperty(cx, a, 0, s);
        return a;
    }
    /* step 23 */
    int q = p;
    /* step 24 */
    int lastStart = -1;
    MatchState matcher = rx.getRegExpMatcher().matcher(s);
    while (q != size) {
        /* steps 24.a-d */
        boolean match = matcher.find(q);
        /* step 24.e (implicit) */
        if (!match) {
            break;
        }
        RegExpConstructor.storeLastMatchResult(cx, s, matcher.toMatchResult());
        /* steps 24.f.i-ii */
        int e = matcher.end();
        /* step 24.f.iii-iv */
        if (e == p) {
            /* step 24.f.iii */
            q = AdvanceStringIndex(s, q, unicodeMatching);
        } else {
            /* step 24.f.iv */
            String t = s.substring(p, lastStart = matcher.start());
            CreateDataProperty(cx, a, lengthA, t);
            lengthA += 1;
            if (lengthA == lim) {
                return a;
            }
            Iterator<String> iterator = groupIterator(matcher, matcher.groupCount());
            while (iterator.hasNext()) {
                String cap = iterator.next();
                CreateDataProperty(cx, a, lengthA, cap != null ? cap : UNDEFINED);
                lengthA += 1;
                if (lengthA == lim) {
                    return a;
                }
            }
            p = e;
            q = p;
        }
    }
    if (lastStart == size) {
        return a;
    }
    /* step 25 */
    String t = s.substring(p, size);
    /* steps 26-27 */
    CreateDataProperty(cx, a, lengthA, t);
    /* step 28 */
    return a;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatchState(com.github.anba.es6draft.regexp.MatchState)

Example 29 with ArrayObject

use of com.github.anba.es6draft.runtime.types.builtins.ArrayObject in project es6draft by anba.

the class RegExpPrototype method toMatchArray.

/**
     * 21.2.5.2.2 Runtime Semantics: RegExpBuiltinExec ( R, S ) (2)
     * 
     * @param cx
     *            the execution context
     * @param s
     *            the string
     * @param m
     *            the match result
     * @return the match result script object
     */
private static ArrayObject toMatchArray(ExecutionContext cx, String s, MatchResult m) {
    /* steps 16-17 */
    int e = m.end();
    /* step 19 */
    int n = m.groupCount();
    /* step 20 */
    ArrayObject array = ArrayCreate(cx, n + 1);
    /* step 21 (omitted) */
    /* step 22 */
    int matchIndex = m.start();
    /* steps 23-25 */
    CreateDataProperty(cx, array, "index", matchIndex);
    CreateDataProperty(cx, array, "input", s);
    /* step 26 */
    String matchedSubstr = s.substring(matchIndex, e);
    /* step 27 */
    CreateDataProperty(cx, array, 0, matchedSubstr);
    /* step 28 */
    Iterator<String> iterator = groupIterator(m, n);
    for (int i = 1; iterator.hasNext(); ++i) {
        String capture = iterator.next();
        CreateDataProperty(cx, array, i, (capture != null ? capture : UNDEFINED));
    }
    /* step 29 */
    return array;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject)

Example 30 with ArrayObject

use of com.github.anba.es6draft.runtime.types.builtins.ArrayObject in project es6draft by anba.

the class ReflectParser method visit.

@Override
public Object visit(ArrayLiteral node, Void value) {
    ArrayObject elements = createList(node.getElements(), value);
    if (hasBuilder(Type.ArrayExpression)) {
        return call(Type.ArrayExpression, node, elements);
    }
    OrdinaryObject expression = createExpression(node, Type.ArrayExpression);
    addProperty(expression, "elements", elements);
    return expression;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)

Aggregations

ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)50 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)43 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)32 RegExpObject (com.github.anba.es6draft.runtime.objects.text.RegExpObject)25 ArrayList (java.util.ArrayList)5 ImmutablePrototypeObject (com.github.anba.es6draft.runtime.types.builtins.ImmutablePrototypeObject)3 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)2 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)1 MatchState (com.github.anba.es6draft.regexp.MatchState)1 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)1 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)1 DateObject (com.github.anba.es6draft.runtime.objects.date.DateObject)1 Callable (com.github.anba.es6draft.runtime.types.Callable)1 Property (com.github.anba.es6draft.runtime.types.Property)1 Symbol (com.github.anba.es6draft.runtime.types.Symbol)1 AbstractMap (java.util.AbstractMap)1 Date (java.util.Date)1 Map (java.util.Map)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1