Search in sources :

Example 1 with OrdinaryAsyncFunction

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

the class FunctionCodeGenerator method asyncFunctionCall.

/**
 * Generate bytecode for:
 *
 * <pre>
 * calleeContext = newFunctionExecutionContext(function, null, thisValue)
 * function_init(calleeContext, function, arguments)
 * return EvaluateBody(calleeContext, generator)
 * </pre>
 *
 * @param codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void asyncFunctionCall(CodeGenerator codegen, FunctionNode node, MethodCode method, FunctionCode function) {
    callMethod(node, method, mv -> {
        Variable<OrdinaryAsyncFunction> fn = mv.getFunction(OrdinaryAsyncFunction.class);
        Variable<Object> thisValue = mv.getThisValue();
        Variable<Object[]> arguments = mv.getArguments();
        Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
        // (1) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, fn, thisValue, mv);
        // (2) Perform FunctionDeclarationInstantiation
        {
            TryCatchLabel startCatch = new TryCatchLabel();
            TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
            Jump noException = new Jump();
            mv.mark(startCatch);
            functionDeclarationInstantiation(function.instantiation, calleeContext, fn, arguments, mv);
            mv.goTo(noException);
            mv.mark(endCatch);
            mv.catchHandler(handlerCatch, Types.ScriptException);
            {
                // stack: [exception] -> [cx, exception]
                mv.load(calleeContext);
                mv.swap();
                // stack: [cx, exception] -> [promise]
                mv.invoke(Methods.PromiseAbstractOperations_PromiseOf);
                mv._return();
            }
            mv.mark(noException);
            mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
        }
        // (3) Perform EvaluateBody
        mv.load(calleeContext);
        mv.load(fn);
        mv.invoke(Methods.OrdinaryAsyncFunction_EvaluateBody);
        // (4) Return result value
        mv._return();
    });
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 2 with OrdinaryAsyncFunction

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

the class FunctionOperations method EvaluateAsyncFunctionExpression.

/**
 * Extension: Async Function Definitions
 *
 * @param fd
 *            the function runtime info object
 * @param cx
 *            the execution context
 * @return the new async function instance
 */
public static OrdinaryAsyncFunction EvaluateAsyncFunctionExpression(RuntimeInfo.Function fd, ExecutionContext cx) {
    OrdinaryAsyncFunction closure;
    if (!fd.is(RuntimeInfo.FunctionFlags.ScopedName)) {
        /* step 1 (not applicable) */
        /* step 2 */
        LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
        /* step 3 */
        closure = AsyncFunctionCreate(cx, FunctionKind.Normal, fd, scope);
    } else {
        /* step 1 (not applicable) */
        /* step 2 */
        LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
        /* step 3 */
        LexicalEnvironment<DeclarativeEnvironmentRecord> funcEnv = newDeclarativeEnvironment(scope);
        /* step 4 */
        DeclarativeEnvironmentRecord envRec = funcEnv.getEnvRec();
        /* step 5 */
        String name = fd.functionName();
        /* step 6 */
        envRec.createImmutableBinding(name, false);
        /* step 7 */
        closure = AsyncFunctionCreate(cx, FunctionKind.Normal, fd, funcEnv);
        /* step 8 */
        SetFunctionName(closure, name);
        /* step 9 */
        envRec.initializeBinding(name, closure);
    }
    /* step 4/10 */
    return closure;
}
Also used : OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 3 with OrdinaryAsyncFunction

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

the class FunctionOperations method EvaluatePropertyDefinitionAsync.

/**
 * Extension: Async Function Definitions
 *
 * @param object
 *            the script object
 * @param propKey
 *            the property key
 * @param cx
 *            the execution context
 * @param fd
 *            the function runtime info object
 * @return the async method
 */
public static OrdinaryAsyncFunction EvaluatePropertyDefinitionAsync(OrdinaryObject object, Object propKey, RuntimeInfo.Function fd, ExecutionContext cx) {
    /* steps 1-2 (generated code) */
    /* step 3 (not applicable) */
    /* step 4 */
    LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
    /* step 5 */
    OrdinaryAsyncFunction closure = AsyncFunctionCreate(cx, FunctionKind.Method, fd, scope);
    /* step 6 */
    MakeMethod(closure, object);
    /* step 7 */
    SetFunctionName(closure, propKey);
    /* steps 8-9 (generated code) */
    return closure;
}
Also used : OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction)

