Search in sources :

Example 36 with ExecutionContext

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

the class ScriptCodeGenerator method generateGlobalScriptEvaluation.

/**
     * 15.1.7 Runtime Semantics: ScriptEvaluation
     * 
     * @param node
     *            the script node
     * @param mv
     *            the instruction visitor
     */
private void generateGlobalScriptEvaluation(Script node, InstructionVisitor mv) {
    Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<com.github.anba.es6draft.Script> script = mv.getParameter(SCRIPT, com.github.anba.es6draft.Script.class);
    Variable<Realm> realm = mv.newVariable("realm", Realm.class);
    Variable<ExecutionContext> scriptCxt = mv.newVariable("scriptCxt", ExecutionContext.class);
    Variable<ExecutionContext> oldScriptContext = mv.newVariable("oldScriptContext", ExecutionContext.class);
    Variable<Object> result = mv.newVariable("result", Object.class);
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    getRealm(callerContext, realm, mv);
    /* steps 1-2 (not applicable) */
    /* steps 3-7 */
    newScriptExecutionContext(realm, script, scriptCxt, mv);
    /* step 8 */
    getScriptContext(realm, oldScriptContext, mv);
    /* step 9 */
    setScriptContext(realm, scriptCxt, mv);
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    mv.mark(startFinally);
    {
        /* step 10 */
        mv.load(scriptCxt);
        mv.invoke(codegen.methodDesc(node, ScriptName.Init));
        /* steps 11-12 */
        mv.load(scriptCxt);
        mv.invoke(codegen.methodDesc(node, ScriptName.Code));
        mv.store(result);
        /* steps 13-15  */
        setScriptContext(realm, oldScriptContext, mv);
        /* step 16 */
        mv.load(result);
        mv._return();
    }
    mv.mark(endFinally);
    // Exception: Restore script context and then rethrow exception
    mv.finallyHandler(handlerFinally);
    mv.store(throwable);
    /* steps 13-15 */
    setScriptContext(realm, oldScriptContext, mv);
    mv.load(throwable);
    mv.athrow();
    mv.tryFinally(startFinally, endFinally, handlerFinally);
}
Also used : Script(com.github.anba.es6draft.ast.Script) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) Realm(com.github.anba.es6draft.runtime.Realm)

Example 37 with ExecutionContext

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

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

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

