use of com.github.anba.es6draft.runtime.GlobalEnvironmentRecord in project es6draft by anba.
the class GeneratorFunctionConstructor method CreateDynamicGenerator.
private static OrdinaryGenerator CreateDynamicGenerator(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
/* step 1 (not applicable) */
/* step 2 (not applicable) */
/* step 3 */
Intrinsics fallbackProto = Intrinsics.Generator;
/* steps 4-10 */
String[] sourceText = functionSourceText(cx, args);
String parameters = sourceText[0], bodyText = sourceText[1];
/* steps 11, 13-20 */
Source source = functionSource(SourceKind.Generator, cx.getRealm(), callerContext);
RuntimeInfo.Function function;
try {
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
function = scriptLoader.generator(source, parameters, bodyText).getFunction();
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 12 */
boolean strict = function.isStrict();
/* steps 21-22 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* step 23 */
OrdinaryGenerator f = OrdinaryGenerator.FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
/* steps 24-25 */
LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
/* step 26 */
FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
/* step 27 */
OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.GeneratorPrototype);
f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
/* step 28 (not applicable) */
/* step 29 */
SetFunctionName(f, "anonymous");
/* step 30 */
return f;
}
use of com.github.anba.es6draft.runtime.GlobalEnvironmentRecord in project es6draft by anba.
the class InterpretedScriptBody method scriptEvaluation.
/**
* 15.1.7 Runtime Semantics: ScriptEvaluation
*
* @param cx
* the execution context
* @param script
* the script object
* @return the script evaluation result
*/
private Object scriptEvaluation(ExecutionContext cx, Script script) {
Realm realm = cx.getRealm();
/* step 1 (not applicable) */
/* step 2 */
LexicalEnvironment<GlobalEnvironmentRecord> globalEnv = realm.getGlobalEnv();
/* steps 3-7 */
ExecutionContext scriptCxt = newScriptExecutionContext(realm, script);
/* steps 8-9 */
ExecutionContext oldScriptContext = realm.getScriptContext();
try {
realm.setScriptContext(scriptCxt);
/* step 10 */
GlobalDeclarationInstantiation(scriptCxt, parsedScript, globalEnv);
/* steps 11-12 */
Object result = parsedScript.accept(new Interpreter(parsedScript), scriptCxt);
/* step 16 */
return result;
} finally {
/* steps 13-15 */
realm.setScriptContext(oldScriptContext);
}
}
use of com.github.anba.es6draft.runtime.GlobalEnvironmentRecord in project es6draft by anba.
the class AsyncGeneratorFunctionConstructor method CreateDynamicFunction.
/**
* 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
*
* @param callerContext
* the caller execution context
* @param cx
* the execution context
* @param newTarget
* the newTarget constructor function
* @param args
* the function arguments
* @return the new async generator function object
*/
private static FunctionObject CreateDynamicFunction(ExecutionContext callerContext, ExecutionContext cx, Constructor newTarget, Object... args) {
/* step 1 (not applicable) */
/* step 2 (not applicable) */
/* step 3 */
Intrinsics fallbackProto = Intrinsics.AsyncGenerator;
/* steps 4-10 */
String[] sourceText = functionSourceText(cx, args);
String parameters = sourceText[0], bodyText = sourceText[1];
/* steps 11, 13-20 */
Source source = functionSource(SourceKind.AsyncGenerator, cx.getRealm(), callerContext);
RuntimeInfo.Function function;
try {
ScriptLoader scriptLoader = cx.getRealm().getScriptLoader();
function = scriptLoader.asyncGenerator(source, parameters, bodyText).getFunction();
} catch (ParserException | CompilationException e) {
throw e.toScriptException(cx);
}
/* step 12 */
boolean strict = function.isStrict();
/* steps 21-22 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* step 23 */
OrdinaryAsyncGenerator f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal);
/* steps 24-25 */
LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
/* step 26 */
FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
/* step 27 */
OrdinaryObject prototype = ObjectCreate(cx, Intrinsics.AsyncGeneratorPrototype);
f.infallibleDefineOwnProperty("prototype", new Property(prototype, true, false, false));
/* step 28 (not applicable) */
/* step 29 */
SetFunctionName(f, "anonymous");
/* step 30 */
return f;
}
use of com.github.anba.es6draft.runtime.GlobalEnvironmentRecord in project es6draft by anba.
the class FunctionConstructor method CreateDynamicFunction.
/**
* 19.2.1.1.1 RuntimeSemantics: CreateDynamicFunction(constructor, newTarget, kind, args)
*
* @param cx
* the execution context
* @param source
* the source object
* @param function
* the compiled function
* @param newTarget
* the newTarget constructor function
* @param fallbackProto
* the fallback prototype
* @return the new function object
*/
private static FunctionObject CreateDynamicFunction(ExecutionContext cx, Source source, RuntimeInfo.Function function, Constructor newTarget, Intrinsics fallbackProto) {
/* steps 1-11, 13-20 (not applicable) */
/* step 12 */
boolean strict = function.isStrict();
/* steps 21-22 */
ScriptObject proto = GetPrototypeFromConstructor(cx, newTarget, fallbackProto);
/* step 23 */
FunctionObject f;
if (function.is(RuntimeInfo.FunctionFlags.Legacy)) {
assert !strict;
f = LegacyConstructorFunction.FunctionAllocate(cx, proto);
} else {
f = FunctionAllocate(cx, proto, strict, FunctionKind.Normal, ConstructorKind.Base);
}
/* steps 24-25 */
LexicalEnvironment<GlobalEnvironmentRecord> scope = f.getRealm().getGlobalEnv();
/* step 26 */
FunctionInitialize(f, FunctionKind.Normal, function, scope, newFunctionExecutable(source));
// Work around for: https://bugs.eclipse.org/bugs/show_bug.cgi?id=479802
if (f instanceof LegacyConstructorFunction) {
MakeConstructor(cx, (LegacyConstructorFunction) f);
} else {
MakeConstructor(cx, (OrdinaryConstructorFunction) f);
}
/* step 29 */
SetFunctionName(f, "anonymous");
/* step 30 */
return f;
}
use of com.github.anba.es6draft.runtime.GlobalEnvironmentRecord in project es6draft by anba.
the class EvalDeclarationInstantiationGenerator method generateGlobal.
private void generateGlobal(Script evalScript, EvalDeclInitVisitor mv) {
assert evalScript.isGlobalCode() && !evalScript.isStrict() && !evalScript.isScripting();
Variable<ExecutionContext> context = mv.getExecutionContext();
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 (isEnclosedByLexicalOrHasRestrictedVar(evalScript)) {
checkLexicalRedeclaration(evalScript, context, varEnv, lexEnv, varNames, mv);
}
}
// Extension: Class Fields
checkUndeclaredPrivateNames(evalScript, context, 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