Search in sources :

Example 6 with Variable

use of com.github.anba.es6draft.compiler.assembler.Variable in project es6draft by anba.

the class DefaultCodeGenerator method asyncIteratorClose.

/**
     * Emit:
     * 
     * <pre>
     * Callable returnMethod = GetMethod(cx, iterator, "return");
     * if (returnMethod != null) {
     *   try {
     *     returnMethod.call(cx, iterator);
     *     await;
     *   } catch (ScriptException e) {
     *     if (throwable != e) {
     *       throwable.addSuppressed(e);
     *     }
     *   }
     * }
     * </pre>
     * 
     * @param node
     *            the ast node
     * @param iterator
     *            the script iterator object
     * @param mv
     *            the code visitor
     */
final void asyncIteratorClose(Node node, Variable<ScriptObject> iterator, Variable<? extends Throwable> throwable, CodeVisitor mv) {
    IteratorClose(node, iterator, returnMethod -> {
        TryCatchLabel startCatch = new TryCatchLabel();
        TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
        Jump noException = new Jump();
        mv.mark(startCatch);
        {
            InvokeMethod(node, mv, returnMethod, iterator);
            await(node, mv);
            mv.pop();
            mv.goTo(noException);
        }
        mv.mark(endCatch);
        mv.catchHandler(handlerCatch, Types.ScriptException);
        {
            mv.enterVariableScope();
            Variable<ScriptException> exception = mv.newVariable("exception", ScriptException.class);
            mv.store(exception);
            mv.load(throwable);
            mv.load(exception);
            mv.ifacmpeq(noException);
            {
                mv.load(throwable);
                mv.load(exception);
                mv.invoke(Methods.Throwable_addSuppressed);
            }
            mv.exitVariableScope();
        }
        mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
        mv.mark(noException);
    }, mv);
}
Also used : ScriptException(com.github.anba.es6draft.runtime.internal.ScriptException) Variable(com.github.anba.es6draft.compiler.assembler.Variable) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 7 with Variable

use of com.github.anba.es6draft.compiler.assembler.Variable in project es6draft by anba.

the class ExpressionGenerator method EvaluateDirectCallEval.

/**
     * [12.3.4.3 Runtime Semantics: EvaluateDirectCall( func, thisValue, arguments, tailPosition )]
     * 
     * @param call
     *            the function call expression
     * @param arguments
     *            the function arguments
     * @param hasThisValue
     *            {@code true} if the thisValue is on the stack
     * @param mv
     *            the code visitor
     */
private ValType EvaluateDirectCallEval(Expression call, List<Expression> arguments, boolean hasThisValue, CodeVisitor mv) {
    Jump afterCall = new Jump(), notEval = new Jump();
    if (hasThisValue) {
        // stack: [func, thisValue] -> [thisValue, func]
        mv.swap();
    }
    /* steps 1-2 (EvaluateDirectCall) */
    // stack: [thisValue?, func] -> [thisValue?, args?, func]
    boolean constantArguments = hasConstantArguments(arguments);
    if (!constantArguments) {
        ArgumentListEvaluation(call, arguments, mv);
        mv.swap();
    }
    // Emit line info after evaluating arguments.
    mv.lineInfo(call);
    /* steps 3-4 (EvaluateDirectCall) */
    // stack: [thisValue?, args?, func] -> [thisValue?, args?, func(Callable)]
    mv.loadExecutionContext();
    mv.invoke(Methods.ScriptRuntime_CheckCallable);
    // stack: [thisValue?, args?, func(Callable)] -> [thisValue?, args?, func(Callable)]
    mv.dup();
    mv.loadExecutionContext();
    mv.invoke(Methods.ScriptRuntime_IsBuiltinEval);
    mv.ifeq(notEval);
    {
        PerformEval(call, arguments, hasThisValue, afterCall, mv);
    }
    mv.mark(notEval);
    // stack: [thisValue?, args?, func(Callable)] -> [func(Callable), cx, thisValue, args]
    if (constantArguments) {
        if (hasThisValue) {
            // stack: [thisValue, func(Callable)] -> [...]
            mv.loadExecutionContext();
            mv.swap1_2();
            ArgumentListEvaluation(call, arguments, mv);
        } else {
            // stack: [func(Callable)] -> [...]
            mv.loadExecutionContext();
            mv.loadUndefined();
            ArgumentListEvaluation(call, arguments, mv);
        }
    } else {
        if (hasThisValue) {
            // stack: [thisValue, args, func(Callable)] -> [...]
            mv.loadExecutionContext();
            mv.swap2();
        } else {
            // stack: [args, func(Callable)] -> [...]
            mv.swap();
            mv.loadExecutionContext();
            mv.loadUndefined();
            mv.swap1_2();
        }
    }
    if (codegen.isEnabled(CompatibilityOption.Realm)) {
        // direct-eval fallback hook
        Jump noEvalHook = new Jump();
        mv.loadExecutionContext();
        mv.invoke(Methods.ScriptRuntime_directEvalFallbackHook);
        mv.ifnull(noEvalHook);
        {
            // stack: [func(Callable), cx, thisValue, args] -> [args']
            mv.invoke(Methods.ScriptRuntime_directEvalFallbackArguments);
            // stack: [args'] -> []
            Variable<Object[]> fallbackArguments = mv.newScratchVariable(Object[].class);
            mv.store(fallbackArguments);
            // stack: [] -> [func(Callable), cx, thisValue, args']
            mv.loadExecutionContext();
            mv.invoke(Methods.ScriptRuntime_directEvalFallbackHook);
            mv.loadExecutionContext();
            mv.loadExecutionContext();
            mv.invoke(Methods.ScriptRuntime_directEvalFallbackThisArgument);
            mv.load(fallbackArguments);
            mv.freeVariable(fallbackArguments);
        }
        mv.mark(noEvalHook);
    }
    /* steps 5-9 (EvaluateDirectCall) */
    if (isTailCall(call, mv)) {
        // stack: [func(Callable), cx, thisValue, args'] -> [<func(Callable), thisValue, args>]
        mv.invoke(Methods.ScriptRuntime_PrepareForTailCall);
    } else {
        // stack: [func(Callable), cx, thisValue, args] -> [result]
        invokeDynamicCall(mv);
    }
    mv.mark(afterCall);
    return ValType.Any;
}
Also used : Variable(com.github.anba.es6draft.compiler.assembler.Variable) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 8 with Variable

