Search in sources :

Example 6 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable in project es6draft by anba.

the class SetConstructor method construct.

/**
     * 23.2.1.1 Set ([ iterable ])
     */
@Override
public SetObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object iterable = argument(args, 0);
    /* step 1 (not applicable) */
    /* steps 2-4 */
    SetObject set = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.SetPrototype, SetObject::new);
    /* steps 5-6, 8 */
    if (Type.isUndefinedOrNull(iterable)) {
        return set;
    }
    /* step 7 */
    Object _adder = Get(calleeContext, set, "add");
    if (!IsCallable(_adder)) {
        throw newTypeError(calleeContext, Messages.Key.PropertyNotCallable, "add");
    }
    Callable adder = (Callable) _adder;
    boolean isBuiltin = SetPrototype.isBuiltinAdd(_adder);
    if (isBuiltin && iterable instanceof SetObject) {
        SetObject other = (SetObject) iterable;
        if (ScriptIterators.isBuiltinIterator(calleeContext, other)) {
            set.getSetData().setAll(other.getSetData());
            return set;
        }
    }
    ScriptIterator<?> iter = GetScriptIterator(calleeContext, iterable);
    /* step 9 */
    try {
        while (iter.hasNext()) {
            Object nextValue = iter.next();
            if (isBuiltin) {
                set.getSetData().set(nextValue, null);
            } else {
                adder.call(calleeContext, set, nextValue);
            }
        }
        return set;
    } catch (ScriptException e) {
        iter.close(e);
        throw e;
    }
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 7 with IsCallable

use of com.github.anba.es6draft.runtime.AbstractOperations.IsCallable in project es6draft by anba.

the class ObservablePrototype method ExecuteSubscriber.

/**
     * Runtime Semantics: ExecuteSubscriber ( subscriber, observer )
     * 
     * @param cx
     *            the execution context
     * @param subscriber
     *            the subscriber function
     * @param observer
     *            the observer object
     * @return the subscriber result value
     */
public static Callable ExecuteSubscriber(ExecutionContext cx, Callable subscriber, SubscriptionObserverObject observer) {
    /* steps 1-2 (implicit) */
    /* steps 3-4 */
    Object subscriberResult = subscriber.call(cx, UNDEFINED, observer);
    /* step 5 */
    if (Type.isUndefinedOrNull(subscriberResult)) {
        return null;
    }
    /* step 6 */
    if (IsCallable(subscriberResult)) {
        return (Callable) subscriberResult;
    }
    /* steps 7-8 */
    Callable result = GetMethod(cx, subscriberResult, "unsubscribe");
    /* step 9 */
    if (result == null) {
        throw newTypeError(cx, Messages.Key.PropertyNotCallable, "unsubscribe");
    }
    /* steps 10-11 */
    SubscriptionCleanupFunction cleanupFunction = new SubscriptionCleanupFunction(cx.getRealm(), subscriberResult);
    // FIXME: spec bug - typo 'cancelFunction'
    return cleanupFunction;
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) PromiseObject(com.github.anba.es6draft.runtime.objects.promise.PromiseObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) IsCallable(com.github.anba.es6draft.runtime.AbstractOperations.IsCallable) Callable(com.github.anba.es6draft.runtime.types.Callable)

Aggregations

IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)7 Callable (com.github.anba.es6draft.runtime.types.Callable)7 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)5 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)5 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 ExecutionContext.newScriptingExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext.newScriptingExecutionContext)1 Realm (com.github.anba.es6draft.runtime.Realm)1 Console (com.github.anba.es6draft.runtime.internal.Console)1 RuntimeContext (com.github.anba.es6draft.runtime.internal.RuntimeContext)1 DateObject (com.github.anba.es6draft.runtime.objects.date.DateObject)1 PromiseObject (com.github.anba.es6draft.runtime.objects.promise.PromiseObject)1 RegExpObject (com.github.anba.es6draft.runtime.objects.text.RegExpObject)1 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1