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);
}
}
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);
});
}
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);
});
}
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();
}
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);
}
Aggregations