Search in sources :

Example 1 with TopLevelScope

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

the class ExpressionGenerator method isGlobalThis.

private static boolean isGlobalThis(Scope currentScope) {
    for (Scope scope = currentScope; ; ) {
        TopLevelScope top = scope.getTop();
        TopLevelNode<?> node = top.getNode();
        if (node instanceof Script) {
            return ((Script) node).isGlobalThis();
        }
        if (node instanceof Module) {
            return true;
        }
        assert node instanceof FunctionNode : "class=" + node.getClass();
        if (((FunctionNode) node).getThisMode() != FunctionNode.ThisMode.Lexical) {
            return false;
        }
        scope = top.getEnclosingScope();
    }
}
Also used : WithScope(com.github.anba.es6draft.ast.scope.WithScope) Scope(com.github.anba.es6draft.ast.scope.Scope) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope)

Example 2 with TopLevelScope

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

the class StatementGenerator method visit.

/**
 * 14.1.20 Runtime Semantics: Evaluation
 */
@Override
public Completion visit(FunctionDeclaration node, CodeVisitor mv) {
    /* B.3.3 Block-Level Function Declarations Web Legacy Compatibility Semantics */
    if (node.isLegacyBlockScoped()) {
        Name name = node.getIdentifier().getName();
        TopLevelScope top = mv.getScope().getTop();
        if (mv.isFunction()) {
            assert top instanceof FunctionScope;
            Name varName = ((FunctionScope) top).variableScope().resolveName(name);
            assert varName != null && name != varName;
            /* step 1.a.ii.3.1 */
            Value<DeclarativeEnvironmentRecord> fenv = getVariableEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
            /* steps 1.a.ii.3.5-6 */
            BindingOp.of(fenv, varName).setMutableBinding(fenv, varName, __ -> {
                /* step 1.a.ii.3.2 */
                Value<DeclarativeEnvironmentRecord> benv = getLexicalEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
                /* steps 1.a.ii.3.3-4 */
                BindingOp.of(benv, name).getBindingValue(benv, name, false, mv);
            }, false, mv);
        } else {
            assert top instanceof ScriptScope;
            Name varName = name;
            int functionId = node.getLegacyBlockScopeId();
            Jump isLegacyScoped = null;
            if (functionId > 0) {
                isLegacyScoped = new Jump();
                mv.loadExecutionContext();
                mv.iconst(functionId);
                mv.invoke(Methods.DeclarationOperations_isLegacyBlockFunction);
                mv.ifeq(isLegacyScoped);
            }
            // The variable environment record is either:
            // 1. The global environment record for global (eval) scripts.
            // 2. Or a (function) declarative environment record for eval in functions.
            // 3. Or a script-context environment record for eval in JSR-223 scripting.
            Value<EnvironmentRecord> genv = getVariableEnvironmentRecord(Types.EnvironmentRecord, mv);
            BindingOp.of(genv, varName).setMutableBinding(genv, varName, __ -> {
                Value<DeclarativeEnvironmentRecord> benv = getLexicalEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
                BindingOp.of(benv, name).getBindingValue(benv, name, false, mv);
            }, false, mv);
            if (isLegacyScoped != null) {
                mv.mark(isLegacyScoped);
            }
        }
    }
    /* step 1 */
    return Completion.Normal;
}
Also used : ScriptScope(com.github.anba.es6draft.ast.scope.ScriptScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope) FunctionScope(com.github.anba.es6draft.ast.scope.FunctionScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) 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 3 with TopLevelScope

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

the class ExpressionGenerator method isEnclosedByLexicalDeclaration.

private boolean isEnclosedByLexicalDeclaration(Scope currentScope) {
    final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
    TopLevelScope top = currentScope.getTop();
    for (Scope scope : currentScope) {
        if (scope instanceof BlockScope) {
            if (catchVar) {
                ScopedNode node = scope.getNode();
                if (node instanceof CatchClause && ((CatchClause) node).getCatchParameter() instanceof BindingIdentifier) {
                    continue;
                }
            }
            if (!((BlockScope) scope).lexicallyDeclaredNames().isEmpty()) {
                return true;
            }
        } else if (scope == top) {
            break;
        }
    }
    if (!top.lexicallyDeclaredNames().isEmpty()) {
        TopLevelNode<?> topNode = top.getNode();
        if (topNode instanceof Script) {
            return ((Script) topNode).isEvalScript();
        }
        return true;
    }
    return codegen.isEnabled(Parser.Option.EnclosedByLexicalDeclaration);
}
Also used : WithScope(com.github.anba.es6draft.ast.scope.WithScope) Scope(com.github.anba.es6draft.ast.scope.Scope) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) TopLevelScope(com.github.anba.es6draft.ast.scope.TopLevelScope)

Aggregations

TopLevelScope (com.github.anba.es6draft.ast.scope.TopLevelScope)3 BlockScope (com.github.anba.es6draft.ast.scope.BlockScope)2 Scope (com.github.anba.es6draft.ast.scope.Scope)2 WithScope (com.github.anba.es6draft.ast.scope.WithScope)2 FunctionScope (com.github.anba.es6draft.ast.scope.FunctionScope)1 Name (com.github.anba.es6draft.ast.scope.Name)1 ScriptScope (com.github.anba.es6draft.ast.scope.ScriptScope)1 InitializeBoundName (com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName)1 Jump (com.github.anba.es6draft.compiler.assembler.Jump)1 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)1 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)1 EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)1