Search in sources :

Example 36 with Name

use of com.github.anba.es6draft.ast.scope.Name in project es6draft by anba.

the class StatementGenerator method visit.

@Override
public Completion visit(ClassFieldInitializer node, CodeVisitor mv) {
    ClassFieldDefinition field = node.getField();
    // stack: [] -> [fieldName]
    ValType type = expressionBoxed(node.getFieldName(), mv);
    // stack: [fieldName] -> [fieldName, value]
    Expression initializer = field.getInitializer();
    ClassFieldScope scope = node.getScope();
    assert (initializer != null) == (scope != null);
    if (initializer != null) {
        mv.enterScope(node);
        if (!scope.isPresent()) {
            expressionBoxed(initializer, mv);
        } else {
            Variable<LexicalEnvironment<?>> env = saveEnvironment(mv);
            newClassFieldInitializerEnvironment(env, mv);
            if (!scope.varDeclaredNames().isEmpty()) {
                Variable<DeclarativeEnvironmentRecord> classFieldEnvRec = mv.newVariable("classFieldEnvRec", DeclarativeEnvironmentRecord.class);
                getVariableEnvironmentRecord(classFieldEnvRec, mv);
                for (Name varName : scope.varDeclaredNames()) {
                    BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(classFieldEnvRec, varName);
                    op.createMutableBinding(classFieldEnvRec, varName, false, mv);
                    op.initializeBinding(classFieldEnvRec, varName, mv.undefinedValue(), mv);
                }
            }
            expressionBoxed(initializer, mv);
            setVariableAndLexicalEnvironment(env, mv);
        }
        mv.exitScope();
        if (IsAnonymousFunctionDefinition(initializer)) {
            SetFunctionName(initializer, type, mv);
        }
    } else {
        mv.loadUndefined();
    }
    mv.loadExecutionContext();
    mv.lineInfo(node);
    mv.invoke(Methods.ClassOperations_DefineField);
    return Completion.Normal;
}
Also used : ClassFieldScope(com.github.anba.es6draft.ast.scope.ClassFieldScope) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 37 with Name

use of com.github.anba.es6draft.ast.scope.Name 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 */
            if (catchParameter != null) {
                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);
        // stack: [ex] -> []
        if (catchParameter != null) {
            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)

Example 38 with Name

use of com.github.anba.es6draft.ast.scope.Name in project es6draft by anba.

the class StatementGenerator method visit.

/**
 * 13.7.4 The for Statement
 * <p>
 * 13.1.8 Runtime Semantics: Evaluation<br>
 * 13.1.7 Runtime Semantics: LabelledEvaluation<br>
 * 13.7.4.7 Runtime Semantics: LabelledEvaluation
 */
@Override
public Completion visit(ForStatement node, CodeVisitor mv) {
    assert mv.getStackSize() == 0;
    boolean perIterationsLets = false;
    BlockScope scope = node.getScope();
    Node head = node.getHead();
    if (head == null) {
    // empty
    } else if (head instanceof Expression) {
        ValType type = expression(((Expression) head).emptyCompletion(), mv);
        mv.pop(type);
    } else if (head instanceof VariableStatement) {
        head.accept(this, mv);
    } else {
        assert head instanceof LexicalDeclaration;
        LexicalDeclaration lexDecl = (LexicalDeclaration) head;
        List<Name> boundNames = BoundNames(lexDecl);
        boolean isConst = IsConstantDeclaration(lexDecl);
        perIterationsLets = !isConst && !boundNames.isEmpty();
        if (scope.isPresent()) {
            mv.enterVariableScope();
            Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            // stack: [] -> [loopEnv]
            newDeclarativeEnvironment(scope, mv);
            // stack: [loopEnv] -> [loopEnv]
            getEnvRec(envRec, mv);
            // stack: [loopEnv] -> [loopEnv]
            for (Name dn : boundNames) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, dn);
                if (isConst) {
                    op.createImmutableBinding(envRec, dn, true, mv);
                } else {
                    op.createMutableBinding(envRec, dn, false, mv);
                }
            }
            // stack: [loopEnv] -> []
            pushLexicalEnvironment(mv);
            mv.exitVariableScope();
        }
        mv.enterScope(node);
        lexDecl.accept(this, mv);
    }
    Completion result = ForBodyEvaluation(node, perIterationsLets, mv);
    if (head instanceof LexicalDeclaration) {
        mv.exitScope();
        if (scope.isPresent() && !result.isAbrupt()) {
            popLexicalEnvironment(mv);
        }
    }
    return result;
}
Also used : InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 39 with Name

use of com.github.anba.es6draft.ast.scope.Name in project es6draft by anba.

the class StatementGenerator method ForInOfHeadEvaluation.

