use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class ScriptRuntime method EvaluateAsyncFunctionExpression.
/**
* Extension: Async Function Definitions
*
* @param fd
* the function runtime info object
* @param cx
* the execution context
* @return the new async function instance
*/
public static OrdinaryAsyncFunction EvaluateAsyncFunctionExpression(RuntimeInfo.Function fd, ExecutionContext cx) {
OrdinaryAsyncFunction closure;
if (!fd.is(RuntimeInfo.FunctionFlags.ScopedName)) {
/* step 1 (not applicable) */
/* step 2 */
LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
/* step 3 */
closure = AsyncFunctionCreate(cx, FunctionKind.Normal, fd, scope);
} else {
/* step 1 (not applicable) */
/* step 2 */
LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
/* step 3 */
LexicalEnvironment<DeclarativeEnvironmentRecord> funcEnv = newDeclarativeEnvironment(scope);
/* step 4 */
DeclarativeEnvironmentRecord envRec = funcEnv.getEnvRec();
/* step 5 */
String name = fd.functionName();
/* step 6 */
envRec.createImmutableBinding(name, false);
/* step 7 */
closure = AsyncFunctionCreate(cx, FunctionKind.Normal, fd, funcEnv);
/* step 8 */
SetFunctionName(closure, name);
/* step 9 */
envRec.initializeBinding(name, closure);
}
/* step 4/10 */
return closure;
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class ScriptRuntime method canDeclareVarBinding.
/**
* 18.2.1.2 Runtime Semantics: EvalDeclarationInstantiation( body, varEnv, lexEnv, strict)
*
* @param varEnv
* the variable environment
* @param lexEnv
* the lexical environment
* @param name
* the function name
* @param catchVar
* {@code true} if variable redeclarations are allowed in catch clauses
* @return {@code true} if the name can be declared
*/
public static boolean canDeclareVarBinding(LexicalEnvironment<?> varEnv, LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv, String name, boolean catchVar) {
for (LexicalEnvironment<?> thisEnv = lexEnv; thisEnv != varEnv; thisEnv = thisEnv.getOuter()) {
EnvironmentRecord thisEnvRec = thisEnv.getEnvRec();
if (thisEnvRec instanceof ObjectEnvironmentRecord) {
continue;
}
DeclarativeEnvironmentRecord declEnvRec = (DeclarativeEnvironmentRecord) thisEnvRec;
if (declEnvRec.hasBinding(name) && !(catchVar && declEnvRec.isCatchEnvironment())) {
return false;
}
}
return true;
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class ScriptRuntime method EvaluateLegacyFunctionExpression.
/**
* 14.1 Function Definitions
* <p>
* 14.1.21 Runtime Semantics: Evaluation
* <ul>
* <li>FunctionExpression : function ( FormalParameters ) { FunctionBody }
* <li>FunctionExpression : function BindingIdentifier ( FormalParameters ) { FunctionBody }
* </ul>
*
* @param fd
* the function runtime info object
* @param cx
* the execution context
* @return the new function instance
*/
public static LegacyConstructorFunction EvaluateLegacyFunctionExpression(RuntimeInfo.Function fd, ExecutionContext cx) {
LegacyConstructorFunction closure;
if (!fd.is(RuntimeInfo.FunctionFlags.ScopedName)) {
/* step 1 (not applicable) */
/* step 2 */
LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
/* step 3 */
closure = LegacyFunctionCreate(cx, fd, scope);
/* step 4 */
MakeConstructor(cx, closure);
} else {
/* step 1 (not applicable) */
/* step 2 */
LexicalEnvironment<?> scope = cx.getLexicalEnvironment();
/* step 3 */
LexicalEnvironment<DeclarativeEnvironmentRecord> funcEnv = newDeclarativeEnvironment(scope);
/* step 4 */
DeclarativeEnvironmentRecord envRec = funcEnv.getEnvRec();
/* step 5 */
String name = fd.functionName();
/* step 6 */
envRec.createImmutableBinding(name, false);
/* step 7 */
closure = LegacyFunctionCreate(cx, fd, funcEnv);
/* step 8 */
MakeConstructor(cx, closure);
/* step 9 */
SetFunctionName(closure, name);
/* step 10 */
envRec.initializeBinding(name, closure);
}
/* step 5/11 */
return closure;
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class FunctionDeclarationInstantiationGenerator method generate.
private void generate(FunctionNode function, CodeVisitor mv) {
Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<LexicalEnvironment<FunctionEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
Variable<FunctionEnvironmentRecord> envRec = mv.newVariable("envRec", FunctionEnvironmentRecord.class);
Variable<FunctionObject> fo = null;
Variable<Undefined> undefined = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undefined);
FunctionScope fscope = function.getScope();
boolean hasParameters = !function.getParameters().getFormals().isEmpty();
Variable<Iterator<?>> iterator = null;
if (hasParameters) {
iterator = mv.newVariable("iterator", Iterator.class).uncheckedCast();
mv.loadParameter(ARGUMENTS, Object[].class);
mv.invoke(Methods.Arrays_asList);
mv.invoke(Methods.List_iterator);
mv.store(iterator);
}
/* step 1 (omitted) */
/* step 2 */
getLexicalEnvironment(context, env, mv);
/* step 3 */
getEnvironmentRecord(env, envRec, mv);
/* step 4 */
// RuntimeInfo.Function code = func.getCode();
/* step 5 */
boolean strict = IsStrict(function);
/* step 6 */
FormalParameterList formals = function.getParameters();
/* step 7 */
List<Name> parameterNames = BoundNames(formals);
HashSet<Name> parameterNamesSet = new HashSet<>(parameterNames);
/* step 8 */
boolean hasDuplicates = parameterNames.size() != parameterNamesSet.size();
/* step 9 */
boolean simpleParameterList = IsSimpleParameterList(formals);
/* step 10 */
boolean hasParameterExpressions = ContainsExpression(formals);
// invariant: hasDuplicates => simpleParameterList
assert !hasDuplicates || simpleParameterList;
// invariant: hasParameterExpressions => !simpleParameterList
assert !hasParameterExpressions || !simpleParameterList;
/* step 11 */
Set<Name> varNames = VarDeclaredNames(function);
/* step 12 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(function);
/* step 13 */
Set<Name> lexicalNames = LexicallyDeclaredNames(function);
/* step 14 */
HashSet<Name> functionNames = new HashSet<>();
/* step 15 */
ArrayDeque<HoistableDeclaration> functionsToInitialize = new ArrayDeque<>();
/* step 16 */
for (StatementListItem item : reverse(varDeclarations)) {
if (item instanceof HoistableDeclaration) {
HoistableDeclaration d = (HoistableDeclaration) item;
Name fn = BoundName(d);
if (functionNames.add(fn)) {
functionsToInitialize.addFirst(d);
}
}
}
if (!functionsToInitialize.isEmpty()) {
fo = mv.newVariable("fo", FunctionObject.class);
}
/* step 17 */
// Optimization: Skip 'arguments' allocation if it's not referenced within the function.
boolean argumentsObjectNeeded = function.getScope().needsArguments();
Name arguments = function.getScope().arguments();
argumentsObjectNeeded &= arguments != null;
/* step 18 */
if (function.getThisMode() == FunctionNode.ThisMode.Lexical) {
argumentsObjectNeeded = false;
} else /* step 19 */
if (parameterNamesSet.contains(arguments)) {
argumentsObjectNeeded = false;
} else /* step 20 */
if (!hasParameterExpressions) {
if (functionNames.contains(arguments) || lexicalNames.contains(arguments)) {
argumentsObjectNeeded = false;
}
}
/* step 21 */
for (Name paramName : function.getScope().parameterNames()) {
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, paramName);
op.createMutableBinding(envRec, paramName, false, mv);
if (hasDuplicates) {
op.initializeBinding(envRec, paramName, undefined, mv);
}
}
/* step 22 */
if (argumentsObjectNeeded) {
assert arguments != null;
Variable<ArgumentsObject> argumentsObj = mv.newVariable("argumentsObj", ArgumentsObject.class);
if (strict || !simpleParameterList) {
CreateUnmappedArgumentsObject(mv);
} else if (formals.getFormals().isEmpty()) {
CreateMappedArgumentsObject(mv);
} else {
CreateMappedArgumentsObject(env, formals, mv);
}
mv.store(argumentsObj);
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, arguments);
if (strict) {
op.createImmutableBinding(envRec, arguments, false, mv);
} else {
op.createMutableBinding(envRec, arguments, false, mv);
}
op.initializeBinding(envRec, arguments, argumentsObj, mv);
parameterNames.add(arguments);
parameterNamesSet.add(arguments);
}
/* steps 24-26 */
if (hasParameters) {
if (hasDuplicates) {
/* step 24 */
BindingInitialization(codegen, function, env, iterator, mv);
} else {
/* step 25 */
BindingInitialization(codegen, function, env, envRec, iterator, mv);
}
}
/* steps 27-28 */
HashSet<Name> instantiatedVarNames;
Variable<? extends LexicalEnvironment<?>> varEnv;
Variable<? extends DeclarativeEnvironmentRecord> varEnvRec;
if (!hasParameterExpressions) {
assert fscope == fscope.variableScope();
/* step 27.a (note) */
/* step 27.b */
instantiatedVarNames = new HashSet<>(parameterNames);
/* step 27.c */
for (Name varName : varNames) {
if (instantiatedVarNames.add(varName)) {
BindingOp<FunctionEnvironmentRecord> op = BindingOp.of(envRec, varName);
op.createMutableBinding(envRec, varName, false, mv);
op.initializeBinding(envRec, varName, undefined, mv);
}
}
/* steps 27.d-27.e */
varEnv = env;
varEnvRec = envRec;
} else {
assert fscope != fscope.variableScope();
mv.enterScope(fscope.variableScope());
/* step 28.a (note) */
/* step 28.b */
varEnv = mv.newVariable("varEnv", LexicalEnvironment.class).uncheckedCast();
newDeclarativeEnvironment(env, mv);
mv.store(varEnv);
/* step 28.c */
varEnvRec = mv.newVariable("varEnvRec", DeclarativeEnvironmentRecord.class);
getEnvironmentRecord(varEnv, varEnvRec, mv);
/* step 28.d */
setVariableEnvironment(varEnv, mv);
/* step 28.e */
instantiatedVarNames = new HashSet<>();
/* step 28.f */
Variable<Object> tempValue = null;
for (Name varName : varNames) {
if (instantiatedVarNames.add(varName)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, varName);
op.createMutableBinding(varEnvRec, varName, false, mv);
if (!parameterNamesSet.contains(varName) || functionNames.contains(varName)) {
op.initializeBinding(varEnvRec, varName, undefined, mv);
} else {
BindingOp.of(envRec, varName).getBindingValue(envRec, varName, strict, mv);
if (tempValue == null) {
tempValue = mv.newVariable("tempValue", Object.class);
}
mv.store(tempValue);
op.initializeBinding(varEnvRec, varName, tempValue, mv);
}
}
}
}
/* step 29 (B.3.3 Block-Level Function Declarations Web Legacy Compatibility Semantics) */
for (Name fname : function.getScope().blockFunctionNames()) {
if (instantiatedVarNames.add(fname)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, fname);
op.createMutableBinding(varEnvRec, fname, false, mv);
op.initializeBinding(varEnvRec, fname, undefined, mv);
}
}
/* steps 30-32 */
Variable<? extends LexicalEnvironment<?>> lexEnv;
Variable<? extends DeclarativeEnvironmentRecord> lexEnvRec;
assert strict || fscope.variableScope() != fscope.lexicalScope();
if (!strict || fscope.variableScope() != fscope.lexicalScope()) {
// NB: Scopes are unmodifiable once constructed, that means we need to emit the extra
// scope for functions with deferred strict-ness, even if this scope is not present in
// the specification.
mv.enterScope(fscope.lexicalScope());
if (!lexicalNames.isEmpty()) {
/* step 30 */
lexEnv = mv.newVariable("lexEnv", LexicalEnvironment.class).uncheckedCast();
newDeclarativeEnvironment(varEnv, mv);
mv.store(lexEnv);
/* step 32 */
lexEnvRec = mv.newVariable("lexEnvRec", DeclarativeEnvironmentRecord.class);
getEnvironmentRecord(lexEnv, lexEnvRec, mv);
} else {
// Optimization: Skip environment allocation if no lexical names are defined.
/* step 30 */
lexEnv = varEnv;
/* step 32 */
lexEnvRec = varEnvRec;
}
} else {
/* step 30 */
lexEnv = varEnv;
/* step 32 */
lexEnvRec = varEnvRec;
}
/* step 33 */
if (lexEnv != env) {
setLexicalEnvironment(lexEnv, mv);
}
/* step 34 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(function);
/* step 35 */
for (Declaration d : lexDeclarations) {
assert !(d instanceof HoistableDeclaration);
for (Name dn : BoundNames(d)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(lexEnvRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(lexEnvRec, dn, true, mv);
} else {
op.createMutableBinding(lexEnvRec, dn, false, mv);
}
}
}
/* step 36 */
for (HoistableDeclaration f : functionsToInitialize) {
Name fn = BoundName(f);
// stack: [] -> [fo]
InstantiateFunctionObject(context, lexEnv, f, mv);
mv.store(fo);
// stack: [fo] -> []
// Resolve the actual binding name: function(a){ function a(){} }
// TODO: Can be removed when StaticIdResolution handles this case.
Name name = fscope.variableScope().resolveName(fn, false);
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(varEnvRec, name);
op.setMutableBinding(varEnvRec, name, fo, false, mv);
}
/* step 37 */
mv._return();
}
use of com.github.anba.es6draft.runtime.DeclarativeEnvironmentRecord in project es6draft by anba.
the class StatementGenerator method visit.
/**
* Extension: 'let' statement
*/
@Override
public Completion visit(LetStatement node, CodeVisitor mv) {
BlockScope scope = node.getScope();
if (scope.isPresent()) {
mv.enterVariableScope();
Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> env = mv.newVariable("env", LexicalEnvironment.class).uncheckedCast();
Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class);
newDeclarativeEnvironment(scope, mv);
mv.store(env);
getEnvRec(env, envRec, mv);
for (LexicalBinding lexical : node.getBindings()) {
Binding binding = lexical.getBinding();
Expression initializer = lexical.getInitializer();
for (Name name : BoundNames(binding)) {
BindingOp<DeclarativeEnvironmentRecord> op = BindingOp.of(envRec, name);
op.createMutableBinding(envRec, name, false, mv);
}
if (initializer == null) {
// LexicalBinding : BindingIdentifier
assert binding instanceof BindingIdentifier;
Name name = ((BindingIdentifier) binding).getName();
/* steps 1-2 */
InitializeBoundNameWithUndefined(envRec, name, mv);
} else if (binding instanceof BindingIdentifier) {
// LexicalBinding : BindingIdentifier Initializer
Name name = ((BindingIdentifier) binding).getName();
/* steps 1-7 */
InitializeBoundNameWithInitializer(codegen, envRec, name, initializer, mv);
} else {
// LexicalBinding : BindingPattern Initializer
assert binding instanceof BindingPattern;
/* steps 1-3 */
expressionBoxed(initializer, mv);
/* steps 4-5 */
BindingInitialization(codegen, envRec, (BindingPattern) binding, mv);
}
}
mv.load(env);
pushLexicalEnvironment(mv);
mv.exitVariableScope();
}
mv.enterScope(node);
Completion result = node.getStatement().accept(this, mv);
mv.exitScope();
if (scope.isPresent() && !result.isAbrupt()) {
popLexicalEnvironment(mv);
}
return result;
}
Aggregations