Search in sources :

Example 21 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class ComprehensionGenerator method visit.

/**
     * Runtime Semantics: ComprehensionEvaluation
     */
@Override
public Void visit(LegacyComprehension node, CodeVisitor mv) {
    BlockScope scope = node.getScope();
    if (scope.isPresent()) {
        mv.enterVariableScope();
        Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
        Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
        newDeclarativeEnvironment(scope, mv);
        mv.store(env);
        getEnvRec(env, envRec, mv);
        for (Name name : LexicallyDeclaredNames(node.getScope())) {
            BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
            op.createMutableBinding(envRec, name, false, mv);
            InitializeBoundNameWithUndefined(envRec, name, mv);
        }
        mv.load(env);
        pushLexicalEnvironment(mv);
        mv.exitVariableScope();
    }
    mv.enterScope(node);
    visit((Comprehension) node, mv);
    mv.exitScope();
    if (scope.isPresent()) {
        popLexicalEnvironment(mv);
    }
    return null;
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 22 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class ExpressionGenerator method visit.

/**
     * Extension: 'let' expression
     */
@Override
public ValType visit(LetExpression node, CodeVisitor mv) {
    BlockScope scope = node.getScope();
    if (scope.isPresent()) {
        mv.enterVariableScope();
        Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
        Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
        newDeclarativeEnvironment(scope, mv);
        mv.store(env);
        getEnvRec(env, envRec, mv);
        for (LexicalBinding lexical : node.getBindings()) {
            Binding binding = lexical.getBinding();
            Expression initializer = lexical.getInitializer();
            for (Name name : BoundNames(binding)) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
                op.createMutableBinding(envRec, name, false, mv);
            }
            if (initializer == null) {
                // LexicalBinding : BindingIdentifier
                assert binding instanceof BindingIdentifier;
                Name name = ((BindingIdentifier) binding).getName();
                /* steps 1-2 */
                // stack: [] -> []
                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 */
                BindingInitializationGenerator.BindingInitialization(codegen, envRec, (BindingPattern) binding, mv);
            }
        }
        mv.load(env);
        pushLexicalEnvironment(mv);
        mv.exitVariableScope();
    }
    mv.enterScope(node);
    ValType type = node.getExpression().accept(this, mv);
    mv.exitScope();
    if (scope.isPresent()) {
        popLexicalEnvironment(mv);
    }
    return type;
}
Also used : ValType(com.github.anba.es6draft.compiler.DefaultCodeGenerator.ValType) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) FieldName(com.github.anba.es6draft.compiler.assembler.FieldName) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 23 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class CodeGenerator method compile.

MethodName compile(BlockStatement node, List<Declaration> declarations, BlockDeclarationInstantiationGenerator generator) {
    MethodCode method = newMethod2(node);
    BlockDeclInitVisitor body = new BlockDeclInitVisitor(method);
    body.lineInfo(node);
    body.begin();
    Variable<ExecutionContext> cx = body.getExecutionContext();
    Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = body.getLexicalEnvironment();
    generator.generateMethod(declarations, cx, env, body);
    body._return();
    body.end();
    return methodDesc(node, method.methodName);
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) MethodCode(com.github.anba.es6draft.compiler.assembler.Code.MethodCode)

Example 24 with LexicalEnvironment

use of com.github.anba.es6draft.runtime.LexicalEnvironment in project es6draft by anba.

the class StatementGenerator method visitTryFinally.

/**
 * 13.15.8 Runtime Semantics: Evaluation<br>
 *
 * <code>try-finally</code>
 *
 * @param node
 *            the try-statement
 * @param mv
 *            the code visitor
 * @return the completion value
 */
private Completion visitTryFinally(TryStatement node, CodeVisitor mv) {
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    TryCatchLabel handlerFinallyStackOverflow = new TryCatchLabel();
    Jump noException = new Jump();
    mv.enterVariableScope();
    Variable<LexicalEnvironment<?>> savedEnv = saveEnvironment(mv);
    MutableValue<Object> completion = mv.enterFinallyScoped(node);
    /* step 1 */
    // Emit try-block
    mv.mark(startFinally);
    Completion tryResult = emitTryBlock(node, noException, mv);
    mv.mark(endFinally);
    // Restore temporary abrupt targets
    List<TempLabel> tempLabels = mv.exitFinallyScoped();
    /* step 2 */
    // Emit finally-block
    Completion finallyResult = emitFinallyBlock(node, savedEnv, completion, tryResult, Completion.Abrupt, handlerFinally, handlerFinallyStackOverflow, noException, tempLabels, mv);
    mv.exitVariableScope();
    mv.tryCatch(startFinally, endFinally, handlerFinally, Types.ScriptException);
    mv.tryCatch(startFinally, endFinally, handlerFinallyStackOverflow, Types.Error);
    /* steps 3-6 */
    return finallyResult.then(tryResult);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) Jump(com.github.anba.es6draft.compiler.assembler.Jump) TempLabel(com.github.anba.es6draft.compiler.Labels.TempLabel)

Example 25 with LexicalEnvironment

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

Aggregations

LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)32 Name (com.github.anba.es6draft.ast.scope.Name)16 Jump (com.github.anba.es6draft.compiler.assembler.Jump)16 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)15 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)15 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)15 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)12 Declaration (com.github.anba.es6draft.ast.Declaration)10 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)10 HashSet (java.util.HashSet)10 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)9 BreakLabel (com.github.anba.es6draft.compiler.Labels.BreakLabel)8 ArrayDeque (java.util.ArrayDeque)8 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)7 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)7 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)6 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)6 ContinueLabel (com.github.anba.es6draft.compiler.Labels.ContinueLabel)6 Undefined (com.github.anba.es6draft.runtime.types.Undefined)6 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)5