Search in sources :

Example 41 with Callable

use of com.github.anba.es6draft.runtime.types.Callable 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 42 with Callable

use of com.github.anba.es6draft.runtime.types.Callable 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)

Example 43 with Callable

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

the class PromiseConstructor method construct.

/**
     * 25.4.3.1 Promise ( executor )
     */
@Override
public PromiseObject construct(ExecutionContext callerContext, Constructor newTarget, Object... args) {
    ExecutionContext calleeContext = calleeContext();
    Object executor = argument(args, 0);
    /* step 2 */
    if (!IsCallable(executor)) {
        throw newTypeError(calleeContext, Messages.Key.NotCallable);
    }
    /* steps 3-7 */
    PromiseObject promise = OrdinaryCreateFromConstructor(calleeContext, newTarget, Intrinsics.PromisePrototype, GetPromiseAllocator(calleeContext.getRealm()));
    /* step 8 */
    ResolvingFunctions resolvingFunctions = CreateResolvingFunctions(calleeContext, promise);
    /* steps 9-10 */
    try {
        /* step 9 */
        ((Callable) executor).call(calleeContext, UNDEFINED, resolvingFunctions.getResolve(), resolvingFunctions.getReject());
    } catch (ScriptException e) {
        /* step 10 */
        resolvingFunctions.getReject().call(calleeContext, UNDEFINED, e.getValue());
    }
    /* step 11 */
    return promise;
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) ResolvingFunctions(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.ResolvingFunctions) CreateResolvingFunctions(com.github.anba.es6draft.runtime.objects.promise.PromiseAbstractOperations.CreateResolvingFunctions) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 44 with Callable

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

the class ReflectParser method parse.

/**
     * Parses the given script code and returns the matching Reflect AST nodes.
     * 
     * @param cx
     *            the execution context
     * @param source
     *            the source string
     * @param options
     *            the options object
     * @return the parsed node
     */
public static Object parse(ExecutionContext cx, String source, ScriptObject options) {
    boolean location = true;
    String sourceInfo = null;
    int line = 1;
    EnumMap<Type, Callable> builder = new EnumMap<>(Type.class);
    if (options != null) {
        if (HasProperty(cx, options, "loc")) {
            location = ToBoolean(Get(cx, options, "loc"));
        }
        if (HasProperty(cx, options, "source")) {
            sourceInfo = ToFlatString(cx, Get(cx, options, "source"));
        }
        if (HasProperty(cx, options, "line")) {
            line = ToInt32(cx, Get(cx, options, "line"));
        }
        if (HasProperty(cx, options, "builder")) {
            Object value = Get(cx, options, "builder");
            if (!isUndefinedOrNull(value)) {
                builder = toBuilderMap(cx, ToObject(cx, value));
            }
        }
    }
    return parse(cx, source, location, sourceInfo, line, builder);
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RegExpObject(com.github.anba.es6draft.runtime.objects.text.RegExpObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) EnumMap(java.util.EnumMap) Callable(com.github.anba.es6draft.runtime.types.Callable)

Example 45 with Callable

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

the class ArgumentsObject method CreateUnmappedArgumentsObject.

/**
     * 9.4.4.6 CreateUnmappedArgumentsObject(argumentsList)
     * <p>
     * [Called from generated code]
     * 
     * @param cx
     *            the execution context
     * @param argumentsList
     *            the function arguments
     * @return the strict mode arguments object
     */
public static ArgumentsObject CreateUnmappedArgumentsObject(ExecutionContext cx, Object[] argumentsList) {
    /* step 1 */
    int len = argumentsList.length;
    /* steps 2-3 */
    ArgumentsObject obj = new ArgumentsObject(cx.getRealm());
    obj.setPrototype(cx.getIntrinsic(Intrinsics.ObjectPrototype));
    /* step 4 */
    obj.infallibleDefineOwnProperty("length", new Property(len, true, false, true));
    /* steps 5-6 */
    for (int index = 0; index < len; ++index) {
        obj.setIndexed(index, argumentsList[index]);
    }
    Callable thrower = cx.getRealm().getThrowTypeError();
    /* step 7 */
    obj.infallibleDefineOwnProperty(BuiltinSymbol.iterator.get(), new Property(cx.getIntrinsic(Intrinsics.ArrayProto_values), true, false, true));
    /* step 8 */
    obj.infallibleDefineOwnProperty("callee", new Property(thrower, thrower, false, false));
    /* step 9 */
    obj.infallibleDefineOwnProperty("caller", new Property(thrower, thrower, false, false));
    /* step 11 */
    return obj;
}
Also used : Property(com.github.anba.es6draft.runtime.types.Property) Callable(com.github.anba.es6draft.runtime.types.Callable)

Aggregations

Callable (com.github.anba.es6draft.runtime.types.Callable)59 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)49 Property (com.github.anba.es6draft.runtime.types.Property)26 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)10 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)7 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)7 PropertyDescriptor (com.github.anba.es6draft.runtime.types.PropertyDescriptor)6 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)3 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)3 ArrayList (java.util.ArrayList)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)2 Realm (com.github.anba.es6draft.runtime.Realm)2 PromiseObject (com.github.anba.es6draft.runtime.objects.promise.PromiseObject)2 com.github.anba.es6draft.ast (com.github.anba.es6draft.ast)1 Abrupt (com.github.anba.es6draft.ast.AbruptNode.Abrupt)1 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)1 ModuleScope (com.github.anba.es6draft.ast.scope.ModuleScope)1 Name (com.github.anba.es6draft.ast.scope.Name)1 Scope (com.github.anba.es6draft.ast.scope.Scope)1 ScriptScope (com.github.anba.es6draft.ast.scope.ScriptScope)1