Example 4 with OrdinaryAsyncFunction

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

the class FunctionOperations method InstantiateAsyncFunctionObject.

/**
 * Extension: Async Function Definitions
 *
 * @param scope
 *            the current lexical scope
 * @param cx
 *            the execution context
 * @param fd
 *            the function runtime info object
 * @return the new async function instance
 */
public static OrdinaryAsyncFunction InstantiateAsyncFunctionObject(LexicalEnvironment<?> scope, ExecutionContext cx, RuntimeInfo.Function fd) {
    /* step 1 (not applicable) */
    /* step 2 */
    String name = fd.functionName();
    /* step 3 */
    OrdinaryAsyncFunction f = AsyncFunctionCreate(cx, FunctionKind.Normal, fd, scope);
    /* step 4 */
    SetFunctionName(f, name);
    /* step 5 */
    return f;
}
Also used : OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction)

Example 5 with OrdinaryAsyncFunction

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

the class FunctionConstructor method CreateDynamicFunction.

/**
 * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
 *
 * @param cx
 *            the execution context
 * @param kind
 *            the function kind
 * @param compiledFunction
 *            the compiled function
 * @param proto
 *            the function prototype
 * @return the new function object
 */
public static FunctionObject CreateDynamicFunction(ExecutionContext cx, SourceKind kind, CompiledFunction compiledFunction, ScriptObject proto) {
    RuntimeInfo.Function function = compiledFunction.getFunction();
    /* step 18 */
    boolean strict = function.isStrict();
    /* step 30 */
    ObjectAllocator<? extends FunctionObject> allocator;
    switch(kind) {
        case AsyncFunction:
            allocator = OrdinaryAsyncFunction::new;
            break;
        case AsyncGenerator:
            allocator = OrdinaryAsyncGenerator::new;
            break;
        case Function:
            if (function.is(RuntimeInfo.FunctionFlags.Legacy)) {
                assert !strict;
                allocator = LegacyConstructorFunction::new;
            } else {
                allocator = OrdinaryConstructorFunction::new;
            }
            break;
        case Generator:
            allocator = OrdinaryGenerator::new;
            break;
        default:
            throw new AssertionError();
    }
    FunctionObject f = FunctionAllocate(cx, allocator, proto, strict, FunctionKind.Normal);
    /* steps 31-32 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 33 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, compiledFunction);
    /* steps 34-36 */
    switch(kind) {
        case AsyncFunction:
            /* step 36 */
            break;
        case AsyncGenerator:
            {
                OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.AsyncGeneratorPrototype);
                f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
                break;
            }
        case Function:
            /* step 35 */
            if (f instanceof LegacyConstructorFunction) {
                MakeConstructor(cx, (LegacyConstructorFunction) f);
            } else {
                MakeConstructor(cx, (OrdinaryConstructorFunction) f);
            }
            break;
        case Generator:
            {
                /* step 34 */
                OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
                f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
                break;
            }
        default:
            throw new AssertionError();
    }
    /* step 37 */
    SetFunctionName(f, "anonymous");
    /* step 38 */
    return f;
}
Also used : RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) OrdinaryConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction) OrdinaryAsyncGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncGenerator) OrdinaryGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryGenerator) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) Property(com.github.anba.es6draft.runtime.types.Property) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

Aggregations

OrdinaryAsyncFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction)8 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)2 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)2 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)2 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)2 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)2 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 ParserException (com.github.anba.es6draft.parser.ParserException)1 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)1 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)1 Source (com.github.anba.es6draft.runtime.internal.Source)1 FunctionConstructor.functionSource (com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource)1 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)1 Property (com.github.anba.es6draft.runtime.types.Property)1 LegacyConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)1 OrdinaryAsyncGenerator (com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncGenerator)1 OrdinaryConstructorFunction (com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorFunction)1 OrdinaryGenerator (com.github.anba.es6draft.runtime.types.builtins.OrdinaryGenerator)1