Search in sources :

Example 1 with EnvironmentRecord

use of com.github.anba.es6draft.runtime.EnvironmentRecord 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) ScriptName(com.github.anba.es6draft.compiler.CodeGenerator.ScriptName) MethodName(com.github.anba.es6draft.compiler.assembler.MethodName) Name(com.github.anba.es6draft.ast.scope.Name)

Example 2 with EnvironmentRecord

use of com.github.anba.es6draft.runtime.EnvironmentRecord 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, false);
            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, asm -> {
                Value<DeclarativeEnvironmentRecord> benv = getLexicalEnvironmentRecord(Types.DeclarativeEnvironmentRecord, mv);
                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.ScriptRuntime_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, asm -> {
                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.Empty;
}
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 3 with EnvironmentRecord

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

the class ScriptRuntime method canDeclareVarBinding.

/**
     * 18.2.1.2 Runtime Semantics: EvalDeclarationInstantiation( body, varEnv, lexEnv, strict)
     * 
     * @param varEnv
     *            the variable environment
     * @param lexEnv
     *            the lexical environment
     * @param name
     *            the function name
     * @param catchVar
     *            {@code true} if variable redeclarations are allowed in catch clauses
     * @return {@code true} if the name can be declared
     */
public static boolean canDeclareVarBinding(LexicalEnvironment<?> varEnv, LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv, String name, boolean catchVar) {
    for (LexicalEnvironment<?> thisEnv = lexEnv; thisEnv != varEnv; thisEnv = thisEnv.getOuter()) {
        EnvironmentRecord thisEnvRec = thisEnv.getEnvRec();
        if (thisEnvRec instanceof ObjectEnvironmentRecord) {
            continue;
        }
        DeclarativeEnvironmentRecord declEnvRec = (DeclarativeEnvironmentRecord) thisEnvRec;
        if (declEnvRec.hasBinding(name) && !(catchVar && declEnvRec.isCatchEnvironment())) {
            return false;
        }
    }
    return true;
}
Also used : ObjectEnvironmentRecord(com.github.anba.es6draft.runtime.ObjectEnvironmentRecord) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) ObjectEnvironmentRecord(com.github.anba.es6draft.runtime.ObjectEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)

Example 4 with EnvironmentRecord

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

the class ScriptRuntime method GetSuperConstructor.

/**
     * 12.3.5 The super Keyword
     * <p>
     * 12.3.5.2 Runtime Semantics: GetSuperConstructor ( )
     * 
     * @param cx
     *            the execution context
     * @return the super reference
     */
public static Constructor GetSuperConstructor(ExecutionContext cx) {
    /* step 1 */
    EnvironmentRecord envRec = cx.getThisEnvironment();
    /* step 2 */
    assert envRec instanceof FunctionEnvironmentRecord;
    FunctionEnvironmentRecord fEnvRec = (FunctionEnvironmentRecord) envRec;
    /* step 3 */
    FunctionObject activeFunction = fEnvRec.getFunctionObject();
    /* steps 4-5 */
    ScriptObject superConstructor = activeFunction.getPrototypeOf(cx);
    /* step 6 */
    if (!IsConstructor(superConstructor)) {
        throw newTypeError(cx, Messages.Key.NotConstructor);
    }
    /* step 7 */
    return (Constructor) superConstructor;
}
Also used : FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) ObjectEnvironmentRecord(com.github.anba.es6draft.runtime.ObjectEnvironmentRecord)

Example 5 with EnvironmentRecord

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

the class ScriptRuntime method BindThisValue.

/**
     * 12.3.5 The super Keyword
     * 
     * @param result
     *            the new {@code this} binding value
     * @param cx
     *            the execution context
     */
public static void BindThisValue(ScriptObject result, ExecutionContext cx) {
    EnvironmentRecord thisEnvironment = cx.getThisEnvironment();
    assert thisEnvironment instanceof FunctionEnvironmentRecord;
    ((FunctionEnvironmentRecord) thisEnvironment).bindThisValue(cx, result);
}
Also used : FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) ModuleEnvironmentRecord(com.github.anba.es6draft.runtime.ModuleEnvironmentRecord) FunctionEnvironmentRecord(com.github.anba.es6draft.runtime.FunctionEnvironmentRecord) DeclarativeEnvironmentRecord(com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) ObjectEnvironmentRecord(com.github.anba.es6draft.runtime.ObjectEnvironmentRecord)

Aggregations

EnvironmentRecord (com.github.anba.es6draft.runtime.EnvironmentRecord)10 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)9 GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)8 Name (com.github.anba.es6draft.ast.scope.Name)6 Jump (com.github.anba.es6draft.compiler.assembler.Jump)5 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)5 ScriptName (com.github.anba.es6draft.compiler.CodeGenerator.ScriptName)4 ObjectEnvironmentRecord (com.github.anba.es6draft.runtime.ObjectEnvironmentRecord)4 FunctionEnvironmentRecord (com.github.anba.es6draft.runtime.FunctionEnvironmentRecord)3 ModuleEnvironmentRecord (com.github.anba.es6draft.runtime.ModuleEnvironmentRecord)3 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)1 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)1 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)1 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)1 FunctionScope (com.github.anba.es6draft.ast.scope.FunctionScope)1 ScriptScope (com.github.anba.es6draft.ast.scope.ScriptScope)1 TopLevelScope (com.github.anba.es6draft.ast.scope.TopLevelScope)1 InitializeBoundName (com.github.anba.es6draft.compiler.BindingInitializationGenerator.InitializeBoundName)1 CompilationException (com.github.anba.es6draft.compiler.CompilationException)1 ParserException (com.github.anba.es6draft.parser.ParserException)1