Search in sources :

Example 11 with Name

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

the class EvalDeclarationInstantiationGenerator method declareBlockFunctions.

private <ENVREC extends EnvironmentRecord> void declareBlockFunctions(Script evalScript, HashSet<Name> declaredFunctionOrVarNames, Variable<ExecutionContext> context, Variable<LexicalEnvironment<ENVREC>> varEnv, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Variable<ENVREC> varEnvRec, Variable<Undefined> undef, InstructionVisitor mv) {
    final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
    int idCounter = 0;
    List<FunctionDeclaration> blockFunctions = evalScript.getScope().blockFunctions();
    for (FunctionDeclaration f : blockFunctions) {
        Name fn = f.getName();
        Jump next = null;
        if (isEnclosedByLexical(evalScript)) {
            // Runtime check only necessary when enclosed by lexical declarations.
            f.setLegacyBlockScopeId(++idCounter);
            next = new Jump();
            canDeclareVarBinding(varEnv, lexEnv, fn, catchVar, next, mv);
            setLegacyBlockFunction(context, f, mv);
        }
        if (declaredFunctionOrVarNames.add(fn)) {
            BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
            Jump varAlreadyDeclared = new Jump();
            op.hasBinding(varEnvRec, fn, mv);
            mv.ifne(varAlreadyDeclared);
            {
                op.createMutableBinding(varEnvRec, fn, true, mv);
                op.initializeBinding(varEnvRec, fn, undef, mv);
            }
            mv.mark(varAlreadyDeclared);
        }
        if (next != null) {
            mv.mark(next);
        }
    }
}
Also used : FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) Jump(com.github.anba.es6draft.compiler.assembler.Jump) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) IdentifierName(com.github.anba.es6draft.ast.IdentifierName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 12 with Name

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

the class EvalDeclarationInstantiationGenerator method generateStrict.

private void generateStrict(Script evalScript, EvalDeclInitVisitor mv) {
    assert evalScript.isStrict() && !evalScript.isScripting();
    Variable<ExecutionContext> context = mv.getExecutionContext();
    Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> varEnv = mv.newVariable("varEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv = mv.newVariable("lexEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<FunctionObject> fo = null;
    Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
    mv.loadUndefined();
    mv.store(undef);
    getVariableEnvironment(context, varEnv, mv);
    getLexicalEnvironment(context, lexEnv, mv);
    /* step 1 */
    Set<Name> varNames = VarDeclaredNames(evalScript);
    /* step 2 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(evalScript);
    /* step 3 */
    Variable<DeclarativeEnvironmentRecord> lexEnvRec = mv.newVariable("lexEnvRec", DeclarativeEnvironmentRecord.class);
    getEnvironmentRecord(lexEnv, lexEnvRec, mv);
    /* step 4 */
    Variable<DeclarativeEnvironmentRecord> varEnvRec = mv.newVariable("varEnvRec", DeclarativeEnvironmentRecord.class);
    getEnvironmentRecord(varEnv, varEnvRec, mv);
    /* step 5 (not applicable) */
    // Extension: Class Fields
    checkUndeclaredPrivateNames(evalScript, context, mv);
    /* step 6 */
    ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
    /* step 7 */
    HashSet<Name> declaredFunctionNames = new HashSet<>();
    /* step 8 */
    if (findFunctionDeclarations(varDeclarations, functionsToInitialize, declaredFunctionNames)) {
        fo = mv.newVariable("fo", FunctionObject.class);
    }
    /* step 9 */
    LinkedHashSet<Name> declaredVarNames = new LinkedHashSet<>(varNames);
    /* step 10 */
    declaredVarNames.removeAll(declaredFunctionNames);
    // ES2016: Block-scoped global function declarations
    assert !hasBlockFunctions(evalScript);
    /* step 12 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(evalScript);
    /* step 13 */
    createLexicalDeclarations(lexDeclarations, lexEnvRec, mv);
    /* step 14 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        // stack: [] -> []
        InstantiateFunctionObject(context, lexEnv, f, mv);
        mv.store(fo);
        // Early error semantics ensure that fn does not already exist in varEnvRec.
        BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, fn);
        op.createMutableBinding(varEnvRec, fn, true, mv);
        op.initializeBinding(varEnvRec, fn, fo, mv);
    }
    /* step 15 */
    for (Name vn : declaredVarNames) {
        // Early error semantics ensure that vn does not already exist in varEnvRec.
        BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, vn);
        op.createMutableBinding(varEnvRec, vn, true, mv);
        op.initializeBinding(varEnvRec, vn, undef, mv);
    }
    /* step 16 */
    mv._return();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Undefined(com.github.anba.es6draft.runtime.types.Undefined) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ArrayDeque(java.util.ArrayDeque) IdentifierName(com.github.anba.es6draft.ast.IdentifierName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) StatementListItem(com.github.anba.es6draft.ast.StatementListItem) FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) VariableDeclaration(com.github.anba.es6draft.ast.VariableDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 13 with Name

use of com.github.anba.es6draft.ast.scope.Name 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 14 with Name

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

the class StatementGenerator method ForInOfBodyEvaluationInner.

private <FORSTATEMENT extends IterationStatement & ForIterationNode> Completion ForInOfBodyEvaluationInner(FORSTATEMENT node, Variable<Object> nextValue, CodeVisitor mv) {
    BlockScope scope = node.getScope();
    Node lhs = node.getHead();
    /* steps 5.f-j */
    if (lhs instanceof Expression) {
        /* steps 5.f, 5.h-j */
        LeftHandSideExpression lhsExpr = (LeftHandSideExpression) lhs;
        if (!(lhsExpr instanceof AssignmentPattern)) {
            ReferenceOp<LeftHandSideExpression> op = ReferenceOp.of(lhsExpr);
            /* step 5.f.i.1 */
            // stack: [] -> [<ref>]
            ValType ref = op.reference(lhsExpr, mv, codegen);
            /* steps 5.h.i, 5.h.iii */
            // stack: [<ref>] -> []
            mv.load(nextValue);
            op.putValue(lhsExpr, ref, ValType.Any, mv);
        } else {
            /* step 5.i.i */
            mv.load(nextValue);
            DestructuringAssignment(codegen, (AssignmentPattern) lhs, mv);
        }
    } else if (lhs instanceof VariableStatement) {
        /* steps 5.f, 5.h-j */
        Binding binding = forVarDeclarationBinding((VariableStatement) lhs);
        if (binding instanceof BindingIdentifier) {
            BindingIdentifier bindingId = (BindingIdentifier) binding;
            /* step 5.f.i.1 */
            // 13.7.5.14 Runtime Semantics: Evaluation
            IdReferenceOp op = IdReferenceOp.of(bindingId);
            op.resolveBinding(bindingId, mv);
            /* steps 5.h.i, 5.h.iii */
            // stack: [<ref>] -> []
            mv.load(nextValue);
            op.putValue(bindingId, ValType.Any, mv);
        } else {
            /* step 5.i.ii */
            mv.load(nextValue);
            BindingInitialization(codegen, (BindingPattern) binding, mv);
        }
    } else {
        /* steps 5.g-j */
        Binding binding = forDeclarationBinding((LexicalDeclaration) lhs);
        Variable<DeclarativeEnvironmentRecord> envRec = null;
        if (scope.isPresent()) {
            mv.enterVariableScope();
            envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
            newDeclarativeEnvironment(scope, mv);
            getEnvRec(envRec, mv);
            BindingInstantiation(envRec, (LexicalDeclaration) lhs, mv);
            pushLexicalEnvironment(mv);
        }
        mv.enterScope(node);
        /* step 5.h */
        if (binding instanceof BindingIdentifier) {
            /* step 5.h.ii */
            BindingIdentifier bindingId = (BindingIdentifier) binding;
            Name name = bindingId.getName();
            BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
            op.initializeBinding(envRec, name, nextValue, mv);
        } else {
            /* step 5.i.iii */
            // 13.7.5.9 Runtime Semantics: BindingInitialization
            BindingInitialization(codegen, envRec, (BindingPattern) binding, nextValue, mv);
        }
        if (scope.isPresent()) {
            mv.exitVariableScope();
        }
    }
    /* step 5.k */
    Completion result = node.getStatement().accept(this, mv);
    /* step 5.l */
    if (lhs instanceof LexicalDeclaration) {
        mv.exitScope();
        if (scope.isPresent() && !result.isAbrupt()) {
            popLexicalEnvironment(mv);
        }
    }
    return result;
}
Also used : Variable(com.github.anba.es6draft.compiler.assembler.Variable) 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)

Example 15 with Name

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

the class DeclarationBindingInstantiation method GlobalDeclarationInstantiation.

/**
 * 15.1.11 Runtime Semantics: GlobalDeclarationInstantiation (script, env)
 *
 * @param cx
 *            the execution context
 * @param script
 *            the global script to instantiate
 * @param env
 *            the global environment
 */
public static void GlobalDeclarationInstantiation(ExecutionContext cx, Script script, LexicalEnvironment<GlobalEnvironmentRecord> env) {
    /* steps 1-2 */
    GlobalEnvironmentRecord envRec = env.getEnvRec();
    /* step 3 */
    assert LexicallyDeclaredNames(script).isEmpty();
    /* step 4 */
    Set<Name> varNames = VarDeclaredNames(script);
    /* step 6 */
    for (Name name : varNames) {
        DeclarationOperations.canDeclareVarScopedOrThrow(cx, envRec, name.getIdentifier());
    }
    /* step 7 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(script);
    /* steps 8-10 (not applicable) */
    /* step 11 */
    LinkedHashSet<Name> declaredVarNames = new LinkedHashSet<>();
    /* step 12 */
    for (StatementListItem d : varDeclarations) {
        assert d instanceof VariableStatement;
        for (Name vn : BoundNames((VariableStatement) d)) {
            DeclarationOperations.canDeclareGlobalVarOrThrow(cx, envRec, vn.getIdentifier());
            declaredVarNames.add(vn);
        }
    }
    /* step 18 */
    for (Name vn : declaredVarNames) {
        envRec.createGlobalVarBinding(vn.getIdentifier(), false);
    }
/* step 19 (return) */
}
Also used : LinkedHashSet(java.util.LinkedHashSet) VariableStatement(com.github.anba.es6draft.ast.VariableStatement) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) StatementListItem(com.github.anba.es6draft.ast.StatementListItem) 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