Search in sources :

Example 6 with Declaration

use of com.github.anba.es6draft.ast.Declaration in project es6draft by anba.

the class EvalDeclarationInstantiationGenerator method generateGlobal.

private void generateGlobal(Script evalScript, InstructionVisitor mv) {
    assert evalScript.isGlobalCode() && !evalScript.isStrict() && !evalScript.isScripting();
    Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<LexicalEnvironment<GlobalEnvironmentRecord>> varEnv = mv.newVariable("varEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv = mv.newVariable("lexEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<FunctionObject> fo = null;
    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);
    /* step 4 */
    Variable<GlobalEnvironmentRecord> varEnvRec = mv.newVariable("varEnvRec", GlobalEnvironmentRecord.class);
    getEnvironmentRecord(varEnv, varEnvRec, mv);
    /* step 5 */
    if (!varNames.isEmpty()) {
        /* step 5.a */
        // Iterate over declarations to be able to emit line-info entries.
        HashSet<Name> checkedVarNames = new HashSet<>();
        for (StatementListItem item : VarScopedDeclarations(evalScript)) {
            if (item instanceof VariableStatement) {
                for (VariableDeclaration vd : ((VariableStatement) item).getElements()) {
                    for (Name name : BoundNames(vd)) {
                        if (checkedVarNames.add(name)) {
                            canDeclareVarScopedOrThrow(context, varEnvRec, vd, name, mv);
                        }
                    }
                }
            } else {
                HoistableDeclaration d = (HoistableDeclaration) item;
                Name name = BoundName(d);
                if (checkedVarNames.add(name)) {
                    canDeclareVarScopedOrThrow(context, varEnvRec, d, name, mv);
                }
            }
        }
        /* steps 5.b-d */
        if (isEnclosedByLexicalOrHasVarForOf(evalScript)) {
            checkLexicalRedeclaration(evalScript, context, varEnv, lexEnv, varNames, mv);
        }
    }
    /* step 6 */
    ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
    /* step 7 */
    HashSet<Name> declaredFunctionNames = new HashSet<>();
    /* step 8 */
    for (StatementListItem item : reverse(varDeclarations)) {
        if (item instanceof HoistableDeclaration) {
            HoistableDeclaration d = (HoistableDeclaration) item;
            Name fn = BoundName(d);
            if (declaredFunctionNames.add(fn)) {
                canDeclareGlobalFunctionOrThrow(context, varEnvRec, d, fn, mv);
                functionsToInitialize.addFirst(d);
            }
        }
    }
    if (!functionsToInitialize.isEmpty()) {
        fo = mv.newVariable("fo", FunctionObject.class);
    }
    /* step 9 */
    LinkedHashMap<Name, VariableDeclaration> declaredVarNames = new LinkedHashMap<>();
    /* step 10 */
    for (StatementListItem d : varDeclarations) {
        if (d instanceof VariableStatement) {
            for (VariableDeclaration vd : ((VariableStatement) d).getElements()) {
                for (Name vn : BoundNames(vd)) {
                    if (!declaredFunctionNames.contains(vn)) {
                        canDeclareGlobalVarOrThrow(context, varEnvRec, vd, vn, mv);
                        declaredVarNames.put(vn, vd);
                    }
                }
            }
        }
    }
    // ES2016: Block-scoped global function declarations
    if (hasBlockFunctions(evalScript)) {
        final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
        int idCounter = 0;
        List<FunctionDeclaration> blockFunctions = evalScript.getScope().blockFunctions();
        HashSet<Name> declaredFunctionOrVarNames = new HashSet<>();
        declaredFunctionOrVarNames.addAll(declaredFunctionNames);
        declaredFunctionOrVarNames.addAll(declaredVarNames.keySet());
        for (FunctionDeclaration f : blockFunctions) {
            Name fn = f.getName();
            Jump next = new Jump();
            // Runtime check always required for global block-level function declarations.
            f.setLegacyBlockScopeId(++idCounter);
            if (isEnclosedByLexical(evalScript)) {
                canDeclareVarBinding(varEnv, lexEnv, fn, catchVar, next, mv);
            }
            // FIXME: spec issue - avoid (observable!) duplicate checks for same name?
            // FIXME: spec issue - property creation order important?
            canDeclareGlobalFunction(varEnvRec, f, fn, next, mv);
            setLegacyBlockFunction(context, f, mv);
            if (declaredFunctionOrVarNames.add(fn)) {
                createGlobalFunctionBinding(varEnvRec, f, fn, true, mv);
            }
            mv.mark(next);
        }
    }
    /* step 12 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(evalScript);
    /* step 13 */
    if (!lexDeclarations.isEmpty()) {
        getEnvironmentRecord(lexEnv, lexEnvRec, mv);
        createLexicalDeclarations(lexDeclarations, lexEnvRec, mv);
    }
    /* step 14 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        InstantiateFunctionObject(context, lexEnv, f, mv);
        mv.store(fo);
        createGlobalFunctionBinding(varEnvRec, f, fn, fo, true, mv);
    }
    /* step 15 */
    for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
        createGlobalVarBinding(varEnvRec, e.getValue(), e.getKey(), true, mv);
    }
    /* step 16 */
    mv._return();
}
Also used : GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) LinkedHashMap(java.util.LinkedHashMap) FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) VariableDeclaration(com.github.anba.es6draft.ast.VariableDeclaration) 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) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) Jump(com.github.anba.es6draft.compiler.assembler.Jump) ArrayDeque(java.util.ArrayDeque) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) VariableStatement(com.github.anba.es6draft.ast.VariableStatement) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) StatementListItem(com.github.anba.es6draft.ast.StatementListItem) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with Declaration

use of com.github.anba.es6draft.ast.Declaration in project es6draft by anba.

the class BlockDeclarationInstantiationGenerator method generate.

private void generate(List<Declaration> declarations, Variable<ExecutionContext> cx, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env, InstructionVisitor mv) {
    Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
    Variable<FunctionObject> fo = null;
    getEnvironmentRecord(env, envRec, mv);
    /* steps 1-2 */
    for (Declaration d : declarations) {
        if (!(d instanceof HoistableDeclaration)) {
            for (Name dn : BoundNames(d)) {
                BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, dn);
                if (IsConstantDeclaration(d)) {
                    op.createImmutableBinding(envRec, dn, true, mv);
                } else {
                    op.createMutableBinding(envRec, dn, false, mv);
                }
            }
        } else {
            Name fn = BoundName((HoistableDeclaration) d);
            BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, fn);
            op.createMutableBinding(envRec, fn, false, mv);
            InstantiateFunctionObject(cx, env, d, mv);
            if (fo == null) {
                fo = mv.newVariable("fo", FunctionObject.class);
            }
            mv.store(fo);
            op.initializeBinding(envRec, fn, fo, mv);
        }
    }
}
Also used : HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Aggregations

Declaration (com.github.anba.es6draft.ast.Declaration)7 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)7 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)6 Name (com.github.anba.es6draft.ast.scope.Name)5 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)5 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)4 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)4 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)4 HashSet (java.util.HashSet)4 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)3 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)3 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)3 ScriptName (com.github.anba.es6draft.compiler.CodeGenerator.ScriptName)3 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)3 ArrayDeque (java.util.ArrayDeque)3 Jump (com.github.anba.es6draft.compiler.assembler.Jump)2 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)2 Undefined (com.github.anba.es6draft.runtime.types.Undefined)2 LinkedHashMap (java.util.LinkedHashMap)2 LinkedHashSet (java.util.LinkedHashSet)2