/**
 * 13.7.5.12 Runtime Semantics: ForIn/OfHeadEvaluation (TDZnames, expr, iterationKind, labelSet)
 * <p>
 * stack: [] {@literal ->} [Iterator]
 *
 * @param <FORSTATEMENT>
 *            the for-statement node type
 * @param node
 *            the for-statement node
 * @param iterationKind
 *            the for-statement's iteration kind
 * @param lblFail
 *            the target instruction if the expression node does not produce an object type
 * @param mv
 *            the code visitor
 * @return the value type of the expression
 */
private <FORSTATEMENT extends IterationStatement & ForIterationNode> ValType ForInOfHeadEvaluation(FORSTATEMENT node, IterationKind iterationKind, Jump lblFail, CodeVisitor mv) {
    /* steps 1-2 */
    BlockScope scope = node.getScope();
    Node lhs = node.getHead();
    List<Name> tdzNames = null;
    if (lhs instanceof LexicalDeclaration) {
        tdzNames = BoundNames(forDeclarationBinding((LexicalDeclaration) lhs));
        assert scope.isPresent() == !tdzNames.isEmpty();
        if (scope.isPresent()) {
            mv.enterVariableScope();
            Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            // stack: [] -> [TDZ]
            newDeclarativeEnvironment(scope, mv);
            // stack: [TDZ] -> [TDZ]
            getEnvRec(envRec, mv);
            // stack: [TDZ] -> [TDZ]
            for (Name name : tdzNames) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
                op.createMutableBinding(envRec, name, false, mv);
            }
            mv.exitVariableScope();
            // stack: [TDZ] -> []
            pushLexicalEnvironment(mv);
        }
        mv.enterScope(node);
    }
    /* steps 3, 5-6 */
    Expression expr = node.getExpression();
    ValType type = expressionBoxed(expr, mv);
    /* step 4 */
    if (tdzNames != null) {
        mv.exitScope();
        if (scope.isPresent()) {
            popLexicalEnvironment(mv);
        }
    }
    /* steps 7-8 */
    if (iterationKind == IterationKind.Enumerate) {
        /* step 7.a */
        if (type != ValType.Object) {
            Jump loopstart = new Jump();
            mv.dup();
            isUndefinedOrNull(mv);
            mv.ifeq(loopstart);
            mv.pop();
            mv.goTo(lblFail);
            mv.mark(loopstart);
        }
        /* steps 7.b-c */
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_enumerate);
    } else if (iterationKind == IterationKind.AsyncIterate) {
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_asyncIterate);
    } else {
        /* step 8 */
        assert iterationKind == IterationKind.Iterate;
        mv.loadExecutionContext();
        mv.lineInfo(expr);
        mv.invoke(Methods.IteratorOperations_iterate);
    }
    return type;
}
Also used : BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 40 with Name

use of com.github.anba.es6draft.ast.scope.Name in project es6draft by anba.

the class StatementGenerator method visit.

/**
 * 13.3.1 Let and Const Declarations
 * <p>
 * 13.3.1.4 Runtime Semantics: Evaluation
 */
@Override
public Completion visit(LexicalDeclaration node, CodeVisitor mv) {
    mv.enterVariableScope();
    Class<? extends EnvironmentRecord> envRecClass = getEnvironmentRecordClass(mv);
    Variable<? extends EnvironmentRecord> envRec = mv.newVariable("envRec", envRecClass);
    getLexicalEnvironmentRecord(envRec, mv);
    /* steps 1-2 */
    for (LexicalBinding lexical : node.getElements()) {
        Binding binding = lexical.getBinding();
        Expression initializer = lexical.getInitializer();
        if (initializer == null) {
            // LexicalBinding : BindingIdentifier
            assert binding instanceof BindingIdentifier;
            Name name = ((BindingIdentifier) binding).getName();
            /* steps 1-2 */
            assert mv.getScope().isDeclared(name);
            InitializeBoundNameWithUndefined(envRec, name, mv);
        } else if (binding instanceof BindingIdentifier) {
            // LexicalBinding : BindingIdentifier Initializer
            Name name = ((BindingIdentifier) binding).getName();
            /* steps 1-7 */
            InitializeBoundNameWithInitializer(codegen, envRec, name, initializer, mv);
        } else {
            // LexicalBinding : BindingPattern Initializer
            assert binding instanceof BindingPattern;
            /* steps 1-3 */
            expressionBoxed(initializer, mv);
            /* steps 4-5 */
            BindingInitialization(codegen, envRec, (BindingPattern) binding, mv);
        }
    }
    mv.exitVariableScope();
    /* step 3 */
    return Completion.Normal;
}
Also used : 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

Name (com.github.anba.es6draft.ast.scope.Name)49 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)31 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)24 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)16 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)13 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)13 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)12 Declaration (com.github.anba.es6draft.ast.Declaration)11 Jump (com.github.anba.es6draft.compiler.assembler.Jump)11 HashSet (java.util.HashSet)11 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)10 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)10 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)9 InitializeBoundName (com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName)9 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)8 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)8 ArrayDeque (java.util.ArrayDeque)8 IdentifierName (com.github.anba.es6draft.ast.IdentifierName)7 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)6 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)6