Search in sources :

Example 6 with EnvironmentRecord

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

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

use of com.github.anba.es6draft.runtime.EnvironmentRecord 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> varForOfNames = evalScript.getScope().varForOfDeclaredNames();
    final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
    final boolean hasWith = codegen.isEnabled(Parser.Option.EnclosedByWithStatement);
    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());
            mv.iconst(catchVar && !varForOfNames.contains(name));
            mv.invoke(Methods.ScriptRuntime_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) 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 9 with EnvironmentRecord

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

the class DeclarationBindingInstantiation method EvalDeclarationInstantiation.

/**
     * 18.2.1.2 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) {
                ScriptRuntime.canDeclareVarScopedOrThrow(cx, gEnvRec, name.getIdentifier());
            }
        }
        /* steps 5.b-d */
        if (!evalScript.isScripting() && !varNames.isEmpty() && isEnclosedByLexicalOrHasVarForOf(evalScript)) {
            checkLexicalRedeclaration(cx, varEnv, lexEnv, varNames);
        }
    }
    /* steps 6-8 (not applicable) */
    /* step 9 */
    LinkedHashSet<Name> declaredNames = new LinkedHashSet<>();
    /* step 10 */
    for (StatementListItem d : varDeclarations) {
        assert d instanceof VariableStatement;
        for (Name vn : BoundNames((VariableStatement) d)) {
            if (nonStrictGlobal) {
                GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
                ScriptRuntime.canDeclareGlobalVarOrThrow(cx, gEnvRec, vn.getIdentifier());
            }
            declaredNames.add(vn);
        }
    }
    /* steps 12-13 */
    assert LexicallyScopedDeclarations(evalScript).isEmpty();
    /* step 15 */
    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 16 (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 10 with EnvironmentRecord

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

the class ModuleNamespaceObject method getValue.

/** 9.4.6.8 [[Get]] (P, Receiver) */
@Override
protected Object getValue(ExecutionContext cx, String propertyKey, Object receiver) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Set<String> exports = this.exports;
    /* step 4 */
    if (!exports.contains(propertyKey)) {
        return UNDEFINED;
    }
    /* step 5 */
    ModuleRecord m = this.module;
    /* steps 6-8 */
    ModuleExport binding;
    try {
        /* steps 6, 8 */
        binding = m.resolveExport(propertyKey, new HashMap<>(), new HashSet<>());
    } catch (IOException e) {
        /* step 7 */
        throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
    } catch (ResolutionException | MalformedNameException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    } catch (ParserException | CompilationException e) {
        /* step 7 */
        throw e.toScriptException(cx);
    }
    /* step 8 */
    assert binding != null && !binding.isAmbiguous();
    /* step 9 */
    ModuleRecord targetModule = binding.getModule();
    /* step 10 */
    assert targetModule != null;
    /* step 11 */
    LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
    /* step 12 */
    if (targetEnv == null) {
        throw newReferenceError(cx, Messages.Key.UninitializedBinding, binding.getBindingName());
    }
    /* step ? (Extension: Export From) */
    if (binding.isNameSpaceExport()) {
        try {
            return GetModuleNamespace(cx, targetModule);
        } catch (IOException e) {
            throw Errors.newInternalError(cx, Messages.Key.ModulesIOException, e.getMessage());
        } catch (MalformedNameException | ResolutionException e) {
            throw e.toScriptException(cx);
        }
    }
    /* step 13 */
    EnvironmentRecord targetEnvRec = targetEnv.getEnvRec();
    /* step 14 */
    return targetEnvRec.getBindingValue(binding.getBindingName(), true);
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) HashMap(java.util.HashMap) MalformedNameException(com.github.anba.es6draft.runtime.modules.MalformedNameException) ToString(com.github.anba.es6draft.runtime.AbstractOperations.ToString) IOException(java.io.IOException) ResolutionException(com.github.anba.es6draft.runtime.modules.ResolutionException) ModuleExport(com.github.anba.es6draft.runtime.modules.ModuleExport) ModuleRecord(com.github.anba.es6draft.runtime.modules.ModuleRecord) EnvironmentRecord(com.github.anba.es6draft.runtime.EnvironmentRecord) HashSet(java.util.HashSet)

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