Search in sources :

Example 61 with ExecutionContext

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

the class SourceTextModuleRecord method evaluate.

/**
     * 15.2.1.16.5 ModuleEvaluation() Concrete Method
     */
@Override
public Object evaluate() throws IOException, MalformedNameException, ResolutionException {
    /* step 1 */
    SourceTextModuleRecord module = this;
    // assert module.instantiated;
    assert module.environment != null : "module is not instantiated";
    /* step 3 */
    Realm realm = module.realm;
    assert realm != null : "module is not linked";
    /* step 4 */
    if (module.evaluated) {
        return UNDEFINED;
    }
    /* step 5 */
    module.evaluated = true;
    // ModuleDeclarationInstantiation did not complete successfully - stop evaluation.
    if (!this.instantiated) {
        return UNDEFINED;
    }
    /* step 6 */
    for (String required : module.requestedModules) {
        /* steps 6.a-b */
        ModuleRecord requiredModule = HostResolveImportedModule(module, required);
        /* steps 6.c-d */
        requiredModule.evaluate();
    }
    /* steps 7-12 */
    ExecutionContext moduleContext = newModuleExecutionContext(realm, module);
    /* steps 13-14 */
    ExecutionContext oldScriptContext = realm.getScriptContext();
    try {
        realm.setScriptContext(moduleContext);
        /* step 15 */
        Object result = module.scriptCode.evaluate(moduleContext);
        /* step 18 */
        return result;
    } finally {
        /* steps 16-17 */
        realm.setScriptContext(oldScriptContext);
    }
}
Also used : ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) ExecutionContext.newModuleDeclarationExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleDeclarationExecutionContext) ExecutionContext.newModuleExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext.newModuleExecutionContext) ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Realm(com.github.anba.es6draft.runtime.Realm)

Example 62 with ExecutionContext

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

the class FunctionCodeGenerator method legacyFunctionCall.

/**
 * 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 codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void legacyFunctionCall(CodeGenerator codegen, FunctionDefinition node, MethodCode method, FunctionCode function) {
    callMethod(node, method, mv -> {
        final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
        final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
        Variable<LegacyConstructorFunction> fn = mv.getFunction(LegacyConstructorFunction.class);
        Variable<ExecutionContext> callerContext = mv.getCallerContext();
        Variable<Object> thisValue = mv.getThisValue();
        Variable<Object[]> arguments = mv.getArguments();
        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) {
            getLegacyCaller(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldCaller);
        if (hasArguments) {
            getLegacyArguments(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldArguments);
        // (2) Update 'caller' and 'arguments' properties
        if (hasCaller) {
            setLegacyCaller(fn, callerContext, mv);
        }
        if (hasArguments) {
            setLegacyArguments(fn, arguments, mv);
        }
        TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
        TryCatchLabel handlerFinally = new TryCatchLabel();
        mv.mark(startFinally);
        {
            // (3) Create a new ExecutionContext
            prepareCallAndBindThis(node, calleeContext, fn, thisValue, mv);
            // (4) Call OrdinaryCallEvaluateBody
            ordinaryCallEvaluateBody(function.instantiation, function.body, calleeContext, fn, arguments, mv);
            // (5) Restore 'caller' and 'arguments'
            restoreLegacyProperties(fn, 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(fn, 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)

Example 63 with ExecutionContext

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

the class FunctionCodeGenerator method legacyFunctionConstruct.

/**
 * Generate bytecode for:
 *
 * <pre>
 * oldCaller = function.getLegacyCaller()
 * oldArguments = function.getLegacyArguments()
 * function.setLegacyCaller(callerContext.getCurrentFunction())
 * try {
 *   thisArgument = OrdinaryCreateFromConstructor(callerContext, newTarget, %ObjectPrototype%)
 *   calleeContext = newFunctionExecutionContext(function, newTarget, thisArgument)
 *   result = OrdinaryCallEvaluateBody(function, argumentsList)
 *   return returnResultOrThis(result)
 * } finally {
 *   function.restoreLegacyProperties(oldCaller, oldArguments)
 * }
 * </pre>
 *
 * @param codegen
 *            the code generator
 * @param node
 *            the function node
 * @param method
 *            the bytecode method
 * @param function
 *            the script function
 */
