use of com.github.anba.es6draft.runtime.EnvironmentRecord 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.EnvironmentRecord in project es6draft by anba.
the class EvalDeclarationInstantiationGenerator method createVarDeclarations.
/**
* 18.2.1.2, step 15
*/
private void createVarDeclarations(LinkedHashSet<Name> declaredVarNames, Variable<? extends EnvironmentRecord> varEnvRec, Variable<Undefined> undef, InstructionVisitor mv) {
for (Name vn : declaredVarNames) {
BindingOp<EnvironmentRecord> op = BindingOp.LOOKUP;
Jump varAlreadyDeclared = new Jump();
op.hasBinding(varEnvRec, vn, mv);
mv.ifne(varAlreadyDeclared);
{
op.createMutableBinding(varEnvRec, vn, true, mv);
op.initializeBinding(varEnvRec, vn, undef, mv);
}
mv.mark(varAlreadyDeclared);
}
}
use of com.github.anba.es6draft.runtime.EnvironmentRecord in project es6draft by anba.
the class EvalDeclarationInstantiationGenerator method checkLexicalRedeclaration.
/**
* 18.2.1.2, steps 5.b-d
*/
private void checkLexicalRedeclaration(Script evalScript, Variable<ExecutionContext> context, Variable<? extends LexicalEnvironment<? extends EnvironmentRecord>> varEnv, Variable<LexicalEnvironment<DeclarativeEnvironmentRecord>> lexEnv, Set<Name> varNames, InstructionVisitor mv) {
Variable<LexicalEnvironment<EnvironmentRecord>> thisLex = mv.newVariable("thisLex", LexicalEnvironment.class).uncheckedCast();
Variable<EnvironmentRecord> thisEnvRec = mv.newVariable("thisEnvRec", EnvironmentRecord.class).uncheckedCast();
Variable<DeclarativeEnvironmentRecord> envRec = mv.newVariable("envRec", DeclarativeEnvironmentRecord.class).uncheckedCast();
Set<Name> varForOfNames = evalScript.getScope().varForOfDeclaredNames();
final boolean catchVar = codegen.isEnabled(CompatibilityOption.CatchVarStatement);
final boolean hasWith = codegen.isEnabled(Parser.Option.EnclosedByWithStatement);
Jump loopTest = new Jump(), loop = new Jump(), objectEnv = new Jump();
mv.load(lexEnv);
if (hasLexicalEnvironment(evalScript)) {
// Don't need to check own lexical environment.
mv.invoke(Methods.LexicalEnvironment_getOuter);
}
mv.store(thisLex);
mv.nonDestructiveGoTo(loopTest);
{
mv.mark(loop);
getEnvironmentRecord(thisLex, thisEnvRec, mv);
if (hasWith) {
mv.load(thisEnvRec);
mv.instanceOf(Types.ObjectEnvironmentRecord);
mv.ifne(objectEnv);
}
mv.load(thisEnvRec);
mv.checkcast(Types.DeclarativeEnvironmentRecord);
mv.store(envRec);
for (Name name : varNames) {
mv.load(context);
mv.load(envRec);
mv.aconst(name.getIdentifier());
mv.iconst(catchVar && !varForOfNames.contains(name));
mv.invoke(Methods.ScriptRuntime_canDeclareVarOrThrow);
}
if (hasWith) {
mv.mark(objectEnv);
}
mv.load(thisLex);
mv.invoke(Methods.LexicalEnvironment_getOuter);
mv.store(thisLex);
}
mv.mark(loopTest);
mv.load(thisLex);
mv.load(varEnv);
mv.ifacmpne(loop);
}
use of com.github.anba.es6draft.runtime.EnvironmentRecord in project es6draft by anba.
the class DeclarationBindingInstantiation method EvalDeclarationInstantiation.
/**
* 18.2.1.2 Runtime Semantics: EvalDeclarationInstantiation (body, varEnv, lexEnv, strict)
*
* @param cx
* the execution context
* @param evalScript
* the global script to instantiate
* @param varEnv
* the variable environment
* @param lexEnv
* the lexical environment
*/
public static void EvalDeclarationInstantiation(ExecutionContext cx, Script evalScript, LexicalEnvironment<?> varEnv, LexicalEnvironment<DeclarativeEnvironmentRecord> lexEnv) {
boolean strict = evalScript.isStrict();
boolean nonStrictGlobal = !strict && evalScript.isGlobalCode() && !evalScript.isScripting();
/* step 1 */
Set<Name> varNames = VarDeclaredNames(evalScript);
/* step 2 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(evalScript);
/* step 3 (not applicable) */
/* step 4 */
EnvironmentRecord varEnvRec = varEnv.getEnvRec();
assert !nonStrictGlobal || varEnvRec instanceof GlobalEnvironmentRecord : String.format("Unexpected environment record type: %s", varEnvRec);
/* step 5 */
if (!strict) {
if (nonStrictGlobal) {
/* step 5.a */
GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
for (Name name : varNames) {
ScriptRuntime.canDeclareVarScopedOrThrow(cx, gEnvRec, name.getIdentifier());
}
}
/* steps 5.b-d */
if (!evalScript.isScripting() && !varNames.isEmpty() && isEnclosedByLexicalOrHasVarForOf(evalScript)) {
checkLexicalRedeclaration(cx, varEnv, lexEnv, varNames);
}
}
/* steps 6-8 (not applicable) */
/* step 9 */
LinkedHashSet<Name> declaredNames = new LinkedHashSet<>();
/* step 10 */
for (StatementListItem d : varDeclarations) {
assert d instanceof VariableStatement;
for (Name vn : BoundNames((VariableStatement) d)) {
if (nonStrictGlobal) {
GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
ScriptRuntime.canDeclareGlobalVarOrThrow(cx, gEnvRec, vn.getIdentifier());
}
declaredNames.add(vn);
}
}
/* steps 12-13 */
assert LexicallyScopedDeclarations(evalScript).isEmpty();
/* step 15 */
for (Name vn : declaredNames) {
if (nonStrictGlobal) {
GlobalEnvironmentRecord gEnvRec = (GlobalEnvironmentRecord) varEnvRec;
gEnvRec.createGlobalVarBinding(vn.getIdentifier(), true);
} else {
boolean bindingExists = varEnvRec.hasBinding(vn.getIdentifier());
if (!bindingExists) {
varEnvRec.createMutableBinding(vn.getIdentifier(), true);
varEnvRec.initializeBinding(vn.getIdentifier(), UNDEFINED);
}
}
}
/* step 16 (return) */
}
use of com.github.anba.es6draft.runtime.EnvironmentRecord in project es6draft by anba.
the class ModuleNamespaceObject method getValue.
/** 9.4.6.8 [[Get]] (P, Receiver) */
@Override
protected Object getValue(ExecutionContext cx, String propertyKey, Object receiver) {
/* step 1 (not applicable) */
/* step 2 (not applicable) */
/* step 3 */
Set<String> exports = this.exports;
/* step 4 */
if (!exports.contains(propertyKey)) {
return UNDEFINED;
}
/* step 5 */
ModuleRecord m = this.module;
/* steps 6-8 */
ModuleExport binding;
try {
/* steps 6, 8 */
binding = m.resolveExport(propertyKey, new HashMap<>(), new HashSet<>());
} catch (IOException e) {
/* step 7 */
throw Errors.newInternalError(cx, e, Messages.Key.ModulesIOException, e.getMessage());
} catch (ResolutionException | MalformedNameException e) {
/* step 7 */
throw e.toScriptException(cx);
} catch (ParserException | CompilationException e) {
/* step 7 */
throw e.toScriptException(cx);
}
/* step 8 */
assert binding != null && !binding.isAmbiguous();
/* step 9 */
ModuleRecord targetModule = binding.getModule();
/* step 10 */
assert targetModule != null;
/* step 11 */
LexicalEnvironment<?> targetEnv = targetModule.getEnvironment();
/* step 12 */
if (targetEnv == null) {
throw newReferenceError(cx, Messages.Key.UninitializedBinding, binding.getBindingName());
}
/* step ? (Extension: Export From) */
if (binding.isNameSpaceExport()) {
try {
return GetModuleNamespace(cx, targetModule);
} catch (IOException e) {
throw Errors.newInternalError(cx, Messages.Key.ModulesIOException, e.getMessage());
} catch (MalformedNameException | ResolutionException e) {
throw e.toScriptException(cx);
}
}
/* step 13 */
EnvironmentRecord targetEnvRec = targetEnv.getEnvRec();
/* step 14 */
return targetEnvRec.getBindingValue(binding.getBindingName(), true);
}
Aggregations