Search in sources :

Example 41 with Name

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

the class EvalDeclarationInstantiationGenerator method createVarDeclarations.

/**
 * 18.2.1.2, step 15
 */
private void createVarDeclarations(LinkedHashSet<Name> declaredVarNames, Variable<? extends EnvironmentRecord> varEnvRec, Variable<Undefined> undef, InstructionVisitor mv) {
    for (Name vn : declaredVarNames) {
        BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
        Jump varAlreadyDeclared = new Jump();
        op.hasBinding(varEnvRec, vn, mv);
        mv.ifne(varAlreadyDeclared);
        {
            op.createMutableBinding(varEnvRec, vn, true, mv);
            op.initializeBinding(varEnvRec, vn, undef, mv);
        }
        mv.mark(varAlreadyDeclared);
    }
}
Also used : EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) IdentifierName(com.github.anba.es6draft.ast.IdentifierName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 42 with Name

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

the class EvalDeclarationInstantiationGenerator method createFunctions.

/**
 * 18.2.1.2, step 14
 */
private void createFunctions(ArrayDeque<HoistableDeclaration> functionsToInitialize, boolean strict, Variable<ExecutionContext> context, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Variable<FunctionObject> fo, Variable<? extends EnvironmentRecord> varEnvRec, InstructionVisitor mv) {
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        // stack: [] -> []
        InstantiateFunctionObject(context, lexEnv, f, mv);
        mv.store(fo);
        BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
        Jump funcAlreadyDeclared = new Jump(), after = new Jump();
        op.hasBinding(varEnvRec, fn, mv);
        mv.ifne(funcAlreadyDeclared);
        {
            op.createMutableBinding(varEnvRec, fn, true, mv);
            op.initializeBinding(varEnvRec, fn, fo, mv);
            mv.goTo(after);
        }
        mv.mark(funcAlreadyDeclared);
        {
            op.setMutableBinding(varEnvRec, fn, fo, strict, mv);
        }
        mv.mark(after);
    }
}
Also used : HoistableDeclaration(com.github.anba.es6draft.ast.HoistableDeclaration) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) IdentifierName(com.github.anba.es6draft.ast.IdentifierName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 43 with Name

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

the class EvalDeclarationInstantiationGenerator method checkLexicalRedeclaration.

/**
 * 18.2.1.2, steps 5.b-d
 */
private void checkLexicalRedeclaration(Script evalScript, Variable<ExecutionContext> context, Variable<? extends LexicalEnvironment<? extends EnvironmentRecord>> varEnv, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Set<Name> varNames, InstructionVisitor mv) {
    Variable<LexicalEnvironment<EnvironmentRecord>> thisLex = mv.newVariable("thisLex", LexicalEnvironment.class).uncheckedCast();
    Variable<EnvironmentRecord> thisEnvRec = mv.newVariable("thisEnvRec", EnvironmentRecord.class).uncheckedCast();
    Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class).uncheckedCast();
    Set<Name> restrictedVarNames = evalScript.getScope().restrictedVarDeclaredNames();
    final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
    final boolean hasWith = codegen.isEnabled(Parser.Option.EnclosedByWithStatement);
    final boolean hasCatch = codegen.isEnabled(Parser.Option.EnclosedByCatchStatement);
    assert hasCatch || restrictedVarNames.isEmpty();
    Jump loopTest = new Jump(), loop = new Jump(), objectEnv = new Jump();
    mv.load(lexEnv);
    if (hasLexicalEnvironment(evalScript)) {
        // Don't need to check own lexical environment.
        mv.invoke(Methods.LexicalEnvironment_getOuter);
    }
    mv.store(thisLex);
    mv.nonDestructiveGoTo(loopTest);
    {
        mv.mark(loop);
        getEnvironmentRecord(thisLex, thisEnvRec, mv);
        if (hasWith) {
            mv.load(thisEnvRec);
            mv.instanceOf(Types.ObjectEnvironmentRecord);
            mv.ifne(objectEnv);
        }
        mv.load(thisEnvRec);
        mv.checkcast(Types.DeclarativeEnvironmentRecord);
        mv.store(envRec);
        for (Name name : varNames) {
            mv.load(context);
            mv.load(envRec);
            mv.aconst(name.getIdentifier());
            if (hasCatch) {
                mv.iconst(catchVar && !restrictedVarNames.contains(name));
                mv.invoke(Methods.DeclarationOperations_canDeclareVarOrThrowMaybeCatch);
            } else {
                mv.invoke(Methods.DeclarationOperations_canDeclareVarOrThrow);
            }
        }
        if (hasWith) {
            mv.mark(objectEnv);
        }
        mv.load(thisLex);
        mv.invoke(Methods.LexicalEnvironment_getOuter);
        mv.store(thisLex);
    }
    mv.mark(loopTest);
    mv.load(thisLex);
    mv.load(varEnv);
    mv.ifacmpne(loop);
}
Also used : LexicalEnvironment(com.github.anba.es6draft.runtime.LexicalEnvironment) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Jump(com.github.anba.es6draft.compiler.assembler.Jump) IdentifierName(com.github.anba.es6draft.ast.IdentifierName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 44 with Name

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

the class DeclarationBindingInstantiation method EvalDeclarationInstantiation.

/**
 * 18.2.1.3 Runtime Semantics: EvalDeclarationInstantiation (body, varEnv, lexEnv, strict)
 *
 * @param cx
 *            the execution context
 * @param evalScript
 *            the global script to instantiate
 * @param varEnv
 *            the variable environment
 * @param lexEnv
 *            the lexical environment
 */
public static void EvalDeclarationInstantiation(ExecutionContext cx, Script evalScript, LexicalEnvironment<?> varEnv, LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv) {
    boolean strict = evalScript.isStrict();
    boolean nonStrictGlobal = !strict && evalScript.isGlobalCode() && !evalScript.isScripting();
    /* step 1 */
    Set<Name> varNames = VarDeclaredNames(evalScript);
    /* step 2 */
    List<StatementListItem> varDeclarations = VarScopedDeclarations(evalScript);
    /* step 3 (not applicable) */
    /* step 4 */
    EnvironmentRecord varEnvRec = varEnv.getEnvRec();
    assert !nonStrictGlobal || varEnvRec instanceof GlobalEnvironmentRecord : String.format("Unexpected environment record type: %s", varEnvRec);
    /* step 5 */
    if (!strict) {
        if (nonStrictGlobal) {
            /* step 5.a */
            GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
            for (Name name : varNames) {
                DeclarationOperations.canDeclareVarScopedOrThrow(cx, gEnvRec, name.getIdentifier());
            }
        }
        /* steps 5.b-d */
        if (!evalScript.isScripting() && !varNames.isEmpty() && isEnclosedByLexicalOrHasRestrictedVar(evalScript)) {
            checkLexicalRedeclaration(cx, varEnv, lexEnv, varNames);
        }
    }
    /* steps 6-8 (not applicable) */
    /* step 9 (note) */
    /* step 10 */
    LinkedHashSet<Name> declaredNames = new LinkedHashSet<>();
    /* step 11 */
    for (StatementListItem d : varDeclarations) {
        assert d instanceof VariableStatement;
        for (Name vn : BoundNames((VariableStatement) d)) {
            if (nonStrictGlobal) {
                GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
                DeclarationOperations.canDeclareGlobalVarOrThrow(cx, gEnvRec, vn.getIdentifier());
            }
            declaredNames.add(vn);
        }
    }
    /* steps 13-14 */
    assert LexicallyScopedDeclarations(evalScript).isEmpty();
    /* step 16 */
    for (Name vn : declaredNames) {
        if (nonStrictGlobal) {
            GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
            gEnvRec.createGlobalVarBinding(vn.getIdentifier(), true);
        } else {
            boolean bindingExists = varEnvRec.hasBinding(vn.getIdentifier());
            if (!bindingExists) {
                varEnvRec.createMutableBinding(vn.getIdentifier(), true);
                varEnvRec.initializeBinding(vn.getIdentifier(), UNDEFINED);
            }
        }
    }
/* step 17 (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) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) ObjectEnvironmentRecord(com.github.anba.es6draft.runtime.ObjectEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) Name(com.github.anba.es6draft.ast.scope.Name)

Example 45 with Name

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

the class ClassPropertyGenerator method InitializeOrGetPrivateName.

private void InitializeOrGetPrivateName(PrivateNameProperty privateName, boolean isAlreadyDefined, MutableValue<PrivateName> privateNameVar, CodeVisitor mv) {
    // TODO: The spec uses a separate lexical environment for private names.
    Name name = privateName.getName();
    Value<DeclarativeEnvironmentRecord> scopeEnvRec = getLexicalEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
    BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(scopeEnvRec, name);
    if (isAlreadyDefined) {
        op.getBindingValue(scopeEnvRec, name, true, mv);
        mv.checkcast(Types.PrivateName);
        mv.store(privateNameVar);
    } else {
        mv.anew(Methods.PrivateName_new, mv.vconst(name.getIdentifier()));
        mv.store(privateNameVar);
        op.initializeBinding(scopeEnvRec, name, privateNameVar, mv);
    }
}
Also used : DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) PropName(com.github.anba.es6draft.semantics.StaticSemantics.PropName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name) FieldName(com.github.anba.es6draft.compiler.assembler.FieldName) PrivateName(com.github.anba.es6draft.runtime.types.PrivateName)

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