Search in sources :

Example 16 with LexicalEnvironment

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

the class GlobalDeclarationInstantiationGenerator method generate.

private void generate(Script script, InstructionVisitor mv) {
    Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    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 14 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(script);
    /* step 15 */
    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 16 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        InstantiateFunctionObject(context, env, f, mv);
        mv.store(fo);
        createGlobalFunctionBinding(envRec, f, fn, fo, false, mv);
    }
    /* step 17 */
    for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
        createGlobalVarBinding(envRec, e.getValue(), e.getKey(), false, mv);
    }
    /* step 18 */
    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) 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 17 with LexicalEnvironment

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

the class ModuleDeclarationInstantiationGenerator method generate.

private void generate(Module module, SourceTextModuleRecord moduleRecord, InstructionVisitor mv) {
    Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<SourceTextModuleRecord> moduleRec = mv.getParameter(MODULE, SourceTextModuleRecord.class);
    Variable<LexicalEnvironment<ModuleEnvironmentRecord>> env = mv.getParameter(MODULE_ENV, LexicalEnvironment.class).uncheckedCast();
    Variable<ModuleEnvironmentRecord> envRec = mv.newVariable("envRec", ModuleEnvironmentRecord.class);
    getEnvironmentRecord(env, envRec, mv);
    Variable<ModuleExport> resolved = mv.newVariable("resolved", ModuleExport.class);
    Variable<ScriptObject> namespace = null;
    Variable<FunctionObject> fo = null;
    Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
    mv.loadUndefined();
    mv.store(undef);
    /* step 9 */
    for (ExportEntry exportEntry : moduleRecord.getIndirectExportEntries()) {
        mv.lineInfo(exportEntry.getLine());
        mv.load(moduleRec);
        mv.aconst(exportEntry.getExportName());
        mv.invoke(Methods.ScriptRuntime_resolveExportOrThrow);
    }
    /* step 12 */
    for (ImportEntry importEntry : moduleRecord.getImportEntries()) {
        mv.lineInfo(importEntry.getLine());
        if (importEntry.isStarImport()) {
            Name localName = new Name(importEntry.getLocalName());
            BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, localName);
            op.createImmutableBinding(envRec, localName, true, mv);
            mv.load(context);
            mv.load(moduleRec);
            mv.aconst(importEntry.getModuleRequest());
            mv.invoke(Methods.ScriptRuntime_getModuleNamespace);
            if (namespace == null) {
                namespace = mv.newVariable("namespace", ScriptObject.class);
            }
            mv.store(namespace);
            op.initializeBinding(envRec, localName, namespace, mv);
        } else {
            mv.load(moduleRec);
            mv.aconst(importEntry.getModuleRequest());
            mv.aconst(importEntry.getImportName());
            mv.invoke(Methods.ScriptRuntime_resolveImportOrThrow);
            mv.store(resolved);
            createImportBinding(context, envRec, importEntry.getLocalName(), resolved, mv);
        }
    }
    /* step 13 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(module);
    HashSet<Name> declaredVarNames = new HashSet<>();
    /* step 14 */
    for (StatementListItem d : varDeclarations) {
        assert d instanceof VariableStatement;
        for (Name dn : BoundNames((VariableStatement) d)) {
            if (declaredVarNames.add(dn)) {
                BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
                op.createMutableBinding(envRec, dn, false, mv);
                op.initializeBinding(envRec, dn, undef, mv);
            }
        }
    }
    /* step 15 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(module);
    /* step 16 */
    for (Declaration d : lexDeclarations) {
        for (Name dn : BoundNames(d)) {
            BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
            if (d.isConstDeclaration()) {
                op.createImmutableBinding(envRec, dn, true, mv);
            } else {
                op.createMutableBinding(envRec, dn, false, mv);
            }
            if (d instanceof HoistableDeclaration) {
                InstantiateFunctionObject(context, env, d, mv);
                if (fo == null) {
                    fo = mv.newVariable("fo", FunctionObject.class);
                }
                mv.store(fo);
                op.initializeBinding(envRec, dn, fo, mv);
            }
        }
    }
    /* step 17 */
    mv._return();
}
Also used : SourceTextModuleRecord(com.github.anba.es6draft.runtime.modules.SourceTextModuleRecord) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ModuleName(com.github.anba.es6draft.compiler.CodeGenerator.ModuleName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) Declaration(com.github.anba.es6draft.ast.Declaration) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord) HashSet(java.util.HashSet) Undefined(com.github.anba.es6draft.runtime.types.Undefined) ExportEntry(com.github.anba.es6draft.runtime.modules.ExportEntry) ImportEntry(com.github.anba.es6draft.runtime.modules.ImportEntry) 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)

Example 18 with LexicalEnvironment

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

the class StatementGenerator method visit.

/**
     * Extension: 'let' statement
     */
@Override
public Completion visit(LetStatement 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 */
                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 */
                BindingInitialization(codegen, envRec, (BindingPattern) binding, mv);
            }
        }
        mv.load(env);
        pushLexicalEnvironment(mv);
        mv.exitVariableScope();
    }
    mv.enterScope(node);
    Completion result = node.getStatement().accept(this, mv);
    mv.exitScope();
    if (scope.isPresent() && !result.isAbrupt()) {
        popLexicalEnvironment(mv);
    }
    return result;
}
Also used : InitializeBoundName(com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) BlockScope(com.github.anba.es6draft.ast.scope.BlockScope) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 19 with LexicalEnvironment

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

the class EvalDeclarationInstantiationGenerator method generateStrict.

private void generateStrict(Script evalScript, InstructionVisitor mv) {
    assert evalScript.isStrict() && !evalScript.isScripting();
    Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    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) */
    /* 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) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) 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 20 with LexicalEnvironment

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

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