the class FunctionCodeGenerator method generateAsyncFunctionCall.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * calleeContext = newFunctionExecutionContext(function, null, thisValue)
     * function_init(calleeContext, function, arguments)
     * return EvaluateBody(calleeContext, generator)
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateAsyncFunctionCall(FunctionNode node, InstructionVisitor mv) {
    Variable<OrdinaryAsyncFunction> function = mv.getParameter(FUNCTION, OrdinaryAsyncFunction.class);
    Variable<Object> thisValue = mv.getParameter(THIS_VALUE, Object.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
    // (1) Create a new ExecutionContext
    prepareCallAndBindThis(node, calleeContext, function, thisValue, mv);
    // (2) Perform FunctionDeclarationInstantiation
    {
        TryCatchLabel startCatch = new TryCatchLabel();
        TryCatchLabel endCatch = new TryCatchLabel(), handlerCatch = new TryCatchLabel();
        Jump noException = new Jump();
        mv.mark(startCatch);
        functionDeclarationInstantiation(node, calleeContext, function, arguments, mv);
        mv.goTo(noException);
        mv.mark(endCatch);
        mv.catchHandler(handlerCatch, Types.ScriptException);
        {
            // stack: [exception] -> [cx, exception]
            mv.load(calleeContext);
            mv.swap();
            // stack: [cx, exception] -> [promise]
            mv.invoke(Methods.PromiseAbstractOperations_PromiseOf);
            mv._return();
        }
        mv.mark(noException);
        mv.tryCatch(startCatch, endCatch, handlerCatch, Types.ScriptException);
    }
    // (3) Perform EvaluateBody
    mv.load(calleeContext);
    mv.load(function);
    mv.invoke(Methods.OrdinaryAsyncFunction_EvaluateBody);
    // (4) Return result value
    mv._return();
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) OrdinaryAsyncFunction(com.github.anba.es6draft.runtime.types.builtins.OrdinaryAsyncFunction) Jump(com.github.anba.es6draft.compiler.assembler.Jump)

Example 40 with ExecutionContext

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

the class FunctionCodeGenerator method generateLegacyFunctionCall.

/**
     * Generate bytecode for:
     * 
     * <pre>
     * oldCaller = function.getLegacyCaller()
     * oldArguments = function.getLegacyArguments()
     * function.setLegacyCaller(callerContext.getCurrentFunction())
     * try {
     *   calleeContext = newFunctionExecutionContext(function, null, thisValue)
     *   return OrdinaryCallEvaluateBody(function, argumentsList)
     * } finally {
     *   function.restoreLegacyProperties(oldCaller, oldArguments)
     * }
     * </pre>
     * 
     * @param node
     *            the function node
     * @param mv
     *            the instruction visitor
     */
private void generateLegacyFunctionCall(FunctionNode node, InstructionVisitor mv) {
    final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
    final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
    Variable<LegacyConstructorFunction> function = mv.getParameter(FUNCTION, LegacyConstructorFunction.class);
    Variable<ExecutionContext> callerContext = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
    Variable<Object> thisValue = mv.getParameter(THIS_VALUE, Object.class);
    Variable<Object[]> arguments = mv.getParameter(ARGUMENTS, Object[].class);
    Variable<ExecutionContext> calleeContext = mv.newVariable("calleeContext", ExecutionContext.class);
    Variable<FunctionObject> oldCaller = mv.newVariable("oldCaller", FunctionObject.class);
    Variable<LegacyConstructorFunction.Arguments> oldArguments = mv.newVariable("oldArguments", LegacyConstructorFunction.Arguments.class);
    Variable<Throwable> throwable = mv.newVariable("throwable", Throwable.class);
    // (1) Retrieve 'caller' and 'arguments' and store in local variables
    if (hasCaller) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyCaller);
    } else {
        mv.anull();
    }
    mv.store(oldCaller);
    if (hasArguments) {
        mv.load(function);
        mv.invoke(Methods.LegacyConstructorFunction_getLegacyArguments);
    } else {
        mv.anull();
    }
    mv.store(oldArguments);
    // (2) Update 'caller' and 'arguments' properties
    if (hasCaller) {
        setLegacyCaller(function, callerContext, mv);
    }
    if (hasArguments) {
        setLegacyArguments(function, arguments, mv);
    }
    TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
    TryCatchLabel handlerFinally = new TryCatchLabel();
    mv.mark(startFinally);
    {
        // (3) Create a new ExecutionContext
        prepareCallAndBindThis(node, calleeContext, function, thisValue, mv);
        // (4) Call OrdinaryCallEvaluateBody
        ordinaryCallEvaluateBody(node, calleeContext, function, arguments, mv);
        // (5) Restore 'caller' and 'arguments'
        restoreLegacyProperties(function, oldCaller, oldArguments, mv);
        // (6) Return result value
        mv._return();
    }
    mv.mark(endFinally);
    // Exception: Restore 'caller' and 'arguments' and then rethrow exception
    mv.finallyHandler(handlerFinally);
    mv.store(throwable);
    restoreLegacyProperties(function, oldCaller, oldArguments, mv);
    mv.load(throwable);
    mv.athrow();
    mv.tryFinally(startFinally, endFinally, handlerFinally);
}
Also used : TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

Aggregations

ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)70 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)46 AbstractOperations (com.github.anba.es6draft.runtime.AbstractOperations)10 Realm (com.github.anba.es6draft.runtime.Realm)10 Callable (com.github.anba.es6draft.runtime.types.Callable)10 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)9 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)8 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)7 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)6 Name (com.github.anba.es6draft.ast.scope.Name)5 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)5 HashSet (java.util.HashSet)5 Declaration (com.github.anba.es6draft.ast.Declaration)4 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)4 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)4 MethodCode (com.github.anba.es6draft.compiler.assembler.Code.MethodCode)4 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)4 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)4 IsCallable (com.github.anba.es6draft.runtime.AbstractOperations.IsCallable)4 ToObject (com.github.anba.es6draft.runtime.AbstractOperations.ToObject)4