static void legacyFunctionConstruct(CodeGenerator codegen, FunctionDefinition node, MethodCode method, FunctionCode function) {
    constructMethod(node, method, mv -> {
        final boolean hasArguments = codegen.isEnabled(CompatibilityOption.FunctionArguments);
        final boolean hasCaller = codegen.isEnabled(CompatibilityOption.FunctionCaller);
        Variable<LegacyConstructorFunction> fn = mv.getFunction(LegacyConstructorFunction.class);
        Variable<ExecutionContext> callerContext = mv.getCallerContext();
        Variable<Constructor> newTarget = mv.getNewTarget();
        Variable<Object[]> arguments = mv.getArguments();
        Variable<ScriptObject> thisArg = mv.newVariable("thisArgument", ScriptObject.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) {
            getLegacyCaller(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldCaller);
        if (hasArguments) {
            getLegacyArguments(fn, mv);
        } else {
            mv.anull();
        }
        mv.store(oldArguments);
        // (2) Update 'caller' and 'arguments' properties
        if (hasCaller) {
            setLegacyCaller(fn, callerContext, mv);
        }
        if (hasArguments) {
            setLegacyArguments(fn, arguments, mv);
        }
        TryCatchLabel startFinally = new TryCatchLabel(), endFinally = new TryCatchLabel();
        TryCatchLabel handlerFinally = new TryCatchLabel();
        mv.mark(startFinally);
        {
            // (3) Create this-argument
            ordinaryCreateFromConstructor(callerContext, newTarget, thisArg, mv);
            // (4) Create a new ExecutionContext
            prepareCallAndBindThis(node, calleeContext, fn, newTarget, thisArg, mv);
            // (5) Call OrdinaryCallEvaluateBody
            ordinaryCallEvaluateBody(function.instantiation, function.body, calleeContext, fn, arguments, mv);
            // (6) Restore 'caller' and 'arguments'
            restoreLegacyProperties(fn, oldCaller, oldArguments, mv);
            // (7) Return result value
            returnResultOrThis(thisArg, false, mv);
        }
        mv.mark(endFinally);
        // Exception: Restore 'caller' and 'arguments' and then rethrow exception
        mv.finallyHandler(handlerFinally);
        mv.store(throwable);
        restoreLegacyProperties(fn, oldCaller, oldArguments, mv);
        mv.load(throwable);
        mv.athrow();
        mv.tryFinally(startFinally, endFinally, handlerFinally);
    });
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) Constructor(com.github.anba.es6draft.runtime.types.Constructor) TryCatchLabel(com.github.anba.es6draft.compiler.assembler.TryCatchLabel) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) ExecutionContext(com.github.anba.es6draft.runtime.ExecutionContext) LegacyConstructorFunction(com.github.anba.es6draft.runtime.types.builtins.LegacyConstructorFunction)

Example 64 with ExecutionContext

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

the class GlobalDeclarationInstantiationGenerator method generate.

private void generate(Script script, GlobalDeclInitVisitor mv) {
    Variable<ExecutionContext> context = mv.getExecutionContext();
    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 15 */
    List<Declaration> lexDeclarations = LexicallyScopedDeclarations(script);
    /* step 16 */
    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 17 */
    for (HoistableDeclaration f : functionsToInitialize) {
        Name fn = BoundName(f);
        InstantiateFunctionObject(context, env, f, mv);
        mv.store(fo);
        createGlobalFunctionBinding(envRec, f, fn, fo, false, mv);
    }
    /* step 18 */
    for (Map.Entry<Name, VariableDeclaration> e : declaredVarNames.entrySet()) {
        createGlobalVarBinding(envRec, e.getValue(), e.getKey(), false, mv);
    }
    /* step 19 */
    mv._return();
}
Also used : GlobalEnvironmentRecord(com.github.anba.es6draft.runtime.GlobalEnvironmentRecord) FunctionObject(com.github.anba.es6draft.runtime.types.builtins.FunctionObject) 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 65 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 scriptInit
 *            the script declaration instantiation method
 * @param scriptBody
 *            the script body method
 * @param mv
 *            the instruction visitor
 */
private static void generateGlobalScriptEvaluation(Script node, MethodName scriptInit, MethodName scriptBody, ScriptEvalVisitor mv) {
    Variable<ExecutionContext> callerContext = mv.getCallerContext();
    Variable<com.github.anba.es6draft.Script> script = mv.getScript();
    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(scriptInit);
        /* steps 11-12 */
        mv.load(scriptCxt);
        mv.invoke(scriptBody);
        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)

Aggregations

ExecutionContext (com.github.anba.es6draft.runtime.ExecutionContext)95 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)61 Realm (com.github.anba.es6draft.runtime.Realm)17 FunctionObject (com.github.anba.es6draft.runtime.types.builtins.FunctionObject)16 LexicalEnvironment (com.github.anba.es6draft.runtime.LexicalEnvironment)15 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)14 AbstractOperations (com.github.anba.es6draft.runtime.AbstractOperations)11 Declaration (com.github.anba.es6draft.ast.Declaration)10 HoistableDeclaration (com.github.anba.es6draft.ast.HoistableDeclaration)10 Name (com.github.anba.es6draft.ast.scope.Name)10 Callable (com.github.anba.es6draft.runtime.types.Callable)10 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)10 HashSet (java.util.HashSet)10 StatementListItem (com.github.anba.es6draft.ast.StatementListItem)9 MethodName (com.github.anba.es6draft.compiler.assembler.MethodName)9 TryCatchLabel (com.github.anba.es6draft.compiler.assembler.TryCatchLabel)8 DeclarativeEnvironmentRecord (com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord)8 ScriptException (com.github.anba.es6draft.runtime.internal.ScriptException)8 ArrayDeque (java.util.ArrayDeque)8 FunctionDeclaration (com.github.anba.es6draft.ast.FunctionDeclaration)7