Search in sources :

Example 46 with Name

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

the class BlockDeclarationInstantiationGenerator method generate.

private void generate(List<Declaration> declarations, HashSet<Name> instantiatedNames, Variable<ExecutionContext> cx, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env, InstructionVisitor mv) {
    assert declarations.size() <= METHOD_LIMIT : declarations.size();
    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);
            boolean alreadyDeclared = !instantiatedNames.add(fn);
            assert !alreadyDeclared || d instanceof FunctionDeclaration;
            if (!alreadyDeclared) {
                op.createMutableBinding(envRec, fn, false, mv);
            }
            InstantiateFunctionObject(cx, env, d, mv);
            if (fo == null) {
                fo = mv.newVariable("fo", FunctionObject.class);
            }
            mv.store(fo);
            if (!alreadyDeclared) {
                op.initializeBinding(envRec, fn, fo, mv);
            } else {
                op.setMutableBinding(envRec, fn, fo, false, mv);
            }
        }
    }
}
Also used : FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) FunctionDeclaration(com.github.anba.es6draft.ast.FunctionDeclaration) 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)

Example 47 with Name

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

the class ExpressionGenerator method visit.

/**
 * 14.5.16 Runtime Semantics: Evaluation
 */
@Override
public ValType visit(ClassExpression node, CodeVisitor mv) {
    /* steps 1-2 */
    Name className = node.getIdentifier() != null ? node.getIdentifier().getName() : null;
    /* steps 3-4 */
    ClassDefinitionEvaluation(node, className, mv);
    /* step 5 */
    if (className != null) {
        SetFunctionName(node, className, mv);
    }
    /* step 6 */
    return ValType.Object;
}
Also used : MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) FieldName(com.github.anba.es6draft.compiler.assembler.FieldName)

Example 48 with Name

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

the class GlobalDeclarationInstantiationGenerator method generate.

private void generate(Script script, GlobalDeclInitVisitor mv) {
    Variable<ExecutionContext> context = mv.getExecutionContext();
    Variable<LexicalEnvironment<GlobalEnvironmentRecord>> env = mv.newVariable("globalEnv", LexicalEnvironment.class).uncheckedCast();
    Variable<GlobalEnvironmentRecord> envRec = mv.newVariable("envRec", GlobalEnvironmentRecord.class);
    Variable<FunctionObject> fo = null;
    /* steps 1-2 */
    getLexicalEnvironment(context, env, mv);
    getEnvironmentRecord(env, envRec, mv);
    /* step 3 */
    HashSet<Name> lexNames = new HashSet<>();
    /* step 4 */
    HashSet<Name> varNames = new HashSet<>();
    // Iterate over declarations to be able to emit line-info entries.
    for (Declaration d : LexicallyScopedDeclarations(script)) {
        assert !(d instanceof HoistableDeclaration);
        for (Name name : BoundNames(d)) {
            if (lexNames.add(name)) {
                canDeclareLexicalScopedOrThrow(context, envRec, d, name, mv);
            }
        }
    }
    // Iterate over declarations to be able to emit line-info entries.
    for (StatementListItem item : VarScopedDeclarations(script)) {
        if (item instanceof VariableStatement) {
            for (VariableDeclaration vd : ((VariableStatement) item).getElements()) {
                for (Name name : BoundNames(vd)) {
                    if (varNames.add(name)) {
                        canDeclareVarScopedOrThrow(context, envRec, vd, name, mv);
                    }
                }
            }
        } else {
            HoistableDeclaration d = (HoistableDeclaration) item;
            Name name = BoundName(d);
            if (varNames.add(name)) {
                canDeclareVarScopedOrThrow(context, envRec, d, name, mv);
            }
        }
    }
    /* step 7 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(script);
    /* step 8 */
    ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
    /* step 9 */
    HashSet<Name> declaredFunctionNames = new HashSet<>();
    /* step 10 */
    for (StatementListItem item : reverse(varDeclarations)) {
        if (item instanceof HoistableDeclaration) {
            HoistableDeclaration d = (HoistableDeclaration) item;
            Name fn = BoundName(d);
            if (declaredFunctionNames.add(fn)) {
                canDeclareGlobalFunctionOrThrow(context, envRec, d, fn, mv);
                functionsToInitialize.addFirst(d);
            }
        }
    }
    if (!functionsToInitialize.isEmpty()) {
        fo = mv.newVariable("fo", FunctionObject.class);
    }
    /* step 11 */
    LinkedHashMap<Name, VariableDeclaration> declaredVarNames = new LinkedHashMap<>();
    /* step 12 */
    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, envRec, vd, vn, mv);
                        declaredVarNames.put(vn, vd);
                    }
                }
            }
        }
    }
    // ES2016: Block-scoped global function declarations
    if (hasBlockFunctions(script)) {
        int idCounter = 0;
        HashSet<Name> declaredFunctionOrVarNames = new HashSet<>();
        declaredFunctionOrVarNames.addAll(declaredFunctionNames);
        declaredFunctionOrVarNames.addAll(declaredVarNames.keySet());
        for (FunctionDeclaration f : script.getScope().blockFunctions()) {
            Name fn = BoundName(f);
            Jump next = new Jump();
            // Runtime check always required for global block-level function declarations.
            f.setLegacyBlockScopeId(++idCounter);
            // FIXME: spec issue - avoid (observable!) duplicate checks for same name?
            // FIXME: spec issue - property creation order important?
            canDeclareGlobalFunction(envRec, f, fn, next, mv);
            setLegacyBlockFunction(context, f, mv);
            if (declaredFunctionOrVarNames.add(fn)) {
                createGlobalFunctionBinding(envRec, f, fn, false, mv);
            }
            mv.mark(next);
        }
    }
    /* step 15 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(script);
    /* step 16 */
    for (Declaration d : lexDeclarations) {
        assert !(d instanceof HoistableDeclaration);
        mv.lineInfo(d);
        for (Name dn : BoundNames(d)) {
            BindingOp<GlobalEnvironmentRecord> op = BindingOp.of(envRec, dn);
            if (d.isConstDeclaration()) {
                op.createImmutableBinding(envRec, dn, true, mv);
            } else {
                op.createMutableBinding(envRec, dn, false, mv);
            }
        }
    }
    /* step 17 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        InstantiateFunctionObject(context, env, f, mv);
        mv.store(fo);
        createGlobalFunctionBinding(envRec, f, fn, fo, false, mv);
    }
    /* step 18 */
    for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
        createGlobalVarBinding(envRec, e.getValue(), e.getKey(), false, mv);
    }
    /* step 19 */
    mv._return();
}
Also used : GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) 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) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) VariableDeclaration(com.github.anba.es6draft.ast.VariableDeclaration) HashSet(java.util.HashSet) 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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 49 with Name

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

