use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class StatementGenerator method visit.
/**
* 13.7.4 The for Statement
* <p>
* 13.1.8 Runtime Semantics: Evaluation<br>
* 13.1.7 Runtime Semantics: LabelledEvaluation<br>
* 13.7.4.7 Runtime Semantics: LabelledEvaluation
*/
@Override
public Completion visit(ForStatement node, CodeVisitor mv) {
assert mv.getStackSize() == 0;
boolean perIterationsLets = false;
BlockScope scope = node.getScope();
Node head = node.getHead();
if (head == null) {
// empty
} else if (head instanceof Expression) {
ValType type = expression(((Expression) head).emptyCompletion(), mv);
mv.pop(type);
} else if (head instanceof VariableStatement) {
head.accept(this, mv);
} else {
assert head instanceof LexicalDeclaration;
LexicalDeclaration lexDecl = (LexicalDeclaration) head;
List<Name> boundNames = BoundNames(lexDecl);
boolean isConst = IsConstantDeclaration(lexDecl);
perIterationsLets = !isConst && !boundNames.isEmpty();
if (scope.isPresent()) {
// stack: [] -> [loopEnv]
newDeclarativeEnvironment(scope, mv);
// stack: [loopEnv] -> [loopEnv]
mv.enterVariableScope();
Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
getEnvRec(envRec, mv);
// stack: [loopEnv] -> [loopEnv]
for (Name dn : boundNames) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, dn);
if (isConst) {
op.createImmutableBinding(envRec, dn, true, mv);
} else {
op.createMutableBinding(envRec, dn, false, mv);
}
}
mv.exitVariableScope();
// stack: [loopEnv] -> []
pushLexicalEnvironment(mv);
}
mv.enterScope(node);
lexDecl.accept(this, mv);
}
Completion result = ForBodyEvaluation(node, perIterationsLets, mv);
if (head instanceof LexicalDeclaration) {
mv.exitScope();
if (scope.isPresent() && !result.isAbrupt()) {
popLexicalEnvironment(mv);
}
}
return result;
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class StatementGenerator method ForInOfHeadEvaluation.
/**
* 13.7.5.12 Runtime Semantics: ForIn/OfHeadEvaluation (TDZnames, expr, iterationKind, labelSet)
* <p>
* stack: [] {@literal ->} [Iterator]
*
* @param <FORSTATEMENT>
* the for-statement node type
* @param node
* the for-statement node
* @param iterationKind
* the for-statement's iteration kind
* @param lblFail
* the target instruction if the expression node does not produce an object type
* @param mv
* the code visitor
* @return the value type of the expression
*/
private <FORSTATEMENT extends IterationStatement & ForIterationNode> ValType ForInOfHeadEvaluation(FORSTATEMENT node, IterationKind iterationKind, Jump lblFail, CodeVisitor mv) {
/* steps 1-2 */
BlockScope scope = node.getScope();
Node lhs = node.getHead();
List<Name> tdzNames = null;
if (lhs instanceof LexicalDeclaration) {
tdzNames = BoundNames(forDeclarationBinding((LexicalDeclaration) lhs));
assert scope.isPresent() == !tdzNames.isEmpty();
if (scope.isPresent()) {
// stack: [] -> [TDZ]
newDeclarativeEnvironment(scope, mv);
mv.enterVariableScope();
Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
getEnvRec(envRec, mv);
// stack: [TDZ] -> [TDZ]
for (Name name : tdzNames) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
op.createMutableBinding(envRec, name, false, mv);
}
mv.exitVariableScope();
// stack: [TDZ] -> []
pushLexicalEnvironment(mv);
}
mv.enterScope(node);
}
/* steps 3, 5-6 */
Expression expr = node.getExpression();
ValType type = expressionBoxed(expr, mv);
/* step 4 */
if (tdzNames != null) {
mv.exitScope();
if (scope.isPresent()) {
popLexicalEnvironment(mv);
}
}
/* steps 7-8 */
if (iterationKind == IterationKind.Enumerate || iterationKind == IterationKind.EnumerateValues) {
/* step 7.a */
if (type != ValType.Object) {
Jump loopstart = new Jump();
mv.dup();
isUndefinedOrNull(mv);
mv.ifeq(loopstart);
mv.pop();
mv.goTo(lblFail);
mv.mark(loopstart);
}
/* steps 7.b-c */
if (codegen.isEnabled(CompatibilityOption.LegacyGenerator)) {
// legacy generator mode, both, for-in and for-each, perform Iterate on generators
Jump l0 = new Jump(), l1 = new Jump();
mv.dup();
mv.instanceOf(Types.GeneratorObject);
mv.ifeq(l0);
mv.dup();
mv.checkcast(Types.GeneratorObject);
mv.invoke(Methods.GeneratorObject_isLegacyGenerator);
mv.ifeq(l0);
mv.loadExecutionContext();
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_iterate);
mv.goTo(l1);
mv.mark(l0);
mv.loadExecutionContext();
if (iterationKind == IterationKind.Enumerate) {
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_enumerate);
} else {
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_enumerateValues);
}
mv.mark(l1);
} else if (iterationKind == IterationKind.Enumerate) {
mv.loadExecutionContext();
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_enumerate);
} else {
mv.loadExecutionContext();
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_enumerateValues);
}
} else if (iterationKind == IterationKind.AsyncIterate) {
mv.loadExecutionContext();
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_asyncIterate);
} else {
/* step 8 */
assert iterationKind == IterationKind.Iterate;
mv.loadExecutionContext();
mv.lineInfo(expr);
mv.invoke(Methods.ScriptRuntime_iterate);
}
return type;
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class EvalDeclarationInstantiationGenerator method createFunctions.
/**
* 18.2.1.2, step 14
*/
private void createFunctions(ArrayDeque<HoistableDeclaration> functionsToInitialize, boolean strict, Variable<ExecutionContext> context, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Variable<FunctionObject> fo, Variable<? extends EnvironmentRecord> varEnvRec, InstructionVisitor mv) {
for (HoistableDeclaration f : functionsToInitialize) {
Name fn = BoundName(f);
// stack: [] -> []
InstantiateFunctionObject(context, lexEnv, f, mv);
mv.store(fo);
BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
Jump funcAlreadyDeclared = new Jump(), after = new Jump();
op.hasBinding(varEnvRec, fn, mv);
mv.ifne(funcAlreadyDeclared);
{
op.createMutableBinding(varEnvRec, fn, true, mv);
op.initializeBinding(varEnvRec, fn, fo, mv);
mv.goTo(after);
}
mv.mark(funcAlreadyDeclared);
{
op.setMutableBinding(varEnvRec, fn, fo, strict, mv);
}
mv.mark(after);
}
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord 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();
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord 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();
}
Aggregations