Search in sources :

Example 11 with GlobalEnvironmentRecord

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

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

Example 13 with GlobalEnvironmentRecord

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

the class AsyncFunctionConstructor method CreateDynamicFunction.

/**
     * 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
     * 
     * @param callerContext
     *            the caller execution context
     * @param cx
     *            the execution context
     * @param newTarget
     *            the newTarget constructor function
     * @param args
     *            the function arguments
     * @return the new generator function object
     */
private static OrdinaryAsyncFunction CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.AsyncFunction;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.AsyncFunction, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.asyncFunction(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryAsyncFunction f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* steps 27-28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Example 14 with GlobalEnvironmentRecord

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

the class GeneratorFunctionConstructor method CreateDynamicConstructorGenerator.

private static OrdinaryConstructorGenerator CreateDynamicConstructorGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
    /* step 1 (not applicable) */
    /* step 2 (not applicable) */
    /* step 3 */
    Intrinsics fallbackProto = Intrinsics.Generator;
    /* steps 4-10 */
    String[] sourceText = functionSourceText(cx, args);
    String parameters = sourceText[0], bodyText = sourceText[1];
    /* steps 11, 13-20 */
    Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
    RuntimeInfo.Function function;
    try {
        ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
        function = scriptLoader.generator(source, parameters, bodyText).getFunction();
    } catch (ParserException | CompilationException e) {
        throw e.toScriptException(cx);
    }
    /* step 12 */
    boolean strict = function.isStrict();
    /* steps 21-22 */
    ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
    /* step 23 */
    OrdinaryConstructorGenerator f = OrdinaryConstructorGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
    /* steps 24-25 */
    LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
    /* step 26 */
    FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
    /* step 27 */
    OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
    MakeConstructor(f, true, prototype);
    /* step 28 (not applicable) */
    /* step 29 */
    SetFunctionName(f, "anonymous");
    /* step 30 */
    return f;
}
Also used : ParserException(com.github.anba.es6draft.parser.ParserException) CompilationException(com.github.anba.es6draft.compiler.CompilationException) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) RuntimeInfo(com.github.anba.es6draft.runtime.internal.RuntimeInfo) GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) OrdinaryConstructorGenerator(com.github.anba.es6draft.runtime.types.builtins.OrdinaryConstructorGenerator) FunctionConstructor.functionSource(com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource) Source(com.github.anba.es6draft.runtime.internal.Source) Intrinsics(com.github.anba.es6draft.runtime.types.Intrinsics) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ScriptLoader(com.github.anba.es6draft.runtime.internal.ScriptLoader)

Aggregations

GlobalEnvironmentRecord (com.github.anba.es6draft.runtime.GlobalEnvironmentRecord)14 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)6 VariableStatement (com.github.anba.es6draft.ast.VariableStatement)6 Name (com.github.anba.es6draft.ast.scope.Name)6 ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)6 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)6 RuntimeInfo (com.github.anba.es6draft.runtime.internal.RuntimeInfo)5 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)5 Declaration (com.github.anba.es6draft.ast.Declaration)4 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)4 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)4 VariableDeclaration (com.github.anba.es6draft.ast.VariableDeclaration)4 CompilationException (com.github.anba.es6draft.compiler.CompilationException)4 Jump (com.github.anba.es6draft.compiler.assembler.Jump)4 ParserException (com.github.anba.es6draft.parser.ParserException)4 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)4 ScriptLoader (com.github.anba.es6draft.runtime.internal.ScriptLoader)4 Source (com.github.anba.es6draft.runtime.internal.Source)4 FunctionConstructor.functionSource (com.github.anba.es6draft.runtime.objects.FunctionConstructor.functionSource)4 Intrinsics (com.github.anba.es6draft.runtime.types.Intrinsics)4