the class StaticSemantics method ExportEntries.

/**
 * 15.2.1.7 Static Semantics: ExportEntries<br>
 * 15.2.3.5 Static Semantics: ExportEntries
 *
 * @param node
 *            the module node
 * @return the list of export entries
 */
public static List<ExportEntry> ExportEntries(Module node) {
    ArrayList<ExportEntry> entries = new ArrayList<>();
    for (ModuleItem item : node.getStatements()) {
        if (item instanceof ExportDeclaration) {
            ExportDeclaration exportDecl = (ExportDeclaration) item;
            switch(exportDecl.getType()) {
                case All:
                    {
                        String module = exportDecl.getModuleSpecifier();
                        entries.add(new ExportEntry(item, module, "*", null, null));
                        break;
                    }
                case External:
                    {
                        String module = exportDecl.getModuleSpecifier();
                        ExportEntriesForModule(exportDecl.getExportClause(), module, entries);
                        break;
                    }
                case Local:
                    ExportEntriesForModule(exportDecl.getExportClause(), null, entries);
                    break;
                case Variable:
                    for (Name name : BoundNames(exportDecl.getVariableStatement())) {
                        String id = name.getIdentifier();
                        entries.add(new ExportEntry(item, null, null, id, id));
                    }
                    break;
                case Declaration:
                    for (Name name : BoundNames(exportDecl.getDeclaration())) {
                        String id = name.getIdentifier();
                        entries.add(new ExportEntry(item, null, null, id, id));
                    }
                    break;
                case DefaultHoistableDeclaration:
                    {
                        Name localName = BoundName(exportDecl.getHoistableDeclaration());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                case DefaultClassDeclaration:
                    {
                        Name localName = BoundName(exportDecl.getClassDeclaration());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                case DefaultExpression:
                    {
                        Name localName = BoundName(exportDecl.getExpression().getBinding());
                        entries.add(new ExportEntry(item, null, null, localName.getIdentifier(), "default"));
                        break;
                    }
                default:
                    throw new AssertionError();
            }
        }
    }
    return entries;
}
Also used : ExportEntry(com.github.anba.es6draft.runtime.modules.ExportEntry) ArrayList(java.util.ArrayList) InlineArrayList(com.github.anba.es6draft.runtime.internal.InlineArrayList) 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