use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.
the class SourceTextModuleRecord method instantiate.
/**
* 15.2.1.16.4 ModuleDeclarationInstantiation( ) Concrete Method
*/
@Override
public void instantiate() throws IOException, MalformedNameException, ResolutionException {
/* step 1 */
SourceTextModuleRecord module = this;
/* step 2 */
Realm realm = module.realm;
/* step 3 */
assert realm != null : "module is not linked";
/* step 4 */
Module code = module.scriptCode;
/* step 5 */
if (module.environment != null) {
return;
}
/* step 6 */
LexicalEnvironment<ModuleEnvironmentRecord> env = newModuleEnvironment(realm.getGlobalEnv());
/* step 7 */
module.environment = env;
/* step 8 */
for (String required : module.requestedModules) {
/* step 8.a (note) */
/* steps 8.b-c */
ModuleRecord requiredModule = HostResolveImportedModule(module, required);
/* steps 8.d-e */
requiredModule.instantiate();
}
/* steps 9-17 */
ExecutionContext context = newModuleDeclarationExecutionContext(realm, code);
code.getModuleBody().moduleDeclarationInstantiation(context, this, env);
module.instantiated = true;
}
use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.
the class ModuleDeclarationInstantiationGenerator method generate.
private void generate(Module module, SourceTextModuleRecord moduleRecord, ModuleDeclInitVisitor mv) {
Variable<ExecutionContext> context = mv.getExecutionContext();
Variable<SourceTextModuleRecord> moduleRec = mv.getModule();
Variable<LexicalEnvironment<ModuleEnvironmentRecord>> env = mv.getModuleEnvironment();
Variable<ModuleEnvironmentRecord> envRec = mv.newVariable("envRec", ModuleEnvironmentRecord.class);
getEnvironmentRecord(env, envRec, mv);
Variable<ResolvedBinding> resolved = mv.newVariable("resolved", ResolvedBinding.class);
Variable<ScriptObject> namespace = null;
Variable<FunctionObject> fo = null;
Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undef);
/* step 1 */
for (ExportEntry exportEntry : moduleRecord.getIndirectExportEntries()) {
mv.lineInfo(exportEntry.getLine());
mv.load(moduleRec);
mv.aconst(exportEntry.getExportName());
mv.invoke(Methods.ModuleOperations_resolveExportOrThrow);
}
/* step 8 */
for (ImportEntry importEntry : moduleRecord.getImportEntries()) {
mv.lineInfo(importEntry.getLine());
if (importEntry.isStarImport()) {
Name localName = new Name(importEntry.getLocalName());
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, localName);
op.createImmutableBinding(envRec, localName, true, mv);
mv.load(context);
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.invoke(Methods.ModuleOperations_getModuleNamespace);
if (namespace == null) {
namespace = mv.newVariable("namespace", ScriptObject.class);
}
mv.store(namespace);
op.initializeBinding(envRec, localName, namespace, mv);
} else {
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.aconst(importEntry.getImportName());
mv.invoke(Methods.ModuleOperations_resolveImportOrThrow);
mv.store(resolved);
/* step 8.d.iii */
createImportBinding(context, envRec, importEntry.getLocalName(), resolved, mv);
}
}
/* step 9 (not applicable) */
/* step 10 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(module);
/* step 11 */
HashSet<Name> declaredVarNames = new HashSet<>();
/* step 12 */
for (StatementListItem d : varDeclarations) {
assert d instanceof VariableStatement;
for (Name dn : BoundNames((VariableStatement) d)) {
if (declaredVarNames.add(dn)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
op.createMutableBinding(envRec, dn, false, mv);
op.initializeBinding(envRec, dn, undef, mv);
}
}
}
/* step 13 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(module);
/* step 14 */
for (Declaration d : lexDeclarations) {
for (Name dn : BoundNames(d)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(envRec, dn, true, mv);
} else {
op.createMutableBinding(envRec, dn, false, mv);
}
if (d instanceof HoistableDeclaration) {
InstantiateFunctionObject(context, env, d, mv);
if (fo == null) {
fo = mv.newVariable("fo", FunctionObject.class);
}
mv.store(fo);
op.initializeBinding(envRec, dn, fo, mv);
}
}
}
mv._return();
}
use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.
the class SourceTextModuleRecord method ModuleDeclarationEnvironmentSetup.
/**
* 15.2.1.16.4.2 ModuleDeclarationEnvironmentSetup( module )
*/
private static void ModuleDeclarationEnvironmentSetup(SourceTextModuleRecord module) throws IOException, ResolutionException, MalformedNameException {
/* step 3 */
Realm realm = module.realm;
/* step 4 */
assert realm != null : "module is not linked";
/* step 5 */
LexicalEnvironment<ModuleEnvironmentRecord> env = newModuleEnvironment(realm.getGlobalEnv());
/* step 6 */
module.environment = env;
/* steps 1-2, 7-14 (generated code) */
Module code = module.scriptCode;
ExecutionContext context = newModuleDeclarationExecutionContext(realm, code);
code.getModuleBody().moduleDeclarationInstantiation(context, module, env);
}
use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.
the class NodeSourceTextModuleRecord method instantiate.
@Override
public void instantiate() throws IOException, MalformedNameException, ResolutionException {
SourceTextModuleRecord.Status previousState = module.getStatus();
module.instantiate();
SourceTextModuleRecord.Status newState = module.getStatus();
// Add "require" function when module is instantiated.
if (newState != previousState && newState == SourceTextModuleRecord.Status.Instantiated) {
ModuleEnvironmentRecord envRec = module.getEnvironment().getEnvRec();
if (!envRec.hasBinding("require")) {
envRec.createImmutableBinding("require", true);
envRec.initializeBinding("require", NodeFunctions.createRequireFunction(this));
}
}
}
use of com.github.anba.es6draft.runtime.ModuleEnvironmentRecord in project es6draft by anba.
the class ModuleDeclarationInstantiationGenerator method generate.
private void generate(Module module, SourceTextModuleRecord moduleRecord, InstructionVisitor mv) {
Variable<ExecutionContext> context = mv.getParameter(EXECUTION_CONTEXT, ExecutionContext.class);
Variable<SourceTextModuleRecord> moduleRec = mv.getParameter(MODULE, SourceTextModuleRecord.class);
Variable<LexicalEnvironment<ModuleEnvironmentRecord>> env = mv.getParameter(MODULE_ENV, LexicalEnvironment.class).uncheckedCast();
Variable<ModuleEnvironmentRecord> envRec = mv.newVariable("envRec", ModuleEnvironmentRecord.class);
getEnvironmentRecord(env, envRec, mv);
Variable<ModuleExport> resolved = mv.newVariable("resolved", ModuleExport.class);
Variable<ScriptObject> namespace = null;
Variable<FunctionObject> fo = null;
Variable<Undefined> undef = mv.newVariable("undef", Undefined.class);
mv.loadUndefined();
mv.store(undef);
/* step 9 */
for (ExportEntry exportEntry : moduleRecord.getIndirectExportEntries()) {
mv.lineInfo(exportEntry.getLine());
mv.load(moduleRec);
mv.aconst(exportEntry.getExportName());
mv.invoke(Methods.ScriptRuntime_resolveExportOrThrow);
}
/* step 12 */
for (ImportEntry importEntry : moduleRecord.getImportEntries()) {
mv.lineInfo(importEntry.getLine());
if (importEntry.isStarImport()) {
Name localName = new Name(importEntry.getLocalName());
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, localName);
op.createImmutableBinding(envRec, localName, true, mv);
mv.load(context);
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.invoke(Methods.ScriptRuntime_getModuleNamespace);
if (namespace == null) {
namespace = mv.newVariable("namespace", ScriptObject.class);
}
mv.store(namespace);
op.initializeBinding(envRec, localName, namespace, mv);
} else {
mv.load(moduleRec);
mv.aconst(importEntry.getModuleRequest());
mv.aconst(importEntry.getImportName());
mv.invoke(Methods.ScriptRuntime_resolveImportOrThrow);
mv.store(resolved);
createImportBinding(context, envRec, importEntry.getLocalName(), resolved, mv);
}
}
/* step 13 */
List<StatementListItem> varDeclarations = VarScopedDeclarations(module);
HashSet<Name> declaredVarNames = new HashSet<>();
/* step 14 */
for (StatementListItem d : varDeclarations) {
assert d instanceof VariableStatement;
for (Name dn : BoundNames((VariableStatement) d)) {
if (declaredVarNames.add(dn)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
op.createMutableBinding(envRec, dn, false, mv);
op.initializeBinding(envRec, dn, undef, mv);
}
}
}
/* step 15 */
List<Declaration> lexDeclarations = LexicallyScopedDeclarations(module);
/* step 16 */
for (Declaration d : lexDeclarations) {
for (Name dn : BoundNames(d)) {
BindingOp<ModuleEnvironmentRecord> op = BindingOp.of(envRec, dn);
if (d.isConstDeclaration()) {
op.createImmutableBinding(envRec, dn, true, mv);
} else {
op.createMutableBinding(envRec, dn, false, mv);
}
if (d instanceof HoistableDeclaration) {
InstantiateFunctionObject(context, env, d, mv);
if (fo == null) {
fo = mv.newVariable("fo", FunctionObject.class);
}
mv.store(fo);
op.initializeBinding(envRec, dn, fo, mv);
}
}
}
/* step 17 */
mv._return();
}
Aggregations