use of com.github.anba.es6draft.compiler.assembler.Variable in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionEvaluation
     */
@Override
public Void visit(Comprehension node, CodeVisitor mv) {
    ArrayList<Node> list = new ArrayList<>(node.getList().size() + 1);
    list.addAll(node.getList());
    list.add(node.getExpression());
    elements = list.iterator();
    // Create variables early so they'll appear next to each other in the local variable map.
    ArrayList<Variable<ScriptIterator<?>>> iters = new ArrayList<>();
    for (ComprehensionQualifier e : node.getList()) {
        if (e instanceof ComprehensionFor || e instanceof LegacyComprehensionFor) {
            Variable<ScriptIterator<?>> iter = mv.newVariable("iter", ScriptIterator.class).uncheckedCast();
            iters.add(iter);
        }
    }
    iterators = iters.iterator();
    // Start generating code.
    elements.next().accept(this, mv);
    return null;
}
Also used : LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) ComprehensionFor(com.github.anba.es6draft.ast.ComprehensionFor) Variable(com.github.anba.es6draft.compiler.assembler.Variable) Node(com.github.anba.es6draft.ast.Node) ComprehensionQualifier(com.github.anba.es6draft.ast.ComprehensionQualifier) ArrayList(java.util.ArrayList) LegacyComprehensionFor(com.github.anba.es6draft.ast.LegacyComprehensionFor) ScriptIterator(com.github.anba.es6draft.runtime.internal.ScriptIterator)

Example 9 with Variable

use of com.github.anba.es6draft.compiler.assembler.Variable in project es6draft by anba.

the class StatementGenerator method enterCatchScope.

private void enterCatchScope(CatchClause node, CodeVisitor mv) {
    BlockScope scope = node.getScope();
    Binding catchParameter = node.getCatchParameter();
    /* steps 1-6 */
    // stack: [e] -> []
    mv.enterVariableScope();
    {
        Variable<Object> exception = mv.newVariable("exception", Object.class);
        mv.invoke(Methods.ScriptException_getValue);
        mv.store(exception);
        /* step 1 (not applicable) */
        /* steps 2-4 */
        Variable<DeclarativeEnvironmentRecord> envRec = null;
        if (scope.isPresent()) {
            /* step 2 */
            // stack: [] -> [catchEnv]
            newCatchEnvironment(catchParameter, scope, mv);
            envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            getEnvRec(envRec, mv);
            /* step 3 */
            for (Name name : BoundNames(catchParameter)) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
                op.createMutableBinding(envRec, name, false, mv);
            }
            /* step 4 */
            // stack: [catchEnv] -> []
            pushLexicalEnvironment(mv);
        }
        mv.enterScope(node);
        /* steps 5-6 */
        // stack: [ex] -> []
        BindingInitialization(codegen, envRec, catchParameter, exception, mv);
    }
    mv.exitVariableScope();
}
Also used : Variable(com.github.anba.es6draft.compiler.assembler.Variable) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Aggregations

Variable (com.github.anba.es6draft.compiler.assembler.Variable)9 Jump (com.github.anba.es6draft.compiler.assembler.Jump)6 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)3 Name (com.github.anba.es6draft.ast.scope.Name)3 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)3 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)3 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)3 InitializeBoundName (com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName)2 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)2 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)2 TempLabel (com.github.anba.es6draft.compiler.Labels.TempLabel)2 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)2 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)2 ScriptIterator (com.github.anba.es6draft.runtime.internal.ScriptIterator)2 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)2 ArrayList (java.util.ArrayList)2 com.github.anba.es6draft.ast (com.github.anba.es6draft.ast)1 Abrupt (com.github.anba.es6draft.ast.AbruptNode.Abrupt)1 ComprehensionFor (com.github.anba.es6draft.ast.ComprehensionFor)1