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();
}
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);
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class CodeGenerator method compile.
MethodName compile(BlockStatement node, List<Declaration> declarations, BlockDeclarationInstantiationGenerator generator) {
MethodCode method = newMethod2(node);
BlockDeclInitVisitor body = new BlockDeclInitVisitor(method);
body.lineInfo(node);
body.begin();
Variable<ExecutionContext> cx = body.getExecutionContext();
Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = body.getLexicalEnvironment();
generator.generateMethod(declarations, cx, env, body);
body._return();
body.end();
return methodDesc(node, method.methodName);
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class ConsoleObject method createConsole.
/**
* Creates a new {@code console} object.
*
* @param realm
* the realm instance
* @return the console object
* @throws IOException
* if there was any I/O error
* @throws URISyntaxException
* the URL is not a valid URI
* @throws MalformedNameException
* if any imported module request cannot be normalized
* @throws ResolutionException
* if any export binding cannot be resolved
*/
public static ScriptObject createConsole(Realm realm) throws IOException, URISyntaxException, MalformedNameException, ResolutionException {
ExecutionContext cx = realm.defaultContext();
ModuleRecord module = NativeCode.loadModule(realm, "console.jsm");
ScriptObject console = NativeCode.getModuleExport(module, "default", ScriptObject.class);
Callable inspectFn = Properties.createFunction(cx, new InspectFunction(), InspectFunction.class);
CreateMethodProperty(cx, console, "_inspect", inspectFn);
return console;
}
use of com.github.anba.es6draft.runtime.ExecutionContext in project es6draft by anba.
the class InterpretedScriptBody method evalScriptEvaluation.
/**
* 18.2.1.1 Runtime Semantics: PerformEval( x, evalRealm, strictCaller, direct)
*
* @param cx
* the execution context
* @param script
* the script object
* @return the script evaluation result
*/
private Object evalScriptEvaluation(ExecutionContext cx, Script script) {
// TODO: Skip allocating lex-env if not needed
/* steps 1-5 (not applicable) */
/* steps 6-7 */
boolean strictEval = parsedScript.isStrict();
/* step 8 (omitted) */
/* steps 9-10 */
LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv;
LexicalEnvironment<?> varEnv;
if (parsedScript.isDirectEval()) {
/* step 9 */
lexEnv = newDeclarativeEnvironment(cx.getLexicalEnvironment());
varEnv = cx.getVariableEnvironment();
} else {
Realm evalRealm = cx.getRealm();
/* step 10 */
lexEnv = newDeclarativeEnvironment(evalRealm.getGlobalEnv());
varEnv = evalRealm.getGlobalEnv();
}
/* step 11 */
if (strictEval) {
varEnv = lexEnv;
}
/* steps 12-17 */
ExecutionContext evalCxt = newEvalExecutionContext(cx, script, varEnv, lexEnv);
/* step 18 */
EvalDeclarationInstantiation(evalCxt, parsedScript, varEnv, lexEnv);
/* steps 19-23 */
return parsedScript.accept(new Interpreter(parsedScript), evalCxt);
}
Aggregations